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