]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - fortune/strfile/strfile.c
1 /* $NetBSD: strfile.c,v 1.8 1998/09/13 15:27:28 hubertf 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.8 1998/09/13 15:27:28 hubertf Exp $");
53 # include <sys/types.h>
54 # include <sys/param.h>
64 # define MAXPATHLEN 1024
65 # endif /* MAXPATHLEN */
68 * This program takes a file composed of strings seperated by
69 * lines starting with two consecutive delimiting character (default
70 * character is '%') and creates another file which consists of a table
71 * describing the file (structure from "strfile.h"), a table of seek
72 * pointers to the start of the strings, and the strings, each terminated
73 * by a null byte. Usage:
75 * % strfile [-iorsx] [ -cC ] sourcefile [ datafile ]
77 * c - Change delimiting character from '%' to 'C'
78 * s - Silent. Give no summary of data processed at the end of
80 * o - order the strings in alphabetic order
81 * i - if ordering, ignore case
82 * r - randomize the order of the strings
85 * Ken Arnold Sept. 7, 1978 --
87 * Added ordering options.
93 # define STORING_PTRS (Oflag || Rflag)
94 # define CHUNKSIZE 512
97 # define ALWAYS atoi("1")
101 # define ALLOC(ptr,sz) if (ALWAYS) { \
103 ptr = malloc((unsigned int) (CHUNKSIZE * sizeof *ptr)); \
104 else if (((sz) + 1) % CHUNKSIZE == 0) \
105 ptr = realloc((void *) ptr, ((unsigned int) ((sz) + CHUNKSIZE) * sizeof *ptr)); \
107 errx(1, "out of space"); \
119 char *Infile
= NULL
, /* input file name */
120 Outfile
[MAXPATHLEN
] = "", /* output file name */
121 Delimch
= '%'; /* delimiting character */
123 int Sflag
= FALSE
; /* silent run flag */
124 int Oflag
= FALSE
; /* ordering flag */
125 int Iflag
= FALSE
; /* ignore case flag */
126 int Rflag
= FALSE
; /* randomize order flag */
127 int Xflag
= FALSE
; /* set rotated bit */
128 long Num_pts
= 0; /* number of pointers/strings */
132 FILE *Sort_1
, *Sort_2
; /* pointers for sorting */
134 STRFILE Tbl
; /* statistics table */
136 STR
*Firstch
; /* first chars of each string */
138 void add_offset
__P((FILE *, off_t
));
139 int cmp_str
__P((const void *, const void *));
140 void do_order
__P((void));
141 void getargs
__P((int, char *[]));
142 int main
__P((int, char *[]));
143 void randomize
__P((void));
144 char *unctrl
__P((char));
145 void usage
__P((void)) __attribute__((__noreturn__
));
150 * Drive the sucker. There are two main modes -- either we store
151 * the seek pointers, if the table is to be sorted or randomized,
152 * or we write the pointer directly to the file, if we are to stay
153 * in file order. If the former, we allocate and re-allocate in
154 * CHUNKSIZE blocks; if the latter, we just write each pointer,
155 * and then seek back to the beginning to write in the table.
164 off_t last_off
, length
, pos
, *p
;
168 static char string
[257];
170 getargs(ac
, av
); /* evalute arguments */
172 if ((inf
= fopen(Infile
, "r")) == NULL
)
173 err(1, "open `%s'", Infile
);
175 if ((outf
= fopen(Outfile
, "w")) == NULL
)
176 err(1, "open `%s'", Outfile
);
178 (void) fseek(outf
, sizeof Tbl
, 0);
181 * Write the strings onto the file
185 Tbl
.str_shortlen
= (unsigned int) 0xffffffff;
187 Tbl
.str_version
= VERSION
;
189 add_offset(outf
, ftell(inf
));
192 sp
= fgets(string
, 256, inf
);
193 if (sp
== NULL
|| (sp
[0] == dc
&& sp
[1] == '\n')) {
195 length
= pos
- last_off
- (sp
? strlen(sp
) : 0);
199 add_offset(outf
, pos
);
200 if (Tbl
.str_longlen
< length
)
201 Tbl
.str_longlen
= length
;
202 if (Tbl
.str_shortlen
> length
)
203 Tbl
.str_shortlen
= length
;
207 for (nsp
= sp
; !isalnum(*nsp
); nsp
++)
209 ALLOC(Firstch
, Num_pts
);
210 fp
= &Firstch
[Num_pts
- 1];
211 if (Iflag
&& isupper(*nsp
))
212 fp
->first
= tolower(*nsp
);
215 fp
->pos
= Seekpts
[Num_pts
- 1];
218 } while (sp
!= NULL
);
221 * write the tables in
232 Tbl
.str_flags
|= STR_ROTATED
;
235 printf("\"%s\" created\n", Outfile
);
237 puts("There was 1 string");
239 printf("There were %d strings\n", (int)(Num_pts
- 1));
240 printf("Longest string: %lu byte%s\n", Tbl
.str_longlen
,
241 Tbl
.str_longlen
== 1 ? "" : "s");
242 printf("Shortest string: %lu byte%s\n", Tbl
.str_shortlen
,
243 Tbl
.str_shortlen
== 1 ? "" : "s");
246 (void) fseek(outf
, (off_t
) 0, 0);
247 Tbl
.str_version
= htonl(Tbl
.str_version
);
248 Tbl
.str_numstr
= htonl(Num_pts
- 1);
249 Tbl
.str_longlen
= htonl(Tbl
.str_longlen
);
250 Tbl
.str_shortlen
= htonl(Tbl
.str_shortlen
);
251 Tbl
.str_flags
= htonl(Tbl
.str_flags
);
252 (void) fwrite((char *) &Tbl
, sizeof Tbl
, 1, outf
);
254 for (p
= Seekpts
, cnt
= Num_pts
; cnt
--; ++p
)
256 (void) fwrite((char *) Seekpts
, sizeof *Seekpts
, (int) Num_pts
, outf
);
263 * This routine evaluates arguments from the command line
272 while ((ch
= getopt(argc
, argv
, "c:iorsx")) != -1)
274 case 'c': /* new delimiting char */
276 if (!isascii(Delimch
)) {
277 printf("bad delimiting character: '\\%o\n'",
281 case 'i': /* ignore case in ordering */
284 case 'o': /* order strings */
287 case 'r': /* randomize pointers */
290 case 's': /* silent */
293 case 'x': /* set the rotated bit */
305 (void) strcpy(Outfile
, *argv
);
308 puts("No input file name");
311 if (*Outfile
== '\0') {
312 (void) strcpy(Outfile
, Infile
);
313 (void) strcat(Outfile
, ".dat");
320 (void) fprintf(stderr
,
321 "strfile [-iorsx] [-c char] sourcefile [datafile]\n");
327 * Add an offset to the list, or write it out, as appropriate.
338 fwrite(&net
, 1, sizeof net
, fp
);
340 ALLOC(Seekpts
, Num_pts
+ 1);
341 Seekpts
[Num_pts
] = off
;
348 * Order the strings alphabetically (possibly ignoring case).
357 Sort_1
= fopen(Infile
, "r");
358 Sort_2
= fopen(Infile
, "r");
359 qsort((char *) Firstch
, (int) Tbl
.str_numstr
, sizeof *Firstch
, cmp_str
);
365 (void) fclose(Sort_1
);
366 (void) fclose(Sort_2
);
367 Tbl
.str_flags
|= STR_ORDERED
;
372 * Compare two strings in the file
384 else if (c
== 0177) {
390 buf
[1] = c
+ 'A' - 1;
397 const void *vp1
, *vp2
;
406 # define SET_N(nf,ch) (nf = (ch == '\n'))
407 # define IS_END(ch,nf) (ch == Delimch && nf)
414 (void) fseek(Sort_1
, p1
->pos
, 0);
415 (void) fseek(Sort_2
, p2
->pos
, 0);
419 while (!isalnum(c1
= getc(Sort_1
)) && c1
!= '\0')
421 while (!isalnum(c2
= getc(Sort_2
)) && c2
!= '\0')
424 while (!IS_END(c1
, n1
) && !IS_END(c2
, n2
)) {
447 * Randomize the order of the string table. We must be careful
448 * not to randomize across delimiter boundaries. All
449 * randomization is done within each block.
458 srandom((int)(time((time_t *) NULL
) + getpid()));
460 Tbl
.str_flags
|= STR_RANDOM
;
461 cnt
= Tbl
.str_numstr
;
464 * move things around randomly
467 for (sp
= Seekpts
; cnt
> 0; cnt
--, sp
++) {