-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirwalk.h
executable file
·41 lines (37 loc) · 1.05 KB
/
dirwalk.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
*Reference: Kernighan; Dennis M. Ritchie (March 1988). The C Programming Language (2nd ed.).
* Englewood Cliffs, NJ: Prentice Hall. ISBN 0-13-110362-8. http://cm.bell-labs.com/cm/cs/cbook/.
*note: I have gained a basic understanding of recursive file directory search by reading this textbook p160 - 164
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>
#define MAX_PATH 1024
void dirwalk(char *dir, void(*fcn)(char *))
{
char name[MAX_PATH];
struct dirent *dp;
DIR *dfd;
if ((dfd = opendir(dir)) == NULL){
fprintf(stderr, "dirwalk: can't open %s\n", dir);
return;
}
while((dp = readdir(dfd)) != NULL){
if (strcmp(dp->d_name, ".") == 0 ||
strcmp(dp->d_name, "..") == 0)
continue; //skip self and parent
if (strlen(dir)+strlen(dp->d_name)+2 > sizeof(name))
fprintf(stderr, "dirwalk: name %s %s too long\n",
dir, dp->d_name);
else {
sprintf(name, "%s/%s", dir, dp->d_name);
(*fcn)(name);
}
}
closedir(dfd);
}