3 * Copyright (C) 2002 Simone Piccardi
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /*****************************************************************************
21 * File DirScan.c: routine for directory scan
23 * Author: S. Piccardi Jan. 2003
25 * $Id: DirScan.c,v 1.4 2003/05/02 09:55:13 piccardi Exp $
27 *****************************************************************************/
28 #include <sys/types.h>
30 #include <dirent.h> /* directory */
31 #include <stdlib.h> /* C standard library */
38 * Scan all entries in a directory, executing the provided function
41 * Input: the directory name and a computation function
42 * Return: 0 if OK, -1 on errors
44 int DirScan(char * dirname, int(*compute)(struct dirent *))
47 struct dirent *direntry;
50 if ( (dir = opendir(dirname)) == NULL) { /* oper directory */
51 printf("Opening %s\n", dirname); /* on error print messages */
52 perror("Cannot open directory"); /* and then return */
55 fd = dirfd(dir); /* get file descriptor */
56 fchdir(fd); /* change directory */
57 /* loop on directory entries */
58 while ( (direntry = readdir(dir)) != NULL) { /* read entry */
59 if (compute(direntry)) { /* execute function on it */
60 return -1; /* on error return */