List files problem

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

List files problem

Post by ne0h »

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 »

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&#40;n<0&#41;&#123;
                   printf&#40;"Directory not found or error reading!"&#41;;
                   &#125;
          while&#40;b--&#41;&#123;
                      if&#40;a!=n&#41;&#123;     
                      a++;
                      printf&#40;"%s\n", namelist&#91;a&#93;->d_name&#41;;
                      &#125;
                      &#125;


int file_select&#40;namelist&#41;
 
		&#123;if &#40;&#40;strcmp&#40;namelist->d_name, "."&#41; == 0&#41; ||
						&#40;strcmp&#40;namelist->d_name, ".."&#41; == 0&#41;&#41;
						 return &#40;FALSE&#41;;
				else
								return &#40;TRUE&#41;;
		&#125;
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. »

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.
Post Reply