int my_rmdir (const TCHAR *name) { struct my_opendir_s *od; int cnt; TCHAR tname[MAX_DPATH]; memset(tname, 0, sizeof(TCHAR) * MAX_DPATH); /* SHFileOperation() ignores FOF_NORECURSION when deleting directories.. */ od = my_opendir (name); if (!od) { // SetLastError (ERROR_FILE_NOT_FOUND); return -1; } cnt = 0; while (my_readdir (od, tname)) { if (!_tcscmp (tname, _T(".")) || !_tcscmp (tname, _T(".."))) continue; cnt++; break; } my_closedir (od); if (cnt > 0) { // SetLastError (ERROR_CURRENT_DIRECTORY); return -1; } return rmdir (name); }
int main(int argc, char **argv) { if(argc < 2) { printf("Usage...\n"); exit(1); } if(my_readdir(argv[1]) < 0) { exit(1); } exit(0); }
void filer_read_dir(char *directory) { struct dirent *fd; DIR * rep = opendir (directory); memset(&list, 0, sizeof(struct dirent)); int i; for (i = 0; i < list_num; i++) { free(roms[i]); } free(roms); list_num = 0; while((fd = my_readdir(rep, &list[list_num])) && (list_num<MAXDIRNUM)) { if (list[list_num].d_name[0] == '.') continue; else if (list[list_num].d_type == DT_DIR) continue; if(fd != NULL) list_num++; } closedir(rep); if (list_start >= list_num) { list_start = list_num-1; } if (list_start < 0) { list_start = 0; } if (list_curpos >= list_num) { list_curpos = list_num-1; } if (list_curpos < 0) { list_curpos = 0; } if(list_num < 1) noDir=1; else noDir=0; roms = (char**) malloc(list_num * sizeof(char*)); for (i = 0; i < list_num; i++) { roms[i] = (char*) malloc(256); strcpy(roms[i], list[i].d_name); } sort(); }
/* Find the name REL in directory DIRNAME. If we find a file that * has exactly the same name, return REL. If we find a file that * has the same name when compared case-insensitively, return a * malloced string that contains the name we found. If no file * exists that compares equal to REL, return 0. */ TCHAR *fsdb_search_dir (const TCHAR *dirname, TCHAR *rel) { TCHAR *p = 0; struct dirent *de; TCHAR fn[MAX_DPATH]; struct my_opendir_s *dir = my_opendir (dirname); /* This really shouldn't happen... */ if (! dir) return 0; while (p == 0 && (de = my_readdir (dir, fn)) != 0) { if (strcmp (de->d_name, rel) == 0) p = rel; else if (strcasecmp (de->d_name, rel) == 0) p = my_strdup (de->d_name); } my_closedir (dir); return p; }
t_pos *my_ls_parser(const char *path) { DIR *dir; t_dirent *element; t_elem *elem; t_pos *list; if (!(dir = opendir(path))) { my_printf("ls: cannot acces %s", path); perror(""); return (NULL); } while ((element = my_readdir(dir))) { init_elem(elem); elem = get_info(elem, element); create_list_ls(elem, list); } return (list); }
/* dirwalk: aplica fcn a todos los archivos de dir */ void dirwalk( char *dir, void (*fcn)(char *) ){ char name[ MAX_PATH ]; Dirent *dp; myDIR *dfd; if( (dfd = my_opendir( dir )) == NULL ){ fprintf( stderr, "dirwalk: no se puede abrir %s\n", dir ); return; } while( (dp = my_readdir( dfd )) != NULL ){ if( strcmp( dp->name, "." ) == 0 || strcmp( dp->name, ".." ) ) continue; /* se ignora a si mismo y a su padre */ if( strlen( dir ) + strlen( dp->name ) + 2 > sizeof( name ) ) fprintf( stderr, "dirwalk: nombre %s %s demasiado largo\n", dir, dp->name ); else { sprintf( name, "%s/%s", dir, dp->name ); (*fcn)( name ); } } my_closedir( dfd ); }