da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/***********************************************************************
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* This software is part of the ast package *
3e14f97f673e8a630f076077de35afdd43dc1587Roger A. Faulkner* Copyright (c) 1992-2010 AT&T Intellectual Property *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* and is licensed under the *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Common Public License, Version 1.0 *
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin* by AT&T Intellectual Property *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* A copy of the License is available at *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Information and Software Systems Research *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* AT&T Research *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Florham Park NJ *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Glenn Fowler <gsf@research.att.com> *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* David Korn <dgk@research.att.com> *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin***********************************************************************/
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * David Korn
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * AT&T Bell Laboratories
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz"[-?\n@(#)$Id: tee (AT&T Research) 2009-06-19 $\n]"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin"[+NAME?tee - duplicate standard input]"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin"[+DESCRIPTION?\btee\b copies standard input to standard output "
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "and to zero or more files. The options determine whether "
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "the specified files are overwritten or appended to. The "
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "\btee\b utility does not buffer output. If writes to any "
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "\afile\a fail, writes to other files continue although \btee\b "
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "will exit with a non-zero exit status.]"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin"[+?The number of \afile\a operands that can be specified is limited "
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "by the underlying operating system.]"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin"[a:append?Append the standard input to the given files rather "
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "than overwriting them.]"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin"[i:ignore-interrupts?Ignore SIGINT signal.]"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin"[l:linebuffer?Set the standard output to be line buffered.]"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin"\n[file ...]\n"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin"[+EXIT STATUS?]{"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "[+0?All files copies successfully.]"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin "[+>0?An error occurred.]"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin"[+SEE ALSO?\bcat\b(1), \bsignal\b(3)]"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * This discipline writes to each file in the list given in handle
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainztee_write(Sfio_t* fp, const void* buf, size_t n, Sfdisc_t* handle)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin register ssize_t r;
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz register int n;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin register int oflag = O_WRONLY|O_TRUNC|O_CREAT|O_BINARY;
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz if (context && (tp = (Tee_t*)sh_context(context)->data))
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz cmdinit(argc, argv, context, ERROR_CATALOG, ERROR_CALLBACK);
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz if (tp = (Tee_t*)stakalloc(sizeof(Tee_t) + argc * sizeof(int)))
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz if ((*hp = open(cp, oflag, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0)
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz error(ERROR_system(0), "%s: cannot create", cp);
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz if ((sfmove(sfstdin, sfstdout, SF_UNBOUND, -1) < 0 || !sfeof(sfstdin)) && errno != EPIPE)