Discuss the development of new homebrew software, tools and libraries.
Moderators: cheriff , TyRaNiD
ne0h
Posts: 386 Joined: Thu Feb 21, 2008 2:15 am
Post
by ne0h » Wed Mar 19, 2008 3:12 am
Excuse me, but i use this function to get the name of the files in a directory, but I don't wan't to read "." and "..", i've try this many times but
now for this function i haven't a functionally solutions!
Can anyone help me?
And if i want to print to screen only file .xxx?
Code: Select all
int scandir(const char *dir, struct dirent ***namelist,
int (*select)(const struct dirent *),
int (*compar)(const struct dirent **, const struct dirent **))
{
DIR *d;
struct dirent *entry;
register int i=0;
size_t entrysize;
if ((d=opendir(dir)) == NULL)
return(-1);
*namelist=NULL;
while ((entry=readdir(d)) != NULL)
{
if (select == NULL || (select != NULL && (*select)(entry)))
{
*namelist=(struct dirent **)realloc((void *)(*namelist),
(size_t)((i+1)*sizeof(struct dirent *)));
if (*namelist == NULL) return(-1);
entrysize=sizeof(struct dirent)-sizeof(entry->d_name)+strlen(entry->d_name)+1;
(*namelist)[i]=(struct dirent *)malloc(entrysize);
if ((*namelist)[i] == NULL) return(-1);
memcpy((*namelist)[i], entry, entrysize);
i++;
}
}
if (closedir(d)) return(-1);
if (i == 0) return(-1);
if (compar != NULL)
qsort((void *)(*namelist), (size_t)i, sizeof(struct dirent *), compar);
return(i);
}
Tanks in advance!
Last edited by
ne0h on Thu Mar 20, 2008 3:57 am, edited 2 times in total.
ne0h
Posts: 386 Joined: Thu Feb 21, 2008 2:15 am
Post
by ne0h » Wed Mar 19, 2008 3:57 am
I've look here:
http://www.cs.cf.ac.uk/Dave/C/node20.html
but not work, maybe i integrate this code in a bad mode!
Please, give me help...
Here is the code in main funciton:
Code: Select all
struct dirent **namelist;
char dir[100]="ms0:/MUSIC/Minutes to Midnight/";
int b;
int a=-1;
int n;
pspDebugScreenSetXY(0, 4);
n = scandir(dir, &namelist, 0, alphasort);
int file_select();
b=n;
if(n<0){
printf("Directory not found or error reading!");
}
while(b--){
if(a!=n){
a++;
printf("%s\n", namelist[a]->d_name);
}
}
int file_select(namelist)
{if ((strcmp(namelist->d_name, ".") == 0) ||
(strcmp(namelist->d_name, "..") == 0))
return (FALSE);
else
return (TRUE);
}
Last edited by
ne0h on Thu Mar 20, 2008 3:58 am, edited 1 time in total.
J.F.
Posts: 2906 Joined: Sun Feb 22, 2004 11:41 am
Post
by J.F. » Wed Mar 19, 2008 7:31 am
Please use the [ code ] [ /code ] bbs tags around any code you post. It makes it that much harder for folks if you don't as the formatting is removed, making the code hard to read.