Debian Codegraph @

trip/filter.c

raw
1 /*
2 * Reads stdin and writes it to stdout or stderr depending on parameters.
3 * 1) "line" or "char"
4 * "line" based, stdin is split into lines
5 * "char" based, treat as binary blob - default
6 * 2) "stderr" or not supplied
7 * "stderr", if "line" based every second line written to stderr (others written to stdout)
8 * if "char" based all chars written to stderr
9 * if not supplied everthing written to stdout - default
10 */
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <signal.h>
14 #include <fcntl.h>
15
16 #define MAX_BUF 20000000
17
18 void writeline( FILE *fp, char *buf, int line_based, int use_stderr, int *to_stdout )
19 {
20 FILE *fp1;
21 char *where;
22 if ( use_stderr )
23 {
24 if ( *to_stdout )
25 {
26 fp1 = stdout;
27 where = "stdout";
28 *to_stdout = 0;
29 }
30 else
31 {
32 fp1 = stderr;
33 where = "stderr";
34 *to_stdout = 1;
35 }
36 }
37 else
38 {
39 fp1 = stdout;
40 where = "stdout";
41 *to_stdout = 0;
42 }
43 fprintf( fp1, "%s\n", buf );
44 // FGC: Speedup: fprintf(fp,"write to %s use_stderr %d to_stdout %d buf[%s]\n",where, use_stderr, *to_stdout,buf);
45 }
46
47 int main( int argc, char *argv[] )
48 {
49 char ch[2];
50 char *buf;
51 char *start;
52 size_t rc;
53 int count = 0, i=0;
54 int use_stderr = 0;
55 int to_stdout = 1, skip;
56 int line_based = 0;
57 long max_buf = 1000;
58 FILE *fp;
59
60 #ifdef SIGPIPE
61 signal( SIGPIPE, SIG_IGN );
62 #endif
63
64 fp = fopen( "filter.log", "a" );
65 if ( argc > 1 )
66 {
67 max_buf = atol( argv[1] );
68 if ( max_buf == -1 )
69 {
70 fprintf( fp,"Invalid argument 1: %s. Should be a valid number\n", argv[1] );
71 fclose(fp);
72 exit( 1 );
73 }
74 }
75 if ( argc > 2 )
76 {
77 if ( strcmp( argv[2], "line" ) == 0 )
78 line_based = 1;
79 else if ( strcmp( argv[2], "char" ) == 0 )
80 line_based = 0;
81 else
82 {
83 fprintf( fp,"Invalid argument 2: %s. Should be \"line\" or \"char\"\n", argv[2] );
84 fclose(fp);
85 exit( 1 );
86 }
87 }
88 if ( argc > 3 )
89 {
90 if ( strcmp( argv[3], "stderr" ) == 0 )
91 use_stderr = 1;
92 else
93 {
94 fprintf( fp,"Invalid argument 3: %s. Should be \"stderr\" or not supplied\n", argv[3] );
95 fclose(fp);
96 exit( 1);
97 }
98 }
99 fprintf( fp,"\nfilter starting. Args:");
100 for ( i = 1; i < argc; i++ )
101 {
102 fprintf( fp," %s", argv[i]);
103 }
104 fprintf( fp,"\n");
105 #ifdef _MSC_VER
106 _setmode(_fileno(stdin), _O_BINARY);
107 _setmode(_fileno(stdout), _O_BINARY);
108 _setmode(_fileno(stderr), _O_BINARY);
109 #endif
110 buf = malloc( max_buf+1 );
111 if ( buf == NULL )
112 {
113 fprintf( fp,"Unable to allocate: %d bytes. Aborting\n", max_buf );
114 fclose(fp);
115 exit( 1 );
116 }
117 skip = 0;
118 for( i = 0;;)
119 {
120 rc = fread( &ch, sizeof(char), 1, stdin );
121 if ( rc == EOF)
122 {
123 fprintf( fp,"filter: End read with EOF char.\n");
124 break;
125 }
126 if ( feof( stdin ) )
127 {
128 fprintf( fp,"filter: End read with feof().\n");
129 break;
130 }
131 count++;
132 if ( count == max_buf )
133 {
134 fprintf( fp,"Buffer size of %d exceeded. Aborting\n", max_buf );
135 fclose(fp);
136 exit( 1 );
137 }
138 if ( skip == 0 )
139 {
140 if ( line_based )
141 {
142
143 #ifdef _MSC_VER
144 if ( ch[0] == 0x0d )
145 {
146 skip = 1;
147 #else
148 if ( ch[0] == 0x0a )
149 {
150 skip = 0;
151 #endif
152 buf[i] = '\0';
153 writeline( fp, buf, line_based, use_stderr, &to_stdout );
154 i = 0;
155 }
156 else
157 {
158 buf[i] = ch[0];
159 i++;
160 }
161 }
162 else
163 {
164 buf[i] = ch[0];
165 i++;
166 }
167 }
168 else
169 skip = 0;
170 }
171 if ( line_based == 0 )
172 {
173 if ( use_stderr )
174 fwrite( buf, sizeof(char), i, stderr );
175 else
176 fwrite( buf, sizeof(char), i, stdout );
177 }
178 else
179 {
180 buf[i] = '\0';
181 if ( strlen( buf ) != 0 )
182 writeline( fp, buf, line_based, use_stderr, &to_stdout );
183 }
184 fprintf( fp,"filter: Read %d chars. Could have read %d chars.\n",count,max_buf);
185 fclose(fp);
186 exit(0);
187 }