tee.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * tee-- pipe fitting
  3. */
  4. #include <u.h>
  5. #include <libc.h>
  6. int uflag;
  7. int aflag;
  8. int openf[100];
  9. char in[8192];
  10. int intignore(void*, char*);
  11. void
  12. main(int argc, char **argv)
  13. {
  14. int i;
  15. int r, n;
  16. ARGBEGIN {
  17. case 'a':
  18. aflag++;
  19. break;
  20. case 'i':
  21. atnotify(intignore, 1);
  22. break;
  23. case 'u':
  24. uflag++;
  25. /* uflag is ignored and undocumented; it's a relic from Unix */
  26. break;
  27. default:
  28. fprint(2, "usage: tee [-ai] [file ...]\n");
  29. exits("usage");
  30. } ARGEND
  31. USED(argc);
  32. n = 0;
  33. while(*argv) {
  34. if(aflag) {
  35. openf[n] = open(argv[0], OWRITE);
  36. if(openf[n] < 0)
  37. openf[n] = create(argv[0], OWRITE, 0666);
  38. seek(openf[n], 0L, 2);
  39. } else
  40. openf[n] = create(argv[0], OWRITE, 0666);
  41. if(openf[n] < 0) {
  42. fprint(2, "tee: cannot open %s: %r\n", argv[0]);
  43. } else
  44. n++;
  45. argv++;
  46. }
  47. openf[n++] = 1;
  48. for(;;) {
  49. r = read(0, in, sizeof in);
  50. if(r <= 0)
  51. exits(nil);
  52. for(i=0; i<n; i++)
  53. write(openf[i], in, r);
  54. }
  55. }
  56. int
  57. intignore(void *a, char *msg)
  58. {
  59. USED(a);
  60. if(strcmp(msg, "interrupt") == 0)
  61. return 1;
  62. return 0;
  63. }