Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/games/fortune/strfile Don't use any NetBSD specific features...



details:   https://anonhg.NetBSD.org/src/rev/4b84f8fe23b0
branches:  trunk
changeset: 480001:4b84f8fe23b0
user:      simonb <simonb%NetBSD.org@localhost>
date:      Thu Dec 30 01:32:33 1999 +0000

description:
Don't use any NetBSD specific features (<err.h> and friends mainly
- replaiced with local die() and dieperror() routines).  Use ANSI
prototypes (no dependance on the __P() macro).  Add new fwrite_be_offt()
function which writes out a big-endian 64bit number regards of the size
of off_t on the host machine.  Remove unused unctrl() function.

Now builds correct fortune .dat files on non-NetBSD machines as well
- tested on Ultrix and Solaris (but Solaris needs -lsocket to get
htonl()).

diffstat:

 games/fortune/strfile/strfile.c |  133 ++++++++++++++++++++++++---------------
 1 files changed, 82 insertions(+), 51 deletions(-)

diffs (245 lines):

diff -r 75f32fcd2e15 -r 4b84f8fe23b0 games/fortune/strfile/strfile.c
--- a/games/fortune/strfile/strfile.c   Thu Dec 30 01:26:44 1999 +0000
+++ b/games/fortune/strfile/strfile.c   Thu Dec 30 01:32:33 1999 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: strfile.c,v 1.15 1999/12/07 23:07:39 jsm Exp $ */
+/*     $NetBSD: strfile.c,v 1.16 1999/12/30 01:32:33 simonb Exp $      */
 
 /*-
  * Copyright (c) 1989, 1993
@@ -36,6 +36,7 @@
  * SUCH DAMAGE.
  */
 
+#ifdef __NetBSD__
 #include <sys/cdefs.h>
 #ifndef lint
 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
@@ -46,19 +47,23 @@
 #if 0
 static char sccsid[] = "@(#)strfile.c  8.1 (Berkeley) 5/31/93";
 #else
-__RCSID("$NetBSD: strfile.c,v 1.15 1999/12/07 23:07:39 jsm Exp $");
+__RCSID("$NetBSD: strfile.c,v 1.16 1999/12/30 01:32:33 simonb Exp $");
 #endif
 #endif /* not lint */
+#endif /* __NetBSD__ */
 
 # include      <sys/types.h>
 # include      <sys/param.h>
-# include      <err.h>
 # include      <ctype.h>
 # include      <stdio.h>
 # include      <stdlib.h>
 # include      <string.h>
 # include      <time.h>
 # include      <unistd.h>
+
+# ifndef u_int32_t
+# define u_int32_t     unsigned int
+# endif
 # include      "strfile.h"
 
 # ifndef MAXPATHLEN
@@ -100,7 +105,7 @@
                        else if (((sz) + 1) % CHUNKSIZE == 0) \
                                ptr = realloc((void *) ptr, ((unsigned int) ((sz) + CHUNKSIZE) * sizeof *ptr)); \
                        if (ptr == NULL) \
-                               errx(1, "out of space"); \
+                               die("out of space"); \
                } while (0)
 
 #ifdef NO_VOID
@@ -131,14 +136,22 @@
 
 STR    *Firstch;                       /* first chars of each string */
 
-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__));
+#ifdef __GCC__
+#define NORETURN       __attribute__((__noreturn__))
+#else
+#define NORETURN
+#endif
+
+void   add_offset(FILE *, off_t);
+int    cmp_str(const void *, const void *);
+void   die(const char *) NORETURN;
+void   dieperror(const char *, char *) NORETURN;
+void   do_order(void);
+void   fwrite_be_offt(off_t, FILE *);
+void   getargs(int, char *[]);
+int    main(int, char *[]);
+void   randomize(void);
+void   usage(void) NORETURN;
 
 
 /*
@@ -163,13 +176,17 @@
        STR             *fp;
        static char     string[257];
 
+       /* sanity test */
+       if (sizeof(u_int32_t) != 4)
+               die("sizeof(unsigned int) != 4");
+
        getargs(ac, av);                /* evalute arguments */
        dc = Delimch;
        if ((inf = fopen(Infile, "r")) == NULL)
-               err(1, "open `%s'", Infile);
+               dieperror("open `%s'", Infile);
 
        if ((outf = fopen(Outfile, "w")) == NULL)
-               err(1, "open `%s'", Outfile);
+               dieperror("open `%s'", Outfile);
        if (!STORING_PTRS)
                (void) fseek(outf, sizeof Tbl, SEEK_SET);
 
@@ -178,7 +195,7 @@
         */
 
        Tbl.str_longlen = 0;
-       Tbl.str_shortlen = (unsigned int) 0xffffffff;
+       Tbl.str_shortlen = (unsigned int) 0x7fffffff;
        Tbl.str_delim = dc;
        Tbl.str_version = VERSION;
        first = Oflag;
@@ -240,20 +257,19 @@
        }
 
        (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);
+       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)
-                       HTOBE64(*p);
-               (void) fwrite((char *) Seekpts, sizeof *Seekpts, (int) Num_pts, outf);
+                       fwrite_be_offt(*p, outf);
        }
        fflush(outf);
        if (ferror(outf))
-               err(1, "fwrite %s", Outfile);
+               dieperror("fwrite %s", Outfile);
        (void) fclose(outf);
        exit(0);
 }
@@ -267,6 +283,8 @@
        char    **argv;
 {
        int     ch;
+       extern  int optind;
+       extern  char *optarg;
 
        while ((ch = getopt(argc, argv, "c:iorsx")) != -1)
                switch(ch) {
@@ -321,6 +339,26 @@
        exit(1);
 }
 
+void
+die(str)
+       const char *str;
+{
+       fprintf(stderr, "strfile: %s\n", str);
+       exit(1);
+}
+
+void
+dieperror(fmt, file)
+       const char *fmt;
+       char *file;
+{
+       fprintf(stderr, "strfile: ");
+       fprintf(stderr, fmt, file);
+       fprintf(stderr, ": ");
+       perror(NULL);
+       exit(1);
+}
+
 /*
  * add_offset:
  *     Add an offset to the list, or write it out, as appropriate.
@@ -330,11 +368,9 @@
        FILE    *fp;
        off_t   off;
 {
-       off_t net;
 
        if (!STORING_PTRS) {
-               net = htobe64(off);
-               fwrite(&net, 1, sizeof net, fp);
+               fwrite_be_offt(off, fp);
        } else {
                ALLOC(Seekpts, Num_pts + 1);
                Seekpts[Num_pts] = off;
@@ -366,31 +402,6 @@
        Tbl.str_flags |= STR_ORDERED;
 }
 
-/*
- * cmp_str:
- *     Compare two strings in the file
- */
-char *
-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;
-}
-
 int
 cmp_str(vp1, vp2)
        const void *vp1, *vp2;
@@ -470,3 +481,23 @@
                sp[i] = tmp;
        }
 }
+
+/*
+ * fwrite_be_offt:
+ *     Write out the off paramater as a 64 bit big endian number
+ */
+
+void
+fwrite_be_offt(off, f)
+       off_t    off;
+       FILE    *f;
+{
+       int             i;
+       unsigned char   c[8];
+
+       for (i = 7; i >= 0; i--) {
+               c[i] = off & 0xff;
+               off >>= 8;
+       }
+       fwrite(c, sizeof(c), 1, f);
+}



Home | Main Index | Thread Index | Old Index