1 /* btee.c, a buffered tee clone - written for ROCK Linux
3 Copyright (C) 1998, 1999, 2001 Clifford Wolf
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <sys/types.h>
28 #define BUFFER_SIZE (8*1024-1)
29 static char buffer[BUFFER_SIZE+1];
33 int main(int argc, char ** argv) {
38 if ( argc!=3 || (argv[1][0]!='a' && argv[1][0]!='t') ) {
39 printf("Usage: %s {a|t} [file]\n",argv[0]);
44 mode=O_WRONLY|O_CREAT|O_APPEND;
46 mode=O_WRONLY|O_CREAT|O_TRUNC;
49 if (pos >= BUFFER_SIZE) {
50 fprintf(stderr,"%s: Buffer is full -> drop data!\n",argv[0]);
54 rc=read(0,buffer+pos,BUFFER_SIZE-pos);
55 if (rc <= 0) return 0;
59 write(1,buffer+pos,rc);
61 for (x=0; x<rc; x++) {
62 if (buffer[pos+x]==EOT) { loop=0; rc=x; break; }
63 if (buffer[pos+x]=='\r' &&
64 buffer[pos+x+1]!='\n') {
65 for (y=pos+x; y>=0; y--) {
66 if (buffer[y]=='\n') break;
76 for (x=y=0; x<pos; x++) {
78 buffer[y++]=buffer[x];
80 pos=y; remove_zeros=0;
83 rc=open(argv[2],mode,0666);
88 mode=O_WRONLY|O_APPEND;