]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - fortune/strfile/strfile.c
1 /* $NetBSD: strfile.c,v 1.38 2013/09/19 00:34:00 uwe 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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #if HAVE_NBTOOL_CONFIG_H
36 #include "nbtool_config.h"
40 #include <sys/cdefs.h>
42 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
43 The Regents of the University of California. All rights reserved.");
48 static char sccsid
[] = "@(#)strfile.c 8.1 (Berkeley) 5/31/93";
50 __RCSID("$NetBSD: strfile.c,v 1.38 2013/09/19 00:34:00 uwe Exp $");
53 #endif /* __NetBSD__ */
55 #include <sys/types.h>
56 #include <sys/param.h>
69 #define MAXPATHLEN 1024
70 #endif /* MAXPATHLEN */
73 * This program takes a file composed of strings separated by
74 * lines starting with two consecutive delimiting character (default
75 * character is '%') and creates another file which consists of a table
76 * describing the file (structure from "strfile.h"), a table of seek
77 * pointers to the start of the strings, and the strings, each terminated
78 * by a null byte. Usage:
80 * % strfile [-iorsx] [ -cC ] sourcefile [ datafile ]
82 * c - Change delimiting character from '%' to 'C'
83 * s - Silent. Give no summary of data processed at the end of
85 * o - order the strings in alphabetic order
86 * i - if ordering, ignore case
87 * r - randomize the order of the strings
90 * Ken Arnold Sept. 7, 1978 --
92 * Added ordering options.
95 # define STORING_PTRS (Oflag || Rflag)
96 # define CHUNKSIZE 512
98 # define ALLOC(ptr,sz) do { \
100 ptr = malloc(CHUNKSIZE * sizeof *ptr); \
101 else if (((sz) + 1) % CHUNKSIZE == 0) \
102 ptr = realloc(ptr, ((sz) + CHUNKSIZE) * sizeof *ptr); \
104 err(1, "out of space"); \
112 static char *Infile
= NULL
; /* input file name */
113 static char Outfile
[MAXPATHLEN
] = ""; /* output file name */
114 static char Delimch
= '%'; /* delimiting character */
116 static int Sflag
= 0; /* silent run flag */
117 static int Oflag
= 0; /* ordering flag */
118 static int Iflag
= 0; /* ignore case flag */
119 static int Rflag
= 0; /* randomize order flag */
120 static int Xflag
= 0; /* set rotated bit */
121 static long Num_pts
= 0; /* number of pointers/strings */
123 static off_t
*Seekpts
;
125 static FILE *Sort_1
, *Sort_2
; /* pointers for sorting */
127 static STRFILE Tbl
; /* statistics table */
129 static STR
*Firstch
; /* first chars of each string */
132 static uint32_t h2nl(uint32_t h
);
133 static void getargs(int argc
, char **argv
);
134 static void usage(void) __dead
;
135 static void add_offset(FILE *fp
, off_t off
);
136 static void do_order(void);
137 static int cmp_str(const void *vp1
, const void *vp2
);
138 static void randomize(void);
139 static void fwrite_be_offt(off_t off
, FILE *f
);
144 * Drive the sucker. There are two main modes -- either we store
145 * the seek pointers, if the table is to be sorted or randomized,
146 * or we write the pointer directly to the file, if we are to stay
147 * in file order. If the former, we allocate and re-allocate in
148 * CHUNKSIZE blocks; if the latter, we just write each pointer,
149 * and then seek back to the beginning to write in the table.
152 main(int ac
, char **av
)
156 off_t last_off
, length
, pos
;
160 static char string
[257];
164 if (sizeof(uint32_t) != 4)
165 errx(1, "sizeof(uint32_t) != 4");
167 getargs(ac
, av
); /* evalute arguments */
169 if ((inf
= fopen(Infile
, "r")) == NULL
)
170 err(1, "open `%s'", Infile
);
172 if ((outf
= fopen(Outfile
, "w")) == NULL
)
173 err(1, "open `%s'", Outfile
);
175 (void) fseek(outf
, sizeof Tbl
, SEEK_SET
);
178 * Write the strings onto the file
182 Tbl
.str_shortlen
= (unsigned int) 0x7fffffff;
184 Tbl
.str_version
= VERSION
;
186 add_offset(outf
, ftell(inf
));
189 sp
= fgets(string
, 256, inf
);
190 if (sp
== NULL
|| (sp
[0] == dc
&& sp
[1] == '\n')) {
192 length
= pos
- last_off
- (sp
? strlen(sp
) : 0);
196 add_offset(outf
, pos
);
197 if ((off_t
)Tbl
.str_longlen
< length
)
198 Tbl
.str_longlen
= length
;
199 if ((off_t
)Tbl
.str_shortlen
> length
)
200 Tbl
.str_shortlen
= length
;
204 for (nsp
= sp
; !isalnum((unsigned char)*nsp
); nsp
++)
206 ALLOC(Firstch
, Num_pts
);
207 fp
= &Firstch
[Num_pts
- 1];
208 if (Iflag
&& isupper((unsigned char)*nsp
))
209 fp
->first
= tolower((unsigned char)*nsp
);
212 fp
->pos
= Seekpts
[Num_pts
- 1];
215 } while (sp
!= NULL
);
218 * write the tables in
229 Tbl
.str_flags
|= STR_ROTATED
;
232 printf("\"%s\" created\n", Outfile
);
234 puts("There was 1 string");
236 printf("There were %d strings\n", (int)(Num_pts
- 1));
237 printf("Longest string: %lu byte%s\n", (unsigned long)Tbl
.str_longlen
,
238 Tbl
.str_longlen
== 1 ? "" : "s");
239 printf("Shortest string: %lu byte%s\n", (unsigned long)Tbl
.str_shortlen
,
240 Tbl
.str_shortlen
== 1 ? "" : "s");
243 (void) fseek(outf
, (off_t
) 0, SEEK_SET
);
244 Tbl
.str_version
= h2nl(Tbl
.str_version
);
245 Tbl
.str_numstr
= h2nl(Num_pts
- 1);
246 Tbl
.str_longlen
= h2nl(Tbl
.str_longlen
);
247 Tbl
.str_shortlen
= h2nl(Tbl
.str_shortlen
);
248 Tbl
.str_flags
= h2nl(Tbl
.str_flags
);
249 (void) fwrite((char *) &Tbl
, sizeof Tbl
, 1, outf
);
251 for (i
= 0; i
< Num_pts
; i
++)
252 fwrite_be_offt(Seekpts
[i
], outf
);
256 err(1, "fwrite %s", Outfile
);
262 * This routine evaluates arguments from the command line
265 getargs(int argc
, char **argv
)
271 while ((ch
= getopt(argc
, argv
, "c:iorsx")) != -1)
273 case 'c': /* new delimiting char */
275 if (!isascii(Delimch
)) {
276 printf("bad delimiting character: '\\%o\n'",
280 case 'i': /* ignore case in ordering */
283 case 'o': /* order strings */
286 case 'r': /* randomize pointers */
289 case 's': /* silent */
292 case 'x': /* set the rotated bit */
304 (void) strcpy(Outfile
, *argv
);
307 puts("No input file name");
310 if (*Outfile
== '\0') {
311 (void) strcpy(Outfile
, Infile
);
312 (void) strcat(Outfile
, ".dat");
319 (void) fprintf(stderr
,
320 "Usage: %s [-iorsx] [-c char] sourcefile [datafile]\n",
327 * Add an offset to the list, or write it out, as appropriate.
330 add_offset(FILE *fp
, off_t off
)
334 fwrite_be_offt(off
, fp
);
336 ALLOC(Seekpts
, Num_pts
+ 1);
337 Seekpts
[Num_pts
] = off
;
344 * Order the strings alphabetically (possibly ignoring case).
353 Sort_1
= fopen(Infile
, "r");
354 Sort_2
= fopen(Infile
, "r");
355 qsort((char *) Firstch
, (int) Tbl
.str_numstr
, sizeof *Firstch
, cmp_str
);
361 (void) fclose(Sort_1
);
362 (void) fclose(Sort_2
);
363 Tbl
.str_flags
|= STR_ORDERED
;
367 cmp_str(const void *vp1
, const void *vp2
)
373 p1
= (const STR
*)vp1
;
374 p2
= (const STR
*)vp2
;
376 # define SET_N(nf,ch) (nf = (ch == '\n'))
377 # define IS_END(ch,nf) (ch == Delimch && nf)
384 (void) fseek(Sort_1
, p1
->pos
, SEEK_SET
);
385 (void) fseek(Sort_2
, p2
->pos
, SEEK_SET
);
389 while (!isalnum(c1
= getc(Sort_1
)) && c1
!= '\0')
391 while (!isalnum(c2
= getc(Sort_2
)) && c2
!= '\0')
394 while (!IS_END(c1
, n1
) && !IS_END(c2
, n2
)) {
417 * Randomize the order of the string table. We must be careful
418 * not to randomize across delimiter boundaries. All
419 * randomization is done within each block.
428 srandom((int)(time(NULL
) + getpid()));
430 Tbl
.str_flags
|= STR_RANDOM
;
431 cnt
= Tbl
.str_numstr
;
434 * move things around randomly
437 for (sp
= Seekpts
; cnt
> 0; cnt
--, sp
++) {
447 * Write out the off paramater as a 64 bit big endian number
451 fwrite_be_offt(off_t off
, FILE *f
)
456 for (i
= 7; i
>= 0; i
--) {
460 fwrite(c
, sizeof(c
), 1, f
);
469 c
[0] = (h
>> 24) & 0xff;
470 c
[1] = (h
>> 16) & 0xff;
471 c
[2] = (h
>> 8) & 0xff;
472 c
[3] = (h
>> 0) & 0xff;
473 memcpy(&rv
, c
, sizeof rv
);