/* tee.c -- copy stdin to both output and named files */ /* $Id$ */ /* Carlos Duarte , 990404 */ #include #include #include static int do_args(int first, int ac, char *av[]); #define ARG() \ s[1] ? (t=s+1, s+=strlen(s)-1, t) : ++i #ifdef HAVE_SYS_RESOURCE_H #include #include #include static int max_nr_files() { struct rlimit rlimbuf; if (getrlimit(RLIMIT_NOFILE, &rlimbuf) == -1) return 15; return rlimbuf.rlim_cur; } #else #define max_nr_files() (18) #endif int tee(FILE *rf, char *av[], int nfiles) { FILE *wf[256]; int maxf = max_nr_files()-3; /* stdin, out, err */ char *mode; int i, j; FILE *orig_rf = rf; if (!nfiles) { int c; while ((c = getc(rf)) != EOF) putchar(c); return 0; } if (opts.append) mode = "ab"; else mode = "wb"; if (maxf > sizeof(wf)/sizeof(*wf)) maxf = sizeof(wf)/sizeof(*wf); for (i=0; i= nfiles) top = nfiles; for (j=i; j 0) { /* on first run, write also to stdout */ if (i == 0) fwrite(buf, sizeof(*buf), n, stdout); for (j=i; j shold have the same contents and * rewindable */ if (orig_rf == rf) { for (j=i; j