Localize it.

off_t -> long.
Install strfile & unstr
-Wall cleanup
Fix matching bug with offensive database
This commit is contained in:
Andrey A. Chernov 1996-05-27 22:43:43 +00:00
parent d9378aac9a
commit f17947b74a
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=15944
9 changed files with 130 additions and 130 deletions

View File

@ -1,11 +1,5 @@
# @(#)Makefile 8.1 (Berkeley) 5/31/93 # @(#)Makefile 8.1 (Berkeley) 5/31/93
SUBDIR= fortune SUBDIR= fortune strfile datfiles unstr
.if !make(install) && !make(distribute)
SUBDIR+=strfile
.endif
SUBDIR+= datfiles
.include <bsd.subdir.mk> .include <bsd.subdir.mk>

View File

@ -25,8 +25,10 @@ CAESAR=/usr/games/caesar
.if exists(${.CURDIR}/../strfile/obj/strfile) .if exists(${.CURDIR}/../strfile/obj/strfile)
STRFILE=${.CURDIR}/../strfile/obj/strfile STRFILE=${.CURDIR}/../strfile/obj/strfile
.else .elif exists(${.CURDIR}/../strfile/strfile)
STRFILE=${.CURDIR}/../strfile/strfile STRFILE=${.CURDIR}/../strfile/strfile
.else
STRFILE=/usr/games/strfile
.endif .endif
CLEANFILES+=${BLDS} CLEANFILES+=${BLDS}

View File

@ -2,7 +2,7 @@
PROG= fortune PROG= fortune
MAN6= fortune.6 MAN6= fortune.6
CFLAGS+=-I${.CURDIR}/../strfile CFLAGS+=-DDEBUG -Wall -I${.CURDIR}/../strfile
DPADD= ${COMPAT} DPADD= ${COMPAT}
LDADD= -lcompat LDADD= -lcompat

View File

@ -35,13 +35,13 @@
*/ */
#ifndef lint #ifndef lint
static char copyright[] = static const char copyright[] =
"@(#) Copyright (c) 1986, 1993\n\ "@(#) Copyright (c) 1986, 1993\n\
The Regents of the University of California. All rights reserved.\n"; The Regents of the University of California. All rights reserved.\n";
#endif /* not lint */ #endif /* not lint */
#ifndef lint #ifndef lint
static char sccsid[] = "@(#)fortune.c 8.1 (Berkeley) 5/31/93"; static const char sccsid[] = "@(#)fortune.c 8.1 (Berkeley) 5/31/93";
#endif /* not lint */ #endif /* not lint */
# include <sys/param.h> # include <sys/param.h>
@ -55,6 +55,8 @@ static char sccsid[] = "@(#)fortune.c 8.1 (Berkeley) 5/31/93";
# include <ctype.h> # include <ctype.h>
# include <stdlib.h> # include <stdlib.h>
# include <string.h> # include <string.h>
# include <locale.h>
# include <time.h>
# include "strfile.h" # include "strfile.h"
# include "pathnames.h" # include "pathnames.h"
@ -66,11 +68,11 @@ static char sccsid[] = "@(#)fortune.c 8.1 (Berkeley) 5/31/93";
# define CPERS 20 /* # of chars for each sec */ # define CPERS 20 /* # of chars for each sec */
# define SLEN 160 /* # of chars in short fortune */ # define SLEN 160 /* # of chars in short fortune */
# define POS_UNKNOWN ((off_t) -1) /* pos for file unknown */ # define POS_UNKNOWN ((long) -1) /* pos for file unknown */
# define NO_PROB (-1) /* no prob specified for file */ # define NO_PROB (-1) /* no prob specified for file */
# ifdef DEBUG # ifdef DEBUG
# define DPRINTF(l,x) if (Debug >= l) fprintf x; else # define DPRINTF(l,x) { if (Debug >= l) fprintf x; }
# undef NDEBUG # undef NDEBUG
# else # else
# define DPRINTF(l,x) # define DPRINTF(l,x)
@ -80,7 +82,7 @@ static char sccsid[] = "@(#)fortune.c 8.1 (Berkeley) 5/31/93";
typedef struct fd { typedef struct fd {
int percent; int percent;
int fd, datfd; int fd, datfd;
off_t pos; long pos;
FILE *inf; FILE *inf;
char *name; char *name;
char *path; char *path;
@ -112,7 +114,7 @@ char *Fortbuf = NULL; /* fortune buffer for -m */
int Fort_len = 0; int Fort_len = 0;
off_t Seekpts[2]; /* seek pointers to fortunes */ long Seekpts[2]; /* seek pointers to fortunes */
FILEDESC *File_list = NULL, /* Head of file list */ FILEDESC *File_list = NULL, /* Head of file list */
*File_tail = NULL; /* Tail of file list */ *File_tail = NULL; /* Tail of file list */
@ -186,6 +188,8 @@ char *av[];
int fd; int fd;
#endif /* OK_TO_WRITE_DISK */ #endif /* OK_TO_WRITE_DISK */
(void) setlocale(LC_CTYPE, "");
getargs(ac, av); getargs(ac, av);
#ifndef NO_REGEX #ifndef NO_REGEX
@ -194,7 +198,7 @@ char *av[];
#endif #endif
init_prob(); init_prob();
srandom((int)(time((time_t *) NULL) + getpid())); srandom((int)(time((time_t *) NULL) ^ getpid()));
do { do {
get_fort(); get_fort();
} while ((Short_only && fortlen() > SLEN) || } while ((Short_only && fortlen() > SLEN) ||
@ -235,19 +239,23 @@ void
display(fp) display(fp)
FILEDESC *fp; FILEDESC *fp;
{ {
register char *p, ch; register char *p;
register unsigned char ch;
char line[BUFSIZ]; char line[BUFSIZ];
open_fp(fp); open_fp(fp);
(void) fseek(fp->inf, (long)Seekpts[0], 0); (void) fseek(fp->inf, Seekpts[0], 0);
for (Fort_len = 0; fgets(line, sizeof line, fp->inf) != NULL && for (Fort_len = 0; fgets(line, sizeof line, fp->inf) != NULL &&
!STR_ENDSTRING(line, fp->tbl); Fort_len++) { !STR_ENDSTRING(line, fp->tbl); Fort_len++) {
if (fp->tbl.str_flags & STR_ROTATED) if (fp->tbl.str_flags & STR_ROTATED)
for (p = line; ch = *p; ++p) for (p = line; (ch = *p) != '\0'; ++p) {
if (isupper(ch)) if (isascii(ch)) {
*p = 'A' + (ch - 'A' + 13) % 26; if (isupper(ch))
else if (islower(ch)) *p = 'A' + (ch - 'A' + 13) % 26;
*p = 'a' + (ch - 'a' + 13) % 26; else if (islower(ch))
*p = 'a' + (ch - 'a' + 13) % 26;
}
}
fputs(line, stdout); fputs(line, stdout);
} }
(void) fflush(stdout); (void) fflush(stdout);
@ -267,7 +275,7 @@ fortlen()
nchar = (Seekpts[1] - Seekpts[0] <= SLEN); nchar = (Seekpts[1] - Seekpts[0] <= SLEN);
else { else {
open_fp(Fortfile); open_fp(Fortfile);
(void) fseek(Fortfile->inf, (long)Seekpts[0], 0); (void) fseek(Fortfile->inf, Seekpts[0], 0);
nchar = 0; nchar = 0;
while (fgets(line, sizeof line, Fortfile->inf) != NULL && while (fgets(line, sizeof line, Fortfile->inf) != NULL &&
!STR_ENDSTRING(line, Fortfile->tbl)) !STR_ENDSTRING(line, Fortfile->tbl))
@ -399,11 +407,11 @@ register int file_cnt;
&File_list, &File_tail, NULL); &File_list, &File_tail, NULL);
for (i = 0; i < file_cnt; i++) { for (i = 0; i < file_cnt; i++) {
percent = NO_PROB; percent = NO_PROB;
if (!isdigit(files[i][0])) if (!isdigit((unsigned char)files[i][0]))
sp = files[i]; sp = files[i];
else { else {
percent = 0; percent = 0;
for (sp = files[i]; isdigit(*sp); sp++) for (sp = files[i]; isdigit((unsigned char)*sp); sp++)
percent = percent * 10 + *sp - '0'; percent = percent * 10 + *sp - '0';
if (percent > 100) { if (percent > 100) {
fprintf(stderr, "percentages must be <= 100\n"); fprintf(stderr, "percentages must be <= 100\n");
@ -791,12 +799,14 @@ int check_for_offend;
*datp = datfile; *datp = datfile;
else else
free(datfile); free(datfile);
#ifdef OK_TO_WRITE_DISK
if (posp != NULL) { if (posp != NULL) {
#ifdef OK_TO_WRITE_DISK
*posp = copy(file, (unsigned int) (strlen(file) + 4)); /* +4 for ".dat" */ *posp = copy(file, (unsigned int) (strlen(file) + 4)); /* +4 for ".dat" */
(void) strcat(*posp, ".pos"); (void) strcat(*posp, ".pos");
} #else
*posp = NULL;
#endif /* OK_TO_WRITE_DISK */ #endif /* OK_TO_WRITE_DISK */
}
DPRINTF(2, (stderr, "TRUE\n")); DPRINTF(2, (stderr, "TRUE\n"));
return TRUE; return TRUE;
} }
@ -856,7 +866,7 @@ void *ptr;
void void
init_prob() init_prob()
{ {
register FILEDESC *fp, *last; register FILEDESC *fp, *last = NULL;
register int percent, num_noprob, frac; register int percent, num_noprob, frac;
/* /*
@ -955,17 +965,17 @@ get_fort()
if (fp->next != NULL) { if (fp->next != NULL) {
sum_noprobs(fp); sum_noprobs(fp);
choice = random() % Noprob_tbl.str_numstr; choice = random() % Noprob_tbl.str_numstr;
DPRINTF(1, (stderr, "choice = %d (of %d) \n", choice, DPRINTF(1, (stderr, "choice = %d (of %ld) \n", choice,
Noprob_tbl.str_numstr)); Noprob_tbl.str_numstr));
while (choice >= fp->tbl.str_numstr) { while (choice >= fp->tbl.str_numstr) {
choice -= fp->tbl.str_numstr; choice -= fp->tbl.str_numstr;
fp = fp->next; fp = fp->next;
DPRINTF(1, (stderr, DPRINTF(1, (stderr,
" skip \"%s\", %d (choice = %d)\n", " skip \"%s\", %ld (choice = %d)\n",
fp->name, fp->tbl.str_numstr, fp->name, fp->tbl.str_numstr,
choice)); choice));
} }
DPRINTF(1, (stderr, "using \"%s\", %d\n", fp->name, DPRINTF(1, (stderr, "using \"%s\", %ld\n", fp->name,
fp->tbl.str_numstr)); fp->tbl.str_numstr));
} }
get_tbl(fp); get_tbl(fp);
@ -1007,15 +1017,15 @@ FILEDESC *parent;
else { else {
get_tbl(parent); get_tbl(parent);
choice = random() % parent->tbl.str_numstr; choice = random() % parent->tbl.str_numstr;
DPRINTF(1, (stderr, " choice = %d (of %d)\n", DPRINTF(1, (stderr, " choice = %d (of %ld)\n",
choice, parent->tbl.str_numstr)); choice, parent->tbl.str_numstr));
for (fp = parent->child; choice >= fp->tbl.str_numstr; for (fp = parent->child; choice >= fp->tbl.str_numstr;
fp = fp->next) { fp = fp->next) {
choice -= fp->tbl.str_numstr; choice -= fp->tbl.str_numstr;
DPRINTF(1, (stderr, "\tskip %s, %d (choice = %d)\n", DPRINTF(1, (stderr, "\tskip %s, %ld (choice = %d)\n",
fp->name, fp->tbl.str_numstr, choice)); fp->name, fp->tbl.str_numstr, choice));
} }
DPRINTF(1, (stderr, " using %s, %d\n", fp->name, DPRINTF(1, (stderr, " using %s, %ld\n", fp->name,
fp->tbl.str_numstr)); fp->tbl.str_numstr));
return fp; return fp;
} }
@ -1106,7 +1116,7 @@ FILEDESC *fp;
} }
if (++(fp->pos) >= fp->tbl.str_numstr) if (++(fp->pos) >= fp->tbl.str_numstr)
fp->pos -= fp->tbl.str_numstr; fp->pos -= fp->tbl.str_numstr;
DPRINTF(1, (stderr, "pos for %s is %qd\n", fp->name, fp->pos)); DPRINTF(1, (stderr, "pos for %s is %ld\n", fp->name, fp->pos));
} }
/* /*
@ -1159,7 +1169,7 @@ register STRFILE *tp;
{ {
tp->str_numstr = 0; tp->str_numstr = 0;
tp->str_longlen = 0; tp->str_longlen = 0;
tp->str_shortlen = -1; tp->str_shortlen = ~((unsigned long)0);
} }
/* /*
@ -1207,7 +1217,6 @@ int lev;
fprintf(stderr, " %s", STR(list->name)); fprintf(stderr, " %s", STR(list->name));
DPRINTF(1, (stderr, " (%s, %s, %s)\n", STR(list->path), DPRINTF(1, (stderr, " (%s, %s, %s)\n", STR(list->path),
STR(list->datfile), STR(list->posfile))); STR(list->datfile), STR(list->posfile)));
putc('\n', stderr);
if (list->child != NULL) if (list->child != NULL)
print_list(list->child, lev + 1); print_list(list->child, lev + 1);
list = list->next; list = list->next;
@ -1229,7 +1238,7 @@ register char *orig;
cnt = 1; /* allow for '\0' */ cnt = 1; /* allow for '\0' */
for (sp = orig; *sp != '\0'; sp++) for (sp = orig; *sp != '\0'; sp++)
if (isalpha(*sp)) if (isalpha((unsigned char)*sp))
cnt += 4; cnt += 4;
else else
cnt++; cnt++;
@ -1239,16 +1248,16 @@ register char *orig;
} }
for (sp = new; *orig != '\0'; orig++) { for (sp = new; *orig != '\0'; orig++) {
if (islower(*orig)) { if (islower((unsigned char)*orig)) {
*sp++ = '['; *sp++ = '[';
*sp++ = *orig; *sp++ = *orig;
*sp++ = toupper(*orig); *sp++ = toupper((unsigned char)*orig);
*sp++ = ']'; *sp++ = ']';
} }
else if (isupper(*orig)) { else if (isupper((unsigned char)*orig)) {
*sp++ = '['; *sp++ = '[';
*sp++ = *orig; *sp++ = *orig;
*sp++ = tolower(*orig); *sp++ = tolower((unsigned char)*orig);
*sp++ = ']'; *sp++ = ']';
} }
else else
@ -1310,9 +1319,10 @@ void
matches_in_list(list) matches_in_list(list)
FILEDESC *list; FILEDESC *list;
{ {
register char *sp; register char *sp, *p;
register FILEDESC *fp; register FILEDESC *fp;
int in_file; int in_file;
unsigned char ch;
for (fp = list; fp != NULL; fp = fp->next) { for (fp = list; fp != NULL; fp = fp->next) {
if (fp->child != NULL) { if (fp->child != NULL) {
@ -1328,6 +1338,15 @@ FILEDESC *list;
sp += strlen(sp); sp += strlen(sp);
else { else {
*sp = '\0'; *sp = '\0';
if (fp->tbl.str_flags & STR_ROTATED)
for (p = Fortbuf; (ch = *p) != '\0'; ++p) {
if (isascii(ch)) {
if (isupper(ch))
*p = 'A' + (ch - 'A' + 13) % 26;
else if (islower(ch))
*p = 'a' + (ch - 'a' + 13) % 26;
}
}
if (RE_EXEC(Fortbuf)) { if (RE_EXEC(Fortbuf)) {
printf("%c%c", fp->tbl.str_delim, printf("%c%c", fp->tbl.str_delim,
fp->tbl.str_delim); fp->tbl.str_delim);

View File

@ -1,6 +1,9 @@
# @(#)Makefile 8.1 (Berkeley) 5/31/93 # @(#)Makefile 8.1 (Berkeley) 5/31/93
PROG= strfile PROG= strfile
NOMAN= noman MAN8= strfile.8
MLINKS= strfile.8 unstr.8
CFLAGS+= -Wall
.include "${.CURDIR}/../../Makefile.inc"
.include <bsd.prog.mk> .include <bsd.prog.mk>

View File

@ -35,25 +35,25 @@
*/ */
#ifndef lint #ifndef lint
static char copyright[] = static const char copyright[] =
"@(#) Copyright (c) 1989, 1993\n\ "@(#) Copyright (c) 1989, 1993\n\
The Regents of the University of California. All rights reserved.\n"; The Regents of the University of California. All rights reserved.\n";
#endif /* not lint */ #endif /* not lint */
#ifndef lint #ifndef lint
static char sccsid[] = "@(#)strfile.c 8.1 (Berkeley) 5/31/93"; static const char sccsid[] = "@(#)strfile.c 8.1 (Berkeley) 5/31/93";
#endif /* not lint */ #endif /* not lint */
# include <machine/endian.h>
# include <sys/param.h> # include <sys/param.h>
# include <stdio.h> # include <stdio.h>
# include <stdlib.h>
# include <ctype.h> # include <ctype.h>
# include <string.h>
# include <time.h>
# include <locale.h>
# include <unistd.h>
# include "strfile.h" # include "strfile.h"
# ifndef MAXPATHLEN
# define MAXPATHLEN 1024
# endif /* MAXPATHLEN */
/* /*
* This program takes a file composed of strings seperated by * This program takes a file composed of strings seperated by
* lines starting with two consecutive delimiting character (default * lines starting with two consecutive delimiting character (default
@ -83,12 +83,7 @@ static char sccsid[] = "@(#)strfile.c 8.1 (Berkeley) 5/31/93";
# define STORING_PTRS (Oflag || Rflag) # define STORING_PTRS (Oflag || Rflag)
# define CHUNKSIZE 512 # define CHUNKSIZE 512
#ifdef lint # define ALLOC(ptr,sz) { \
# define ALWAYS atoi("1")
#else
# define ALWAYS 1
#endif
# define ALLOC(ptr,sz) if (ALWAYS) { \
if (ptr == NULL) \ if (ptr == NULL) \
ptr = malloc((unsigned int) (CHUNKSIZE * sizeof *ptr)); \ ptr = malloc((unsigned int) (CHUNKSIZE * sizeof *ptr)); \
else if (((sz) + 1) % CHUNKSIZE == 0) \ else if (((sz) + 1) % CHUNKSIZE == 0) \
@ -97,7 +92,7 @@ static char sccsid[] = "@(#)strfile.c 8.1 (Berkeley) 5/31/93";
fprintf(stderr, "out of space\n"); \ fprintf(stderr, "out of space\n"); \
exit(1); \ exit(1); \
} \ } \
} else }
#ifdef NO_VOID #ifdef NO_VOID
# define void char # define void char
@ -105,7 +100,7 @@ static char sccsid[] = "@(#)strfile.c 8.1 (Berkeley) 5/31/93";
typedef struct { typedef struct {
char first; char first;
off_t pos; long pos;
} STR; } STR;
char *Infile = NULL, /* input file name */ char *Infile = NULL, /* input file name */
@ -119,7 +114,7 @@ int Rflag = FALSE; /* randomize order flag */
int Xflag = FALSE; /* set rotated bit */ int Xflag = FALSE; /* set rotated bit */
long Num_pts = 0; /* number of pointers/strings */ long Num_pts = 0; /* number of pointers/strings */
off_t *Seekpts; long *Seekpts;
FILE *Sort_1, *Sort_2; /* pointers for sorting */ FILE *Sort_1, *Sort_2; /* pointers for sorting */
@ -127,9 +122,8 @@ STRFILE Tbl; /* statistics table */
STR *Firstch; /* first chars of each string */ STR *Firstch; /* first chars of each string */
char *fgets(), *strcpy(), *strcat(); void getargs(), add_offset(), do_order(), randomize(), usage();
int cmp_str();
void *malloc(), *realloc();
/* /*
* main: * main:
@ -140,18 +134,20 @@ void *malloc(), *realloc();
* CHUNKSIZE blocks; if the latter, we just write each pointer, * CHUNKSIZE blocks; if the latter, we just write each pointer,
* and then seek back to the beginning to write in the table. * and then seek back to the beginning to write in the table.
*/ */
main(ac, av) void main(ac, av)
int ac; int ac;
char **av; char **av;
{ {
register char *sp, dc; register char *sp, dc;
register FILE *inf, *outf; register FILE *inf, *outf;
register off_t last_off, length, pos, *p; register long last_off, length, pos, *p;
register int first, cnt; register int first, cnt;
register char *nsp; register char *nsp;
register STR *fp; register STR *fp;
static char string[257]; static char string[257];
(void) setlocale(LC_CTYPE, "");
getargs(ac, av); /* evalute arguments */ getargs(ac, av); /* evalute arguments */
dc = Delimch; dc = Delimch;
if ((inf = fopen(Infile, "r")) == NULL) { if ((inf = fopen(Infile, "r")) == NULL) {
@ -164,14 +160,14 @@ char **av;
exit(1); exit(1);
} }
if (!STORING_PTRS) if (!STORING_PTRS)
(void) fseek(outf, sizeof Tbl, 0); (void) fseek(outf, (long) sizeof Tbl, 0);
/* /*
* Write the strings onto the file * Write the strings onto the file
*/ */
Tbl.str_longlen = 0; Tbl.str_longlen = 0;
Tbl.str_shortlen = (unsigned int) 0xffffffff; Tbl.str_shortlen = ~((unsigned long) 0);
Tbl.str_delim = dc; Tbl.str_delim = dc;
Tbl.str_version = VERSION; Tbl.str_version = VERSION;
first = Oflag; first = Oflag;
@ -179,7 +175,7 @@ char **av;
last_off = 0; last_off = 0;
do { do {
sp = fgets(string, 256, inf); sp = fgets(string, 256, inf);
if (sp == NULL || sp[0] == dc && sp[1] == '\n') { if (sp == NULL || (sp[0] == dc && sp[1] == '\n')) {
pos = ftell(inf); pos = ftell(inf);
length = pos - last_off - (sp ? strlen(sp) : 0); length = pos - last_off - (sp ? strlen(sp) : 0);
last_off = pos; last_off = pos;
@ -193,12 +189,12 @@ char **av;
first = Oflag; first = Oflag;
} }
else if (first) { else if (first) {
for (nsp = sp; !isalnum(*nsp); nsp++) for (nsp = sp; !isalnum((unsigned char)*nsp); nsp++)
continue; continue;
ALLOC(Firstch, Num_pts); ALLOC(Firstch, Num_pts);
fp = &Firstch[Num_pts - 1]; fp = &Firstch[Num_pts - 1];
if (Iflag && isupper(*nsp)) if (Iflag && isupper((unsigned char)*nsp))
fp->first = tolower(*nsp); fp->first = tolower((unsigned char)*nsp);
else else
fp->first = *nsp; fp->first = *nsp;
fp->pos = Seekpts[Num_pts - 1]; fp->pos = Seekpts[Num_pts - 1];
@ -225,14 +221,14 @@ char **av;
if (Num_pts == 2) if (Num_pts == 2)
puts("There was 1 string"); puts("There was 1 string");
else else
printf("There were %d strings\n", Num_pts - 1); printf("There were %ld strings\n", Num_pts - 1);
printf("Longest string: %lu byte%s\n", Tbl.str_longlen, printf("Longest string: %lu byte%s\n", Tbl.str_longlen,
Tbl.str_longlen == 1 ? "" : "s"); Tbl.str_longlen == 1 ? "" : "s");
printf("Shortest string: %lu byte%s\n", Tbl.str_shortlen, printf("Shortest string: %lu byte%s\n", Tbl.str_shortlen,
Tbl.str_shortlen == 1 ? "" : "s"); Tbl.str_shortlen == 1 ? "" : "s");
} }
(void) fseek(outf, (off_t) 0, 0); rewind(outf);
Tbl.str_version = htonl(Tbl.str_version); Tbl.str_version = htonl(Tbl.str_version);
Tbl.str_numstr = htonl(Num_pts - 1); Tbl.str_numstr = htonl(Num_pts - 1);
Tbl.str_longlen = htonl(Tbl.str_longlen); Tbl.str_longlen = htonl(Tbl.str_longlen);
@ -251,7 +247,7 @@ char **av;
/* /*
* This routine evaluates arguments from the command line * This routine evaluates arguments from the command line
*/ */
getargs(argc, argv) void getargs(argc, argv)
int argc; int argc;
char **argv; char **argv;
{ {
@ -265,7 +261,7 @@ char **argv;
Delimch = *optarg; Delimch = *optarg;
if (!isascii(Delimch)) { if (!isascii(Delimch)) {
printf("bad delimiting character: '\\%o\n'", printf("bad delimiting character: '\\%o\n'",
Delimch); (unsigned char)Delimch);
} }
break; break;
case 'i': /* ignore case in ordering */ case 'i': /* ignore case in ordering */
@ -304,7 +300,7 @@ char **argv;
} }
} }
usage() void usage()
{ {
(void) fprintf(stderr, (void) fprintf(stderr,
"strfile [-iorsx] [-c char] sourcefile [datafile]\n"); "strfile [-iorsx] [-c char] sourcefile [datafile]\n");
@ -315,11 +311,11 @@ usage()
* add_offset: * add_offset:
* Add an offset to the list, or write it out, as appropriate. * Add an offset to the list, or write it out, as appropriate.
*/ */
add_offset(fp, off) void add_offset(fp, off)
FILE *fp; FILE *fp;
off_t off; long off;
{ {
off_t net; long net;
if (!STORING_PTRS) { if (!STORING_PTRS) {
net = htonl(off); net = htonl(off);
@ -335,12 +331,11 @@ off_t off;
* do_order: * do_order:
* Order the strings alphabetically (possibly ignoring case). * Order the strings alphabetically (possibly ignoring case).
*/ */
do_order() void do_order()
{ {
register int i; register int i;
register off_t *lp; register long *lp;
register STR *fp; register STR *fp;
extern int cmp_str();
Sort_1 = fopen(Infile, "r"); Sort_1 = fopen(Infile, "r");
Sort_2 = fopen(Infile, "r"); Sort_2 = fopen(Infile, "r");
@ -359,38 +354,21 @@ do_order()
* cmp_str: * cmp_str:
* Compare two strings in the file * Compare two strings in the file
*/ */
char * int cmp_str(p1, p2)
unctrl(c)
char c;
{
static char buf[3];
if (isprint(c)) {
buf[0] = c;
buf[1] = '\0';
}
else if (c == 0177) {
buf[0] = '^';
buf[1] = '?';
}
else {
buf[0] = '^';
buf[1] = c + 'A' - 1;
}
return buf;
}
cmp_str(p1, p2)
STR *p1, *p2; STR *p1, *p2;
{ {
register int c1, c2; register int c1, c2;
register int n1, n2; register int n1, n2;
static char s1[2], s2[2];
int r;
# define SET_N(nf,ch) (nf = (ch == '\n')) # define SET_N(nf,ch) (nf = (ch == '\n'))
# define IS_END(ch,nf) (ch == Delimch && nf) # define IS_END(ch,nf) (ch == EOF || (ch == (unsigned char) Delimch && nf))
c1 = p1->first; s1[0] = c1 = (unsigned char) p1->first;
c2 = p2->first; s2[0] = c2 = (unsigned char) p2->first;
if ((r = strcoll(s1, s2)) != 0)
return r;
if (c1 != c2) if (c1 != c2)
return c1 - c2; return c1 - c2;
@ -399,9 +377,9 @@ STR *p1, *p2;
n1 = FALSE; n1 = FALSE;
n2 = FALSE; n2 = FALSE;
while (!isalnum(c1 = getc(Sort_1)) && c1 != '\0') while (!isalnum(c1 = getc(Sort_1)) && c1 != '\0' && c1 != EOF)
SET_N(n1, c1); SET_N(n1, c1);
while (!isalnum(c2 = getc(Sort_2)) && c2 != '\0') while (!isalnum(c2 = getc(Sort_2)) && c2 != '\0' && c2 != EOF)
SET_N(n2, c2); SET_N(n2, c2);
while (!IS_END(c1, n1) && !IS_END(c2, n2)) { while (!IS_END(c1, n1) && !IS_END(c2, n2)) {
@ -411,6 +389,10 @@ STR *p1, *p2;
if (isupper(c2)) if (isupper(c2))
c2 = tolower(c2); c2 = tolower(c2);
} }
s1[0] = c1;
s2[0] = c2;
if ((r = strcoll(s1, s2)) != 0)
return r;
if (c1 != c2) if (c1 != c2)
return c1 - c2; return c1 - c2;
SET_N(n1, c1); SET_N(n1, c1);
@ -422,6 +404,10 @@ STR *p1, *p2;
c1 = 0; c1 = 0;
if (IS_END(c2, n2)) if (IS_END(c2, n2))
c2 = 0; c2 = 0;
s1[0] = c1;
s2[0] = c2;
if ((r = strcoll(s1, s2)) != 0)
return r;
return c1 - c2; return c1 - c2;
} }
@ -431,14 +417,13 @@ STR *p1, *p2;
* not to randomize across delimiter boundaries. All * not to randomize across delimiter boundaries. All
* randomization is done within each block. * randomization is done within each block.
*/ */
randomize() void randomize()
{ {
register int cnt, i; register int cnt, i;
register off_t tmp; register long tmp;
register off_t *sp; register long *sp;
extern time_t time();
srandom((int)(time((time_t *) NULL) + getpid())); srandom((int)(time((time_t *) NULL) ^ getpid()));
Tbl.str_flags |= STR_RANDOM; Tbl.str_flags |= STR_RANDOM;
cnt = Tbl.str_numstr; cnt = Tbl.str_numstr;

View File

@ -37,7 +37,7 @@
*/ */
#define STR_ENDSTRING(line,tbl) \ #define STR_ENDSTRING(line,tbl) \
((line)[0] == (tbl).str_delim && (line)[1] == '\n') (((unsigned char)(line)[0]) == (tbl).str_delim && (line)[1] == '\n')
typedef struct { /* information table */ typedef struct { /* information table */
#define VERSION 1 #define VERSION 1

View File

@ -2,6 +2,7 @@
PROG= unstr PROG= unstr
NOMAN= noman NOMAN= noman
CFLAGS+=-I${.CURDIR}/../strfile CFLAGS+=-Wall -I${.CURDIR}/../strfile
.include "${.CURDIR}/../../Makefile.inc"
.include <bsd.prog.mk> .include <bsd.prog.mk>

View File

@ -35,13 +35,13 @@
*/ */
#ifndef lint #ifndef lint
static char copyright[] = static const char copyright[] =
"@(#) Copyright (c) 1991, 1993\n\ "@(#) Copyright (c) 1991, 1993\n\
The Regents of the University of California. All rights reserved.\n"; The Regents of the University of California. All rights reserved.\n";
#endif /* not lint */ #endif /* not lint */
#ifndef lint #ifndef lint
static char sccsid[] = "@(#)unstr.c 8.1 (Berkeley) 5/31/93"; static const char sccsid[] = "@(#)unstr.c 8.1 (Berkeley) 5/31/93";
#endif /* not lint */ #endif /* not lint */
/* /*
@ -57,15 +57,11 @@ static char sccsid[] = "@(#)unstr.c 8.1 (Berkeley) 5/31/93";
* Ken Arnold Aug 13, 1978 * Ken Arnold Aug 13, 1978
*/ */
# include <machine/endian.h>
# include <sys/param.h> # include <sys/param.h>
# include "strfile.h"
# include <stdio.h> # include <stdio.h>
# include <ctype.h> # include <ctype.h>
# include <string.h>
# ifndef MAXPATHLEN # include "strfile.h"
# define MAXPATHLEN 1024
# endif /* MAXPATHLEN */
char *Infile, /* name of input file */ char *Infile, /* name of input file */
Datafile[MAXPATHLEN], /* name of data file */ Datafile[MAXPATHLEN], /* name of data file */
@ -73,10 +69,10 @@ char *Infile, /* name of input file */
FILE *Inf, *Dataf; FILE *Inf, *Dataf;
char *strcat(), *strcpy(); void getargs(), order_unstr();
/* ARGSUSED */ /* ARGSUSED */
main(ac, av) void main(ac, av)
int ac; int ac;
char **av; char **av;
{ {
@ -108,7 +104,7 @@ char **av;
exit(0); exit(0);
} }
getargs(av) void getargs(av)
register char *av[]; register char *av[];
{ {
if (!*++av) { if (!*++av) {
@ -120,12 +116,12 @@ register char *av[];
(void) strcat(Datafile, ".dat"); (void) strcat(Datafile, ".dat");
} }
order_unstr(tbl) void order_unstr(tbl)
register STRFILE *tbl; register STRFILE *tbl;
{ {
register int i; register int i;
register char *sp; register char *sp;
auto off_t pos; long pos;
char buf[BUFSIZ]; char buf[BUFSIZ];
for (i = 0; i < tbl->str_numstr; i++) { for (i = 0; i < tbl->str_numstr; i++) {