auxclog.c (1112B)
1 #include <u.h> 2 #include <libc.h> 3 #include <bio.h> 4 5 #define openlog auxclog_openlog /* libc symbol on OS X */ 6 7 char *argv0; 8 9 int 10 openlog(char *name) 11 { 12 int fd; 13 14 fd = open(name, OWRITE); 15 if(fd < 0) 16 fd = create(name, OWRITE, DMAPPEND|0666); 17 if(fd < 0){ 18 fprint(2, "%s: can't open %s: %r\n", argv0, name); 19 return -1; 20 } 21 seek(fd, 0, 2); 22 return fd; 23 } 24 25 void 26 main(int argc, char **argv) 27 { 28 Biobuf in; 29 int fd; 30 char *p, *t; 31 char buf[8192]; 32 33 argv0 = argv[0]; 34 if(argc != 3){ 35 fprint(2, "usage: %s console logfile \n", argv0); 36 exits("usage"); 37 } 38 39 if(strcmp(argv[1], "-") == 0) 40 fd = 0; 41 else 42 fd = open(argv[1], OREAD); 43 if(fd < 0){ 44 fprint(2, "%s: can't open %s: %r\n", argv0, argv[1]); 45 exits("open"); 46 } 47 Binit(&in, fd, OREAD); 48 49 fd = openlog(argv[2]); 50 51 for(;;){ 52 if(p = Brdline(&in, '\n')){ 53 p[Blinelen(&in)-1] = 0; 54 t = ctime(time(0)); 55 t[19] = 0; 56 if(fprint(fd, "%s: %s\n", t, p) < 0){ 57 close(fd); 58 fd = openlog(argv[2]); 59 fprint(fd, "%s: %s\n", t, p); 60 } 61 } else if(Blinelen(&in) == 0) /* true eof */ 62 break; 63 else { 64 Bread(&in, buf, sizeof buf); 65 } 66 } 67 exits(0); 68 }