clog.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <bio.h>
  12. char *argv0;
  13. int
  14. openlog(char *name)
  15. {
  16. int fd;
  17. fd = open(name, OWRITE);
  18. if(fd < 0){
  19. fprint(2, "%s: can't open %s: %r\n", argv0, name);
  20. return -1;
  21. }
  22. seek(fd, 0, 2);
  23. return fd;
  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. argv0 = argv[0];
  33. if(argc != 4){
  34. fprint(2, "usage: %s console logfile prefix\n", argv0);
  35. exits("usage");
  36. }
  37. fd = open(argv[1], OREAD);
  38. if(fd < 0){
  39. fprint(2, "%s: can't open %s: %r\n", argv0, argv[1]);
  40. exits("open");
  41. }
  42. Binit(&in, fd, OREAD);
  43. fd = openlog(argv[2]);
  44. for(;;){
  45. if(p = Brdline(&in, '\n')){
  46. p[Blinelen(&in)-1] = 0;
  47. if(fprint(fd, "%s: %s\n", argv[3], p) < 0){
  48. close(fd);
  49. fd = openlog(argv[2]);
  50. fprint(fd, "%s: %s\n", t, p);
  51. }
  52. } else if(Blinelen(&in) == 0) // true eof
  53. break;
  54. else {
  55. Bread(&in, buf, sizeof buf);
  56. }
  57. }
  58. exits(0);
  59. }