]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - fortune/strfile/strfile.c
f8ff4a2fc405b1d3d05f355f1a2394e815a9b36d
1 /* $NetBSD: strfile.c,v 1.14 1999/09/18 19:38:50 jsm 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
39 #include <sys/cdefs.h>
41 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
42 The Regents of the University of California. All rights reserved.\n");
47 static char sccsid
[] = "@(#)strfile.c 8.1 (Berkeley) 5/31/93";
49 __RCSID("$NetBSD: strfile.c,v 1.14 1999/09/18 19:38:50 jsm Exp $");
53 # include <sys/types.h>
54 # include <sys/param.h>
65 # define MAXPATHLEN 1024
66 # endif /* MAXPATHLEN */
69 * This program takes a file composed of strings seperated by
70 * lines starting with two consecutive delimiting character (default
71 * character is '%') and creates another file which consists of a table
72 * describing the file (structure from "strfile.h"), a table of seek
73 * pointers to the start of the strings, and the strings, each terminated
74 * by a null byte. Usage:
76 * % strfile [-iorsx] [ -cC ] sourcefile [ datafile ]
78 * c - Change delimiting character from '%' to 'C'
79 * s - Silent. Give no summary of data processed at the end of
81 * o - order the strings in alphabetic order
82 * i - if ordering, ignore case
83 * r - randomize the order of the strings
86 * Ken Arnold Sept. 7, 1978 --
88 * Added ordering options.
94 # define STORING_PTRS (Oflag || Rflag)
95 # define CHUNKSIZE 512
98 # define ALWAYS atoi("1")
102 # define ALLOC(ptr,sz) if (ALWAYS) { \
104 ptr = malloc((unsigned int) (CHUNKSIZE * sizeof *ptr)); \
105 else if (((sz) + 1) % CHUNKSIZE == 0) \
106 ptr = realloc((void *) ptr, ((unsigned int) ((sz) + CHUNKSIZE) * sizeof *ptr)); \
108 errx(1, "out of space"); \
120 char *Infile
= NULL
, /* input file name */
121 Outfile
[MAXPATHLEN
] = "", /* output file name */
122 Delimch
= '%'; /* delimiting character */
124 int Sflag
= FALSE
; /* silent run flag */
125 int Oflag
= FALSE
; /* ordering flag */
126 int Iflag
= FALSE
; /* ignore case flag */
127 int Rflag
= FALSE
; /* randomize order flag */
128 int Xflag
= FALSE
; /* set rotated bit */
129 long Num_pts
= 0; /* number of pointers/strings */
133 FILE *Sort_1
, *Sort_2
; /* pointers for sorting */
135 STRFILE Tbl
; /* statistics table */
137 STR
*Firstch
; /* first chars of each string */
139 void add_offset
__P((FILE *, off_t
));
140 int cmp_str
__P((const void *, const void *));
141 void do_order
__P((void));
142 void getargs
__P((int, char *[]));
143 int main
__P((int, char *[]));
144 void randomize
__P((void));
145 char *unctrl
__P((char));
146 void usage
__P((void)) __attribute__((__noreturn__
));
151 * Drive the sucker. There are two main modes -- either we store
152 * the seek pointers, if the table is to be sorted or randomized,
153 * or we write the pointer directly to the file, if we are to stay
154 * in file order. If the former, we allocate and re-allocate in
155 * CHUNKSIZE blocks; if the latter, we just write each pointer,
156 * and then seek back to the beginning to write in the table.
165 off_t last_off
, length
, pos
, *p
;
169 static char string
[257];
171 getargs(ac
, av
); /* evalute arguments */
173 if ((inf
= fopen(Infile
, "r")) == NULL
)
174 err(1, "open `%s'", Infile
);
176 if ((outf
= fopen(Outfile
, "w")) == NULL
)
177 err(1, "open `%s'", Outfile
);
179 (void) fseek(outf
, sizeof Tbl
, SEEK_SET
);
182 * Write the strings onto the file
186 Tbl
.str_shortlen
= (unsigned int) 0xffffffff;
188 Tbl
.str_version
= VERSION
;
190 add_offset(outf
, ftell(inf
));
193 sp
= fgets(string
, 256, inf
);
194 if (sp
== NULL
|| (sp
[0] == dc
&& sp
[1] == '\n')) {
196 length
= pos
- last_off
- (sp
? strlen(sp
) : 0);
200 add_offset(outf
, pos
);
201 if ((off_t
)Tbl
.str_longlen
< length
)
202 Tbl
.str_longlen
= length
;
203 if ((off_t
)Tbl
.str_shortlen
> length
)
204 Tbl
.str_shortlen
= length
;
208 for (nsp
= sp
; !isalnum(*nsp
); nsp
++)
210 ALLOC(Firstch
, Num_pts
);
211 fp
= &Firstch
[Num_pts
- 1];
212 if (Iflag
&& isupper(*nsp
))
213 fp
->first
= tolower(*nsp
);
216 fp
->pos
= Seekpts
[Num_pts
- 1];
219 } while (sp
!= NULL
);
222 * write the tables in
233 Tbl
.str_flags
|= STR_ROTATED
;
236 printf("\"%s\" created\n", Outfile
);
238 puts("There was 1 string");
240 printf("There were %d strings\n", (int)(Num_pts
- 1));
241 printf("Longest string: %lu byte%s\n", Tbl
.str_longlen
,
242 Tbl
.str_longlen
== 1 ? "" : "s");
243 printf("Shortest string: %lu byte%s\n", Tbl
.str_shortlen
,
244 Tbl
.str_shortlen
== 1 ? "" : "s");
247 (void) fseek(outf
, (off_t
) 0, SEEK_SET
);
248 HTOBE32(Tbl
.str_version
);
249 Tbl
.str_numstr
= htobe32(Num_pts
- 1);
250 HTOBE32(Tbl
.str_longlen
);
251 HTOBE32(Tbl
.str_shortlen
);
252 HTOBE32(Tbl
.str_flags
);
253 (void) fwrite((char *) &Tbl
, sizeof Tbl
, 1, outf
);
255 for (p
= Seekpts
, cnt
= Num_pts
; cnt
--; ++p
)
257 (void) fwrite((char *) Seekpts
, sizeof *Seekpts
, (int) Num_pts
, outf
);
261 err(1, "fwrite %s", Outfile
);
267 * This routine evaluates arguments from the command line
276 while ((ch
= getopt(argc
, argv
, "c:iorsx")) != -1)
278 case 'c': /* new delimiting char */
280 if (!isascii(Delimch
)) {
281 printf("bad delimiting character: '\\%o\n'",
285 case 'i': /* ignore case in ordering */
288 case 'o': /* order strings */
291 case 'r': /* randomize pointers */
294 case 's': /* silent */
297 case 'x': /* set the rotated bit */
309 (void) strcpy(Outfile
, *argv
);
312 puts("No input file name");
315 if (*Outfile
== '\0') {
316 (void) strcpy(Outfile
, Infile
);
317 (void) strcat(Outfile
, ".dat");
324 (void) fprintf(stderr
,
325 "strfile [-iorsx] [-c char] sourcefile [datafile]\n");
331 * Add an offset to the list, or write it out, as appropriate.
342 fwrite(&net
, 1, sizeof net
, fp
);
344 ALLOC(Seekpts
, Num_pts
+ 1);
345 Seekpts
[Num_pts
] = off
;
352 * Order the strings alphabetically (possibly ignoring case).
361 Sort_1
= fopen(Infile
, "r");
362 Sort_2
= fopen(Infile
, "r");
363 qsort((char *) Firstch
, (int) Tbl
.str_numstr
, sizeof *Firstch
, cmp_str
);
369 (void) fclose(Sort_1
);
370 (void) fclose(Sort_2
);
371 Tbl
.str_flags
|= STR_ORDERED
;
376 * Compare two strings in the file
388 else if (c
== 0177) {
394 buf
[1] = c
+ 'A' - 1;
401 const void *vp1
, *vp2
;
407 p1
= (const STR
*)vp1
;
408 p2
= (const STR
*)vp2
;
410 # define SET_N(nf,ch) (nf = (ch == '\n'))
411 # define IS_END(ch,nf) (ch == Delimch && nf)
418 (void) fseek(Sort_1
, p1
->pos
, SEEK_SET
);
419 (void) fseek(Sort_2
, p2
->pos
, SEEK_SET
);
423 while (!isalnum(c1
= getc(Sort_1
)) && c1
!= '\0')
425 while (!isalnum(c2
= getc(Sort_2
)) && c2
!= '\0')
428 while (!IS_END(c1
, n1
) && !IS_END(c2
, n2
)) {
451 * Randomize the order of the string table. We must be careful
452 * not to randomize across delimiter boundaries. All
453 * randomization is done within each block.
462 srandom((int)(time((time_t *) NULL
) + getpid()));
464 Tbl
.str_flags
|= STR_RANDOM
;
465 cnt
= Tbl
.str_numstr
;
468 * move things around randomly
471 for (sp
= Seekpts
; cnt
> 0; cnt
--, sp
++) {