8fcb1f9d6f2c9cb5b4d92ba5c639e395766f23d8
[dragonfly.git] / test / fastbulk / getpkgsrcdir.c
1 /*
2  *
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <string.h>
9 #include <sys/stat.h>
10
11 static void usage(void);
12
13 /*
14  * Process paths on stdin and generate a directory path if it looks
15  * like a pkgsrc directory.
16  *
17  * av[1]
18  */
19 int
20 main(int ac, char **av)
21 {
22         char buf[1024];
23         char *path;
24         char *bpath;
25         struct stat st;
26         size_t len;
27         size_t blen;
28         int count;
29         int ch;
30         int stripOpt = 0;
31
32         while ((ch = getopt(ac, av, "s")) != -1) {
33                 switch(ch) {
34                 case 's':
35                         stripOpt = 1;
36                         break;
37                 default:
38                         usage();
39                         /* NOT REACHED */
40                 }
41         }
42         ac -= optind;
43         av += optind;
44         if (ac != 1) {
45                 fprintf(stderr, "requires base directory as first argument\n");
46                 exit(1);
47         }
48
49         /*
50          * Base dir
51          */
52         bpath = strdup(av[0]);
53         blen = strlen(bpath);
54         while (blen && bpath[blen-1] == '/')
55                 --blen;
56         bpath[blen] = 0;
57
58         /*
59          * Process lines
60          */
61         while (fgets(buf, sizeof(buf) - 32, stdin) != NULL) {
62                 path = strtok(buf, " \t\r\n");
63                 if (path == NULL || *path == 0)
64                         continue;
65                 len = strlen(path);
66                 if (len < blen || bcmp(path, bpath, blen) != 0)
67                         continue;
68                 if (stat(path, &st) != 0)
69                         continue;
70                 len = strlen(path);
71                 if (!S_ISDIR(st.st_mode)) {
72                         while (len && path[len-1] != '/')
73                                 --len;
74                 }
75                 while (len && path[len-1] == '/')
76                         --len;
77                 strcpy(path + len, "/Makefile");
78                 if (stat(path, &st) != 0)
79                         continue;
80                 strcpy(path + len, "/DESCR");
81                 if (stat(path, &st) != 0)
82                         continue;
83                 path[len] = 0;
84
85                 /*
86                  * Must be at least one sub-directory
87                  */
88                 count = 0;
89                 for (len = blen; path[len]; ++len) {
90                         if (path[len] == '/')
91                                 ++count;
92                 }
93                 if (count < 2)
94                         continue;
95                 if (stripOpt)
96                         printf("%s\n", path + blen + 1);
97                 else
98                         printf("%s\n", path);
99         }
100         return(0);
101 }
102
103 static void
104 usage(void)
105 {
106         fprintf(stderr, "getpkgsrcdir: unsupported option\n");
107         exit(1);
108 }