1 /* $NetBSD: strfile.c,v 1.28 2008/09/29 12:30:12 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\
39 The Regents of the University of California. All rights reserved.");
44 static char sccsid
[] = "@(#)strfile.c 8.1 (Berkeley) 5/31/93";
46 __RCSID("$NetBSD: strfile.c,v 1.28 2008/09/29 12:30:12 agc 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>
61 # include <inttypes.h>
66 # define MAXPATHLEN 1024
67 # endif /* MAXPATHLEN */
69 static uint32_t h2nl(uint32_t h
);
70 static void getargs(int argc
, char **argv
);
71 static void usage(void);
72 static void die(const char *str
);
73 static void dieperror(const char *fmt
, char *file
);
74 static void add_offset(FILE *fp
, off_t off
);
75 static void do_order(void);
76 static int cmp_str(const void *vp1
, const void *vp2
);
77 static void randomize(void);
78 static void fwrite_be_offt(off_t off
, FILE *f
);
86 c
[0] = (h
>> 24) & 0xff;
87 c
[1] = (h
>> 16) & 0xff;
88 c
[2] = (h
>> 8) & 0xff;
89 c
[3] = (h
>> 0) & 0xff;
90 memcpy(&rv
, c
, sizeof rv
);
96 * This program takes a file composed of strings separated by
97 * lines starting with two consecutive delimiting character (default
98 * character is '%') and creates another file which consists of a table
99 * describing the file (structure from "strfile.h"), a table of seek
100 * pointers to the start of the strings, and the strings, each terminated
101 * by a null byte. Usage:
103 * % strfile [-iorsx] [ -cC ] sourcefile [ datafile ]
105 * c - Change delimiting character from '%' to 'C'
106 * s - Silent. Give no summary of data processed at the end of
108 * o - order the strings in alphabetic order
109 * i - if ordering, ignore case
110 * r - randomize the order of the strings
111 * x - set rotated bit
113 * Ken Arnold Sept. 7, 1978 --
115 * Added ordering options.
121 # define STORING_PTRS (Oflag || Rflag)
122 # define CHUNKSIZE 512
124 # define ALLOC(ptr,sz) do { \
126 ptr = malloc(CHUNKSIZE * sizeof *ptr); \
127 else if (((sz) + 1) % CHUNKSIZE == 0) \
128 ptr = realloc(ptr, ((sz) + CHUNKSIZE) * sizeof *ptr); \
130 die("out of space"); \
138 static char *Infile
= NULL
; /* input file name */
139 static char Outfile
[MAXPATHLEN
] = ""; /* output file name */
140 static char Delimch
= '%'; /* delimiting character */
142 static int Sflag
= FALSE
; /* silent run flag */
143 static int Oflag
= FALSE
; /* ordering flag */
144 static int Iflag
= FALSE
; /* ignore case flag */
145 static int Rflag
= FALSE
; /* randomize order flag */
146 static int Xflag
= FALSE
; /* set rotated bit */
147 static long Num_pts
= 0; /* number of pointers/strings */
149 static off_t
*Seekpts
;
151 static FILE *Sort_1
, *Sort_2
; /* pointers for sorting */
153 static STRFILE Tbl
; /* statistics table */
155 static STR
*Firstch
; /* first chars of each string */
158 #define NORETURN __dead
163 #ifndef __dead /* not NetBSD, presumably */
167 void add_offset(FILE *, off_t
);
168 int cmp_str(const void *, const void *);
169 void die(const char *) NORETURN
;
170 void dieperror(const char *, char *) NORETURN
;
172 void fwrite_be_offt(off_t
, FILE *);
173 void getargs(int, char *[]);
174 int main(int, char *[]);
175 void randomize(void);
176 void usage(void) NORETURN
;
181 * Drive the sucker. There are two main modes -- either we store
182 * the seek pointers, if the table is to be sorted or randomized,
183 * or we write the pointer directly to the file, if we are to stay
184 * in file order. If the former, we allocate and re-allocate in
185 * CHUNKSIZE blocks; if the latter, we just write each pointer,
186 * and then seek back to the beginning to write in the table.
189 main(int ac
, char **av
)
193 off_t last_off
, length
, pos
, *p
;
197 static char string
[257];
200 if (sizeof(uint32_t) != 4)
201 die("sizeof(uint32_t) != 4");
203 getargs(ac
, av
); /* evalute arguments */
205 if ((inf
= fopen(Infile
, "r")) == NULL
)
206 dieperror("open `%s'", Infile
);
208 if ((outf
= fopen(Outfile
, "w")) == NULL
)
209 dieperror("open `%s'", Outfile
);
211 (void) fseek(outf
, sizeof Tbl
, SEEK_SET
);
214 * Write the strings onto the file
218 Tbl
.str_shortlen
= (unsigned int) 0x7fffffff;
220 Tbl
.str_version
= VERSION
;
222 add_offset(outf
, ftell(inf
));
225 sp
= fgets(string
, 256, inf
);
226 if (sp
== NULL
|| (sp
[0] == dc
&& sp
[1] == '\n')) {
228 length
= pos
- last_off
- (sp
? strlen(sp
) : 0);
232 add_offset(outf
, pos
);
233 if ((off_t
)Tbl
.str_longlen
< length
)
234 Tbl
.str_longlen
= length
;
235 if ((off_t
)Tbl
.str_shortlen
> length
)
236 Tbl
.str_shortlen
= length
;
240 for (nsp
= sp
; !isalnum((unsigned char)*nsp
); nsp
++)
242 ALLOC(Firstch
, Num_pts
);
243 fp
= &Firstch
[Num_pts
- 1];
244 if (Iflag
&& isupper((unsigned char)*nsp
))
245 fp
->first
= tolower((unsigned char)*nsp
);
248 fp
->pos
= Seekpts
[Num_pts
- 1];
251 } while (sp
!= NULL
);
254 * write the tables in
265 Tbl
.str_flags
|= STR_ROTATED
;
268 printf("\"%s\" created\n", Outfile
);
270 puts("There was 1 string");
272 printf("There were %d strings\n", (int)(Num_pts
- 1));
273 printf("Longest string: %lu byte%s\n", (unsigned long)Tbl
.str_longlen
,
274 Tbl
.str_longlen
== 1 ? "" : "s");
275 printf("Shortest string: %lu byte%s\n", (unsigned long)Tbl
.str_shortlen
,
276 Tbl
.str_shortlen
== 1 ? "" : "s");
279 (void) fseek(outf
, (off_t
) 0, SEEK_SET
);
280 Tbl
.str_version
= h2nl(Tbl
.str_version
);
281 Tbl
.str_numstr
= h2nl(Num_pts
- 1);
282 Tbl
.str_longlen
= h2nl(Tbl
.str_longlen
);
283 Tbl
.str_shortlen
= h2nl(Tbl
.str_shortlen
);
284 Tbl
.str_flags
= h2nl(Tbl
.str_flags
);
285 (void) fwrite((char *) &Tbl
, sizeof Tbl
, 1, outf
);
287 for (p
= Seekpts
, cnt
= Num_pts
; cnt
--; ++p
)
288 fwrite_be_offt(*p
, outf
);
292 dieperror("fwrite %s", Outfile
);
298 * This routine evaluates arguments from the command line
301 getargs(int argc
, char **argv
)
307 while ((ch
= getopt(argc
, argv
, "c:iorsx")) != -1)
309 case 'c': /* new delimiting char */
311 if (!isascii(Delimch
)) {
312 printf("bad delimiting character: '\\%o\n'",
316 case 'i': /* ignore case in ordering */
319 case 'o': /* order strings */
322 case 'r': /* randomize pointers */
325 case 's': /* silent */
328 case 'x': /* set the rotated bit */
340 (void) strcpy(Outfile
, *argv
);
343 puts("No input file name");
346 if (*Outfile
== '\0') {
347 (void) strcpy(Outfile
, Infile
);
348 (void) strcat(Outfile
, ".dat");
355 (void) fprintf(stderr
,
356 "strfile [-iorsx] [-c char] sourcefile [datafile]\n");
363 fprintf(stderr
, "strfile: %s\n", str
);
368 dieperror(const char *fmt
, char *file
)
370 fprintf(stderr
, "strfile: ");
371 fprintf(stderr
, fmt
, file
);
372 fprintf(stderr
, ": ");
379 * Add an offset to the list, or write it out, as appropriate.
382 add_offset(FILE *fp
, off_t off
)
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
;
419 cmp_str(const void *vp1
, const void *vp2
)
425 p1
= (const STR
*)vp1
;
426 p2
= (const STR
*)vp2
;
428 # define SET_N(nf,ch) (nf = (ch == '\n'))
429 # define IS_END(ch,nf) (ch == Delimch && nf)
436 (void) fseek(Sort_1
, p1
->pos
, SEEK_SET
);
437 (void) fseek(Sort_2
, p2
->pos
, SEEK_SET
);
441 while (!isalnum(c1
= getc(Sort_1
)) && c1
!= '\0')
443 while (!isalnum(c2
= getc(Sort_2
)) && c2
!= '\0')
446 while (!IS_END(c1
, n1
) && !IS_END(c2
, n2
)) {
469 * Randomize the order of the string table. We must be careful
470 * not to randomize across delimiter boundaries. All
471 * randomization is done within each block.
480 srandom((int)(time((time_t *) NULL
) + getpid()));
482 Tbl
.str_flags
|= STR_RANDOM
;
483 cnt
= Tbl
.str_numstr
;
486 * move things around randomly
489 for (sp
= Seekpts
; cnt
> 0; cnt
--, sp
++) {
499 * Write out the off paramater as a 64 bit big endian number
503 fwrite_be_offt(off_t off
, FILE *f
)
508 for (i
= 7; i
>= 0; i
--) {
512 fwrite(c
, sizeof(c
), 1, f
);