]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - fortune/strfile/strfile.c
1 /* $NetBSD: strfile.c,v 1.25 2007/12/18 08:45:03 dogcow 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
36 #include <sys/cdefs.h>
38 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
39 The Regents of the University of California. All rights reserved.\n");
44 static char sccsid
[] = "@(#)strfile.c 8.1 (Berkeley) 5/31/93";
46 __RCSID("$NetBSD: strfile.c,v 1.25 2007/12/18 08:45:03 dogcow Exp $");
49 #endif /* __NetBSD__ */
51 /* n.b.: this file is used at build-time - i.e. during build.sh. */
53 # include <sys/types.h>
54 # include <sys/param.h>
63 # define u_int32_t unsigned int
68 # define MAXPATHLEN 1024
69 # endif /* MAXPATHLEN */
77 c
[0] = (h
>> 24) & 0xff;
78 c
[1] = (h
>> 16) & 0xff;
79 c
[2] = (h
>> 8) & 0xff;
80 c
[3] = (h
>> 0) & 0xff;
81 memcpy(&rv
, c
, sizeof rv
);
87 * This program takes a file composed of strings separated by
88 * lines starting with two consecutive delimiting character (default
89 * character is '%') and creates another file which consists of a table
90 * describing the file (structure from "strfile.h"), a table of seek
91 * pointers to the start of the strings, and the strings, each terminated
92 * by a null byte. Usage:
94 * % strfile [-iorsx] [ -cC ] sourcefile [ datafile ]
96 * c - Change delimiting character from '%' to 'C'
97 * s - Silent. Give no summary of data processed at the end of
99 * o - order the strings in alphabetic order
100 * i - if ordering, ignore case
101 * r - randomize the order of the strings
102 * x - set rotated bit
104 * Ken Arnold Sept. 7, 1978 --
106 * Added ordering options.
112 # define STORING_PTRS (Oflag || Rflag)
113 # define CHUNKSIZE 512
115 # define ALLOC(ptr,sz) do { \
117 ptr = malloc(CHUNKSIZE * sizeof *ptr); \
118 else if (((sz) + 1) % CHUNKSIZE == 0) \
119 ptr = realloc(ptr, ((sz) + CHUNKSIZE) * sizeof *ptr); \
121 die("out of space"); \
129 char *Infile
= NULL
, /* input file name */
130 Outfile
[MAXPATHLEN
] = "", /* output file name */
131 Delimch
= '%'; /* delimiting character */
133 int Sflag
= FALSE
; /* silent run flag */
134 int Oflag
= FALSE
; /* ordering flag */
135 int Iflag
= FALSE
; /* ignore case flag */
136 int Rflag
= FALSE
; /* randomize order flag */
137 int Xflag
= FALSE
; /* set rotated bit */
138 long Num_pts
= 0; /* number of pointers/strings */
142 FILE *Sort_1
, *Sort_2
; /* pointers for sorting */
144 STRFILE Tbl
; /* statistics table */
146 STR
*Firstch
; /* first chars of each string */
149 #define NORETURN __dead
154 #ifndef __dead /* not NetBSD, presumably */
158 void add_offset(FILE *, off_t
);
159 int cmp_str(const void *, const void *);
160 void die(const char *) NORETURN
;
161 void dieperror(const char *, char *) NORETURN
;
163 void fwrite_be_offt(off_t
, FILE *);
164 void getargs(int, char *[]);
165 int main(int, char *[]);
166 void randomize(void);
167 void usage(void) NORETURN
;
172 * Drive the sucker. There are two main modes -- either we store
173 * the seek pointers, if the table is to be sorted or randomized,
174 * or we write the pointer directly to the file, if we are to stay
175 * in file order. If the former, we allocate and re-allocate in
176 * CHUNKSIZE blocks; if the latter, we just write each pointer,
177 * and then seek back to the beginning to write in the table.
186 off_t last_off
, length
, pos
, *p
;
190 static char string
[257];
193 if (sizeof(u_int32_t
) != 4)
194 die("sizeof(unsigned int) != 4");
196 getargs(ac
, av
); /* evalute arguments */
198 if ((inf
= fopen(Infile
, "r")) == NULL
)
199 dieperror("open `%s'", Infile
);
201 if ((outf
= fopen(Outfile
, "w")) == NULL
)
202 dieperror("open `%s'", Outfile
);
204 (void) fseek(outf
, sizeof Tbl
, SEEK_SET
);
207 * Write the strings onto the file
211 Tbl
.str_shortlen
= (unsigned int) 0x7fffffff;
213 Tbl
.str_version
= VERSION
;
215 add_offset(outf
, ftell(inf
));
218 sp
= fgets(string
, 256, inf
);
219 if (sp
== NULL
|| (sp
[0] == dc
&& sp
[1] == '\n')) {
221 length
= pos
- last_off
- (sp
? strlen(sp
) : 0);
225 add_offset(outf
, pos
);
226 if ((off_t
)Tbl
.str_longlen
< length
)
227 Tbl
.str_longlen
= length
;
228 if ((off_t
)Tbl
.str_shortlen
> length
)
229 Tbl
.str_shortlen
= length
;
233 for (nsp
= sp
; !isalnum((unsigned char)*nsp
); nsp
++)
235 ALLOC(Firstch
, Num_pts
);
236 fp
= &Firstch
[Num_pts
- 1];
237 if (Iflag
&& isupper((unsigned char)*nsp
))
238 fp
->first
= tolower((unsigned char)*nsp
);
241 fp
->pos
= Seekpts
[Num_pts
- 1];
244 } while (sp
!= NULL
);
247 * write the tables in
258 Tbl
.str_flags
|= STR_ROTATED
;
261 printf("\"%s\" created\n", Outfile
);
263 puts("There was 1 string");
265 printf("There were %d strings\n", (int)(Num_pts
- 1));
266 printf("Longest string: %lu byte%s\n", (unsigned long)Tbl
.str_longlen
,
267 Tbl
.str_longlen
== 1 ? "" : "s");
268 printf("Shortest string: %lu byte%s\n", (unsigned long)Tbl
.str_shortlen
,
269 Tbl
.str_shortlen
== 1 ? "" : "s");
272 (void) fseek(outf
, (off_t
) 0, SEEK_SET
);
273 Tbl
.str_version
= h2nl(Tbl
.str_version
);
274 Tbl
.str_numstr
= h2nl(Num_pts
- 1);
275 Tbl
.str_longlen
= h2nl(Tbl
.str_longlen
);
276 Tbl
.str_shortlen
= h2nl(Tbl
.str_shortlen
);
277 Tbl
.str_flags
= h2nl(Tbl
.str_flags
);
278 (void) fwrite((char *) &Tbl
, sizeof Tbl
, 1, outf
);
280 for (p
= Seekpts
, cnt
= Num_pts
; cnt
--; ++p
)
281 fwrite_be_offt(*p
, outf
);
285 dieperror("fwrite %s", Outfile
);
291 * This routine evaluates arguments from the command line
302 while ((ch
= getopt(argc
, argv
, "c:iorsx")) != -1)
304 case 'c': /* new delimiting char */
306 if (!isascii(Delimch
)) {
307 printf("bad delimiting character: '\\%o\n'",
311 case 'i': /* ignore case in ordering */
314 case 'o': /* order strings */
317 case 'r': /* randomize pointers */
320 case 's': /* silent */
323 case 'x': /* set the rotated bit */
335 (void) strcpy(Outfile
, *argv
);
338 puts("No input file name");
341 if (*Outfile
== '\0') {
342 (void) strcpy(Outfile
, Infile
);
343 (void) strcat(Outfile
, ".dat");
350 (void) fprintf(stderr
,
351 "strfile [-iorsx] [-c char] sourcefile [datafile]\n");
359 fprintf(stderr
, "strfile: %s\n", str
);
368 fprintf(stderr
, "strfile: ");
369 fprintf(stderr
, fmt
, file
);
370 fprintf(stderr
, ": ");
377 * Add an offset to the list, or write it out, as appropriate.
386 fwrite_be_offt(off
, fp
);
388 ALLOC(Seekpts
, Num_pts
+ 1);
389 Seekpts
[Num_pts
] = off
;
396 * Order the strings alphabetically (possibly ignoring case).
405 Sort_1
= fopen(Infile
, "r");
406 Sort_2
= fopen(Infile
, "r");
407 qsort((char *) Firstch
, (int) Tbl
.str_numstr
, sizeof *Firstch
, cmp_str
);
413 (void) fclose(Sort_1
);
414 (void) fclose(Sort_2
);
415 Tbl
.str_flags
|= STR_ORDERED
;
420 const void *vp1
, *vp2
;
426 p1
= (const STR
*)vp1
;
427 p2
= (const STR
*)vp2
;
429 # define SET_N(nf,ch) (nf = (ch == '\n'))
430 # define IS_END(ch,nf) (ch == Delimch && nf)
437 (void) fseek(Sort_1
, p1
->pos
, SEEK_SET
);
438 (void) fseek(Sort_2
, p2
->pos
, SEEK_SET
);
442 while (!isalnum(c1
= getc(Sort_1
)) && c1
!= '\0')
444 while (!isalnum(c2
= getc(Sort_2
)) && c2
!= '\0')
447 while (!IS_END(c1
, n1
) && !IS_END(c2
, n2
)) {
470 * Randomize the order of the string table. We must be careful
471 * not to randomize across delimiter boundaries. All
472 * randomization is done within each block.
481 srandom((int)(time((time_t *) NULL
) + getpid()));
483 Tbl
.str_flags
|= STR_RANDOM
;
484 cnt
= Tbl
.str_numstr
;
487 * move things around randomly
490 for (sp
= Seekpts
; cnt
> 0; cnt
--, sp
++) {
500 * Write out the off paramater as a 64 bit big endian number
504 fwrite_be_offt(off
, f
)
511 for (i
= 7; i
>= 0; i
--) {
515 fwrite(c
, sizeof(c
), 1, f
);