--- /dev/null
+/* 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 <sys/types.h>
+#include <sys/stat.h>
+#include <dirent.h> /* directory */
+#include <stdlib.h> /* 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;
+}