tee.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. default:
  32. fprint(2, "usage: tee [-ai] [file ...]\n");
  33. exits("usage");
  34. } ARGEND
  35. openf = malloc((1+argc)*sizeof(int));
  36. if(openf == nil)
  37. sysfatal("out of memory: %r");
  38. n = 0;
  39. while(*argv) {
  40. if(aflag) {
  41. openf[n] = open(argv[0], OWRITE);
  42. if(openf[n] < 0)
  43. openf[n] = create(argv[0], OWRITE, 0666);
  44. seek(openf[n], 0L, 2);
  45. } else
  46. openf[n] = create(argv[0], OWRITE, 0666);
  47. if(openf[n] < 0) {
  48. fprint(2, "tee: cannot open %s: %r\n", argv[0]);
  49. } else
  50. n++;
  51. argv++;
  52. }
  53. openf[n++] = 1;
  54. for(;;) {
  55. r = read(0, in, sizeof in);
  56. if(r <= 0)
  57. exits(nil);
  58. for(i=0; i<n; i++)
  59. write(openf[i], in, r);
  60. }
  61. }
  62. int
  63. intignore(void *a, char *msg)
  64. {
  65. USED(a);
  66. if(strcmp(msg, "interrupt") == 0)
  67. return 1;
  68. return 0;
  69. }