Victor86B digital multimeter app
[tridge/junkcode.git] / findbroken.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <dirent.h>
5 #include <sys/stat.h>
6
7
8 static void findit(char *dir)
9 {
10         DIR *d;
11         struct dirent *de;
12         
13         d = opendir(dir);
14         if (!d) return;
15         
16         
17         while ((de = readdir(d))) {
18                 char *fname;
19                 struct stat st, st2;
20                 
21                 if (strcmp(de->d_name,".")==0) continue;
22                 if (strcmp(de->d_name,"..")==0) continue;
23                 
24                 fname = (char *)malloc(strlen(dir) + strlen(de->d_name) + 2);
25                 if (!fname) {
26                         fprintf(stderr,"out of memory\n");
27                         exit(1);
28                 }
29                 sprintf(fname,"%s/%s", dir, de->d_name);
30                 
31                 if (lstat(fname, &st)) {
32                         perror(fname);
33                         continue;
34                 }
35                 
36                 if (S_ISLNK(st.st_mode) && 
37                     (stat(fname,&st2) || st2.st_uid != st.st_uid)) {
38                         char buf[1024];
39                         readlink(fname, buf, sizeof(buf)-1);
40                         printf("%s -> %s\n", fname, buf);
41                 }
42                 
43                 if (S_ISDIR(st.st_mode)) {
44                         findit(fname);
45                 }
46                 
47                 free(fname);
48         }
49         
50         closedir(d);
51 }
52
53
54 int main(int argc, char *argv[])
55 {
56         int size;
57         
58         if (argc < 2) {
59                 fprintf(stderr,"%s: <dir>\n", argv[0]);
60                 exit(1);
61         }
62         
63         findit(argv[1]);
64         return 0;
65 }