]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - fortune/strfile/strfile.c
1 /* $NetBSD: strfile.c,v 1.21 2001/07/22 13:34:00 wiz 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 #include <sys/cdefs.h>
42 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
43 The Regents of the University of California. All rights reserved.\n");
48 static char sccsid
[] = "@(#)strfile.c 8.1 (Berkeley) 5/31/93";
50 __RCSID("$NetBSD: strfile.c,v 1.21 2001/07/22 13:34:00 wiz Exp $");
53 #endif /* __NetBSD__ */
55 # include <sys/types.h>
56 # include <sys/param.h>
65 # define u_int32_t unsigned int
70 # define MAXPATHLEN 1024
71 # endif /* MAXPATHLEN */
79 c
[0] = (h
>> 24) & 0xff;
80 c
[1] = (h
>> 16) & 0xff;
81 c
[2] = (h
>> 8) & 0xff;
82 c
[3] = (h
>> 0) & 0xff;
83 memcpy(&rv
, c
, sizeof rv
);
89 * This program takes a file composed of strings separated by
90 * lines starting with two consecutive delimiting character (default
91 * character is '%') and creates another file which consists of a table
92 * describing the file (structure from "strfile.h"), a table of seek
93 * pointers to the start of the strings, and the strings, each terminated
94 * by a null byte. Usage:
96 * % strfile [-iorsx] [ -cC ] sourcefile [ datafile ]
98 * c - Change delimiting character from '%' to 'C'
99 * s - Silent. Give no summary of data processed at the end of
101 * o - order the strings in alphabetic order
102 * i - if ordering, ignore case
103 * r - randomize the order of the strings
104 * x - set rotated bit
106 * Ken Arnold Sept. 7, 1978 --
108 * Added ordering options.
114 # define STORING_PTRS (Oflag || Rflag)
115 # define CHUNKSIZE 512
117 # define ALLOC(ptr,sz) do { \
119 ptr = malloc(CHUNKSIZE * sizeof *ptr); \
120 else if (((sz) + 1) % CHUNKSIZE == 0) \
121 ptr = realloc(ptr, ((sz) + CHUNKSIZE) * sizeof *ptr); \
123 die("out of space"); \
131 char *Infile
= NULL
, /* input file name */
132 Outfile
[MAXPATHLEN
] = "", /* output file name */
133 Delimch
= '%'; /* delimiting character */
135 int Sflag
= FALSE
; /* silent run flag */
136 int Oflag
= FALSE
; /* ordering flag */
137 int Iflag
= FALSE
; /* ignore case flag */
138 int Rflag
= FALSE
; /* randomize order flag */
139 int Xflag
= FALSE
; /* set rotated bit */
140 long Num_pts
= 0; /* number of pointers/strings */
144 FILE *Sort_1
, *Sort_2
; /* pointers for sorting */
146 STRFILE Tbl
; /* statistics table */
148 STR
*Firstch
; /* first chars of each string */
151 #define NORETURN __attribute__((__noreturn__))
156 void add_offset(FILE *, off_t
);
157 int cmp_str(const void *, const void *);
158 void die(const char *) NORETURN
;
159 void dieperror(const char *, char *) NORETURN
;
161 void fwrite_be_offt(off_t
, FILE *);
162 void getargs(int, char *[]);
163 int main(int, char *[]);
164 void randomize(void);
165 void usage(void) NORETURN
;
170 * Drive the sucker. There are two main modes -- either we store
171 * the seek pointers, if the table is to be sorted or randomized,
172 * or we write the pointer directly to the file, if we are to stay
173 * in file order. If the former, we allocate and re-allocate in
174 * CHUNKSIZE blocks; if the latter, we just write each pointer,
175 * and then seek back to the beginning to write in the table.
184 off_t last_off
, length
, pos
, *p
;
188 static char string
[257];
191 if (sizeof(u_int32_t
) != 4)
192 die("sizeof(unsigned int) != 4");
194 getargs(ac
, av
); /* evalute arguments */
196 if ((inf
= fopen(Infile
, "r")) == NULL
)
197 dieperror("open `%s'", Infile
);
199 if ((outf
= fopen(Outfile
, "w")) == NULL
)
200 dieperror("open `%s'", Outfile
);
202 (void) fseek(outf
, sizeof Tbl
, SEEK_SET
);
205 * Write the strings onto the file
209 Tbl
.str_shortlen
= (unsigned int) 0x7fffffff;
211 Tbl
.str_version
= VERSION
;
213 add_offset(outf
, ftell(inf
));
216 sp
= fgets(string
, 256, inf
);
217 if (sp
== NULL
|| (sp
[0] == dc
&& sp
[1] == '\n')) {
219 length
= pos
- last_off
- (sp
? strlen(sp
) : 0);
223 add_offset(outf
, pos
);
224 if ((off_t
)Tbl
.str_longlen
< length
)
225 Tbl
.str_longlen
= length
;
226 if ((off_t
)Tbl
.str_shortlen
> length
)
227 Tbl
.str_shortlen
= length
;
231 for (nsp
= sp
; !isalnum(*nsp
); nsp
++)
233 ALLOC(Firstch
, Num_pts
);
234 fp
= &Firstch
[Num_pts
- 1];
235 if (Iflag
&& isupper(*nsp
))
236 fp
->first
= tolower(*nsp
);
239 fp
->pos
= Seekpts
[Num_pts
- 1];
242 } while (sp
!= NULL
);
245 * write the tables in
256 Tbl
.str_flags
|= STR_ROTATED
;
259 printf("\"%s\" created\n", Outfile
);
261 puts("There was 1 string");
263 printf("There were %d strings\n", (int)(Num_pts
- 1));
264 printf("Longest string: %lu byte%s\n", (unsigned long)Tbl
.str_longlen
,
265 Tbl
.str_longlen
== 1 ? "" : "s");
266 printf("Shortest string: %lu byte%s\n", (unsigned long)Tbl
.str_shortlen
,
267 Tbl
.str_shortlen
== 1 ? "" : "s");
270 (void) fseek(outf
, (off_t
) 0, SEEK_SET
);
271 Tbl
.str_version
= h2nl(Tbl
.str_version
);
272 Tbl
.str_numstr
= h2nl(Num_pts
- 1);
273 Tbl
.str_longlen
= h2nl(Tbl
.str_longlen
);
274 Tbl
.str_shortlen
= h2nl(Tbl
.str_shortlen
);
275 Tbl
.str_flags
= h2nl(Tbl
.str_flags
);
276 (void) fwrite((char *) &Tbl
, sizeof Tbl
, 1, outf
);
278 for (p
= Seekpts
, cnt
= Num_pts
; cnt
--; ++p
)
279 fwrite_be_offt(*p
, outf
);
283 dieperror("fwrite %s", Outfile
);
289 * This routine evaluates arguments from the command line
300 while ((ch
= getopt(argc
, argv
, "c:iorsx")) != -1)
302 case 'c': /* new delimiting char */
304 if (!isascii(Delimch
)) {
305 printf("bad delimiting character: '\\%o\n'",
309 case 'i': /* ignore case in ordering */
312 case 'o': /* order strings */
315 case 'r': /* randomize pointers */
318 case 's': /* silent */
321 case 'x': /* set the rotated bit */
333 (void) strcpy(Outfile
, *argv
);
336 puts("No input file name");
339 if (*Outfile
== '\0') {
340 (void) strcpy(Outfile
, Infile
);
341 (void) strcat(Outfile
, ".dat");
348 (void) fprintf(stderr
,
349 "strfile [-iorsx] [-c char] sourcefile [datafile]\n");
357 fprintf(stderr
, "strfile: %s\n", str
);
366 fprintf(stderr
, "strfile: ");
367 fprintf(stderr
, fmt
, file
);
368 fprintf(stderr
, ": ");
375 * Add an offset to the list, or write it out, as appropriate.
384 fwrite_be_offt(off
, fp
);
386 ALLOC(Seekpts
, Num_pts
+ 1);
387 Seekpts
[Num_pts
] = off
;
394 * Order the strings alphabetically (possibly ignoring case).
403 Sort_1
= fopen(Infile
, "r");
404 Sort_2
= fopen(Infile
, "r");
405 qsort((char *) Firstch
, (int) Tbl
.str_numstr
, sizeof *Firstch
, cmp_str
);
411 (void) fclose(Sort_1
);
412 (void) fclose(Sort_2
);
413 Tbl
.str_flags
|= STR_ORDERED
;
418 const void *vp1
, *vp2
;
424 p1
= (const STR
*)vp1
;
425 p2
= (const STR
*)vp2
;
427 # define SET_N(nf,ch) (nf = (ch == '\n'))
428 # define IS_END(ch,nf) (ch == Delimch && nf)
435 (void) fseek(Sort_1
, p1
->pos
, SEEK_SET
);
436 (void) fseek(Sort_2
, p2
->pos
, SEEK_SET
);
440 while (!isalnum(c1
= getc(Sort_1
)) && c1
!= '\0')
442 while (!isalnum(c2
= getc(Sort_2
)) && c2
!= '\0')
445 while (!IS_END(c1
, n1
) && !IS_END(c2
, n2
)) {
468 * Randomize the order of the string table. We must be careful
469 * not to randomize across delimiter boundaries. All
470 * randomization is done within each block.
479 srandom((int)(time((time_t *) NULL
) + getpid()));
481 Tbl
.str_flags
|= STR_RANDOM
;
482 cnt
= Tbl
.str_numstr
;
485 * move things around randomly
488 for (sp
= Seekpts
; cnt
> 0; cnt
--, sp
++) {
498 * Write out the off paramater as a 64 bit big endian number
502 fwrite_be_offt(off
, f
)
509 for (i
= 7; i
>= 0; i
--) {
513 fwrite(c
, sizeof(c
), 1, f
);