diff options
author | Thorsten Lockert <tholo@cvs.openbsd.org> | 1996-10-06 03:22:24 +0000 |
---|---|---|
committer | Thorsten Lockert <tholo@cvs.openbsd.org> | 1996-10-06 03:22:24 +0000 |
commit | 5bd6bf2efed6f5df9b8670886242754339cec249 (patch) | |
tree | 1739a7dc7122c2acc21ef9a51c67eb2be256e6be /games | |
parent | b833779cf25d4a06fd8f7fd2f40a31d243536d2d (diff) |
Store the fortune data files with fixed data types, and document this.
Bump database version to 2.
Diffstat (limited to 'games')
-rw-r--r-- | games/fortune/fortune/fortune.c | 51 | ||||
-rw-r--r-- | games/fortune/strfile/strfile.8 | 19 | ||||
-rw-r--r-- | games/fortune/strfile/strfile.c | 44 | ||||
-rw-r--r-- | games/fortune/strfile/strfile.h | 14 | ||||
-rw-r--r-- | games/fortune/unstr/unstr.c | 14 |
5 files changed, 94 insertions, 48 deletions
diff --git a/games/fortune/fortune/fortune.c b/games/fortune/fortune/fortune.c index c8b96eaa1ca..64b699f0531 100644 --- a/games/fortune/fortune/fortune.c +++ b/games/fortune/fortune/fortune.c @@ -72,7 +72,7 @@ static char rcsid[] = "$NetBSD: fortune.c,v 1.8 1995/03/23 08:28:40 cgd Exp $"; # define CPERS 20 /* # of chars for each sec */ # define SLEN 160 /* # of chars in short fortune */ -# define POS_UNKNOWN ((off_t) -1) /* pos for file unknown */ +# define POS_UNKNOWN ((int32_t) -1) /* pos for file unknown */ # define NO_PROB (-1) /* no prob specified for file */ # ifdef DEBUG @@ -86,7 +86,7 @@ static char rcsid[] = "$NetBSD: fortune.c,v 1.8 1995/03/23 08:28:40 cgd Exp $"; typedef struct fd { int percent; int fd, datfd; - off_t pos; + int32_t pos; FILE *inf; char *name; char *path; @@ -118,7 +118,7 @@ char *Fortbuf = NULL; /* fortune buffer for -m */ int Fort_len = 0; -off_t Seekpts[2]; /* seek pointers to fortunes */ +int32_t Seekpts[2]; /* seek pointers to fortunes */ FILEDESC *File_list = NULL, /* Head of file list */ *File_tail = NULL; /* Tail of file list */ @@ -221,7 +221,9 @@ char *av[]; */ (void) flock(fd, LOCK_EX); #endif /* LOCK_EX */ + Fortfile->pos = htonl(Fortfile->pos); write(fd, (char *) &Fortfile->pos, sizeof Fortfile->pos); + Fortfile->pos = ntohl(Fortfile->pos); if (!Fortfile->was_pos_file) (void) chmod(Fortfile->path, 0666); #ifdef LOCK_EX @@ -794,7 +796,7 @@ int check_for_offend; free(datfile); #ifdef OK_TO_WRITE_DISK if (posp != NULL) { - *posp = copy(file, (unsigned int) (strlen(file) + 4)); /* +4 for ".dat" */ + *posp = copy(file, (unsigned int) (strlen(file) + 4)); /* +4 for ".pos" */ (void) strcat(*posp, ".pos"); } #endif /* OK_TO_WRITE_DISK */ @@ -980,8 +982,9 @@ get_fort() open_dat(fp); (void) lseek(fp->datfd, (off_t) (sizeof fp->tbl + fp->pos * sizeof Seekpts[0]), 0); - read(fp->datfd, Seekpts, sizeof Seekpts); + read(fp->datfd, &Seekpts[0], sizeof Seekpts[0]); Seekpts[0] = ntohl(Seekpts[0]); + read(fp->datfd, &Seekpts[1], sizeof Seekpts[1]); Seekpts[1] = ntohl(Seekpts[1]); } @@ -1097,8 +1100,10 @@ FILEDESC *fp; if ((fd = open(fp->posfile, 0)) < 0 || read(fd, &fp->pos, sizeof fp->pos) != sizeof fp->pos) fp->pos = random() % fp->tbl.str_numstr; - else if (fp->pos >= fp->tbl.str_numstr) + else if (ntohl(fp->pos) >= fp->tbl.str_numstr) fp->pos %= fp->tbl.str_numstr; + else + fp->pos = ntohl(fp->pos); if (fd >= 0) (void) close(fd); #else @@ -1128,11 +1133,43 @@ FILEDESC *fp; perror(fp->datfile); exit(1); } - if (read(fd, (char *) &fp->tbl, sizeof fp->tbl) != sizeof fp->tbl) { + if (read(fd, &fp->tbl.str_version, sizeof(fp->tbl.str_version)) != + sizeof(fp->tbl.str_version)) { + (void)fprintf(stderr, + "fortune: %s corrupted\n", fp->path); + exit(1); + } + if (read(fd, &fp->tbl.str_numstr, sizeof(fp->tbl.str_numstr)) != + sizeof(fp->tbl.str_numstr)) { + (void)fprintf(stderr, + "fortune: %s corrupted\n", fp->path); + exit(1); + } + if (read(fd, &fp->tbl.str_longlen, sizeof(fp->tbl.str_longlen)) != + sizeof(fp->tbl.str_longlen)) { + (void)fprintf(stderr, + "fortune: %s corrupted\n", fp->path); + exit(1); + } + if (read(fd, &fp->tbl.str_shortlen, sizeof(fp->tbl.str_shortlen)) != + sizeof(fp->tbl.str_shortlen)) { + (void)fprintf(stderr, + "fortune: %s corrupted\n", fp->path); + exit(1); + } + if (read(fd, &fp->tbl.str_flags, sizeof(fp->tbl.str_flags)) != + sizeof(fp->tbl.str_flags)) { (void)fprintf(stderr, "fortune: %s corrupted\n", fp->path); exit(1); } + if (read(fd, fp->tbl.stuff, sizeof(fp->tbl.stuff)) != + sizeof(fp->tbl.stuff)) { + (void)fprintf(stderr, + "fortune: %s corrupted\n", fp->path); + exit(1); + } + /* fp->tbl.str_version = ntohl(fp->tbl.str_version); */ fp->tbl.str_numstr = ntohl(fp->tbl.str_numstr); fp->tbl.str_longlen = ntohl(fp->tbl.str_longlen); diff --git a/games/fortune/strfile/strfile.8 b/games/fortune/strfile/strfile.8 index 9883610380a..7271f237f34 100644 --- a/games/fortune/strfile/strfile.8 +++ b/games/fortune/strfile/strfile.8 @@ -103,19 +103,22 @@ field to be set. .Pp The format of the header is: .Bd -literal -#define VERSION 1 -unsigned long str_version; /* version number */ -unsigned long str_numstr; /* # of strings in the file */ -unsigned long str_longlen; /* length of longest string */ -unsigned long str_shortlen; /* length of shortest string */ +#define VERSION 2 +u_int32_t str_version; /* version number */ +u_int32_t str_numstr; /* # of strings in the file */ +u_int32_t str_longlen; /* length of longest string */ +u_int32_t str_shortlen; /* length of shortest string */ #define STR_RANDOM 0x1 /* randomized pointers */ #define STR_ORDERED 0x2 /* ordered pointers */ #define STR_ROTATED 0x4 /* rot-13'd text */ -unsigned long str_flags; /* bit field for flags */ -char str_delim; /* delimiting character */ +u_int32_t str_flags; /* bit field for flags */ +u_int8_t str_delim; /* delimiting character */ +u_int8_t str_pad[3]; /* padding */ .Ed .Pp -All fields are written in network byte order. +All fields are written in network byte order. Each field is also +written independantly so as to avoid structure padding problems on +some architectures. .Pp The purpose of .Nm unstr diff --git a/games/fortune/strfile/strfile.c b/games/fortune/strfile/strfile.c index 641b8bc8171..4992f6c174a 100644 --- a/games/fortune/strfile/strfile.c +++ b/games/fortune/strfile/strfile.c @@ -112,7 +112,7 @@ static char rcsid[] = "$NetBSD: strfile.c,v 1.4 1995/04/24 12:23:09 cgd Exp $"; typedef struct { char first; - off_t pos; + int32_t pos; } STR; char *Infile = NULL, /* input file name */ @@ -126,7 +126,7 @@ int Rflag = FALSE; /* randomize order flag */ int Xflag = FALSE; /* set rotated bit */ long Num_pts = 0; /* number of pointers/strings */ -off_t *Seekpts; +int32_t *Seekpts; FILE *Sort_1, *Sort_2; /* pointers for sorting */ @@ -153,7 +153,8 @@ char **av; { register char *sp, dc; register FILE *inf, *outf; - register off_t last_off, length, pos, *p; + register int32_t last_off, length, pos; + register int32_t *p; register int first, cnt; register char *nsp; register STR *fp; @@ -239,18 +240,23 @@ char **av; Tbl.str_shortlen == 1 ? "" : "s"); } - (void) fseek(outf, (off_t) 0, 0); + (void) fseek(outf, (long) 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) fwrite((char *) &Tbl, sizeof Tbl, 1, outf); - if (STORING_PTRS) { - for (p = Seekpts, cnt = Num_pts; cnt--; ++p) + (void) fwrite(&Tbl.str_version, sizeof(Tbl.str_version), 1, outf); + (void) fwrite(&Tbl.str_numstr, sizeof(Tbl.str_numstr), 1, outf); + (void) fwrite(&Tbl.str_longlen, sizeof(Tbl.str_longlen), 1, outf); + (void) fwrite(&Tbl.str_shortlen, sizeof(Tbl.str_shortlen), 1, outf); + (void) fwrite(&Tbl.str_flags, sizeof(Tbl.str_flags), 1, outf); + (void) fwrite( Tbl.stuff, sizeof(Tbl.stuff), 1, outf); + if (STORING_PTRS) + for (p = Seekpts, cnt = Num_pts; cnt--; ++p) { *p = htonl(*p); - (void) fwrite((char *) Seekpts, sizeof *Seekpts, (int) Num_pts, outf); - } + (void) fwrite(p, sizeof(*p), 1, outf); + } (void) fclose(outf); exit(0); } @@ -324,9 +330,9 @@ usage() */ add_offset(fp, off) FILE *fp; -off_t off; +int32_t off; { - off_t net; + int32_t net; if (!STORING_PTRS) { net = htonl(off); @@ -344,10 +350,10 @@ off_t off; */ do_order() { - register int i; - register off_t *lp; - register STR *fp; - extern int cmp_str(); + register int i; + register int32_t *lp; + register STR *fp; + extern int cmp_str(); Sort_1 = fopen(Infile, "r"); Sort_2 = fopen(Infile, "r"); @@ -440,10 +446,10 @@ STR *p1, *p2; */ randomize() { - register int cnt, i; - register off_t tmp; - register off_t *sp; - extern time_t time(); + register int cnt, i; + register int32_t tmp; + register int32_t *sp; + extern time_t time(); srandom((int)(time((time_t *) NULL) + getpid())); diff --git a/games/fortune/strfile/strfile.h b/games/fortune/strfile/strfile.h index 48efc747115..6be8240b666 100644 --- a/games/fortune/strfile/strfile.h +++ b/games/fortune/strfile/strfile.h @@ -42,15 +42,15 @@ ((line)[0] == (tbl).str_delim && (line)[1] == '\n') typedef struct { /* information table */ -#define VERSION 1 - unsigned long str_version; /* version number */ - unsigned long str_numstr; /* # of strings in the file */ - unsigned long str_longlen; /* length of longest string */ - unsigned long str_shortlen; /* length of shortest string */ +#define VERSION 2 + u_int32_t str_version; /* version number */ + u_int32_t str_numstr; /* # of strings in the file */ + u_int32_t str_longlen; /* length of longest string */ + u_int32_t str_shortlen; /* length of shortest string */ #define STR_RANDOM 0x1 /* randomized pointers */ #define STR_ORDERED 0x2 /* ordered pointers */ #define STR_ROTATED 0x4 /* rot-13'd text */ - unsigned long str_flags; /* bit field for flags */ - unsigned char stuff[4]; /* long aligned space */ + u_int32_t str_flags; /* bit field for flags */ + u_int8_t stuff[4]; /* long aligned space */ #define str_delim stuff[0] /* delimiting character */ } STRFILE; diff --git a/games/fortune/unstr/unstr.c b/games/fortune/unstr/unstr.c index 5ea33d26341..fe4b307375d 100644 --- a/games/fortune/unstr/unstr.c +++ b/games/fortune/unstr/unstr.c @@ -93,12 +93,12 @@ char **av; perror(Datafile); exit(1); } - (void) fread((char *) &tbl, sizeof tbl, 1, Dataf); - tbl.str_version = ntohl(tbl.str_version); - tbl.str_numstr = ntohl(tbl.str_numstr); - tbl.str_longlen = ntohl(tbl.str_longlen); - tbl.str_shortlen = ntohl(tbl.str_shortlen); - tbl.str_flags = ntohl(tbl.str_flags); + (void) fread(&tbl.str_version, sizeof(tbl.str_version), 1, Dataf); + (void) fread(&tbl.str_numstr, sizeof(tbl.str_numstr), 1, Dataf); + (void) fread(&tbl.str_longlen, sizeof(tbl.str_longlen), 1, Dataf); + (void) fread(&tbl.str_shortlen, sizeof(tbl.str_shortlen), 1, Dataf); + (void) fread(&tbl.str_flags, sizeof(tbl.str_flags), 1, Dataf); + (void) fread( tbl.stuff, sizeof(tbl.stuff), 1, Dataf); if (!(tbl.str_flags & (STR_ORDERED | STR_RANDOM))) { fprintf(stderr, "nothing to do -- table in file order\n"); exit(1); @@ -127,7 +127,7 @@ register STRFILE *tbl; { register int i; register char *sp; - auto off_t pos; + auto int32_t pos; char buf[BUFSIZ]; for (i = 0; i < tbl->str_numstr; i++) { |