1994-09-04 04:03:31 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1989, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Ken Arnold.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2010-02-15 15:10:21 +00:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1994-09-04 04:03:31 +00:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2003-05-05 09:52:25 +00:00
|
|
|
#if 0
|
1994-09-04 04:03:31 +00:00
|
|
|
#ifndef lint
|
1996-05-27 22:43:43 +00:00
|
|
|
static const char copyright[] =
|
1994-09-04 04:03:31 +00:00
|
|
|
"@(#) Copyright (c) 1989, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1996-05-27 22:43:43 +00:00
|
|
|
static const char sccsid[] = "@(#)strfile.c 8.1 (Berkeley) 5/31/93";
|
1994-09-04 04:03:31 +00:00
|
|
|
#endif /* not lint */
|
2003-05-05 09:52:25 +00:00
|
|
|
#endif
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-09-04 04:03:31 +00:00
|
|
|
|
2010-02-15 15:10:21 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/endian.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <locale.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "strfile.h"
|
1994-09-04 04:03:31 +00:00
|
|
|
|
|
|
|
/*
|
2001-02-06 10:39:38 +00:00
|
|
|
* This program takes a file composed of strings separated by
|
1994-09-04 04:03:31 +00:00
|
|
|
* lines starting with two consecutive delimiting character (default
|
|
|
|
* character is '%') and creates another file which consists of a table
|
|
|
|
* describing the file (structure from "strfile.h"), a table of seek
|
|
|
|
* pointers to the start of the strings, and the strings, each terminated
|
|
|
|
* by a null byte. Usage:
|
|
|
|
*
|
|
|
|
* % strfile [-iorsx] [ -cC ] sourcefile [ datafile ]
|
|
|
|
*
|
1999-10-02 12:33:37 +00:00
|
|
|
* C - Allow comments marked by a double delimiter at line's beginning
|
1994-09-04 04:03:31 +00:00
|
|
|
* c - Change delimiting character from '%' to 'C'
|
|
|
|
* s - Silent. Give no summary of data processed at the end of
|
|
|
|
* the run.
|
|
|
|
* o - order the strings in alphabetic order
|
1995-05-30 03:37:36 +00:00
|
|
|
* i - if ordering, ignore case
|
1994-09-04 04:03:31 +00:00
|
|
|
* r - randomize the order of the strings
|
|
|
|
* x - set rotated bit
|
|
|
|
*
|
|
|
|
* Ken Arnold Sept. 7, 1978 --
|
|
|
|
*
|
|
|
|
* Added ordering options.
|
|
|
|
*/
|
|
|
|
|
2010-02-15 15:10:21 +00:00
|
|
|
#define STORING_PTRS (Oflag || Rflag)
|
|
|
|
#define CHUNKSIZE 512
|
1994-09-04 04:03:31 +00:00
|
|
|
|
2010-02-15 15:10:21 +00:00
|
|
|
#define ALLOC(ptr, sz) do { \
|
1994-09-04 04:03:31 +00:00
|
|
|
if (ptr == NULL) \
|
2010-02-15 15:10:21 +00:00
|
|
|
ptr = malloc(CHUNKSIZE * sizeof(*ptr)); \
|
1994-09-04 04:03:31 +00:00
|
|
|
else if (((sz) + 1) % CHUNKSIZE == 0) \
|
2010-02-15 15:10:21 +00:00
|
|
|
ptr = realloc(ptr, ((sz) + CHUNKSIZE) * sizeof(*ptr)); \
|
1994-09-04 04:03:31 +00:00
|
|
|
if (ptr == NULL) { \
|
|
|
|
fprintf(stderr, "out of space\n"); \
|
|
|
|
exit(1); \
|
|
|
|
} \
|
2010-02-15 15:10:21 +00:00
|
|
|
} while (0)
|
1994-09-04 04:03:31 +00:00
|
|
|
|
|
|
|
typedef struct {
|
2003-12-07 17:34:52 +00:00
|
|
|
int first;
|
2005-02-17 18:06:37 +00:00
|
|
|
off_t pos;
|
1994-09-04 04:03:31 +00:00
|
|
|
} STR;
|
|
|
|
|
2003-12-07 17:34:52 +00:00
|
|
|
static char *Infile = NULL, /* input file name */
|
|
|
|
Outfile[MAXPATHLEN] = "", /* output file name */
|
|
|
|
Delimch = '%'; /* delimiting character */
|
1994-09-04 04:03:31 +00:00
|
|
|
|
2010-02-15 15:10:21 +00:00
|
|
|
static int Cflag = false; /* embedded comments */
|
|
|
|
static int Sflag = false; /* silent run flag */
|
|
|
|
static int Oflag = false; /* ordering flag */
|
|
|
|
static int Iflag = false; /* ignore case flag */
|
|
|
|
static int Rflag = false; /* randomize order flag */
|
|
|
|
static int Xflag = false; /* set rotated bit */
|
2005-02-17 18:06:37 +00:00
|
|
|
static uint32_t Num_pts = 0; /* number of pointers/strings */
|
1994-09-04 04:03:31 +00:00
|
|
|
|
2005-02-17 18:06:37 +00:00
|
|
|
static off_t *Seekpts;
|
1994-09-04 04:03:31 +00:00
|
|
|
|
2003-12-07 17:34:52 +00:00
|
|
|
static FILE *Sort_1, *Sort_2; /* pointers for sorting */
|
1994-09-04 04:03:31 +00:00
|
|
|
|
2003-12-07 17:34:52 +00:00
|
|
|
static STRFILE Tbl; /* statistics table */
|
1994-09-04 04:03:31 +00:00
|
|
|
|
2003-12-07 17:34:52 +00:00
|
|
|
static STR *Firstch; /* first chars of each string */
|
1994-09-04 04:03:31 +00:00
|
|
|
|
2010-02-15 15:10:21 +00:00
|
|
|
static void add_offset(FILE *, off_t);
|
|
|
|
static int cmp_str(const void *, const void *);
|
|
|
|
static int stable_collate_range_cmp(int, int);
|
|
|
|
static void do_order(void);
|
|
|
|
static void getargs(int, char **);
|
|
|
|
static void randomize(void);
|
|
|
|
static void usage(void);
|
1994-09-04 04:03:31 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* main:
|
|
|
|
* Drive the sucker. There are two main modes -- either we store
|
|
|
|
* the seek pointers, if the table is to be sorted or randomized,
|
|
|
|
* or we write the pointer directly to the file, if we are to stay
|
|
|
|
* in file order. If the former, we allocate and re-allocate in
|
|
|
|
* CHUNKSIZE blocks; if the latter, we just write each pointer,
|
|
|
|
* and then seek back to the beginning to write in the table.
|
|
|
|
*/
|
2009-12-29 08:42:58 +00:00
|
|
|
int
|
|
|
|
main(int ac, char *av[])
|
1994-09-04 04:03:31 +00:00
|
|
|
{
|
2010-02-15 15:10:21 +00:00
|
|
|
char *sp, *nsp, dc;
|
|
|
|
FILE *inf, *outf;
|
|
|
|
off_t last_off, pos, *p;
|
|
|
|
size_t length;
|
|
|
|
int first;
|
|
|
|
uint32_t cnt;
|
|
|
|
STR *fp;
|
|
|
|
static char string[257];
|
|
|
|
|
|
|
|
setlocale(LC_ALL, "");
|
1996-05-27 22:43:43 +00:00
|
|
|
|
1994-09-04 04:03:31 +00:00
|
|
|
getargs(ac, av); /* evalute arguments */
|
|
|
|
dc = Delimch;
|
|
|
|
if ((inf = fopen(Infile, "r")) == NULL) {
|
|
|
|
perror(Infile);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((outf = fopen(Outfile, "w")) == NULL) {
|
|
|
|
perror(Outfile);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (!STORING_PTRS)
|
2010-02-15 15:10:21 +00:00
|
|
|
fseek(outf, (long)sizeof(Tbl), SEEK_SET);
|
1994-09-04 04:03:31 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Write the strings onto the file
|
|
|
|
*/
|
|
|
|
|
|
|
|
Tbl.str_longlen = 0;
|
2005-02-17 18:06:37 +00:00
|
|
|
Tbl.str_shortlen = 0xffffffff;
|
1994-09-04 04:03:31 +00:00
|
|
|
Tbl.str_delim = dc;
|
|
|
|
Tbl.str_version = VERSION;
|
|
|
|
first = Oflag;
|
2005-02-17 18:06:37 +00:00
|
|
|
add_offset(outf, ftello(inf));
|
1994-09-04 04:03:31 +00:00
|
|
|
last_off = 0;
|
|
|
|
do {
|
|
|
|
sp = fgets(string, 256, inf);
|
1996-05-27 22:43:43 +00:00
|
|
|
if (sp == NULL || (sp[0] == dc && sp[1] == '\n')) {
|
2005-02-17 18:06:37 +00:00
|
|
|
pos = ftello(inf);
|
|
|
|
length = (size_t)(pos - last_off) -
|
|
|
|
(sp != NULL ? strlen(sp) : 0);
|
1994-09-04 04:03:31 +00:00
|
|
|
last_off = pos;
|
2005-02-17 18:06:37 +00:00
|
|
|
if (length == 0)
|
1994-09-04 04:03:31 +00:00
|
|
|
continue;
|
|
|
|
add_offset(outf, pos);
|
2005-02-17 18:06:37 +00:00
|
|
|
if ((size_t)Tbl.str_longlen < length)
|
1994-09-04 04:03:31 +00:00
|
|
|
Tbl.str_longlen = length;
|
2005-02-17 18:06:37 +00:00
|
|
|
if ((size_t)Tbl.str_shortlen > length)
|
1994-09-04 04:03:31 +00:00
|
|
|
Tbl.str_shortlen = length;
|
|
|
|
first = Oflag;
|
|
|
|
}
|
|
|
|
else if (first) {
|
1996-05-27 22:43:43 +00:00
|
|
|
for (nsp = sp; !isalnum((unsigned char)*nsp); nsp++)
|
1994-09-04 04:03:31 +00:00
|
|
|
continue;
|
|
|
|
ALLOC(Firstch, Num_pts);
|
|
|
|
fp = &Firstch[Num_pts - 1];
|
1996-05-27 22:43:43 +00:00
|
|
|
if (Iflag && isupper((unsigned char)*nsp))
|
|
|
|
fp->first = tolower((unsigned char)*nsp);
|
1994-09-04 04:03:31 +00:00
|
|
|
else
|
|
|
|
fp->first = *nsp;
|
|
|
|
fp->pos = Seekpts[Num_pts - 1];
|
2010-02-15 15:10:21 +00:00
|
|
|
first = false;
|
1994-09-04 04:03:31 +00:00
|
|
|
}
|
|
|
|
} while (sp != NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* write the tables in
|
|
|
|
*/
|
|
|
|
|
2010-02-15 15:10:21 +00:00
|
|
|
fclose(inf);
|
1998-02-20 00:47:49 +00:00
|
|
|
Tbl.str_numstr = Num_pts - 1;
|
1994-09-04 04:03:31 +00:00
|
|
|
|
1999-10-02 12:33:37 +00:00
|
|
|
if (Cflag)
|
|
|
|
Tbl.str_flags |= STR_COMMENTS;
|
|
|
|
|
1994-09-04 04:03:31 +00:00
|
|
|
if (Oflag)
|
|
|
|
do_order();
|
|
|
|
else if (Rflag)
|
|
|
|
randomize();
|
|
|
|
|
|
|
|
if (Xflag)
|
|
|
|
Tbl.str_flags |= STR_ROTATED;
|
|
|
|
|
|
|
|
if (!Sflag) {
|
|
|
|
printf("\"%s\" created\n", Outfile);
|
|
|
|
if (Num_pts == 2)
|
|
|
|
puts("There was 1 string");
|
|
|
|
else
|
2005-02-17 18:06:37 +00:00
|
|
|
printf("There were %u strings\n", Num_pts - 1);
|
|
|
|
printf("Longest string: %u byte%s\n", Tbl.str_longlen,
|
1994-09-04 04:03:31 +00:00
|
|
|
Tbl.str_longlen == 1 ? "" : "s");
|
2005-02-17 18:06:37 +00:00
|
|
|
printf("Shortest string: %u byte%s\n", Tbl.str_shortlen,
|
1994-09-04 04:03:31 +00:00
|
|
|
Tbl.str_shortlen == 1 ? "" : "s");
|
|
|
|
}
|
|
|
|
|
1996-05-27 22:43:43 +00:00
|
|
|
rewind(outf);
|
2005-02-17 18:06:37 +00:00
|
|
|
Tbl.str_version = htobe32(Tbl.str_version);
|
|
|
|
Tbl.str_numstr = htobe32(Tbl.str_numstr);
|
|
|
|
Tbl.str_longlen = htobe32(Tbl.str_longlen);
|
|
|
|
Tbl.str_shortlen = htobe32(Tbl.str_shortlen);
|
|
|
|
Tbl.str_flags = htobe32(Tbl.str_flags);
|
2010-02-15 15:10:21 +00:00
|
|
|
fwrite((char *)&Tbl, sizeof(Tbl), 1, outf);
|
1994-09-04 04:03:31 +00:00
|
|
|
if (STORING_PTRS) {
|
|
|
|
for (p = Seekpts, cnt = Num_pts; cnt--; ++p)
|
2005-02-17 18:06:37 +00:00
|
|
|
*p = htobe64(*p);
|
2010-02-15 15:10:21 +00:00
|
|
|
fwrite(Seekpts, sizeof(*Seekpts), (size_t)Num_pts, outf);
|
1994-09-04 04:03:31 +00:00
|
|
|
}
|
2010-02-15 15:10:21 +00:00
|
|
|
fclose(outf);
|
1994-09-04 04:03:31 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This routine evaluates arguments from the command line
|
|
|
|
*/
|
2009-12-29 08:42:58 +00:00
|
|
|
void
|
|
|
|
getargs(int argc, char **argv)
|
1994-09-04 04:03:31 +00:00
|
|
|
{
|
2010-02-15 15:10:21 +00:00
|
|
|
int ch;
|
1994-09-04 04:03:31 +00:00
|
|
|
|
2008-02-19 07:09:19 +00:00
|
|
|
while ((ch = getopt(argc, argv, "Cc:iorsx")) != -1)
|
1994-09-04 04:03:31 +00:00
|
|
|
switch(ch) {
|
1999-10-02 12:33:37 +00:00
|
|
|
case 'C': /* embedded comments */
|
|
|
|
Cflag++;
|
|
|
|
break;
|
1994-09-04 04:03:31 +00:00
|
|
|
case 'c': /* new delimiting char */
|
|
|
|
Delimch = *optarg;
|
|
|
|
if (!isascii(Delimch)) {
|
|
|
|
printf("bad delimiting character: '\\%o\n'",
|
1996-05-27 22:43:43 +00:00
|
|
|
(unsigned char)Delimch);
|
1994-09-04 04:03:31 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'i': /* ignore case in ordering */
|
|
|
|
Iflag++;
|
|
|
|
break;
|
|
|
|
case 'o': /* order strings */
|
|
|
|
Oflag++;
|
|
|
|
break;
|
|
|
|
case 'r': /* randomize pointers */
|
|
|
|
Rflag++;
|
|
|
|
break;
|
|
|
|
case 's': /* silent */
|
|
|
|
Sflag++;
|
|
|
|
break;
|
|
|
|
case 'x': /* set the rotated bit */
|
|
|
|
Xflag++;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (*argv) {
|
|
|
|
Infile = *argv;
|
|
|
|
if (*++argv)
|
2010-02-15 15:10:21 +00:00
|
|
|
strcpy(Outfile, *argv);
|
1994-09-04 04:03:31 +00:00
|
|
|
}
|
|
|
|
if (!Infile) {
|
|
|
|
puts("No input file name");
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
if (*Outfile == '\0') {
|
2017-04-04 19:46:23 +00:00
|
|
|
strlcpy(Outfile, Infile, sizeof(Outfile));
|
|
|
|
strlcat(Outfile, ".dat", sizeof(Outfile));
|
1994-09-04 04:03:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-29 08:42:58 +00:00
|
|
|
void
|
|
|
|
usage(void)
|
1994-09-04 04:03:31 +00:00
|
|
|
{
|
2010-02-15 15:10:21 +00:00
|
|
|
fprintf(stderr,
|
2005-02-09 18:22:15 +00:00
|
|
|
"strfile [-Ciorsx] [-c char] source_file [output_file]\n");
|
1994-09-04 04:03:31 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* add_offset:
|
|
|
|
* Add an offset to the list, or write it out, as appropriate.
|
|
|
|
*/
|
2009-12-29 08:42:58 +00:00
|
|
|
void
|
|
|
|
add_offset(FILE *fp, off_t off)
|
1994-09-04 04:03:31 +00:00
|
|
|
{
|
2005-02-17 18:06:37 +00:00
|
|
|
off_t beoff;
|
1994-09-04 04:03:31 +00:00
|
|
|
|
|
|
|
if (!STORING_PTRS) {
|
2005-02-17 18:06:37 +00:00
|
|
|
beoff = htobe64(off);
|
2010-02-15 15:10:21 +00:00
|
|
|
fwrite(&beoff, 1, sizeof(beoff), fp);
|
1994-09-04 04:03:31 +00:00
|
|
|
} else {
|
|
|
|
ALLOC(Seekpts, Num_pts + 1);
|
|
|
|
Seekpts[Num_pts] = off;
|
|
|
|
}
|
|
|
|
Num_pts++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* do_order:
|
|
|
|
* Order the strings alphabetically (possibly ignoring case).
|
|
|
|
*/
|
2009-12-29 08:42:58 +00:00
|
|
|
void
|
|
|
|
do_order(void)
|
1994-09-04 04:03:31 +00:00
|
|
|
{
|
2005-02-17 18:06:37 +00:00
|
|
|
uint32_t i;
|
2010-02-15 15:10:21 +00:00
|
|
|
off_t *lp;
|
|
|
|
STR *fp;
|
1994-09-04 04:03:31 +00:00
|
|
|
|
|
|
|
Sort_1 = fopen(Infile, "r");
|
|
|
|
Sort_2 = fopen(Infile, "r");
|
2010-02-15 15:10:21 +00:00
|
|
|
qsort(Firstch, (size_t)Tbl.str_numstr, sizeof(*Firstch), cmp_str);
|
1994-09-04 04:03:31 +00:00
|
|
|
i = Tbl.str_numstr;
|
|
|
|
lp = Seekpts;
|
|
|
|
fp = Firstch;
|
|
|
|
while (i--)
|
|
|
|
*lp++ = fp++->pos;
|
2010-02-15 15:10:21 +00:00
|
|
|
fclose(Sort_1);
|
|
|
|
fclose(Sort_2);
|
1994-09-04 04:03:31 +00:00
|
|
|
Tbl.str_flags |= STR_ORDERED;
|
|
|
|
}
|
|
|
|
|
2009-12-29 08:42:58 +00:00
|
|
|
static int
|
|
|
|
stable_collate_range_cmp(int c1, int c2)
|
1996-10-31 14:38:09 +00:00
|
|
|
{
|
|
|
|
static char s1[2], s2[2];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
s1[0] = c1;
|
|
|
|
s2[0] = c2;
|
|
|
|
if ((ret = strcoll(s1, s2)) != 0)
|
|
|
|
return (ret);
|
|
|
|
return (c1 - c2);
|
|
|
|
}
|
|
|
|
|
1994-09-04 04:03:31 +00:00
|
|
|
/*
|
|
|
|
* cmp_str:
|
|
|
|
* Compare two strings in the file
|
|
|
|
*/
|
2009-12-29 08:42:58 +00:00
|
|
|
int
|
|
|
|
cmp_str(const void *s1, const void *s2)
|
1994-09-04 04:03:31 +00:00
|
|
|
{
|
2010-02-15 15:10:21 +00:00
|
|
|
const STR *p1, *p2;
|
|
|
|
int c1, c2, n1, n2, r;
|
|
|
|
|
|
|
|
#define SET_N(nf,ch) (nf = (ch == '\n'))
|
|
|
|
#define IS_END(ch,nf) (ch == EOF || (ch == (unsigned char)Delimch && nf))
|
|
|
|
|
|
|
|
p1 = (const STR *)s1;
|
|
|
|
p2 = (const STR *)s2;
|
|
|
|
|
|
|
|
c1 = (unsigned char)p1->first;
|
|
|
|
c2 = (unsigned char)p2->first;
|
2003-08-03 19:44:36 +00:00
|
|
|
if ((r = stable_collate_range_cmp(c1, c2)) != 0)
|
|
|
|
return (r);
|
1994-09-04 04:03:31 +00:00
|
|
|
|
2010-02-15 15:10:21 +00:00
|
|
|
fseeko(Sort_1, p1->pos, SEEK_SET);
|
|
|
|
fseeko(Sort_2, p2->pos, SEEK_SET);
|
1994-09-04 04:03:31 +00:00
|
|
|
|
2010-02-15 15:10:21 +00:00
|
|
|
n1 = false;
|
|
|
|
n2 = false;
|
1996-05-27 22:43:43 +00:00
|
|
|
while (!isalnum(c1 = getc(Sort_1)) && c1 != '\0' && c1 != EOF)
|
1994-09-04 04:03:31 +00:00
|
|
|
SET_N(n1, c1);
|
1996-05-27 22:43:43 +00:00
|
|
|
while (!isalnum(c2 = getc(Sort_2)) && c2 != '\0' && c2 != EOF)
|
1994-09-04 04:03:31 +00:00
|
|
|
SET_N(n2, c2);
|
|
|
|
|
|
|
|
while (!IS_END(c1, n1) && !IS_END(c2, n2)) {
|
|
|
|
if (Iflag) {
|
|
|
|
if (isupper(c1))
|
|
|
|
c1 = tolower(c1);
|
|
|
|
if (isupper(c2))
|
|
|
|
c2 = tolower(c2);
|
|
|
|
}
|
2003-08-03 19:44:36 +00:00
|
|
|
if ((r = stable_collate_range_cmp(c1, c2)) != 0)
|
|
|
|
return (r);
|
1994-09-04 04:03:31 +00:00
|
|
|
SET_N(n1, c1);
|
|
|
|
SET_N(n2, c2);
|
|
|
|
c1 = getc(Sort_1);
|
|
|
|
c2 = getc(Sort_2);
|
|
|
|
}
|
|
|
|
if (IS_END(c1, n1))
|
|
|
|
c1 = 0;
|
|
|
|
if (IS_END(c2, n2))
|
|
|
|
c2 = 0;
|
2010-02-15 15:10:21 +00:00
|
|
|
|
2003-08-03 19:44:36 +00:00
|
|
|
return (stable_collate_range_cmp(c1, c2));
|
1994-09-04 04:03:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* randomize:
|
|
|
|
* Randomize the order of the string table. We must be careful
|
|
|
|
* not to randomize across delimiter boundaries. All
|
|
|
|
* randomization is done within each block.
|
|
|
|
*/
|
2009-12-29 08:42:58 +00:00
|
|
|
void
|
|
|
|
randomize(void)
|
1994-09-04 04:03:31 +00:00
|
|
|
{
|
2005-02-17 18:06:37 +00:00
|
|
|
uint32_t cnt, i;
|
2010-02-15 15:10:21 +00:00
|
|
|
off_t tmp;
|
|
|
|
off_t *sp;
|
1994-09-04 04:03:31 +00:00
|
|
|
|
|
|
|
Tbl.str_flags |= STR_RANDOM;
|
|
|
|
cnt = Tbl.str_numstr;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* move things around randomly
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (sp = Seekpts; cnt > 0; cnt--, sp++) {
|
2008-08-07 20:05:51 +00:00
|
|
|
i = arc4random_uniform(cnt);
|
1994-09-04 04:03:31 +00:00
|
|
|
tmp = sp[0];
|
|
|
|
sp[0] = sp[i];
|
|
|
|
sp[i] = tmp;
|
|
|
|
}
|
|
|
|
}
|