comparison lib-src/b2m.c @ 173:8eaf7971accc r20-3b13

Import from CVS: tag r20-3b13
author cvs
date Mon, 13 Aug 2007 09:49:09 +0200
parents 43dd3413c7c7
children 2d532a89d707
comparison
equal deleted inserted replaced
172:a38aed19690b 173:8eaf7971accc
68 { 68 {
69 long size; 69 long size;
70 char *buffer; 70 char *buffer;
71 }; 71 };
72 72
73 extern char *strtok(); 73
74 74 static long *xmalloc (unsigned int);
75 long *xmalloc (), *xrealloc (); 75 static long *xrealloc (void *, unsigned int);
76 char *concat (); 76 static char *concat (char *s1, char *s2, char *s3);
77 long readline (); 77 static long readline (struct linebuffer *, FILE *);
78 void fatal (char *); 78 static void fatal (char *);
79 79
80 /* 80 /*
81 * xnew -- allocate storage. SYNOPSIS: Type *xnew (int n, Type); 81 * xnew -- allocate storage. SYNOPSIS: Type *xnew (int n, Type);
82 */ 82 */
83 #define xnew(n, Type) ((Type *) xmalloc ((n) * sizeof (Type))) 83 #define xnew(n, Type) ((Type *) xmalloc ((n) * sizeof (Type)))
85 85
86 86
87 char *progname; 87 char *progname;
88 88
89 int 89 int
90 main (int argc, char **argv) 90 main (int argc, char *argv[])
91 { 91 {
92 logical labels_saved, printing, header; 92 logical labels_saved, printing, header;
93 time_t ltoday; 93 time_t ltoday;
94 char *labels, *p, *today; 94 char *labels = NULL, *p, *today;
95 struct linebuffer data; 95 struct linebuffer data;
96 96
97 #ifdef MSDOS 97 #ifdef MSDOS
98 _fmode = O_BINARY; /* all of files are treated as binary files */ 98 _fmode = O_BINARY; /* all of files are treated as binary files */
99 #if __DJGPP__ > 1 99 #if __DJGPP__ > 1
141 /* Save labels. */ 141 /* Save labels. */
142 readline (&data, stdin); 142 readline (&data, stdin);
143 p = strtok (data.buffer, " ,\r\n\t"); 143 p = strtok (data.buffer, " ,\r\n\t");
144 labels = "X-Babyl-Labels: "; 144 labels = "X-Babyl-Labels: ";
145 145
146 while (p = strtok (NULL, " ,\r\n\t")) 146 while ((p = strtok (NULL, " ,\r\n\t")))
147 labels = concat (labels, p, ", "); 147 labels = concat (labels, p, ", ");
148 148
149 p = &labels[strlen (labels) - 2]; 149 p = &labels[strlen (labels) - 2];
150 if (*p == ',') 150 if (*p == ',')
151 *p = '\0'; 151 *p = '\0';
172 172
173 /* 173 /*
174 * Return a newly-allocated string whose contents 174 * Return a newly-allocated string whose contents
175 * concatenate those of s1, s2, s3. 175 * concatenate those of s1, s2, s3.
176 */ 176 */
177 char * 177 static char *
178 concat (char *s1, char *s2, char *s3) 178 concat (char *s1, char *s2, char *s3)
179 { 179 {
180 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3); 180 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
181 char *result = xnew (len1 + len2 + len3 + 1, char); 181 char *result = xnew (len1 + len2 + len3 + 1, char);
182 182
191 /* 191 /*
192 * Read a line of text from `stream' into `linebuffer'. 192 * Read a line of text from `stream' into `linebuffer'.
193 * Return the number of characters read from `stream', 193 * Return the number of characters read from `stream',
194 * which is the length of the line including the newline, if any. 194 * which is the length of the line including the newline, if any.
195 */ 195 */
196 long 196 static long
197 readline (struct linebuffer *linebuffer, register FILE *stream) 197 readline (struct linebuffer *linebuffer, FILE *stream)
198 { 198 {
199 char *buffer = linebuffer->buffer; 199 char *buffer = linebuffer->buffer;
200 register char *p = linebuffer->buffer; 200 register char *p = linebuffer->buffer;
201 register char *pend; 201 register char *pend;
202 int chars_deleted; 202 int chars_deleted;
240 } 240 }
241 241
242 /* 242 /*
243 * Like malloc but get fatal error if memory is exhausted. 243 * Like malloc but get fatal error if memory is exhausted.
244 */ 244 */
245 long * 245 static long *
246 xmalloc (unsigned int size) 246 xmalloc (unsigned int size)
247 { 247 {
248 long *result = (long *) malloc (size); 248 long *result = (long *) malloc (size);
249 if (result == NULL) 249 if (result == NULL)
250 fatal ("virtual memory exhausted"); 250 fatal ("virtual memory exhausted");
251 return result; 251 return result;
252 } 252 }
253 253
254 long * 254 static long *
255 xrealloc (char *ptr, unsigned int size) 255 xrealloc (void *ptr, unsigned int size)
256 { 256 {
257 long *result = (long *) realloc (ptr, size); 257 long *result = (long *) realloc (ptr, size);
258 if (result == NULL) 258 if (result == NULL)
259 fatal ("virtual memory exhausted"); 259 fatal ("virtual memory exhausted");
260 return result; 260 return result;
261 } 261 }
262 262
263 void 263 static void
264 fatal (char *message) 264 fatal (char *message)
265 { 265 {
266 fprintf (stderr, "%s: %s\n", progname, message); 266 fprintf (stderr, "%s: %s\n", progname, message);
267 exit (BAD); 267 exit (BAD);
268 } 268 }