-/* $NetBSD: strfile.c,v 1.6 1997/10/10 13:04:42 lukem Exp $ */
+/* $NetBSD: strfile.c,v 1.15 1999/12/07 23:07:39 jsm Exp $ */
/*-
* Copyright (c) 1989, 1993
* SUCH DAMAGE.
*/
+#include <sys/cdefs.h>
#ifndef lint
-static char copyright[] =
-"@(#) Copyright (c) 1989, 1993\n\
- The Regents of the University of California. All rights reserved.\n";
+__COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
+ The Regents of the University of California. All rights reserved.\n");
#endif /* not lint */
#ifndef lint
#if 0
static char sccsid[] = "@(#)strfile.c 8.1 (Berkeley) 5/31/93";
#else
-static char rcsid[] = "$NetBSD: strfile.c,v 1.6 1997/10/10 13:04:42 lukem Exp $";
+__RCSID("$NetBSD: strfile.c,v 1.15 1999/12/07 23:07:39 jsm Exp $");
#endif
#endif /* not lint */
# include <sys/types.h>
# include <sys/param.h>
+# include <err.h>
+# include <ctype.h>
# include <stdio.h>
+# include <stdlib.h>
# include <string.h>
-# include <ctype.h>
+# include <time.h>
+# include <unistd.h>
# include "strfile.h"
# ifndef MAXPATHLEN
# define STORING_PTRS (Oflag || Rflag)
# define CHUNKSIZE 512
-#ifdef lint
-# define ALWAYS atoi("1")
-#else
-# define ALWAYS 1
-#endif
-# define ALLOC(ptr,sz) if (ALWAYS) { \
+# define ALLOC(ptr,sz) do { \
if (ptr == NULL) \
ptr = malloc((unsigned int) (CHUNKSIZE * sizeof *ptr)); \
else if (((sz) + 1) % CHUNKSIZE == 0) \
ptr = realloc((void *) ptr, ((unsigned int) ((sz) + CHUNKSIZE) * sizeof *ptr)); \
- if (ptr == NULL) { \
- fprintf(stderr, "out of space\n"); \
- exit(1); \
- } \
- } else
+ if (ptr == NULL) \
+ errx(1, "out of space"); \
+ } while (0)
#ifdef NO_VOID
# define void char
STR *Firstch; /* first chars of each string */
-char *fgets(), *strcpy(), *strcat();
+void add_offset __P((FILE *, off_t));
+int cmp_str __P((const void *, const void *));
+void do_order __P((void));
+void getargs __P((int, char *[]));
+int main __P((int, char *[]));
+void randomize __P((void));
+char *unctrl __P((char));
+void usage __P((void)) __attribute__((__noreturn__));
-void *malloc(), *realloc();
/*
* main:
* CHUNKSIZE blocks; if the latter, we just write each pointer,
* and then seek back to the beginning to write in the table.
*/
+int
main(ac, av)
-int ac;
-char **av;
+ int ac;
+ char *av[];
{
- register char *sp, dc;
- register FILE *inf, *outf;
- register off_t last_off, length, pos, *p;
- register int first, cnt;
- register char *nsp;
- register STR *fp;
- static char string[257];
+ char *sp, dc;
+ FILE *inf, *outf;
+ off_t last_off, length, pos, *p;
+ int first, cnt;
+ char *nsp;
+ STR *fp;
+ static char string[257];
getargs(ac, av); /* evalute arguments */
dc = Delimch;
- if ((inf = fopen(Infile, "r")) == NULL) {
- perror(Infile);
- exit(1);
- }
+ if ((inf = fopen(Infile, "r")) == NULL)
+ err(1, "open `%s'", Infile);
- if ((outf = fopen(Outfile, "w")) == NULL) {
- perror(Outfile);
- exit(1);
- }
+ if ((outf = fopen(Outfile, "w")) == NULL)
+ err(1, "open `%s'", Outfile);
if (!STORING_PTRS)
- (void) fseek(outf, sizeof Tbl, 0);
+ (void) fseek(outf, sizeof Tbl, SEEK_SET);
/*
* Write the strings onto the file
last_off = 0;
do {
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);
length = pos - last_off - (sp ? strlen(sp) : 0);
last_off = pos;
if (!length)
continue;
add_offset(outf, pos);
- if (Tbl.str_longlen < length)
+ if ((off_t)Tbl.str_longlen < length)
Tbl.str_longlen = length;
- if (Tbl.str_shortlen > length)
+ if ((off_t)Tbl.str_shortlen > length)
Tbl.str_shortlen = length;
first = Oflag;
}
if (Num_pts == 2)
puts("There was 1 string");
else
- printf("There were %d strings\n", Num_pts - 1);
+ printf("There were %d strings\n", (int)(Num_pts - 1));
printf("Longest string: %lu byte%s\n", Tbl.str_longlen,
Tbl.str_longlen == 1 ? "" : "s");
printf("Shortest string: %lu byte%s\n", Tbl.str_shortlen,
Tbl.str_shortlen == 1 ? "" : "s");
}
- (void) fseek(outf, (off_t) 0, 0);
- Tbl.str_version = htonl(Tbl.str_version);
- Tbl.str_numstr = htonl(Num_pts - 1);
- Tbl.str_longlen = htonl(Tbl.str_longlen);
- Tbl.str_shortlen = htonl(Tbl.str_shortlen);
- Tbl.str_flags = htonl(Tbl.str_flags);
+ (void) fseek(outf, (off_t) 0, SEEK_SET);
+ HTOBE32(Tbl.str_version);
+ Tbl.str_numstr = htobe32(Num_pts - 1);
+ HTOBE32(Tbl.str_longlen);
+ HTOBE32(Tbl.str_shortlen);
+ HTOBE32(Tbl.str_flags);
(void) fwrite((char *) &Tbl, sizeof Tbl, 1, outf);
if (STORING_PTRS) {
for (p = Seekpts, cnt = Num_pts; cnt--; ++p)
- *p = htonl(*p);
+ HTOBE64(*p);
(void) fwrite((char *) Seekpts, sizeof *Seekpts, (int) Num_pts, outf);
}
+ fflush(outf);
+ if (ferror(outf))
+ err(1, "fwrite %s", Outfile);
(void) fclose(outf);
exit(0);
}
/*
* This routine evaluates arguments from the command line
*/
+void
getargs(argc, argv)
-int argc;
-char **argv;
+ int argc;
+ char **argv;
{
- extern char *optarg;
- extern int optind;
int ch;
while ((ch = getopt(argc, argv, "c:iorsx")) != -1)
}
}
+void
usage()
{
(void) fprintf(stderr,
* add_offset:
* Add an offset to the list, or write it out, as appropriate.
*/
+void
add_offset(fp, off)
-FILE *fp;
-off_t off;
+ FILE *fp;
+ off_t off;
{
off_t net;
if (!STORING_PTRS) {
- net = htonl(off);
+ net = htobe64(off);
fwrite(&net, 1, sizeof net, fp);
} else {
ALLOC(Seekpts, Num_pts + 1);
* do_order:
* Order the strings alphabetically (possibly ignoring case).
*/
+void
do_order()
{
- register int i;
- register off_t *lp;
- register STR *fp;
- extern int cmp_str();
+ int i;
+ off_t *lp;
+ STR *fp;
Sort_1 = fopen(Infile, "r");
Sort_2 = fopen(Infile, "r");
*/
char *
unctrl(c)
-char c;
+ char c;
{
static char buf[3];
return buf;
}
-cmp_str(p1, p2)
-STR *p1, *p2;
+int
+cmp_str(vp1, vp2)
+ const void *vp1, *vp2;
{
- register int c1, c2;
- register int n1, n2;
+ const STR *p1, *p2;
+ int c1, c2;
+ int n1, n2;
+
+ p1 = (const STR *)vp1;
+ p2 = (const STR *)vp2;
# define SET_N(nf,ch) (nf = (ch == '\n'))
# define IS_END(ch,nf) (ch == Delimch && nf)
if (c1 != c2)
return c1 - c2;
- (void) fseek(Sort_1, p1->pos, 0);
- (void) fseek(Sort_2, p2->pos, 0);
+ (void) fseek(Sort_1, p1->pos, SEEK_SET);
+ (void) fseek(Sort_2, p2->pos, SEEK_SET);
n1 = FALSE;
n2 = FALSE;
* not to randomize across delimiter boundaries. All
* randomization is done within each block.
*/
+void
randomize()
{
- register int cnt, i;
- register off_t tmp;
- register off_t *sp;
- extern time_t time();
+ int cnt, i;
+ off_t tmp;
+ off_t *sp;
srandom((int)(time((time_t *) NULL) + getpid()));