1 /* $NetBSD: strfile.c,v 1.3 1995/03/23 08:28:47 cgd Exp $ */
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
40 * Changes, September 1995, to make the damn thing actually sort instead
41 * of just pretending. Amy A. Lewis
45 * Fixed the special cases of %^J% (an empty fortune), no 'separator' at
46 * the end of the file, and a trailing newline at the end of the file, all
47 * of which produced total ballsup at one point or another.
49 * This included adding a routine to go back and write over the last pointer
50 * written or stored, for the case of an empty fortune.
52 * unstr also had to be modified (well, for *lots* of reasons, but this was
53 * one) to be certain to put the delimiters in the right places.
56 #include "fortune-mod-common.h"
59 * This program takes a file composed of strings separated by
60 * lines containing only the delimiting character (the default
61 * character is '%') and creates another file which consists of a table
62 * describing the file (structure from "strfile.h"), a table of seek
63 * pointers to the start of the strings, and the strings, each terminated
64 * by a null byte. Usage:
66 * % strfile [-iorsx] [ -cC ] sourcefile [ datafile ]
68 * c - Change delimiting character from '%' to 'C'
69 * s - Silent. Give no summary of data processed at the end of
71 * o - order the strings in alphabetic order
72 * i - if ordering, ignore case
73 * r - randomize the order of the strings
76 * Ken Arnold Sept. 7, 1978 --
78 * Added ordering options.
80 * Made ordering options do more than set the bloody flag, September 95 A. Lewis
82 * Always make sure that your loop control variables aren't set to bloody
83 * *zero* before distributing the bloody code, all right?
89 #define ALLOC(ptr, sz) \
92 ptr = malloc((unsigned int)(CHUNKSIZE * sizeof *ptr)); \
93 else if (((sz) + 1) % CHUNKSIZE == 0) \
94 ptr = realloc((void *)ptr, \
95 ((unsigned int)((sz) + CHUNKSIZE) * sizeof *ptr)); \
98 fprintf(stderr, "out of space\n"); \
109 static char delimiter_char = '%';
111 static bool Sflag = false; /* silent run flag */
112 static bool Oflag = false; /* ordering flag */
113 static bool Iflag = false; /* ignore case flag */
114 static bool Rflag = false; /* randomize order flag */
115 static bool Xflag = false; /* set rotated bit */
116 static long Num_pts = 0; /* number of pointers/strings */
118 static bool storing_ptrs(void) { return (Oflag || Rflag); }
120 static int32_t *Seekpts;
122 static FILE *Sort_1, *Sort_2; /* pointers for sorting */
124 static STRFILE Tbl; /* statistics table */
126 static STR *Firstch; /* first chars of each string */
128 static void __attribute__((noreturn)) usage(void)
130 fprintf(stderr, "%s", "strfile [-iorsx] [-c char] sourcefile [datafile]\n");
134 #include "fortune-util-set-outfn.h"
137 * This routine evaluates arguments from the command line
139 static void getargs(int argc, char **argv)
143 while ((ch = getopt(argc, argv, "c:iorsx")) != EOF)
147 case 'c': /* new delimiting char */
148 delimiter_char = *optarg;
149 if (!isascii(delimiter_char))
151 printf("bad delimiting character: '\\%o\n'",
152 (unsigned int)delimiter_char);
155 case 'i': /* ignore case in ordering */
158 case 'o': /* order strings */
161 case 'r': /* randomize pointers */
164 case 's': /* silent */
167 case 'x': /* set the rotated bit */
179 input_filename = *argv;
180 set_output_filename(*++argv);
184 puts("No input file name");
187 if (*output_filename == '\0')
189 if (strlen(input_filename) > MAXPATHLEN - 10)
191 puts("input file name too long!");
195 output_filename, COUNT(output_filename), "%s.dat", input_filename);
201 * Add an offset to the list, or write it out, as appropriate.
203 static void add_offset(FILE *fp, int32_t off)
208 net = htonl((uint32_t)off);
209 fwrite(&net, 1, sizeof net, fp);
213 ALLOC(Seekpts, Num_pts + 1);
214 Seekpts[Num_pts] = off;
221 * Used when we have two separators in a row.
223 static void fix_last_offset(FILE *const fp, const int32_t off)
227 uint32_t net = htonl((uint32_t)off);
228 fseek(fp, -(long)(sizeof net), SEEK_CUR);
229 fwrite(&net, 1, sizeof net, fp);
233 Seekpts[Num_pts - 1] = off;
239 * Compare two strings in the file
241 static int cmp_str(const void *v1, const void *v2)
243 #define SET_N(nf, ch) (nf = (ch == '\n'))
244 #define IS_END(ch, nf) (ch == delimiter_char && nf)
246 const STR *p1 = (const STR *)v1;
247 const STR *p2 = (const STR *)v2;
255 fseek(Sort_1, p1->pos, SEEK_SET);
256 fseek(Sort_2, p2->pos, SEEK_SET);
260 while (!isalnum(c1 = getc(Sort_1)) && c1 != '\0')
264 while (!isalnum(c2 = getc(Sort_2)) && c2 != '\0')
269 while (!IS_END(c1, n1) && !IS_END(c2, n2))
304 * Order the strings alphabetically (possibly ignoring case).
306 static void do_order(void)
308 Sort_1 = fopen(input_filename, "r");
309 Sort_2 = fopen(input_filename, "r");
311 (char *)Firstch, (size_t)((int)Num_pts - 1), sizeof *Firstch, cmp_str);
312 /* i = Tbl.str_numstr;
313 * Fucking brilliant. Tbl.str_numstr was initialized to zero, and is still
316 long i = Num_pts - 1;
317 int32_t *lp = Seekpts;
325 Tbl.str_flags |= STR_ORDERED;
330 * Randomize the order of the string table. We must be careful
331 * not to randomize across delimiter boundaries. All
332 * randomization is done within each block.
334 static void randomize(void)
337 Tbl.str_flags |= STR_RANDOM;
338 /* cnt = Tbl.str_numstr;
339 * See comment above. Isn't this stuff distributed worldwide? How
342 int cnt = (int)Num_pts;
345 * move things around randomly
348 int32_t *sp = Seekpts;
349 for (; cnt > 0; cnt--, sp++)
351 const int i = random() % cnt;
352 const int32_t tmp = sp[0];
360 * Drive the sucker. There are two main modes -- either we store
361 * the seek pointers, if the table is to be sorted or randomized,
362 * or we write the pointer directly to the file, if we are to stay
363 * in file order. If the former, we allocate and re-allocate in
364 * CHUNKSIZE blocks; if the latter, we just write each pointer,
365 * and then seek back to the beginning to write in the table.
367 int main(int argc, char **argv)
371 bool len_was_set = false;
373 getargs(argc, argv); /* evalute arguments */
374 if (!(inf = fopen(input_filename, "r")))
376 perror(input_filename);
380 if (!(outf = fopen(output_filename, "w")))
382 perror(output_filename);
387 (void)fseek(outf, sizeof Tbl, SEEK_SET);
391 * Write the strings onto the file
395 Tbl.str_shortlen = (unsigned int)0xffffffff;
396 Tbl.str_delim = (uint8_t)delimiter_char;
397 Tbl.str_version = STRFILE_VERSION;
399 add_offset(outf, (int32_t)ftell(inf));
400 int32_t last_off = 0;
404 sp = fgets(string, 256, inf);
405 if ((!sp) || STR_ENDSTRING(sp, Tbl))
407 const int32_t pos = (int32_t)ftell(inf);
408 const int32_t length =
409 pos - last_off - (int32_t)(sp ? strlen(sp) : 0);
411 /* Here's where we go back and fix things, if the
412 * 'fortune' just read was the null string.
413 * We had to make the assignment of last_off slightly
414 * redundant to achieve this.
417 if (pos - last_off == 2)
419 fix_last_offset(outf, pos);
425 add_offset(outf, pos);
428 Tbl.str_longlen = (uint32_t)length;
429 Tbl.str_shortlen = (uint32_t)length;
434 if ((int)Tbl.str_longlen < length)
436 Tbl.str_longlen = (uint32_t)length;
438 if (Tbl.str_shortlen > (uint32_t)length)
440 Tbl.str_shortlen = (uint32_t)length;
448 for (; !isalnum(*nsp); ++nsp)
451 ALLOC(Firstch, Num_pts);
452 STR *const fp = &Firstch[Num_pts - 1];
454 ((Iflag && isupper(*nsp)) ? ((char)tolower(*nsp)) : (*nsp));
455 fp->pos = Seekpts[Num_pts - 1];
461 * write the tables in
477 Tbl.str_flags |= STR_ROTATED;
482 printf("\"%s\" created\n", output_filename);
485 puts("There was no string");
491 puts("There was 1 string");
495 printf("There were %ld strings\n", Num_pts - 1);
497 printf("Longest string: %lu byte%s\n",
498 (unsigned long)(Tbl.str_longlen),
499 Tbl.str_longlen == 1 ? "" : "s");
500 printf("Shortest string: %lu byte%s\n",
501 (unsigned long)(Tbl.str_shortlen),
502 Tbl.str_shortlen == 1 ? "" : "s");
506 fseek(outf, (off_t)0, SEEK_SET);
507 Tbl.str_version = htonl(Tbl.str_version);
508 Tbl.str_numstr = htonl((uint32_t)(Num_pts - 1));
509 /* Look, Ma! After using the variable three times, let's store
512 Tbl.str_longlen = htonl(Tbl.str_longlen);
513 Tbl.str_shortlen = htonl(Tbl.str_shortlen);
514 Tbl.str_flags = htonl(Tbl.str_flags);
515 fwrite(&Tbl.str_version, sizeof Tbl.str_version, 1, outf);
516 fwrite(&Tbl.str_numstr, sizeof Tbl.str_numstr, 1, outf);
517 fwrite(&Tbl.str_longlen, sizeof Tbl.str_longlen, 1, outf);
518 fwrite(&Tbl.str_shortlen, sizeof Tbl.str_shortlen, 1, outf);
519 fwrite(&Tbl.str_flags, sizeof Tbl.str_flags, 1, outf);
520 fwrite(Tbl.stuff, sizeof Tbl.stuff, 1, outf);
523 int cnt = (int)Num_pts;
524 for (int32_t *p = Seekpts; cnt--; ++p)
526 *p = (int32_t)htonl((uint32_t)*p);
527 fwrite(p, sizeof *p, 1, outf);