tee.c 1.5 KB

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