9base

revived minimalist port of Plan 9 userland to Unix
git clone git://git.suckless.org/9base
Log | Files | Refs | README | LICENSE

tee.c (1062B)


      1 /*
      2  * tee-- pipe fitting
      3  */
      4 
      5 #include <u.h>
      6 #include <libc.h>
      7 
      8 int	uflag;
      9 int	aflag;
     10 int	openf[100];
     11 
     12 char in[8192];
     13 
     14 int	intignore(void*, char*);
     15 
     16 void
     17 main(int argc, char **argv)
     18 {
     19 	int i;
     20 	int r, n;
     21 
     22 	ARGBEGIN {
     23 	case 'a':
     24 		aflag++;
     25 		break;
     26 
     27 	case 'i':
     28 		atnotify(intignore, 1);
     29 		break;
     30 
     31 	case 'u':
     32 		uflag++;
     33 		/* uflag is ignored and undocumented; it's a relic from Unix */
     34 		break;
     35 
     36 	default:
     37 		fprint(2, "usage: tee [-ai] [file ...]\n");
     38 		exits("usage");
     39 	} ARGEND
     40 
     41 	USED(argc);
     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 
     59 	for(;;) {
     60 		r = read(0, in, sizeof in);
     61 		if(r <= 0)
     62 			exits(nil);
     63 		for(i=0; i<n; i++)
     64 			write(openf[i], in, r);
     65 	}
     66 }
     67 
     68 int
     69 intignore(void *a, char *msg)
     70 {
     71 	USED(a);
     72 	if(strcmp(msg, "interrupt") == 0)
     73 		return 1;
     74 	return 0;
     75 }