]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - fortune/strfile/strfile.c
1 /* $NetBSD: strfile.c,v 1.22 2003/08/07 09:37:14 agc 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.22 2003/08/07 09:37:14 agc Exp $");
49 #endif /* __NetBSD__ */
51 # include <sys/types.h>
52 # include <sys/param.h>
61 # define u_int32_t unsigned int
66 # define MAXPATHLEN 1024
67 # endif /* MAXPATHLEN */
75 c
[0] = (h
>> 24) & 0xff;
76 c
[1] = (h
>> 16) & 0xff;
77 c
[2] = (h
>> 8) & 0xff;
78 c
[3] = (h
>> 0) & 0xff;
79 memcpy(&rv
, c
, sizeof rv
);
85 * This program takes a file composed of strings separated by
86 * lines starting with two consecutive delimiting character (default
87 * character is '%') and creates another file which consists of a table
88 * describing the file (structure from "strfile.h"), a table of seek
89 * pointers to the start of the strings, and the strings, each terminated
90 * by a null byte. Usage:
92 * % strfile [-iorsx] [ -cC ] sourcefile [ datafile ]
94 * c - Change delimiting character from '%' to 'C'
95 * s - Silent. Give no summary of data processed at the end of
97 * o - order the strings in alphabetic order
98 * i - if ordering, ignore case
99 * r - randomize the order of the strings
100 * x - set rotated bit
102 * Ken Arnold Sept. 7, 1978 --
104 * Added ordering options.
110 # define STORING_PTRS (Oflag || Rflag)
111 # define CHUNKSIZE 512
113 # define ALLOC(ptr,sz) do { \
115 ptr = malloc(CHUNKSIZE * sizeof *ptr); \
116 else if (((sz) + 1) % CHUNKSIZE == 0) \
117 ptr = realloc(ptr, ((sz) + CHUNKSIZE) * sizeof *ptr); \
119 die("out of space"); \
127 char *Infile
= NULL
, /* input file name */
128 Outfile
[MAXPATHLEN
] = "", /* output file name */
129 Delimch
= '%'; /* delimiting character */
131 int Sflag
= FALSE
; /* silent run flag */
132 int Oflag
= FALSE
; /* ordering flag */
133 int Iflag
= FALSE
; /* ignore case flag */
134 int Rflag
= FALSE
; /* randomize order flag */
135 int Xflag
= FALSE
; /* set rotated bit */
136 long Num_pts
= 0; /* number of pointers/strings */
140 FILE *Sort_1
, *Sort_2
; /* pointers for sorting */
142 STRFILE Tbl
; /* statistics table */
144 STR
*Firstch
; /* first chars of each string */
147 #define NORETURN __attribute__((__noreturn__))
152 void add_offset(FILE *, off_t
);
153 int cmp_str(const void *, const void *);
154 void die(const char *) NORETURN
;
155 void dieperror(const char *, char *) NORETURN
;
157 void fwrite_be_offt(off_t
, FILE *);
158 void getargs(int, char *[]);
159 int main(int, char *[]);
160 void randomize(void);
161 void usage(void) NORETURN
;
166 * Drive the sucker. There are two main modes -- either we store
167 * the seek pointers, if the table is to be sorted or randomized,
168 * or we write the pointer directly to the file, if we are to stay
169 * in file order. If the former, we allocate and re-allocate in
170 * CHUNKSIZE blocks; if the latter, we just write each pointer,
171 * and then seek back to the beginning to write in the table.
180 off_t last_off
, length
, pos
, *p
;
184 static char string
[257];
187 if (sizeof(u_int32_t
) != 4)
188 die("sizeof(unsigned int) != 4");
190 getargs(ac
, av
); /* evalute arguments */
192 if ((inf
= fopen(Infile
, "r")) == NULL
)
193 dieperror("open `%s'", Infile
);
195 if ((outf
= fopen(Outfile
, "w")) == NULL
)
196 dieperror("open `%s'", Outfile
);
198 (void) fseek(outf
, sizeof Tbl
, SEEK_SET
);
201 * Write the strings onto the file
205 Tbl
.str_shortlen
= (unsigned int) 0x7fffffff;
207 Tbl
.str_version
= VERSION
;
209 add_offset(outf
, ftell(inf
));
212 sp
= fgets(string
, 256, inf
);
213 if (sp
== NULL
|| (sp
[0] == dc
&& sp
[1] == '\n')) {
215 length
= pos
- last_off
- (sp
? strlen(sp
) : 0);
219 add_offset(outf
, pos
);
220 if ((off_t
)Tbl
.str_longlen
< length
)
221 Tbl
.str_longlen
= length
;
222 if ((off_t
)Tbl
.str_shortlen
> length
)
223 Tbl
.str_shortlen
= length
;
227 for (nsp
= sp
; !isalnum(*nsp
); nsp
++)
229 ALLOC(Firstch
, Num_pts
);
230 fp
= &Firstch
[Num_pts
- 1];
231 if (Iflag
&& isupper(*nsp
))
232 fp
->first
= tolower(*nsp
);
235 fp
->pos
= Seekpts
[Num_pts
- 1];
238 } while (sp
!= NULL
);
241 * write the tables in
252 Tbl
.str_flags
|= STR_ROTATED
;
255 printf("\"%s\" created\n", Outfile
);
257 puts("There was 1 string");
259 printf("There were %d strings\n", (int)(Num_pts
- 1));
260 printf("Longest string: %lu byte%s\n", (unsigned long)Tbl
.str_longlen
,
261 Tbl
.str_longlen
== 1 ? "" : "s");
262 printf("Shortest string: %lu byte%s\n", (unsigned long)Tbl
.str_shortlen
,
263 Tbl
.str_shortlen
== 1 ? "" : "s");
266 (void) fseek(outf
, (off_t
) 0, SEEK_SET
);
267 Tbl
.str_version
= h2nl(Tbl
.str_version
);
268 Tbl
.str_numstr
= h2nl(Num_pts
- 1);
269 Tbl
.str_longlen
= h2nl(Tbl
.str_longlen
);
270 Tbl
.str_shortlen
= h2nl(Tbl
.str_shortlen
);
271 Tbl
.str_flags
= h2nl(Tbl
.str_flags
);
272 (void) fwrite((char *) &Tbl
, sizeof Tbl
, 1, outf
);
274 for (p
= Seekpts
, cnt
= Num_pts
; cnt
--; ++p
)
275 fwrite_be_offt(*p
, outf
);
279 dieperror("fwrite %s", Outfile
);
285 * This routine evaluates arguments from the command line
296 while ((ch
= getopt(argc
, argv
, "c:iorsx")) != -1)
298 case 'c': /* new delimiting char */
300 if (!isascii(Delimch
)) {
301 printf("bad delimiting character: '\\%o\n'",
305 case 'i': /* ignore case in ordering */
308 case 'o': /* order strings */
311 case 'r': /* randomize pointers */
314 case 's': /* silent */
317 case 'x': /* set the rotated bit */
329 (void) strcpy(Outfile
, *argv
);
332 puts("No input file name");
335 if (*Outfile
== '\0') {
336 (void) strcpy(Outfile
, Infile
);
337 (void) strcat(Outfile
, ".dat");
344 (void) fprintf(stderr
,
345 "strfile [-iorsx] [-c char] sourcefile [datafile]\n");
353 fprintf(stderr
, "strfile: %s\n", str
);
362 fprintf(stderr
, "strfile: ");
363 fprintf(stderr
, fmt
, file
);
364 fprintf(stderr
, ": ");
371 * Add an offset to the list, or write it out, as appropriate.
380 fwrite_be_offt(off
, fp
);
382 ALLOC(Seekpts
, Num_pts
+ 1);
383 Seekpts
[Num_pts
] = off
;
390 * Order the strings alphabetically (possibly ignoring case).
399 Sort_1
= fopen(Infile
, "r");
400 Sort_2
= fopen(Infile
, "r");
401 qsort((char *) Firstch
, (int) Tbl
.str_numstr
, sizeof *Firstch
, cmp_str
);
407 (void) fclose(Sort_1
);
408 (void) fclose(Sort_2
);
409 Tbl
.str_flags
|= STR_ORDERED
;
414 const void *vp1
, *vp2
;
420 p1
= (const STR
*)vp1
;
421 p2
= (const STR
*)vp2
;
423 # define SET_N(nf,ch) (nf = (ch == '\n'))
424 # define IS_END(ch,nf) (ch == Delimch && nf)
431 (void) fseek(Sort_1
, p1
->pos
, SEEK_SET
);
432 (void) fseek(Sort_2
, p2
->pos
, SEEK_SET
);
436 while (!isalnum(c1
= getc(Sort_1
)) && c1
!= '\0')
438 while (!isalnum(c2
= getc(Sort_2
)) && c2
!= '\0')
441 while (!IS_END(c1
, n1
) && !IS_END(c2
, n2
)) {
464 * Randomize the order of the string table. We must be careful
465 * not to randomize across delimiter boundaries. All
466 * randomization is done within each block.
475 srandom((int)(time((time_t *) NULL
) + getpid()));
477 Tbl
.str_flags
|= STR_RANDOM
;
478 cnt
= Tbl
.str_numstr
;
481 * move things around randomly
484 for (sp
= Seekpts
; cnt
> 0; cnt
--, sp
++) {
494 * Write out the off paramater as a 64 bit big endian number
498 fwrite_be_offt(off
, f
)
505 for (i
= 7; i
>= 0; i
--) {
509 fwrite(c
, sizeof(c
), 1, f
);