clog.c 920 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. char *argv0;
  5. int
  6. openlog(char *name)
  7. {
  8. int fd;
  9. fd = open(name, OWRITE);
  10. if(fd < 0){
  11. fprint(2, "%s: can't open %s: %r\n", argv0, name);
  12. return -1;
  13. }
  14. seek(fd, 0, 2);
  15. return fd;
  16. }
  17. void
  18. main(int argc, char **argv)
  19. {
  20. Biobuf in;
  21. int fd;
  22. char *p, *t;
  23. char buf[8192];
  24. argv0 = argv[0];
  25. if(argc != 4){
  26. fprint(2, "usage: %s console logfile prefix\n", argv0);
  27. exits("usage");
  28. }
  29. fd = open(argv[1], OREAD);
  30. if(fd < 0){
  31. fprint(2, "%s: can't open %s: %r\n", argv0, argv[1]);
  32. exits("open");
  33. }
  34. Binit(&in, fd, OREAD);
  35. fd = openlog(argv[2]);
  36. for(;;){
  37. if(p = Brdline(&in, '\n')){
  38. p[Blinelen(&in)-1] = 0;
  39. if(fprint(fd, "%s: %s\n", argv[3], p) < 0){
  40. close(fd);
  41. fd = openlog(argv[2]);
  42. fprint(fd, "%s: %s\n", t, p);
  43. }
  44. } else if(Blinelen(&in) == 0) // true eof
  45. break;
  46. else {
  47. Bread(&in, buf, sizeof buf);
  48. }
  49. }
  50. exits(0);
  51. }