print.c (991B)
1 #include <u.h> 2 #include <libc.h> 3 #include <bio.h> 4 5 /* 6 given uuids prints entries for human consumption 7 */ 8 9 char *wdb = nil; 10 char *data; 11 Biobuf *bout; 12 13 void 14 usage(void) 15 { 16 fprint(2, "usage: %s [-w dir] uuid [uuid ...]\n", argv0); 17 exits("usage"); 18 } 19 20 int 21 fetchval(char *buf, char *uuid, char *field) 22 { 23 int n; 24 char *path = smprint("%s/data/%s/%s", wdb, uuid, field); 25 int fd = open(path, OREAD); 26 free(path); 27 if (fd < 0) sysfatal("%r"); 28 n = read(fd, buf, 8096); 29 close(fd); 30 return n; 31 } 32 33 void 34 printuuid(char *uuid) 35 { 36 char *buf = malloc(8096); 37 int n = fetchval(buf, uuid, "name"); 38 if (n > 0) buf[n - 1] = '\0'; 39 Bprint(bout, "%s/\n%s\n", uuid, buf); 40 free(buf); 41 } 42 43 void 44 main(int argc, char **argv) 45 { 46 ARGBEGIN{ 47 case 'w': 48 wdb = EARGF(usage()); 49 break; 50 default: 51 usage(); 52 }ARGEND; 53 54 if (argc == 0) usage(); 55 56 bout = Bfdopen(1, OWRITE); 57 58 if (wdb == nil) wdb = smprint("/n/wdb"); 59 60 int i; 61 for (i = 0; i < argc; i++) { 62 if (i > 0) Bprint(bout, "----\n"); 63 printuuid(argv[i]); 64 } 65 }