cleanup zombies during run, and check processes still exist
[tridge/junkcode.git] / findlarge.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 void findit(int size, 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;
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 (st.st_size >= size) {
37       printf("%s %dk\n", fname, (int)(st.st_size/1024));
38     }
39
40     if (S_ISDIR(st.st_mode)) {
41       findit(size, fname);
42     }
43
44     free(fname);
45   }
46
47   closedir(d);
48   
49 }
50
51
52 int main(int argc, char *argv[])
53 {
54   int size;
55
56   if (argc < 3) {
57     fprintf(stderr,"%s: <minsize> <dir>\n", argv[0]);
58     exit(1);
59   }
60
61   size = atoi(argv[1]);
62
63   findit(size, argv[2]);
64   return 0;
65 }