X-Git-Url: https://gapil.gnulinux.it/gitweb/?p=gapil.git;a=blobdiff_plain;f=sources%2FDirScan.c;fp=sources%2FDirScan.c;h=38fb555f0db7c5c72fe435c73cc22f044a1fca22;hp=0000000000000000000000000000000000000000;hb=e18f3ea57b725bc148f0d3443e384bf5270b2b9f;hpb=8fecd34eeb7db69bfa3cf2ca4b60ace29cad0356 diff --git a/sources/DirScan.c b/sources/DirScan.c new file mode 100644 index 0000000..38fb555 --- /dev/null +++ b/sources/DirScan.c @@ -0,0 +1,63 @@ +/* DirScan.c + * + * Copyright (C) 2002 Simone Piccardi + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +/***************************************************************************** + * + * File DirScan.c: routine for directory scan + * + * Author: S. Piccardi Jan. 2003 + * + * $Id: DirScan.c,v 1.1 2003/01/03 23:16:05 piccardi Exp $ + * + *****************************************************************************/ +#include +#include +#include /* directory */ +#include /* C standard library */ + +/* + * Function DirScan: + * + * Scan all entries in a directory, executing the provided function + * on each one. + * + * Input: the directory name and a computation function + * Return: 0 if OK, -1 on errors + */ +int DirScan(char * dirname, int(*compute)(struct dirent *)) +{ + DIR * dir; + struct dirent *direntry; + int fd; + + if ( (dir = opendir(dirname)) == NULL) { /* oper directory */ + printf("Opening %s\n", dirname); /* on error print messages */ + perror("Cannot open directory"); /* and then return */ + return -1; + } + fd = dirfd(dir); /* get file descriptor */ + fchdir(fd); /* change directory */ + /* loop on directory entries */ + while ( (direntry = readdir(dir)) != NULL) { /* read entry */ + if (compute(direntry)) { /* execute function on it */ + return -1; /* on error return */ + } + } + closedir(dir); + return 0; +}