strfile.c revision 1.17 1 /* $NetBSD: strfile.c,v 1.17 2000/01/13 16:19:36 jsm Exp $ */
2
3 /*-
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Ken Arnold.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 #ifdef __NetBSD__
40 #include <sys/cdefs.h>
41 #ifndef lint
42 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
43 The Regents of the University of California. All rights reserved.\n");
44 #endif /* not lint */
45
46 #ifndef lint
47 #if 0
48 static char sccsid[] = "@(#)strfile.c 8.1 (Berkeley) 5/31/93";
49 #else
50 __RCSID("$NetBSD: strfile.c,v 1.17 2000/01/13 16:19:36 jsm Exp $");
51 #endif
52 #endif /* not lint */
53 #endif /* __NetBSD__ */
54
55 # include <sys/types.h>
56 # include <sys/param.h>
57 # include <ctype.h>
58 # include <stdio.h>
59 # include <stdlib.h>
60 # include <string.h>
61 # include <time.h>
62 # include <unistd.h>
63
64 # ifndef u_int32_t
65 # define u_int32_t unsigned int
66 # endif
67 # include "strfile.h"
68
69 # ifndef MAXPATHLEN
70 # define MAXPATHLEN 1024
71 # endif /* MAXPATHLEN */
72
73 /*
74 * This program takes a file composed of strings seperated by
75 * lines starting with two consecutive delimiting character (default
76 * character is '%') and creates another file which consists of a table
77 * describing the file (structure from "strfile.h"), a table of seek
78 * pointers to the start of the strings, and the strings, each terminated
79 * by a null byte. Usage:
80 *
81 * % strfile [-iorsx] [ -cC ] sourcefile [ datafile ]
82 *
83 * c - Change delimiting character from '%' to 'C'
84 * s - Silent. Give no summary of data processed at the end of
85 * the run.
86 * o - order the strings in alphabetic order
87 * i - if ordering, ignore case
88 * r - randomize the order of the strings
89 * x - set rotated bit
90 *
91 * Ken Arnold Sept. 7, 1978 --
92 *
93 * Added ordering options.
94 */
95
96 # define TRUE 1
97 # define FALSE 0
98
99 # define STORING_PTRS (Oflag || Rflag)
100 # define CHUNKSIZE 512
101
102 # define ALLOC(ptr,sz) do { \
103 if (ptr == NULL) \
104 ptr = malloc((unsigned int) (CHUNKSIZE * sizeof *ptr)); \
105 else if (((sz) + 1) % CHUNKSIZE == 0) \
106 ptr = realloc((void *) ptr, ((unsigned int) ((sz) + CHUNKSIZE) * sizeof *ptr)); \
107 if (ptr == NULL) \
108 die("out of space"); \
109 } while (0)
110
111 typedef struct {
112 char first;
113 off_t pos;
114 } STR;
115
116 char *Infile = NULL, /* input file name */
117 Outfile[MAXPATHLEN] = "", /* output file name */
118 Delimch = '%'; /* delimiting character */
119
120 int Sflag = FALSE; /* silent run flag */
121 int Oflag = FALSE; /* ordering flag */
122 int Iflag = FALSE; /* ignore case flag */
123 int Rflag = FALSE; /* randomize order flag */
124 int Xflag = FALSE; /* set rotated bit */
125 long Num_pts = 0; /* number of pointers/strings */
126
127 off_t *Seekpts;
128
129 FILE *Sort_1, *Sort_2; /* pointers for sorting */
130
131 STRFILE Tbl; /* statistics table */
132
133 STR *Firstch; /* first chars of each string */
134
135 #ifdef __GCC__
136 #define NORETURN __attribute__((__noreturn__))
137 #else
138 #define NORETURN
139 #endif
140
141 void add_offset(FILE *, off_t);
142 int cmp_str(const void *, const void *);
143 void die(const char *) NORETURN;
144 void dieperror(const char *, char *) NORETURN;
145 void do_order(void);
146 void fwrite_be_offt(off_t, FILE *);
147 void getargs(int, char *[]);
148 int main(int, char *[]);
149 void randomize(void);
150 void usage(void) NORETURN;
151
152
153 /*
154 * main:
155 * Drive the sucker. There are two main modes -- either we store
156 * the seek pointers, if the table is to be sorted or randomized,
157 * or we write the pointer directly to the file, if we are to stay
158 * in file order. If the former, we allocate and re-allocate in
159 * CHUNKSIZE blocks; if the latter, we just write each pointer,
160 * and then seek back to the beginning to write in the table.
161 */
162 int
163 main(ac, av)
164 int ac;
165 char *av[];
166 {
167 char *sp, dc;
168 FILE *inf, *outf;
169 off_t last_off, length, pos, *p;
170 int first, cnt;
171 char *nsp;
172 STR *fp;
173 static char string[257];
174
175 /* sanity test */
176 if (sizeof(u_int32_t) != 4)
177 die("sizeof(unsigned int) != 4");
178
179 getargs(ac, av); /* evalute arguments */
180 dc = Delimch;
181 if ((inf = fopen(Infile, "r")) == NULL)
182 dieperror("open `%s'", Infile);
183
184 if ((outf = fopen(Outfile, "w")) == NULL)
185 dieperror("open `%s'", Outfile);
186 if (!STORING_PTRS)
187 (void) fseek(outf, sizeof Tbl, SEEK_SET);
188
189 /*
190 * Write the strings onto the file
191 */
192
193 Tbl.str_longlen = 0;
194 Tbl.str_shortlen = (unsigned int) 0x7fffffff;
195 Tbl.str_delim = dc;
196 Tbl.str_version = VERSION;
197 first = Oflag;
198 add_offset(outf, ftell(inf));
199 last_off = 0;
200 do {
201 sp = fgets(string, 256, inf);
202 if (sp == NULL || (sp[0] == dc && sp[1] == '\n')) {
203 pos = ftell(inf);
204 length = pos - last_off - (sp ? strlen(sp) : 0);
205 last_off = pos;
206 if (!length)
207 continue;
208 add_offset(outf, pos);
209 if ((off_t)Tbl.str_longlen < length)
210 Tbl.str_longlen = length;
211 if ((off_t)Tbl.str_shortlen > length)
212 Tbl.str_shortlen = length;
213 first = Oflag;
214 }
215 else if (first) {
216 for (nsp = sp; !isalnum(*nsp); nsp++)
217 continue;
218 ALLOC(Firstch, Num_pts);
219 fp = &Firstch[Num_pts - 1];
220 if (Iflag && isupper(*nsp))
221 fp->first = tolower(*nsp);
222 else
223 fp->first = *nsp;
224 fp->pos = Seekpts[Num_pts - 1];
225 first = FALSE;
226 }
227 } while (sp != NULL);
228
229 /*
230 * write the tables in
231 */
232
233 (void) fclose(inf);
234
235 if (Oflag)
236 do_order();
237 else if (Rflag)
238 randomize();
239
240 if (Xflag)
241 Tbl.str_flags |= STR_ROTATED;
242
243 if (!Sflag) {
244 printf("\"%s\" created\n", Outfile);
245 if (Num_pts == 2)
246 puts("There was 1 string");
247 else
248 printf("There were %d strings\n", (int)(Num_pts - 1));
249 printf("Longest string: %lu byte%s\n", Tbl.str_longlen,
250 Tbl.str_longlen == 1 ? "" : "s");
251 printf("Shortest string: %lu byte%s\n", Tbl.str_shortlen,
252 Tbl.str_shortlen == 1 ? "" : "s");
253 }
254
255 (void) fseek(outf, (off_t) 0, SEEK_SET);
256 Tbl.str_version = htonl(Tbl.str_version);
257 Tbl.str_numstr = htonl(Num_pts - 1);
258 Tbl.str_longlen = htonl(Tbl.str_longlen);
259 Tbl.str_shortlen = htonl(Tbl.str_shortlen);
260 Tbl.str_flags = htonl(Tbl.str_flags);
261 (void) fwrite((char *) &Tbl, sizeof Tbl, 1, outf);
262 if (STORING_PTRS) {
263 for (p = Seekpts, cnt = Num_pts; cnt--; ++p)
264 fwrite_be_offt(*p, outf);
265 }
266 fflush(outf);
267 if (ferror(outf))
268 dieperror("fwrite %s", Outfile);
269 (void) fclose(outf);
270 exit(0);
271 }
272
273 /*
274 * This routine evaluates arguments from the command line
275 */
276 void
277 getargs(argc, argv)
278 int argc;
279 char **argv;
280 {
281 int ch;
282 extern int optind;
283 extern char *optarg;
284
285 while ((ch = getopt(argc, argv, "c:iorsx")) != -1)
286 switch(ch) {
287 case 'c': /* new delimiting char */
288 Delimch = *optarg;
289 if (!isascii(Delimch)) {
290 printf("bad delimiting character: '\\%o\n'",
291 Delimch);
292 }
293 break;
294 case 'i': /* ignore case in ordering */
295 Iflag++;
296 break;
297 case 'o': /* order strings */
298 Oflag++;
299 break;
300 case 'r': /* randomize pointers */
301 Rflag++;
302 break;
303 case 's': /* silent */
304 Sflag++;
305 break;
306 case 'x': /* set the rotated bit */
307 Xflag++;
308 break;
309 case '?':
310 default:
311 usage();
312 }
313 argv += optind;
314
315 if (*argv) {
316 Infile = *argv;
317 if (*++argv)
318 (void) strcpy(Outfile, *argv);
319 }
320 if (!Infile) {
321 puts("No input file name");
322 usage();
323 }
324 if (*Outfile == '\0') {
325 (void) strcpy(Outfile, Infile);
326 (void) strcat(Outfile, ".dat");
327 }
328 }
329
330 void
331 usage()
332 {
333 (void) fprintf(stderr,
334 "strfile [-iorsx] [-c char] sourcefile [datafile]\n");
335 exit(1);
336 }
337
338 void
339 die(str)
340 const char *str;
341 {
342 fprintf(stderr, "strfile: %s\n", str);
343 exit(1);
344 }
345
346 void
347 dieperror(fmt, file)
348 const char *fmt;
349 char *file;
350 {
351 fprintf(stderr, "strfile: ");
352 fprintf(stderr, fmt, file);
353 fprintf(stderr, ": ");
354 perror(NULL);
355 exit(1);
356 }
357
358 /*
359 * add_offset:
360 * Add an offset to the list, or write it out, as appropriate.
361 */
362 void
363 add_offset(fp, off)
364 FILE *fp;
365 off_t off;
366 {
367
368 if (!STORING_PTRS) {
369 fwrite_be_offt(off, fp);
370 } else {
371 ALLOC(Seekpts, Num_pts + 1);
372 Seekpts[Num_pts] = off;
373 }
374 Num_pts++;
375 }
376
377 /*
378 * do_order:
379 * Order the strings alphabetically (possibly ignoring case).
380 */
381 void
382 do_order()
383 {
384 int i;
385 off_t *lp;
386 STR *fp;
387
388 Sort_1 = fopen(Infile, "r");
389 Sort_2 = fopen(Infile, "r");
390 qsort((char *) Firstch, (int) Tbl.str_numstr, sizeof *Firstch, cmp_str);
391 i = Tbl.str_numstr;
392 lp = Seekpts;
393 fp = Firstch;
394 while (i--)
395 *lp++ = fp++->pos;
396 (void) fclose(Sort_1);
397 (void) fclose(Sort_2);
398 Tbl.str_flags |= STR_ORDERED;
399 }
400
401 int
402 cmp_str(vp1, vp2)
403 const void *vp1, *vp2;
404 {
405 const STR *p1, *p2;
406 int c1, c2;
407 int n1, n2;
408
409 p1 = (const STR *)vp1;
410 p2 = (const STR *)vp2;
411
412 # define SET_N(nf,ch) (nf = (ch == '\n'))
413 # define IS_END(ch,nf) (ch == Delimch && nf)
414
415 c1 = p1->first;
416 c2 = p2->first;
417 if (c1 != c2)
418 return c1 - c2;
419
420 (void) fseek(Sort_1, p1->pos, SEEK_SET);
421 (void) fseek(Sort_2, p2->pos, SEEK_SET);
422
423 n1 = FALSE;
424 n2 = FALSE;
425 while (!isalnum(c1 = getc(Sort_1)) && c1 != '\0')
426 SET_N(n1, c1);
427 while (!isalnum(c2 = getc(Sort_2)) && c2 != '\0')
428 SET_N(n2, c2);
429
430 while (!IS_END(c1, n1) && !IS_END(c2, n2)) {
431 if (Iflag) {
432 if (isupper(c1))
433 c1 = tolower(c1);
434 if (isupper(c2))
435 c2 = tolower(c2);
436 }
437 if (c1 != c2)
438 return c1 - c2;
439 SET_N(n1, c1);
440 SET_N(n2, c2);
441 c1 = getc(Sort_1);
442 c2 = getc(Sort_2);
443 }
444 if (IS_END(c1, n1))
445 c1 = 0;
446 if (IS_END(c2, n2))
447 c2 = 0;
448 return c1 - c2;
449 }
450
451 /*
452 * randomize:
453 * Randomize the order of the string table. We must be careful
454 * not to randomize across delimiter boundaries. All
455 * randomization is done within each block.
456 */
457 void
458 randomize()
459 {
460 int cnt, i;
461 off_t tmp;
462 off_t *sp;
463
464 srandom((int)(time((time_t *) NULL) + getpid()));
465
466 Tbl.str_flags |= STR_RANDOM;
467 cnt = Tbl.str_numstr;
468
469 /*
470 * move things around randomly
471 */
472
473 for (sp = Seekpts; cnt > 0; cnt--, sp++) {
474 i = random() % cnt;
475 tmp = sp[0];
476 sp[0] = sp[i];
477 sp[i] = tmp;
478 }
479 }
480
481 /*
482 * fwrite_be_offt:
483 * Write out the off paramater as a 64 bit big endian number
484 */
485
486 void
487 fwrite_be_offt(off, f)
488 off_t off;
489 FILE *f;
490 {
491 int i;
492 unsigned char c[8];
493
494 for (i = 7; i >= 0; i--) {
495 c[i] = off & 0xff;
496 off >>= 8;
497 }
498 fwrite(c, sizeof(c), 1, f);
499 }
500