morpheus-base

morpheus base system
git clone git://git.2f30.org/morpheus-base
Log | Files | Refs

recurse.c (716B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <dirent.h>
      3 #include <errno.h>
      4 #include <stdlib.h>
      5 #include <string.h>
      6 #include <unistd.h>
      7 #include <sys/stat.h>
      8 
      9 #include "../util.h"
     10 
     11 void
     12 recurse(const char *path, void (*fn)(const char *))
     13 {
     14 	char *cwd;
     15 	struct dirent *d;
     16 	struct stat st;
     17 	DIR *dp;
     18 
     19 	if(lstat(path, &st) == -1 || !S_ISDIR(st.st_mode)) {
     20 		return;
     21 	} else if(!(dp = opendir(path))) {
     22 		eprintf("opendir %s:", path);
     23 	}
     24 
     25 	cwd = agetcwd();
     26 	if(chdir(path) == -1)
     27 		eprintf("chdir %s:", path);
     28 
     29 	while((d = readdir(dp))) {
     30 		if(strcmp(d->d_name, ".") && strcmp(d->d_name, ".."))
     31 			fn(d->d_name);
     32 	}
     33 
     34 	closedir(dp);
     35 	if(chdir(cwd) == -1)
     36 		eprintf("chdir %s:", cwd);
     37 
     38 	free(cwd);
     39 }
     40