Re: Enumerate files?
From: Alexander Krisak (chris_at_imp.lg.ua)
Date: 08/28/03
- Next message: Loic Domaigne: "Re: 4GB VM for 32 bit processes"
- Previous message: David Schwartz: "Re: 4GB VM for 32 bit processes"
- In reply to: Dominik Reichl: "Enumerate files?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 28 Aug 2003 12:38:14 +0300
Hello, Dominik Reichl
> I am looking for a function to enumerate files based on a pattern
> string like "/home/bla/*.png". I have found the function scandir but
> it seems that this function can enumerate only all files, without a
> pattern.
>
> Is there a function that enumerates files based on a pattern string?
Please, look at 'select' parameter of scandir function - here you can
realize your own function, which will check if name of file is correct
for your wildcard.
;--------------------X8
[12:31:40 delf@nick ~]$ cat q.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
int ispng(const struct dirent* d)
{ char* p;
if ((p = strrchr(d->d_name, '.')) == NULL)
return 0;
if (strcmp(p, ".png") == 0)
return 1;
else return 0;
}
int findfile(const char* dir)
{ struct dirent **namelist;
int n, i;
if ((n = scandir(dir, &namelist, ispng, alphasort)) == -1)
{ perror("scandir");
return -1;
}
if (n == 0)
{ printf("There is no *.png files\n");
return 0;
}
for (i = 0; i < n; i++)
{ printf("%s file found\n", namelist[i]->d_name);
free(namelist[i]);
}
free(namelist);
return 0;
}
int main(int argc, char* argv[])
{
if (argc == 1)
{ printf("%s: no file\n", argv[0]);
return 0;
}
findfile(argv[1]);
return 0;
}
;--------------------X8
-- kris
- Next message: Loic Domaigne: "Re: 4GB VM for 32 bit processes"
- Previous message: David Schwartz: "Re: 4GB VM for 32 bit processes"
- In reply to: Dominik Reichl: "Enumerate files?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|