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)
146 case 'c': /* new delimiting char */
147 delimiter_char = *optarg;
148 if (!isascii(delimiter_char))
150 printf("bad delimiting character: '\\%o\n'",
151 (unsigned int)delimiter_char);
154 case 'i': /* ignore case in ordering */
157 case 'o': /* order strings */
160 case 'r': /* randomize pointers */
163 case 's': /* silent */
166 case 'x': /* set the rotated bit */
177 input_filename = *argv;
178 set_output_filename(*++argv);
182 puts("No input file name");
185 if (*output_filename == '\0')
187 if (strlen(input_filename) > MAXPATHLEN - 10)
189 puts("input file name too long!");
193 output_filename, COUNT(output_filename), "%s.dat", input_filename);
199 * Add an offset to the list, or write it out, as appropriate.
201 static void add_offset(FILE *fp, int32_t off)
206 net = htonl((uint32_t)off);
207 fwrite(&net, 1, sizeof net, fp);
211 ALLOC(Seekpts, Num_pts + 1);
212 Seekpts[Num_pts] = off;
219 * Used when we have two separators in a row.
221 static void fix_last_offset(FILE *const fp, const int32_t off)
225 uint32_t net = htonl((uint32_t)off);
226 fseek(fp, -(long)(sizeof net), SEEK_CUR);
227 fwrite(&net, 1, sizeof net, fp);
231 Seekpts[Num_pts - 1] = off;
237 * Compare two strings in the file
239 static int cmp_str(const void *v1, const void *v2)
241 #define SET_N(nf, ch) (nf = (ch == '\n'))
242 #define IS_END(ch, nf) (ch == delimiter_char && nf)
244 const STR *p1 = (const STR *)v1;
245 const STR *p2 = (const STR *)v2;
253 fseek(Sort_1, p1->pos, SEEK_SET);
254 fseek(Sort_2, p2->pos, SEEK_SET);
258 while (!isalnum(c1 = getc(Sort_1)) && c1 != '\0')
260 while (!isalnum(c2 = getc(Sort_2)) && c2 != '\0')
263 while (!IS_END(c1, n1) && !IS_END(c2, n2))
294 * Order the strings alphabetically (possibly ignoring case).
296 static void do_order(void)
298 Sort_1 = fopen(input_filename, "r");
299 Sort_2 = fopen(input_filename, "r");
301 (char *)Firstch, (size_t)((int)Num_pts - 1), sizeof *Firstch, cmp_str);
302 /* i = Tbl.str_numstr;
303 * Fucking brilliant. Tbl.str_numstr was initialized to zero, and is still
306 long i = Num_pts - 1;
307 int32_t *lp = Seekpts;
315 Tbl.str_flags |= STR_ORDERED;
320 * Randomize the order of the string table. We must be careful
321 * not to randomize across delimiter boundaries. All
322 * randomization is done within each block.
324 static void randomize(void)
326 srandom((unsigned int)(time((time_t *)NULL) + getpid()));
328 Tbl.str_flags |= STR_RANDOM;
329 /* cnt = Tbl.str_numstr;
330 * See comment above. Isn't this stuff distributed worldwide? How
333 int cnt = (int)Num_pts;
336 * move things around randomly
339 int32_t *sp = Seekpts;
340 for (; cnt > 0; cnt--, sp++)
342 const int i = random() % cnt;
343 const int32_t tmp = sp[0];
351 * Drive the sucker. There are two main modes -- either we store
352 * the seek pointers, if the table is to be sorted or randomized,
353 * or we write the pointer directly to the file, if we are to stay
354 * in file order. If the former, we allocate and re-allocate in
355 * CHUNKSIZE blocks; if the latter, we just write each pointer,
356 * and then seek back to the beginning to write in the table.
358 int main(int ac, char **av)
362 bool len_was_set = false;
364 getargs(ac, av); /* evalute arguments */
365 if (!(inf = fopen(input_filename, "r")))
367 perror(input_filename);
371 if (!(outf = fopen(output_filename, "w")))
373 perror(output_filename);
378 (void)fseek(outf, sizeof Tbl, SEEK_SET);
382 * Write the strings onto the file
386 Tbl.str_shortlen = (unsigned int)0xffffffff;
387 Tbl.str_delim = (uint8_t)delimiter_char;
388 Tbl.str_version = STRFILE_VERSION;
390 add_offset(outf, (int32_t)ftell(inf));
391 int32_t last_off = 0;
395 sp = fgets(string, 256, inf);
396 if ((!sp) || STR_ENDSTRING(sp, Tbl))
398 const int32_t pos = (int32_t)ftell(inf);
399 const int32_t length =
400 pos - last_off - (int32_t)(sp ? strlen(sp) : 0);
402 /* Here's where we go back and fix things, if the
403 * 'fortune' just read was the null string.
404 * We had to make the assignment of last_off slightly
405 * redundant to achieve this.
408 if (pos - last_off == 2)
410 fix_last_offset(outf, pos);
416 add_offset(outf, pos);
419 Tbl.str_longlen = (uint32_t)length;
420 Tbl.str_shortlen = (uint32_t)length;
425 if ((int)Tbl.str_longlen < length)
427 Tbl.str_longlen = (uint32_t)length;
429 if (Tbl.str_shortlen > (uint32_t)length)
431 Tbl.str_shortlen = (uint32_t)length;
439 for (; !isalnum(*nsp); ++nsp)
442 ALLOC(Firstch, Num_pts);
443 STR *const fp = &Firstch[Num_pts - 1];
445 ((Iflag && isupper(*nsp)) ? ((char)tolower(*nsp)) : (*nsp));
446 fp->pos = Seekpts[Num_pts - 1];
452 * write the tables in
468 Tbl.str_flags |= STR_ROTATED;
473 printf("\"%s\" created\n", output_filename);
476 puts("There was no string");
482 puts("There was 1 string");
486 printf("There were %ld strings\n", Num_pts - 1);
488 printf("Longest string: %lu byte%s\n",
489 (unsigned long)(Tbl.str_longlen),
490 Tbl.str_longlen == 1 ? "" : "s");
491 printf("Shortest string: %lu byte%s\n",
492 (unsigned long)(Tbl.str_shortlen),
493 Tbl.str_shortlen == 1 ? "" : "s");
497 fseek(outf, (off_t)0, SEEK_SET);
498 Tbl.str_version = htonl(Tbl.str_version);
499 Tbl.str_numstr = htonl((uint32_t)(Num_pts - 1));
500 /* Look, Ma! After using the variable three times, let's store
503 Tbl.str_longlen = htonl(Tbl.str_longlen);
504 Tbl.str_shortlen = htonl(Tbl.str_shortlen);
505 Tbl.str_flags = htonl(Tbl.str_flags);
506 fwrite(&Tbl.str_version, sizeof Tbl.str_version, 1, outf);
507 fwrite(&Tbl.str_numstr, sizeof Tbl.str_numstr, 1, outf);
508 fwrite(&Tbl.str_longlen, sizeof Tbl.str_longlen, 1, outf);
509 fwrite(&Tbl.str_shortlen, sizeof Tbl.str_shortlen, 1, outf);
510 fwrite(&Tbl.str_flags, sizeof Tbl.str_flags, 1, outf);
511 fwrite(Tbl.stuff, sizeof Tbl.stuff, 1, outf);
514 int cnt = (int)Num_pts;
515 for (int32_t *p = Seekpts; cnt--; ++p)
517 *p = (int32_t)htonl((uint32_t)*p);
518 fwrite(p, sizeof *p, 1, outf);