1 /*:ts=8*/
2 /*****************************************************************************
3 * FIDOGATE --- Gateway UNIX Mail/News <-> FIDO NetMail/EchoMail
4 *
5 * $Id: read.c,v 4.8 2004/08/22 20:19:11 n0ll Exp $
6 *
7 * Read lines from stdio FILE, optionally limited by size parameter
8 *
9 *****************************************************************************
10 * Copyright (C) 1990-2004
11 * _____ _____
12 * | |___ | Martin Junius <mj.at.n0ll.dot.net>
13 * | | | | | | Radiumstr. 18
14 * |_|_|_|@home| D-51069 Koeln, Germany
15 *
16 * This file is part of FIDOGATE.
17 *
18 * FIDOGATE is free software; you can redistribute it and/or modify it
19 * under the terms of the GNU General Public License as published by the
20 * Free Software Foundation; either version 2, or (at your option) any
21 * later version.
22 *
23 * FIDOGATE is distributed in the hope that it will be useful, but
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 * General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with FIDOGATE; see the file COPYING. If not, write to the Free
30 * Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
31 *****************************************************************************/
32
33 #include "fidogate.h"
34
35
36
37 static long read_size = -1;
38
39
40
41 /***** Read line from FILE, like fgets() but with read_size checking *********/
42
read_line(char * buf,int n,FILE * stream)43 char *read_line(char *buf, int n, FILE *stream)
44 {
45 int c;
46 char *s;
47
48 if(n <= 1)
49 return NULL;
50 if(read_size!=-1 && read_size<=0)
51 return NULL;
52 if( (c = getc(stream)) == EOF )
53 return NULL;
54
55 s = buf;
56 n--;
57
58 while(TRUE)
59 {
60 *s++ = c;
61 n--;
62 if(read_size != -1)
63 read_size--;
64 if( n<=0 || (read_size!=-1 && read_size<=0) )
65 break;
66 if(c == '\n')
67 {
68 *s = 0;
69 return buf;
70 }
71 if( (c = getc(stream)) == EOF )
72 {
73 *s = 0;
74 return buf;
75 }
76 }
77
78 *s = 0;
79 return buf;
80 }
81
82
83
84 /***** Read "#! rnews nnnn" and set read_size ********************************/
85
read_rnews_size(FILE * stream)86 long read_rnews_size(FILE *stream)
87 {
88 char buffer[32];
89 long n;
90
91 if(!fgets(buffer, sizeof(buffer), stream))
92 return 0;
93 if(buffer[0]!='#' && strlen(buffer)<10)
94 return -1;
95 if(strncmp(buffer, "#! rnews ", 9))
96 return -1;
97
98 n = atol(buffer + 9);
99 if(n > 0)
100 {
101 read_size = n;
102 return n;
103 }
104 else
105 {
106 read_size = -1;
107 return 0;
108 }
109 }
110
111
112
113
114 #ifdef TEST
115
main(int argc,char * argv[])116 int main(int argc, char *argv[])
117 {
118 #if 0
119 char test[16];
120 char buffer[16];
121 int i;
122
123 memset(test, 0, sizeof(test));
124
125 while(read_line(buffer, sizeof(buffer), stdin))
126 {
127 printf("n=%d buffer=<%s>\n", strlen(buffer), buffer);
128 }
129
130 for(i=0; i<sizeof(test); i++)
131 if(test[i])
132 printf("Error - overwrite!\n");
133 #endif
134 #if 1
135 char buffer[512];
136 long n, lines, articles;
137
138 articles = 0;
139 while((n = read_rnews_size(stdin)) > 0)
140 {
141 printf("size=%ld ", n);
142 lines = 0;
143 articles++;
144
145 while(read_line(buffer, sizeof(buffer), stdin))
146 if(buffer[strlen(buffer) - 1] == '\n')
147 lines++;
148 printf("lines=%ld\n", lines);
149 }
150 printf("articles=%ld\n", articles);
151 if(n < 0)
152 printf("Error!\n");
153 #endif
154 }
155
156 #endif /**TEST**/
157