2 * --- T2-COPYRIGHT-NOTE-BEGIN ---
3 * This copyright note is auto-generated by ./scripts/Create-CopyPatch.
5 * T2 SDE: package/.../sysfiles/btee.c
6 * Copyright (C) 2004 - 2005 The T2 SDE Project
8 * More information can be found in the files COPYING and README.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2 of the License. A copy of the
13 * GNU General Public License can be found in the file COPYING.
14 * --- T2-COPYRIGHT-NOTE-END ---
16 /* btee.c, a buffered tee clone - written for ROCK Linux
18 Copyright (C) 1998, 1999, 2001, 2003 Clifford Wolf
20 This program is free software; you can redistribute it and/or modify
21 it under the terms of the GNU General Public License as published by
22 the Free Software Foundation; either version 2 of the License, or
23 (at your option) any later version.
25 This program is distributed in the hope that it will be useful,
26 but WITHOUT ANY WARRANTY; without even the implied warranty of
27 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 GNU General Public License for more details.
30 You should have received a copy of the GNU General Public License
31 along with this program; if not, write to the Free Software
32 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
39 #include <sys/types.h>
45 #define BUFFER_SIZE (8*1024-1)
46 static char buffer
[BUFFER_SIZE
+1];
50 void exit_handler(int sig
) {
54 int main(int argc
, char ** argv
) {
59 if ( argc
!=3 || (argv
[1][0]!='a' && argv
[1][0]!='t') ) {
60 printf("Usage: %s {a|t} [file]\n",argv
[0]);
65 mode
=O_WRONLY
|O_CREAT
|O_APPEND
;
67 mode
=O_WRONLY
|O_CREAT
|O_TRUNC
;
69 signal(SIGALRM
, exit_handler
);
77 if (pos
>= BUFFER_SIZE
) {
78 fprintf(stderr
, "%s: Buffer is full -> "
79 "drop data!\n",argv
[0]);
83 rc
=read(0,buffer
+pos
,BUFFER_SIZE
-pos
);
84 if (rc
<= 0) return 0;
88 for (x
=0; x
<rc
; x
++) {
89 if ( buffer
[pos
+x
] != EOT
)
90 write(1,buffer
+pos
+x
,1);
93 for (x
=0; x
<rc
; x
++) {
94 if (buffer
[pos
+x
]==EOT
) {
95 /* We wait a few seconds so we are
96 * still able to pipe thru 'early
97 * errors' from daemons. */
99 if (!killme
) killme
= 1;
102 if (buffer
[pos
+x
]=='\r' &&
103 buffer
[pos
+x
+1]!='\n') {
104 for (y
=pos
+x
; y
>=0; y
--) {
105 if (buffer
[y
]=='\n') break;
115 for (x
=y
=0; x
<pos
; x
++) {
117 buffer
[y
++]=buffer
[x
];
119 pos
=y
; remove_zeros
=0;
122 rc
=open(argv
[2],mode
,0666);
124 write(rc
,buffer
,pos
);
127 mode
=O_WRONLY
|O_APPEND
;