3 * cat startpoints.list | summer >data.list
4 * summer startpoints... >data.list
5 * prints md5sum of data-list to stderr
12 #include <sys/types.h>
23 #include "nettle/md5-compat.h"
29 static int quiet=0, hidectime=0, hideatime=0;
30 static int hidedirsize=0, hidelinkmtime=0;
31 static int filenamefieldsep=' ';
34 static void malloc_fail(void) { perror("summer: alloc failed"); exit(12); }
36 static void *mmalloc(size_t sz) {
38 r= malloc(sz); if (!r) malloc_fail();
42 static void *mrealloc(void *p, size_t sz) {
44 r= realloc(p, sz); if (!r && sz) malloc_fail();
48 static void fn_escaped(FILE *f, const char *fn) {
51 if (c>=33 && c<=126 && c!='\\') putc(c,f);
52 else fprintf(f,"\\x%02x",(int)(unsigned char)c);
56 static void add_pr(int *pr, int printf_ret) {
57 if (printf_ret == EOF) return;
61 static void vproblemx(const char *path, int padto, int per,
62 const char *fmt, va_list al) {
65 if (errfile==stderr) fputs("summer: error: ",stderr);
66 else add_pr(&pr, fprintf(errfile,"\\["));
68 add_pr(&pr, vfprintf(errfile,fmt,al));
69 if (per) add_pr(&pr, fprintf(errfile,": %s",strerror(e)));
71 if (errfile==stderr) {
73 fn_escaped(stderr,path);
78 add_pr(&pr, printf("]"));
84 static void problem_e(const char *path, int padto, const char *fmt, ...) {
87 vproblemx(path,padto,1,fmt,al);
91 static void problem(const char *path, int padto, const char *fmt, ...) {
94 vproblemx(path,padto,0,fmt,al);
98 static void csum_file(const char *path) {
102 unsigned char digest[16];
107 if (!f) { problem_e(path,sizeof(digest)*2,"open"); return; }
111 r= fread(db,1,sizeof(db),f);
113 problem_e(path,sizeof(digest)*2,"read");
116 if (!r) { assert(feof(f)); break; }
119 MD5Final(digest,&mc);
120 if (fclose(f)) { problem_e(path,sizeof(digest)*2,"close"); return; }
122 for (i=0; i<sizeof(digest); i++)
123 printf("%02x", digest[i]);
126 static void csum_dev(int cb, const struct stat *stab) {
127 printf("%c 0x%08lx %3lu %3lu %3lu %3lu ", cb,
128 (unsigned long)stab->st_rdev,
129 ((unsigned long)stab->st_rdev & 0x0ff000000U) >> 24,
130 ((unsigned long)stab->st_rdev & 0x000ff0000U) >> 16,
131 ((unsigned long)stab->st_rdev & 0x00000ff00U) >> 8,
132 ((unsigned long)stab->st_rdev & 0x0000000ffU) >> 0);
135 static void csum_str(const char *s) {
136 printf("%-*s", CSUMXL, s);
139 static void linktargpath(const char *linktarg) {
141 fn_escaped(stdout, linktarg);
149 static void *hardlinks;
151 static void pu10(void) { printf(" %10s", "?"); }
153 static int hardlink_compar(const void *av, const void *bv) {
154 const struct hardlink *a=av, *b=bv;
155 if (a->ino != b->ino) return b->ino - a->ino;
156 return b->dev - a->dev;
159 static void recurse(const char *path);
161 static void node(const char *path) {
162 char linktarg[MAXFN+1];
163 struct hardlink *foundhl;
164 const struct stat *stab;
168 r= lstat(path, &stabuf);
169 stab= r ? 0 : &stabuf;
172 if (stab && stab->st_nlink>1) {
173 struct hardlink *newhl, **foundhl_node;
174 newhl= mmalloc(sizeof(*newhl) + strlen(path));
175 newhl->dev= stab->st_dev;
176 newhl->ino= stab->st_ino;
177 foundhl_node= tsearch(newhl, &hardlinks, hardlink_compar);
178 if (!foundhl_node) malloc_fail();
179 foundhl= *foundhl_node;
180 if (foundhl!=newhl) {
181 free(newhl); /* hardlink to an earlier object */
183 foundhl= 0; /* new object with link count>1 */
184 strcpy(newhl->path, path);
188 if (!stab) problem_e(path,CSUMXL,"inaccessible");
189 else if (foundhl) csum_str("hardlink");
190 else if (S_ISREG(stab->st_mode)) csum_file(path);
191 else if (S_ISDIR(stab->st_mode)) csum_str("dir");
192 else if (S_ISCHR(stab->st_mode)) csum_dev('c',stab);
193 else if (S_ISBLK(stab->st_mode)) csum_dev('b',stab);
194 else if (S_ISFIFO(stab->st_mode)) csum_str("pipe");
195 else if (S_ISLNK(stab->st_mode)) csum_str("symlink");
196 else if (S_ISSOCK(stab->st_mode)) csum_str("sock");
197 else problem(path,CSUMXL,"badobj: 0x%lx", (unsigned long)stab->st_mode);
199 if (stab && S_ISLNK(stab->st_mode)) {
200 r= readlink(path, linktarg, sizeof(linktarg)-1);
201 if (r==sizeof(linktarg)) { problem(path,-1,"readlink too big"); r=-1; }
202 else if (r<0) { problem_e(path,-1,"readlink"); }
203 else assert(r<sizeof(linktarg));
205 if (r<0) strcpy(linktarg,"\\?");
210 if (S_ISDIR(stab->st_mode) && hidedirsize)
211 printf(" %10s","dir");
214 (unsigned long)stab->st_size);
216 printf(" %4o %10ld %10ld",
217 (unsigned)stab->st_mode & 07777U,
218 (unsigned long)stab->st_uid,
219 (unsigned long)stab->st_gid);
221 printf(" %10s %4s %10s %10s", "?","?","?","?");
227 (unsigned long)stab->st_atime);
233 if (S_ISLNK(stab->st_mode) && hidelinkmtime)
234 printf(" %10s","link");
237 (unsigned long)stab->st_mtime);
244 (unsigned long)stab->st_ctime);
249 putchar(filenamefieldsep);
250 fn_escaped(stdout, path);
252 if (foundhl) linktargpath(foundhl->path);
253 if (stab && S_ISLNK(stab->st_mode)) linktargpath(linktarg);
257 if (ferror(stdout)) { perror("summer: stdout"); exit(12); }
259 if (stab && S_ISDIR(stab->st_mode))
263 static void process(const char *startpoint) {
265 fprintf(stderr,"summer: processing: %s\n",startpoint);
267 tdestroy(hardlinks,free);
271 static int recurse_maxlen;
273 static int recurse_filter(const struct dirent *de) {
275 if (de->d_name[0]=='.' &&
277 (de->d_name[1]=='.' &&
280 l= strlen(de->d_name);
281 if (l > recurse_maxlen) recurse_maxlen= l;
285 static int recurse_compar(const void *av, const void *bv) {
286 const struct dirent *const *a=av, *const *b=bv;
287 return strcmp((*a)->d_name, (*b)->d_name);
290 static void recurse(const char *path_or_buf) {
292 static int buf_allocd;
294 struct dirent **namelist, *const *de;
295 const char *path_or_0= path_or_buf==buf ? 0 : path_or_buf;
296 int nentries, pathl, esave, buf_want, i;
298 pathl= strlen(path_or_buf);
300 nentries= scandir(path_or_buf, &namelist, recurse_filter, recurse_compar);
303 buf_want= pathl+1+recurse_maxlen+1;
304 if (buf_want > buf_allocd) {
305 buf= mrealloc(buf, buf_want);
306 buf_allocd= buf_want;
308 /* NOTE that path_or_buf is invalid after this point because
309 * it might have been realloc'd ! */
310 if (path_or_0) strcpy(buf,path_or_0);
315 strcpy(buf+pathl,"\\?"); errno= esave;
316 problem_e(buf,-1,"scandir failed");
319 for (i=0, de=namelist; i<nentries; i++, de++) {
320 strcpy(buf+pathl, (*de)->d_name);
327 static void from_stdin(void) {
333 fprintf(stderr, "summer: processing stdin lines as startpoints\n");
335 s= fgets(buf,sizeof(buf),stdin);
336 if (ferror(stdin)) { perror("summer: stdin"); exit(12); }
337 if (!s) { if (feof(stdin)) return; else abort(); }
340 if (buf[l-1]!='\n') { fprintf(stderr,"summer: line too long\n"); exit(8); }
346 int main(int argc, const char *const *argv) {
352 if ((arg=argv[1]) && *arg++=='-') {
357 "summer: usage: summer startpoint... >data.list\n"
358 " cat startpoints.list | summer >data.list\n");
364 filenamefieldsep= '\t';
382 fprintf(stderr,"summer: bad usage, try -h\n");
393 fprintf(stderr, "summer: processing command line args as startpoints\n");
394 while ((arg=*++argv)) {
398 if (ferror(stdout) || fclose(stdout)) {
399 perror("summer: stdout (at end)"); exit(12);
402 fputs("summer: done.\n", stderr);