uint8_t AF_Wave::get_next_name_in_dir(char *name) { struct fat16_dir_entry_struct dir_entry; if(fat16_read_dir(dd, &dir_entry)) { strncpy(name, dir_entry.long_name, 12); name[12] = 0; return 1; } return 0; }
uint8_t sd_find_file_in_dir(struct fat16_dir_struct* dd, const char* name, struct fat16_dir_entry_struct* dir_entry) { while(fat16_read_dir(dd, dir_entry)) { if(strcmp(dir_entry->long_name, name) == 0) { fat16_reset_dir(dd); return 1; } } return 0; }
/** * \ingroup tcpcmdcommon * \b DIR-Befehl Verzeichnislisting * * Aufruf von USART direkt über DIR-Befehl. Von FTP wird die Funktion * paketweise vom Datenkanal aus aufgerufen.<br><br> * \b Ausgabe: * - auf USART wird komplettes Listing ausgegeben * - bei TCP wird der outbuffer mit vollständigen Verzeichniseinträgen * bis maximal MAX_WINDOWS_SIZE gefüllt. Falls dann noch Einträge * vorhanden sind wird <tt>tcpsrv_status.LISTcontinue</tt> auf true gesetzt. * Bei abgeschlossenem Listing wird <tt>tcpsrv_status.LISTcontinue</tt> auf * false gesetzt. */ int16_t cmd_MMCdir(char *outbuffer) { #if USE_MMC if (!cwdir_ptr) return 0; #if !FTP_ANONYMOUS if (outbuffer && !tcpsrv_status.loginOK) return cmd_530(outbuffer); #endif #if TCP_SERVICE tcpsrv_status.LISTcontinue = 0; // continue Flag zurücksetzen #endif if (!outbuffer) { usart_write("\n\rSD-Karte:"); } char *pstr = outbuffer; struct fat16_dir_entry_struct dir_entry; uint16_t year; uint8_t month; uint8_t day; uint8_t hour; uint8_t min; uint8_t sec; /* Verzeichniseinträge lesen */ while(fat16_read_dir(cwdir_ptr, &dir_entry)) { fat16_get_file_modification_date(&dir_entry, &year, &month, &day); fat16_get_file_modification_time(&dir_entry, &hour, &min, &sec); if (outbuffer) { #if TCP_SERVICE char tmpbuf[12]; #ifdef UNIX_LIST /* * UNIX style Dateiliste */ char mstr[4]; if (dir_entry.attributes & FAT16_ATTRIB_DIR) { strcpy_P((char *)tmpbuf,PSTR("drwxr-xr-x")); } else { strcpy_P((char *)tmpbuf,PSTR("-rw-rw-rw-")); } strncpy_P(mstr,&US_Monate[(month-1)*3],3); mstr[3] = '\0'; sprintf_P((char *)pstr,PSTR("%s 1 ftp ftp %ld %s/%i/%i %i:%i %s\r\n"),tmpbuf, dir_entry.file_size, mstr, day, year, hour, min, dir_entry.long_name); #endif #ifdef DOS_LIST /* * DOS style Dateiliste * (bei FileZilla nennt sich das DOS - woanders Windows_NT(?) ...) */ if (dir_entry.attributes & FAT16_ATTRIB_DIR) { strcpy_P((char *)tmpbuf,PSTR("<DIR>")); } else { sprintf_P(tmpbuf,PSTR("%ld"),dir_entry.file_size); } sprintf_P((char *)pstr,PSTR("%i-%i-%i %i:%i:%i %8s %s\r\n"),month, day, year, hour, min, sec, tmpbuf,dir_entry.long_name); #endif while (*pstr++); // bis auf '\0' vorzählen --pstr; if (pstr > (outbuffer + MAX_WINDOWS_SIZE - 64)) { // mind. 64 Bytes für nächsten Eintrag freilassen tcpsrv_status.LISTcontinue = 1; // continue Flag setzen break; } #endif } else { usart_write("\r\n%2i.%2i.%i %2i:%2i ",day, month, year, hour, min); if (dir_entry.attributes & FAT16_ATTRIB_DIR) usart_write(" <DIR> "); else usart_write("%9l ",dir_entry.file_size); usart_write("%s",dir_entry.long_name); } } #if TCP_SERVICE if (!tcpsrv_status.LISTcontinue) fat16_reset_dir(cwdir_ptr); #endif if (outbuffer) return strlen(outbuffer); else #endif return 0; }
int main() { /* setup uart */ uart_init(); /* setup stdio */ uart_connect_stdio(); /* setup sd card slot */ if(!sd_raw_init()) { #if DEBUG printf_P(PSTR("MMC/SD initialization failed\n")); #endif return 1; } /* open first partition */ struct partition_struct* partition = partition_open(sd_raw_read, sd_raw_read_interval, sd_raw_write, 0); if(!partition) { /* If the partition did not open, assume the storage device * is a "superfloppy", i.e. has no MBR. */ partition = partition_open(sd_raw_read, sd_raw_read_interval, sd_raw_write, -1 ); if(!partition) { #if DEBUG printf_P(PSTR("opening partition failed\n")); #endif return 1; } } /* open file system */ struct fat16_fs_struct* fs = fat16_open(partition); if(!fs) { #if DEBUG printf_P(PSTR("opening filesystem failed\n")); #endif return 1; } /* open root directory */ struct fat16_dir_entry_struct directory; fat16_get_dir_entry_of_path(fs, "/", &directory); struct fat16_dir_struct* dd = fat16_open_dir(fs, &directory); if(!dd) { #if DEBUG printf_P(PSTR("opening root directory failed\n")); #endif return 1; } /* provide a simple shell */ char buffer[24]; while(1) { /* print prompt */ uart_putc('>'); uart_putc(' '); /* read command */ char* command = buffer; if(read_line(command, sizeof(buffer)) < 1) continue; /* execute command */ if(strncmp_P(command, PSTR("cd "), 3) == 0) { command += 3; if(command[0] == '\0') continue; /* change directory */ struct fat16_dir_entry_struct subdir_entry; if(find_file_in_dir(fs, dd, command, &subdir_entry)) { struct fat16_dir_struct* dd_new = fat16_open_dir(fs, &subdir_entry); if(dd_new) { fat16_close_dir(dd); dd = dd_new; continue; } } printf_P(PSTR("directory not found: %s\n"), command); } else if(strcmp_P(command, PSTR("ls")) == 0) { /* print directory listing */ struct fat16_dir_entry_struct dir_entry; while(fat16_read_dir(dd, &dir_entry)) { printf_P(PSTR("%10lu %s%c\n"), dir_entry.file_size, dir_entry.long_name, (dir_entry.attributes & FAT16_ATTRIB_DIR) ? '/' : ' ' ); } } else if(strncmp_P(command, PSTR("cat "), 4) == 0) { command += 4; if(command[0] == '\0') continue; /* search file in current directory and open it */ struct fat16_file_struct* fd = open_file_in_dir(fs, dd, command); if(!fd) { printf_P(PSTR("error opening %s\n"), command); continue; } /* print file contents */ uint8_t buffer[8]; uint32_t offset = 0; while(fat16_read_file(fd, buffer, sizeof(buffer)) > 0) { printf_P(PSTR("%08lx: %02x %02x %02x %02x %02x %02x %02x %02x\n"), offset, buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5], buffer[6], buffer[7] ); offset += 8; } fat16_close_file(fd); } #if FAT16_WRITE_SUPPORT else if(strncmp_P(command, PSTR("rm "), 3) == 0) { command += 3; if(command[0] == '\0') continue; struct fat16_dir_entry_struct file_entry; if(find_file_in_dir(fs, dd, command, &file_entry)) { if(fat16_delete_file(fs, &file_entry)) continue; } printf_P(PSTR("error deleting file: %s\n"), command); } else if(strncmp_P(command, PSTR("touch "), 6) == 0) { command += 6; if(command[0] == '\0') continue; struct fat16_dir_entry_struct file_entry; if(!fat16_create_file(dd, command, &file_entry)) printf_P(PSTR("error creating file: %s\n"), command); } else if(strncmp_P(command, PSTR("write "), 6) == 0) { command += 6; if(command[0] == '\0') continue; char* offset_value = command; while(*offset_value != ' ' && *offset_value != '\0') ++offset_value; if(*offset_value == ' ') *offset_value++ = '\0'; else continue; /* search file in current directory and open it */ struct fat16_file_struct* fd = open_file_in_dir(fs, dd, command); if(!fd) { printf_P(PSTR("error opening %s\n"), command); continue; } int32_t offset = strtol(offset_value, 0, 0); if(!fat16_seek_file(fd, &offset, FAT16_SEEK_SET)) { printf_P(PSTR("error seeking on %s\n"), command); fat16_close_file(fd); continue; } /* read text from the shell and write it to the file */ uint8_t data_len; while(1) { /* give a different prompt */ uart_putc('<'); uart_putc(' '); /* read one line of text */ data_len = read_line(buffer, sizeof(buffer)); if(!data_len) break; /* write text to file */ if(fat16_write_file(fd, (uint8_t*) buffer, data_len) != data_len) { printf_P(PSTR("error writing to file\n")); break; } } fat16_close_file(fd); } #endif #if SD_RAW_WRITE_BUFFERING else if(strcmp_P(command, PSTR("sync")) == 0) { if(!sd_raw_sync()) printf_P(PSTR("error syncing disk\n")); } #endif else { printf_P(PSTR("unknown command: %s\n"), command); } } /* close file system */ fat16_close(fs); /* close partition */ partition_close(partition); return 0; }