Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This is my code:
- typedef struct {
- char name[255];
- int size;
- int attr;
- } dir_ent;
- #define TYPE_FILTER(x) (strstr(x, ".nds") || strstr(x, ".NDS"))
- //...
- //////////////////
- // SECOND ATTEMPT:
- //////////////////
- DIR* dp = opendir(file_struct->path);
- if(!dp)
- return BROWSER_FILE_NOT_FOUND;
- struct stat fstat;
- s32 pathLen = strlen(file_struct->path);
- s32 num_entries = 1, I = 0;
- dir_ent* dir = (dir_ent*) malloc( num_entries * sizeof(dir_ent) );
- struct dirent *tdir;
- // Read each entry of the directory
- while ((tdir=readdir(dp))!=NULL) {
- u32 tdirNameLen = strlen(tdir->d_name);
- char filename[MAXPATHLEN];
- char div = '/';
- memset(filename, 0, MAXPATHLEN);
- if(MAXPATHLEN - pathLen - tdirNameLen <= 0){
- continue; // TOO LONG!
- // Print an error
- }
- strncat(filename, file_struct->path, pathLen);
- strncat(filename, &div, 1);
- strncat(filename, tdir->d_name, tdirNameLen);
- stat(filename,&fstat);
- // Let's see
- fprintf(stderr, "\nName:%s | Mode: %i\n",filename, fstat.st_mode);
- if((strcmp(filename, ".") != 0 && (fstat.st_mode & S_IFDIR)) || TYPE_FILTER(filename))
- {
- // Make sure we have room for this one
- if(I == num_entries){
- ++num_entries;
- dir = (dir_ent*) realloc( dir, num_entries * sizeof(dir_ent) );
- }
- strcpy(dir[I].name, tdir->d_name);
- dir[I].size = fstat.st_size;
- dir[I].attr = fstat.st_mode;
- ++I;
- }
- }
- closedir(dp);
- ///////////////////
- // FIRST ATTEMPT:
- ///////////////////
- DIR* dp = opendir(file_struct->path);
- // file_struct->path = the path to where our files are.
- if(!dp)
- return BROWSER_FILE_NOT_FOUND;
- struct stat fstat;
- s32 num_entries = 1, I = 0;
- dir_ent* dir = (dir_ent*) malloc( num_entries * sizeof(dir_ent) );
- struct dirent *tdir;
- // Read each entry of the directory
- while ((tdir=readdir(dp))!=NULL) {
- stat(tdir->d_name,&fstat);
- if((strcmp(tdir->d_name, ".") != 0 && (fstat.st_mode & S_IFDIR)) || TYPE_FILTER(tdir->d_name))
- {
- // Make sure we have room for this one
- if(I == num_entries){
- ++num_entries;
- dir = (dir_ent*) realloc( dir, num_entries * sizeof(dir_ent) );
- }
- strcpy(dir[I].name, tdir->d_name);
- dir[I].size = fstat.st_size;
- dir[I].attr = fstat.st_mode;
- ++I;
- }
- }
- closedir(dp);
- /////////////////////////////////////////////////
- //
- // And this is the example in directory.c:
- //
- DIR *pdir;
- struct dirent *pent;
- struct stat statbuf;
- pdir=opendir("/");
- if (!pdir){
- printf ("opendir() failure; terminating\n");
- goto error;
- }
- while ((pent=readdir(pdir))!=NULL) {
- stat(pent->d_name,&statbuf);
- if(strcmp(".", pent->d_name) == 0 || strcmp("..", pent->d_name) == 0)
- continue;
- if(S_ISDIR(statbuf.st_mode))
- printf("%s <dir>\n", pent->d_name);
- if(!(S_ISDIR(statbuf.st_mode)))
- printf("%s %lld\n", pent->d_name, statbuf.st_size);
- }
- closedir(pdir);
Add Comment
Please, Sign In to add comment