Re: Enumerate files?

From: Alexander Krisak (chris_at_imp.lg.ua)
Date: 08/28/03


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


Relevant Pages

  • Re: WILDCARD: output all a* by searching a text file
    ... where * denotes a string of characters. ... When you want to write a program that search a pattern such as /a./ ... int main{ ... int main(int argc,const char** argv){ ...
    (comp.programming)
  • Re: slurping in binary data
    ... int main ... char pattern; ... As a result, "pattern" will now contain a string with only one format specifier, not two, but you have modified the sscanfcall which uses "pattern" to only scan in the line number. ... I like the fortran version just fine. ...
    (comp.lang.c)
  • Re: slurping in binary data
    ... int main ... char pattern; ... As a result, "pattern" will now contain a string with only one format specifier, not two, but you have modified the sscanfcall which uses "pattern" to only scan in the line number. ... I like the fortran version just fine. ...
    (comp.lang.c)
  • Re: regcomp (regex.h) - is it needed to free memory?
    ... int status; ... Match string against the extended regular expression in ... pattern, ... match(const char *string, char *pattern) ...
    (comp.unix.programmer)
  • Re: regcomp (regex.h) - is it needed to free memory?
    ... int status; ... Match string against the extended regular expression in ... pattern, ... match(const char *string, char *pattern) ...
    (comp.unix.programmer)