tee.c 1.4 KB

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