* updated kmousetool (21.12.1 -> 21.12.2), untested
[t2-trunk.git] / package / base / sysfiles / btee.c
blobc204f0c5e0d67f02e8ff203f0ba56be76038c45d
1 /*
2 * --- T2-COPYRIGHT-NOTE-BEGIN ---
3 * This copyright note is auto-generated by ./scripts/Create-CopyPatch.
4 *
5 * T2 SDE: package/.../sysfiles/btee.c
6 * Copyright (C) 2004 - 2005 The T2 SDE Project
7 *
8 * More information can be found in the files COPYING and README.
9 *
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.
35 #define _GNU_SOURCE
37 #include <stdio.h>
38 #include <unistd.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <signal.h>
42 #include <stdlib.h>
43 #include <fcntl.h>
45 #define BUFFER_SIZE (8*1024-1)
46 static char buffer[BUFFER_SIZE+1];
48 #define EOT 004
50 void exit_handler(int sig) {
51 exit(1);
54 int main(int argc, char ** argv) {
55 int rc, mode, x, y;
56 int remove_zeros=0;
57 int pos=0, killme=0;
59 if ( argc!=3 || (argv[1][0]!='a' && argv[1][0]!='t') ) {
60 printf("Usage: %s {a|t} [file]\n",argv[0]);
61 return 1;
64 if (argv[1][0]=='a')
65 mode=O_WRONLY|O_CREAT|O_APPEND;
66 else
67 mode=O_WRONLY|O_CREAT|O_TRUNC;
69 signal(SIGALRM, exit_handler);
71 while (1) {
72 if (killme == 1) {
73 killme = -1;
74 alarm(3);
77 if (pos >= BUFFER_SIZE) {
78 fprintf(stderr, "%s: Buffer is full -> "
79 "drop data!\n",argv[0]);
80 pos=0;
83 rc=read(0,buffer+pos,BUFFER_SIZE-pos);
84 if (rc <= 0) return 0;
85 buffer[pos+rc+1]=0;
87 if (rc>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. */
98 buffer[pos+x]=0;
99 if (!killme) killme = 1;
100 remove_zeros=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;
106 buffer[y]=0;
108 remove_zeros=1;
112 pos+=rc;
114 if (remove_zeros) {
115 for (x=y=0; x<pos; x++) {
116 if (buffer[x])
117 buffer[y++]=buffer[x];
119 pos=y; remove_zeros=0;
122 rc=open(argv[2],mode,0666);
123 if (rc>=0) {
124 write(rc,buffer,pos);
125 close(rc);
126 pos=0;
127 mode=O_WRONLY|O_APPEND;
132 return 0;