208 lines
5.5 KiB
C
208 lines
5.5 KiB
C
/*
|
|
* CDE - Common Desktop Environment
|
|
*
|
|
* Copyright (c) 1993-2012, The Open Group. All rights reserved.
|
|
*
|
|
* These libraries and programs are free software; you can
|
|
* redistribute them and/or modify them under the terms of the GNU
|
|
* Lesser General Public License as published by the Free Software
|
|
* Foundation; either version 2 of the License, or (at your option)
|
|
* any later version.
|
|
*
|
|
* These libraries and programs are distributed in the hope that
|
|
* they will be useful, but WITHOUT ANY WARRANTY; without even the
|
|
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
* PURPOSE. See the GNU Lesser General Public License for more
|
|
* details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
* License along with these libraries and programs; if not, write
|
|
* to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
|
|
* Floor, Boston, MA 02110-1301 USA
|
|
*/
|
|
/* $XConsortium: tee.c /main/3 1995/11/01 19:08:18 rswiston $ */
|
|
/***************************************************************
|
|
* *
|
|
* AT&T - PROPRIETARY *
|
|
* *
|
|
* THIS IS PROPRIETARY SOURCE CODE LICENSED BY *
|
|
* AT&T CORP. *
|
|
* *
|
|
* Copyright (c) 1995 AT&T Corp. *
|
|
* All Rights Reserved *
|
|
* *
|
|
* This software is licensed by AT&T Corp. *
|
|
* under the terms and conditions of the license in *
|
|
* http://www.research.att.com/orgs/ssr/book/reuse *
|
|
* *
|
|
* This software was created by the *
|
|
* Software Engineering Research Department *
|
|
* AT&T Bell Laboratories *
|
|
* *
|
|
* For further information contact *
|
|
* gsf@research.att.com *
|
|
* *
|
|
***************************************************************/
|
|
|
|
/* : : generated by proto : : */
|
|
|
|
#if !defined(__PROTO__)
|
|
#if defined(__STDC__) || defined(__cplusplus) || defined(_proto) || defined(c_plusplus)
|
|
#if defined(__cplusplus)
|
|
#define __MANGLE__ "C"
|
|
#else
|
|
#define __MANGLE__
|
|
#endif
|
|
#define __STDARG__
|
|
#define __PROTO__(x) x
|
|
#define __OTORP__(x)
|
|
#define __PARAM__(n,o) n
|
|
#if !defined(__STDC__) && !defined(__cplusplus)
|
|
#if !defined(c_plusplus)
|
|
#define const
|
|
#endif
|
|
#define signed
|
|
#define void int
|
|
#define volatile
|
|
#define __V_ char
|
|
#else
|
|
#define __V_ void
|
|
#endif
|
|
#else
|
|
#define __PROTO__(x) ()
|
|
#define __OTORP__(x) x
|
|
#define __PARAM__(n,o) o
|
|
#define __MANGLE__
|
|
#define __V_ char
|
|
#define const
|
|
#define signed
|
|
#define void int
|
|
#define volatile
|
|
#endif
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
#define __VARARG__ ...
|
|
#else
|
|
#define __VARARG__
|
|
#endif
|
|
#if defined(__STDARG__)
|
|
#define __VA_START__(p,a) va_start(p,a)
|
|
#else
|
|
#define __VA_START__(p,a) va_start(p)
|
|
#endif
|
|
#endif
|
|
static const char id[] = "\n@(#)tee (AT&T Bell Laboratories) 04/01/93\0\n";
|
|
|
|
#include <cmdlib.h>
|
|
|
|
#include <ls.h>
|
|
#include <sig.h>
|
|
|
|
struct tee
|
|
{
|
|
Sfdisc_t disc;
|
|
int fd[1];
|
|
};
|
|
|
|
/*
|
|
* This discipline writes to each file in the list given in handle
|
|
*/
|
|
|
|
static int tee_write __PARAM__((Sfio_t* fp, const __V_* buf, int n, Sfdisc_t* handle), (fp, buf, n, handle)) __OTORP__(Sfio_t* fp; const __V_* buf; int n; Sfdisc_t* handle;){
|
|
const char* bp;
|
|
const char* ep;
|
|
int* hp = ((struct tee*)handle)->fd;
|
|
int fd = sffileno(fp);
|
|
int r;
|
|
|
|
do
|
|
{
|
|
bp = (const char*)buf;
|
|
ep = bp + n;
|
|
while (bp < ep)
|
|
{
|
|
if ((r = write(fd, bp, ep - bp)) <= 0)
|
|
return(-1);
|
|
bp += r;
|
|
}
|
|
} while ((fd = *hp++) >= 0);
|
|
return(n);
|
|
}
|
|
|
|
static Sfdisc_t tee_disc = { 0, tee_write, 0, 0, 0 };
|
|
|
|
int
|
|
b_tee __PARAM__((int argc, char** argv), (argc, argv)) __OTORP__(int argc; char** argv;){
|
|
struct tee* tp = 0;
|
|
int oflag = O_WRONLY|O_TRUNC|O_CREAT;
|
|
int n;
|
|
int* hp;
|
|
char* cp;
|
|
|
|
NoP(id[0]);
|
|
cmdinit(argv);
|
|
while (n = optget(argv, "ai [file...]")) switch (n)
|
|
{
|
|
case 'a':
|
|
oflag &= ~O_TRUNC;
|
|
oflag |= O_APPEND;
|
|
break;
|
|
case 'i':
|
|
signal(SIGINT, SIG_IGN);
|
|
break;
|
|
case ':':
|
|
error(2, opt_info.arg);
|
|
break;
|
|
case '?':
|
|
error(ERROR_usage(2), opt_info.arg);
|
|
break;
|
|
}
|
|
if(error_info.errors)
|
|
error(ERROR_usage(2), optusage(NiL));
|
|
argv += opt_info.index;
|
|
argc -= opt_info.index;
|
|
|
|
/*
|
|
* for backward compatibility
|
|
*/
|
|
|
|
if (*argv && streq(*argv, "-"))
|
|
{
|
|
signal(SIGINT, SIG_IGN);
|
|
argv++;
|
|
argc--;
|
|
}
|
|
if (argc > 0)
|
|
{
|
|
if (!(tp = (struct tee*)stakalloc(sizeof(struct tee) + argc * sizeof(int))))
|
|
error(ERROR_exit(1), "no space");
|
|
tp->disc = tee_disc;
|
|
hp = tp->fd;
|
|
while (cp = *argv++)
|
|
{
|
|
if ((*hp = open(cp, oflag, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0)
|
|
error(ERROR_system(0), "%s: cannot create", cp);
|
|
else hp++;
|
|
}
|
|
if (hp == tp->fd) tp = 0;
|
|
else
|
|
{
|
|
*hp = -1;
|
|
sfdisc(sfstdout, &tp->disc);
|
|
}
|
|
}
|
|
if (sfmove(sfstdin, sfstdout, SF_UNBOUND, -1) < 0 || !sfeof(sfstdin) || sferror(sfstdout))
|
|
error(ERROR_system(1), "cannot copy");
|
|
|
|
/*
|
|
* close files and free resources
|
|
*/
|
|
|
|
if (tp)
|
|
{
|
|
sfdisc(sfstdout, NiL);
|
|
for(hp = tp->fd; (n = *hp) >= 0; hp++)
|
|
close(n);
|
|
}
|
|
return(error_info.errors);
|
|
}
|