Home | History | Annotate | Line # | Download | only in strfile
strfile.c revision 1.39
      1  1.39       nia /*	$NetBSD: strfile.c,v 1.39 2020/04/29 20:45:05 nia Exp $	*/
      2   1.3       cgd 
      3   1.1       cgd /*-
      4   1.3       cgd  * Copyright (c) 1989, 1993
      5   1.3       cgd  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * This code is derived from software contributed to Berkeley by
      8   1.1       cgd  * Ken Arnold.
      9   1.1       cgd  *
     10   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11   1.1       cgd  * modification, are permitted provided that the following conditions
     12   1.1       cgd  * are met:
     13   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18  1.22       agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.1       cgd  *    may be used to endorse or promote products derived from this software
     20   1.1       cgd  *    without specific prior written permission.
     21   1.1       cgd  *
     22   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1       cgd  * SUCH DAMAGE.
     33   1.1       cgd  */
     34   1.1       cgd 
     35  1.36       uwe #if HAVE_NBTOOL_CONFIG_H
     36  1.36       uwe #include "nbtool_config.h"
     37  1.36       uwe #endif
     38  1.36       uwe 
     39  1.16    simonb #ifdef __NetBSD__
     40   1.7     lukem #include <sys/cdefs.h>
     41   1.1       cgd #ifndef lint
     42  1.26     lukem __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
     43  1.26     lukem  The Regents of the University of California.  All rights reserved.");
     44   1.1       cgd #endif /* not lint */
     45   1.1       cgd 
     46   1.1       cgd #ifndef lint
     47   1.3       cgd #if 0
     48   1.3       cgd static char sccsid[] = "@(#)strfile.c	8.1 (Berkeley) 5/31/93";
     49   1.3       cgd #else
     50  1.39       nia __RCSID("$NetBSD: strfile.c,v 1.39 2020/04/29 20:45:05 nia Exp $");
     51   1.3       cgd #endif
     52   1.1       cgd #endif /* not lint */
     53  1.16    simonb #endif /* __NetBSD__ */
     54   1.1       cgd 
     55  1.30  christos #include <sys/types.h>
     56  1.30  christos #include <sys/param.h>
     57  1.30  christos #include <ctype.h>
     58  1.30  christos #include <stdio.h>
     59  1.30  christos #include <stdlib.h>
     60  1.30  christos #include <string.h>
     61  1.30  christos #include <time.h>
     62  1.30  christos #include <unistd.h>
     63  1.30  christos #include <inttypes.h>
     64  1.30  christos #include <err.h>
     65  1.30  christos 
     66  1.30  christos #include "strfile.h"
     67  1.30  christos 
     68  1.30  christos #ifndef MAXPATHLEN
     69  1.30  christos #define	MAXPATHLEN	1024
     70  1.30  christos #endif	/* MAXPATHLEN */
     71   1.1       cgd 
     72   1.1       cgd /*
     73  1.21       wiz  *	This program takes a file composed of strings separated by
     74   1.1       cgd  * lines starting with two consecutive delimiting character (default
     75   1.1       cgd  * character is '%') and creates another file which consists of a table
     76   1.1       cgd  * describing the file (structure from "strfile.h"), a table of seek
     77   1.1       cgd  * pointers to the start of the strings, and the strings, each terminated
     78   1.1       cgd  * by a null byte.  Usage:
     79   1.1       cgd  *
     80   1.1       cgd  *	% strfile [-iorsx] [ -cC ] sourcefile [ datafile ]
     81   1.1       cgd  *
     82   1.1       cgd  *	c - Change delimiting character from '%' to 'C'
     83   1.1       cgd  *	s - Silent.  Give no summary of data processed at the end of
     84   1.1       cgd  *	    the run.
     85   1.1       cgd  *	o - order the strings in alphabetic order
     86   1.1       cgd  *	i - if ordering, ignore case
     87   1.1       cgd  *	r - randomize the order of the strings
     88   1.1       cgd  *	x - set rotated bit
     89   1.1       cgd  *
     90   1.1       cgd  *		Ken Arnold	Sept. 7, 1978 --
     91   1.1       cgd  *
     92   1.1       cgd  *	Added ordering options.
     93   1.1       cgd  */
     94   1.1       cgd 
     95   1.1       cgd # define	STORING_PTRS	(Oflag || Rflag)
     96   1.1       cgd # define	CHUNKSIZE	512
     97   1.1       cgd 
     98  1.15       jsm # define	ALLOC(ptr,sz)	do { \
     99   1.1       cgd 			if (ptr == NULL) \
    100  1.19       jsm 				ptr = malloc(CHUNKSIZE * sizeof *ptr); \
    101   1.1       cgd 			else if (((sz) + 1) % CHUNKSIZE == 0) \
    102  1.19       jsm 				ptr = realloc(ptr, ((sz) + CHUNKSIZE) * sizeof *ptr); \
    103   1.7     lukem 			if (ptr == NULL) \
    104  1.30  christos 				err(1, "out of space"); \
    105  1.15       jsm 		} while (0)
    106   1.1       cgd 
    107   1.1       cgd typedef struct {
    108   1.1       cgd 	char	first;
    109   1.1       cgd 	off_t	pos;
    110   1.1       cgd } STR;
    111   1.1       cgd 
    112  1.29  dholland static char *Infile = NULL;		/* input file name */
    113  1.29  dholland static char Outfile[MAXPATHLEN] = "";	/* output file name */
    114  1.29  dholland static char Delimch = '%';		/* delimiting character */
    115   1.1       cgd 
    116  1.37       uwe static int Sflag	= 0;		/* silent run flag */
    117  1.37       uwe static int Oflag	= 0;		/* ordering flag */
    118  1.37       uwe static int Iflag	= 0;		/* ignore case flag */
    119  1.37       uwe static int Rflag	= 0;		/* randomize order flag */
    120  1.37       uwe static int Xflag	= 0;		/* set rotated bit */
    121  1.29  dholland static long Num_pts	= 0;		/* number of pointers/strings */
    122   1.1       cgd 
    123  1.29  dholland static off_t *Seekpts;
    124   1.1       cgd 
    125  1.29  dholland static FILE *Sort_1, *Sort_2;		/* pointers for sorting */
    126   1.1       cgd 
    127  1.29  dholland static STRFILE Tbl;			/* statistics table */
    128   1.1       cgd 
    129  1.29  dholland static STR *Firstch;			/* first chars of each string */
    130   1.1       cgd 
    131  1.16    simonb 
    132  1.32  dholland static uint32_t h2nl(uint32_t h);
    133  1.32  dholland static void getargs(int argc, char **argv);
    134  1.38       uwe static void usage(void) __dead;
    135  1.32  dholland static void add_offset(FILE *fp, off_t off);
    136  1.32  dholland static void do_order(void);
    137  1.32  dholland static int cmp_str(const void *vp1, const void *vp2);
    138  1.32  dholland static void randomize(void);
    139  1.32  dholland static void fwrite_be_offt(off_t off, FILE *f);
    140  1.25    dogcow 
    141   1.1       cgd 
    142   1.1       cgd /*
    143   1.1       cgd  * main:
    144   1.1       cgd  *	Drive the sucker.  There are two main modes -- either we store
    145   1.1       cgd  *	the seek pointers, if the table is to be sorted or randomized,
    146   1.1       cgd  *	or we write the pointer directly to the file, if we are to stay
    147   1.1       cgd  *	in file order.  If the former, we allocate and re-allocate in
    148   1.1       cgd  *	CHUNKSIZE blocks; if the latter, we just write each pointer,
    149   1.1       cgd  *	and then seek back to the beginning to write in the table.
    150   1.1       cgd  */
    151   1.7     lukem int
    152  1.27       apb main(int ac, char **av)
    153   1.1       cgd {
    154   1.7     lukem 	char		*sp, dc;
    155   1.7     lukem 	FILE		*inf, *outf;
    156  1.35  dholland 	off_t		last_off, length, pos;
    157  1.35  dholland 	int		first;
    158   1.7     lukem 	char		*nsp;
    159   1.7     lukem 	STR		*fp;
    160   1.7     lukem 	static char	string[257];
    161  1.35  dholland 	long		i;
    162   1.1       cgd 
    163  1.16    simonb 	/* sanity test */
    164  1.27       apb 	if (sizeof(uint32_t) != 4)
    165  1.30  christos 		errx(1, "sizeof(uint32_t) != 4");
    166  1.16    simonb 
    167   1.1       cgd 	getargs(ac, av);		/* evalute arguments */
    168   1.1       cgd 	dc = Delimch;
    169   1.7     lukem 	if ((inf = fopen(Infile, "r")) == NULL)
    170  1.30  christos 		err(1, "open `%s'", Infile);
    171   1.1       cgd 
    172   1.7     lukem 	if ((outf = fopen(Outfile, "w")) == NULL)
    173  1.30  christos 		err(1, "open `%s'", Outfile);
    174   1.1       cgd 	if (!STORING_PTRS)
    175  1.11       jsm 		(void) fseek(outf, sizeof Tbl, SEEK_SET);
    176   1.1       cgd 
    177   1.1       cgd 	/*
    178   1.1       cgd 	 * Write the strings onto the file
    179   1.1       cgd 	 */
    180   1.1       cgd 
    181   1.1       cgd 	Tbl.str_longlen = 0;
    182  1.16    simonb 	Tbl.str_shortlen = (unsigned int) 0x7fffffff;
    183   1.1       cgd 	Tbl.str_delim = dc;
    184   1.1       cgd 	Tbl.str_version = VERSION;
    185   1.1       cgd 	first = Oflag;
    186   1.1       cgd 	add_offset(outf, ftell(inf));
    187   1.1       cgd 	last_off = 0;
    188   1.1       cgd 	do {
    189   1.1       cgd 		sp = fgets(string, 256, inf);
    190   1.7     lukem 		if (sp == NULL || (sp[0] == dc && sp[1] == '\n')) {
    191   1.1       cgd 			pos = ftell(inf);
    192   1.1       cgd 			length = pos - last_off - (sp ? strlen(sp) : 0);
    193   1.1       cgd 			last_off = pos;
    194   1.1       cgd 			if (!length)
    195   1.1       cgd 				continue;
    196   1.1       cgd 			add_offset(outf, pos);
    197  1.14       jsm 			if ((off_t)Tbl.str_longlen < length)
    198   1.1       cgd 				Tbl.str_longlen = length;
    199  1.14       jsm 			if ((off_t)Tbl.str_shortlen > length)
    200   1.1       cgd 				Tbl.str_shortlen = length;
    201   1.1       cgd 			first = Oflag;
    202   1.1       cgd 		}
    203   1.1       cgd 		else if (first) {
    204  1.23    rillig 			for (nsp = sp; !isalnum((unsigned char)*nsp); nsp++)
    205   1.1       cgd 				continue;
    206   1.1       cgd 			ALLOC(Firstch, Num_pts);
    207   1.1       cgd 			fp = &Firstch[Num_pts - 1];
    208  1.23    rillig 			if (Iflag && isupper((unsigned char)*nsp))
    209  1.23    rillig 				fp->first = tolower((unsigned char)*nsp);
    210   1.1       cgd 			else
    211   1.1       cgd 				fp->first = *nsp;
    212   1.1       cgd 			fp->pos = Seekpts[Num_pts - 1];
    213  1.37       uwe 			first = 0;
    214   1.1       cgd 		}
    215   1.1       cgd 	} while (sp != NULL);
    216   1.1       cgd 
    217   1.1       cgd 	/*
    218   1.1       cgd 	 * write the tables in
    219   1.1       cgd 	 */
    220   1.1       cgd 
    221   1.1       cgd 	(void) fclose(inf);
    222   1.1       cgd 
    223   1.1       cgd 	if (Oflag)
    224   1.1       cgd 		do_order();
    225   1.1       cgd 	else if (Rflag)
    226   1.1       cgd 		randomize();
    227   1.1       cgd 
    228   1.1       cgd 	if (Xflag)
    229   1.1       cgd 		Tbl.str_flags |= STR_ROTATED;
    230   1.1       cgd 
    231   1.1       cgd 	if (!Sflag) {
    232   1.1       cgd 		printf("\"%s\" created\n", Outfile);
    233   1.1       cgd 		if (Num_pts == 2)
    234   1.1       cgd 			puts("There was 1 string");
    235   1.1       cgd 		else
    236   1.7     lukem 			printf("There were %d strings\n", (int)(Num_pts - 1));
    237  1.19       jsm 		printf("Longest string: %lu byte%s\n", (unsigned long)Tbl.str_longlen,
    238   1.1       cgd 		       Tbl.str_longlen == 1 ? "" : "s");
    239  1.19       jsm 		printf("Shortest string: %lu byte%s\n", (unsigned long)Tbl.str_shortlen,
    240   1.1       cgd 		       Tbl.str_shortlen == 1 ? "" : "s");
    241   1.1       cgd 	}
    242   1.1       cgd 
    243  1.11       jsm 	(void) fseek(outf, (off_t) 0, SEEK_SET);
    244  1.20    simonb 	Tbl.str_version = h2nl(Tbl.str_version);
    245  1.20    simonb 	Tbl.str_numstr = h2nl(Num_pts - 1);
    246  1.20    simonb 	Tbl.str_longlen = h2nl(Tbl.str_longlen);
    247  1.20    simonb 	Tbl.str_shortlen = h2nl(Tbl.str_shortlen);
    248  1.20    simonb 	Tbl.str_flags = h2nl(Tbl.str_flags);
    249   1.1       cgd 	(void) fwrite((char *) &Tbl, sizeof Tbl, 1, outf);
    250   1.1       cgd 	if (STORING_PTRS) {
    251  1.35  dholland 		for (i = 0; i < Num_pts; i++)
    252  1.35  dholland 			fwrite_be_offt(Seekpts[i], outf);
    253   1.1       cgd 	}
    254  1.13       jsm 	fflush(outf);
    255  1.13       jsm 	if (ferror(outf))
    256  1.30  christos 		err(1, "fwrite %s", Outfile);
    257   1.1       cgd 	(void) fclose(outf);
    258   1.1       cgd 	exit(0);
    259   1.1       cgd }
    260   1.1       cgd 
    261   1.1       cgd /*
    262   1.1       cgd  *	This routine evaluates arguments from the command line
    263   1.1       cgd  */
    264  1.27       apb static void
    265  1.27       apb getargs(int argc, char **argv)
    266   1.1       cgd {
    267   1.1       cgd 	int	ch;
    268  1.16    simonb 	extern	int optind;
    269  1.16    simonb 	extern	char *optarg;
    270  1.39       nia 	size_t	len;
    271   1.1       cgd 
    272   1.6     lukem 	while ((ch = getopt(argc, argv, "c:iorsx")) != -1)
    273   1.1       cgd 		switch(ch) {
    274   1.1       cgd 		case 'c':			/* new delimiting char */
    275   1.1       cgd 			Delimch = *optarg;
    276   1.1       cgd 			if (!isascii(Delimch)) {
    277   1.1       cgd 				printf("bad delimiting character: '\\%o\n'",
    278   1.1       cgd 				       Delimch);
    279   1.1       cgd 			}
    280   1.1       cgd 			break;
    281   1.1       cgd 		case 'i':			/* ignore case in ordering */
    282   1.1       cgd 			Iflag++;
    283   1.1       cgd 			break;
    284   1.1       cgd 		case 'o':			/* order strings */
    285   1.1       cgd 			Oflag++;
    286   1.1       cgd 			break;
    287   1.1       cgd 		case 'r':			/* randomize pointers */
    288   1.1       cgd 			Rflag++;
    289   1.1       cgd 			break;
    290   1.1       cgd 		case 's':			/* silent */
    291   1.1       cgd 			Sflag++;
    292   1.1       cgd 			break;
    293   1.1       cgd 		case 'x':			/* set the rotated bit */
    294   1.1       cgd 			Xflag++;
    295   1.1       cgd 			break;
    296   1.1       cgd 		case '?':
    297   1.1       cgd 		default:
    298   1.1       cgd 			usage();
    299   1.1       cgd 		}
    300   1.1       cgd 	argv += optind;
    301   1.1       cgd 
    302   1.1       cgd 	if (*argv) {
    303   1.1       cgd 		Infile = *argv;
    304  1.39       nia 		if (*++argv) {
    305  1.39       nia 			len = strlen(*argv);
    306  1.39       nia 			if (len >= sizeof(Outfile)) {
    307  1.39       nia 				puts("Bad output filename");
    308  1.39       nia 				usage();
    309  1.39       nia 			}
    310  1.39       nia 			(void) memcpy(Outfile, *argv, len + 1);
    311  1.39       nia 		}
    312   1.1       cgd 	}
    313   1.1       cgd 	if (!Infile) {
    314   1.1       cgd 		puts("No input file name");
    315   1.1       cgd 		usage();
    316   1.1       cgd 	}
    317   1.1       cgd 	if (*Outfile == '\0') {
    318  1.39       nia 		len = strlen(Infile) + sizeof(".dat");
    319  1.39       nia 		if (len > sizeof(Outfile)) {
    320  1.39       nia 			puts("Bad input filename");
    321  1.39       nia 			usage();
    322  1.39       nia 		}
    323   1.1       cgd 		(void) strcpy(Outfile, Infile);
    324   1.1       cgd 		(void) strcat(Outfile, ".dat");
    325   1.1       cgd 	}
    326   1.1       cgd }
    327   1.1       cgd 
    328  1.27       apb static void
    329  1.27       apb usage(void)
    330   1.1       cgd {
    331   1.1       cgd 	(void) fprintf(stderr,
    332  1.30  christos 	    "Usage: %s [-iorsx] [-c char] sourcefile [datafile]\n",
    333  1.30  christos 	    getprogname());
    334  1.16    simonb 	exit(1);
    335  1.16    simonb }
    336  1.16    simonb 
    337   1.1       cgd /*
    338   1.1       cgd  * add_offset:
    339   1.1       cgd  *	Add an offset to the list, or write it out, as appropriate.
    340   1.1       cgd  */
    341  1.27       apb static void
    342  1.27       apb add_offset(FILE *fp, off_t off)
    343   1.1       cgd {
    344   1.1       cgd 
    345   1.1       cgd 	if (!STORING_PTRS) {
    346  1.16    simonb 		fwrite_be_offt(off, fp);
    347   1.1       cgd 	} else {
    348   1.1       cgd 		ALLOC(Seekpts, Num_pts + 1);
    349   1.1       cgd 		Seekpts[Num_pts] = off;
    350   1.1       cgd 	}
    351   1.1       cgd 	Num_pts++;
    352   1.1       cgd }
    353   1.1       cgd 
    354   1.1       cgd /*
    355   1.1       cgd  * do_order:
    356   1.1       cgd  *	Order the strings alphabetically (possibly ignoring case).
    357   1.1       cgd  */
    358  1.27       apb static void
    359  1.27       apb do_order(void)
    360   1.1       cgd {
    361   1.7     lukem 	int	i;
    362   1.7     lukem 	off_t	*lp;
    363   1.7     lukem 	STR	*fp;
    364   1.1       cgd 
    365   1.1       cgd 	Sort_1 = fopen(Infile, "r");
    366   1.1       cgd 	Sort_2 = fopen(Infile, "r");
    367   1.1       cgd 	qsort((char *) Firstch, (int) Tbl.str_numstr, sizeof *Firstch, cmp_str);
    368   1.1       cgd 	i = Tbl.str_numstr;
    369   1.1       cgd 	lp = Seekpts;
    370   1.1       cgd 	fp = Firstch;
    371   1.1       cgd 	while (i--)
    372   1.1       cgd 		*lp++ = fp++->pos;
    373   1.1       cgd 	(void) fclose(Sort_1);
    374   1.1       cgd 	(void) fclose(Sort_2);
    375   1.1       cgd 	Tbl.str_flags |= STR_ORDERED;
    376   1.1       cgd }
    377   1.1       cgd 
    378  1.27       apb static int
    379  1.27       apb cmp_str(const void *vp1, const void *vp2)
    380   1.1       cgd {
    381  1.10       jsm 	const STR	*p1, *p2;
    382   1.7     lukem 	int	c1, c2;
    383   1.7     lukem 	int	n1, n2;
    384   1.7     lukem 
    385  1.10       jsm 	p1 = (const STR *)vp1;
    386  1.10       jsm 	p2 = (const STR *)vp2;
    387   1.1       cgd 
    388   1.1       cgd # define	SET_N(nf,ch)	(nf = (ch == '\n'))
    389   1.1       cgd # define	IS_END(ch,nf)	(ch == Delimch && nf)
    390   1.1       cgd 
    391   1.1       cgd 	c1 = p1->first;
    392   1.1       cgd 	c2 = p2->first;
    393   1.1       cgd 	if (c1 != c2)
    394   1.1       cgd 		return c1 - c2;
    395   1.1       cgd 
    396  1.11       jsm 	(void) fseek(Sort_1, p1->pos, SEEK_SET);
    397  1.11       jsm 	(void) fseek(Sort_2, p2->pos, SEEK_SET);
    398   1.1       cgd 
    399  1.37       uwe 	n1 = 0;
    400  1.37       uwe 	n2 = 0;
    401   1.1       cgd 	while (!isalnum(c1 = getc(Sort_1)) && c1 != '\0')
    402   1.1       cgd 		SET_N(n1, c1);
    403   1.1       cgd 	while (!isalnum(c2 = getc(Sort_2)) && c2 != '\0')
    404   1.1       cgd 		SET_N(n2, c2);
    405   1.1       cgd 
    406   1.1       cgd 	while (!IS_END(c1, n1) && !IS_END(c2, n2)) {
    407   1.1       cgd 		if (Iflag) {
    408   1.1       cgd 			if (isupper(c1))
    409   1.1       cgd 				c1 = tolower(c1);
    410   1.1       cgd 			if (isupper(c2))
    411   1.1       cgd 				c2 = tolower(c2);
    412   1.1       cgd 		}
    413   1.1       cgd 		if (c1 != c2)
    414   1.1       cgd 			return c1 - c2;
    415   1.1       cgd 		SET_N(n1, c1);
    416   1.1       cgd 		SET_N(n2, c2);
    417   1.1       cgd 		c1 = getc(Sort_1);
    418   1.1       cgd 		c2 = getc(Sort_2);
    419   1.1       cgd 	}
    420   1.1       cgd 	if (IS_END(c1, n1))
    421   1.1       cgd 		c1 = 0;
    422   1.1       cgd 	if (IS_END(c2, n2))
    423   1.1       cgd 		c2 = 0;
    424   1.1       cgd 	return c1 - c2;
    425   1.1       cgd }
    426   1.1       cgd 
    427   1.1       cgd /*
    428   1.1       cgd  * randomize:
    429   1.1       cgd  *	Randomize the order of the string table.  We must be careful
    430   1.1       cgd  *	not to randomize across delimiter boundaries.  All
    431   1.1       cgd  *	randomization is done within each block.
    432   1.1       cgd  */
    433  1.27       apb static void
    434  1.27       apb randomize(void)
    435   1.1       cgd {
    436   1.7     lukem 	int	cnt, i;
    437   1.7     lukem 	off_t	tmp;
    438   1.7     lukem 	off_t	*sp;
    439   1.1       cgd 
    440  1.34    plunky 	srandom((int)(time(NULL) + getpid()));
    441   1.1       cgd 
    442   1.1       cgd 	Tbl.str_flags |= STR_RANDOM;
    443   1.1       cgd 	cnt = Tbl.str_numstr;
    444   1.1       cgd 
    445   1.1       cgd 	/*
    446   1.1       cgd 	 * move things around randomly
    447   1.1       cgd 	 */
    448   1.1       cgd 
    449   1.1       cgd 	for (sp = Seekpts; cnt > 0; cnt--, sp++) {
    450   1.1       cgd 		i = random() % cnt;
    451   1.1       cgd 		tmp = sp[0];
    452   1.1       cgd 		sp[0] = sp[i];
    453   1.1       cgd 		sp[i] = tmp;
    454   1.1       cgd 	}
    455  1.16    simonb }
    456  1.16    simonb 
    457  1.16    simonb /*
    458  1.16    simonb  * fwrite_be_offt:
    459  1.16    simonb  *	Write out the off paramater as a 64 bit big endian number
    460  1.16    simonb  */
    461  1.16    simonb 
    462  1.27       apb static void
    463  1.27       apb fwrite_be_offt(off_t off, FILE *f)
    464  1.16    simonb {
    465  1.16    simonb 	int		i;
    466  1.16    simonb 	unsigned char	c[8];
    467  1.16    simonb 
    468  1.16    simonb 	for (i = 7; i >= 0; i--) {
    469  1.16    simonb 		c[i] = off & 0xff;
    470  1.16    simonb 		off >>= 8;
    471  1.16    simonb 	}
    472  1.16    simonb 	fwrite(c, sizeof(c), 1, f);
    473   1.1       cgd }
    474  1.32  dholland 
    475  1.32  dholland static uint32_t
    476  1.32  dholland h2nl(uint32_t h)
    477  1.32  dholland {
    478  1.32  dholland         unsigned char c[4];
    479  1.32  dholland         uint32_t rv;
    480  1.32  dholland 
    481  1.32  dholland         c[0] = (h >> 24) & 0xff;
    482  1.32  dholland         c[1] = (h >> 16) & 0xff;
    483  1.32  dholland         c[2] = (h >>  8) & 0xff;
    484  1.32  dholland         c[3] = (h >>  0) & 0xff;
    485  1.32  dholland         memcpy(&rv, c, sizeof rv);
    486  1.32  dholland 
    487  1.32  dholland         return (rv);
    488  1.32  dholland }
    489