made index file endian-independend

This commit is contained in:
hjp 1994-01-08 18:05:00 +00:00
parent 859b39f6e0
commit 60764e8d3a
1 changed files with 72 additions and 15 deletions

View File

@ -2,8 +2,9 @@
* Fortune -- Print one fortune out of an indexed fortune file * Fortune -- Print one fortune out of an indexed fortune file
*/ */
static char fortune_c_rcsid[] = "$Id: fortune.c,v 3.3 1992-03-24 11:15:45 hjp Exp $"; static char fortune_c_rcsid[] = "$Id: fortune.c,v 3.4 1994-01-08 18:05:00 hjp Exp $";
#include <assert.h>
#include <errno.h> #include <errno.h>
#include <limits.h> #include <limits.h>
#include <stdio.h> #include <stdio.h>
@ -16,14 +17,69 @@ static char fortune_c_rcsid[] = "$Id: fortune.c,v 3.3 1992-03-24 11:15:45 hjp Ex
#include <ant/io.h> #include <ant/io.h>
#include <ant/string.h> #include <ant/string.h>
#include <ant/globals.h>
/* The following parameters will have to be adapted to your system */ /* The following parameters will have to be adapted to your system */
#define DELIMITER "%%\n" #define DELIMITER "%%\n"
#define MAXLINE (80 + 1 + 1) #define MAXLINE (80 + 1 + 1)
char * fortunefile = "/usr/lib/fortune.dat"; char * fortunefile = "/usr/lib/fortune.dat";
char indexfile [PATH_MAX] = "/usr/lib/fortune.idx"; char indexfile [PATH_MAX] = "/usr/lib/fortune.idx";
#define RECLEN 4 /* length of a record in the index
* file. This would normally be the
* size of a long, but could be more
* or less if you want the program
* run on systems with different lengths
* of a long and share the same index
* file.
*/
/* The rest should be generic */ /* The rest should be generic */
void fwritelong (FILE *fp, long offset, long value) {
unsigned char c [RECLEN];
assert (value < (1UL << (8 * RECLEN - 1)));
if (fseek (fp, offset, SEEK_SET) != 0) {
eprintf ("%s: cannot seek to %ld in %s: %s\n",
cmnd, offset, indexfile, strerror (errno));
exit (1);
}
c [0] = value & 0xFF;
c [1] = (value >> 8) & 0xFF;
c [2] = (value >> 16) & 0xFF;
c [3] = (value >> 24) & 0xFF;
if (fwrite (c, RECLEN, 1, fp) != 1) {
eprintf ("%s: cannot write to %s: %s\n",
cmnd, indexfile, strerror (errno));
exit (1);
}
}
long freadlong (FILE *fp, long offset) {
unsigned char c [RECLEN];
unsigned long value;
if (fseek (fp, offset, SEEK_SET) != 0) {
eprintf ("%s: cannot seek to %ld in %s: %s\n",
cmnd, offset, indexfile, strerror (errno));
exit (1);
}
if (fread (c, RECLEN, 1, fp) != 1) {
eprintf ("%s: cannot write to %s: %s\n",
cmnd, indexfile, strerror (errno));
exit (1);
}
value = c [0] + (c [1] << 8) + (c [2] << 16) + (c [3] << 24);
return value > LONG_MAX ? - (long) (- value) : value;
}
int int
main( main(
int argc, int argc,
@ -31,7 +87,9 @@ main(
){ ){
FILE * ffp, * ifp; FILE * ffp, * ifp;
char * p; char * p;
long pos, long pos, ipos, /* position in fortune and
* index file
*/
cnt, /* Number of fortunes in the file cnt, /* Number of fortunes in the file
*/ */
nr, /* number of fortune to read */ nr, /* number of fortune to read */
@ -41,10 +99,12 @@ main(
struct stat statbuf; struct stat statbuf;
char line [MAXLINE]; char line [MAXLINE];
cmnd = argv [0];
if (argc >= 2){ if (argc >= 2){
fortunefile = argv [1]; fortunefile = argv [1];
strncpy (indexfile, argv [1], PATH_MAX); strncpy (indexfile, argv [1], PATH_MAX);
if (p = strrchr (indexfile, '.')) { if ((p = strrchr (indexfile, '.'))) {
strcpy (p, ".idx"); strcpy (p, ".idx");
} else { } else {
strcat (indexfile, ".idx"); strcat (indexfile, ".idx");
@ -94,17 +154,17 @@ main(
exit (3); exit (3);
} }
cnt = 0; cnt = 0;
fwrite (&cnt, sizeof (long), 1, ifp); fwritelong (ifp, 0, cnt);
fseek (ifp, sizeof (long), SEEK_CUR); /* leave position intact */ ipos = RECLEN * 2;
while (fgets (line, sizeof (line), ffp)) { while (fgets (line, sizeof (line), ffp)) {
if (STREQ (line, DELIMITER)) { if (STREQ (line, DELIMITER)) {
pos = ftell (ffp); pos = ftell (ffp);
fwrite (&pos, sizeof (long), 1, ifp); fwritelong (ifp, ipos, pos);
++ cnt; ++ cnt;
ipos += RECLEN;
} }
} }
fseek (ifp, 0, SEEK_SET); fwritelong (ifp, 0, cnt);
fwrite (&cnt, sizeof (long), 1, ifp);
fclose (ifp); fclose (ifp);
fclose (ffp); fclose (ffp);
} }
@ -125,23 +185,20 @@ main(
} }
/* Get number of entries */ /* Get number of entries */
fread (&cnt, sizeof (long), 1, ifp); cnt = freadlong (ifp, 0);
if (cnt == 0) { if (cnt == 0) {
eprintf ("%s: empty fortune file\n", argv [0]); eprintf ("%s: empty fortune file\n", argv [0]);
exit (6); exit (6);
} }
fseek (ifp, sizeof (long), SEEK_SET); nr = freadlong (ifp, RECLEN);
fread (&nr, sizeof (long), 1, ifp);
nr ++; nr ++;
if (nr >= cnt) nr = 0; if (nr >= cnt) nr = 0;
fseek (ifp, sizeof (long), SEEK_SET); fwritelong (ifp, RECLEN, nr);
fwrite (&nr, sizeof (long), 1, ifp);
/* Now look for the start of the fortune in the index file */ /* Now look for the start of the fortune in the index file */
fseek (ifp, (nr + 2) * sizeof (long), SEEK_SET); pos = freadlong (ifp, (nr + 2) * RECLEN);
fread (&pos, sizeof (long), 1, ifp);
/* And seek to it in the fortune file */ /* And seek to it in the fortune file */
fseek (ffp, pos, SEEK_SET); fseek (ffp, pos, SEEK_SET);