]> git.cameronkatri.com Git - bsdgames-darwin.git/blobdiff - larn/io.c
Larn now builds with WARNS=4.
[bsdgames-darwin.git] / larn / io.c
index c7233271d0d62b47bfe9edde24f07f02b8701cc2..23ba9969b175f5b77c9d0dc952e561238a4f0310 100644 (file)
--- a/larn/io.c
+++ b/larn/io.c
@@ -1,4 +1,4 @@
-/*     $NetBSD: io.c,v 1.12 2001/09/24 13:22:29 wiz Exp $      */
+/*     $NetBSD: io.c,v 1.19 2008/01/28 05:38:54 dholland Exp $ */
 
 /*
  * io.c                         Larn is copyrighted 1986 by Noah Morgan.
@@ -27,7 +27,7 @@
  * FILE INPUT ROUTINES
  * 
  * long lgetc()                                read one character from input buffer
- * long lrint()                                read one integer from input buffer
+ * long larn_lrint()                   read one integer from input buffer
  * lrfill(address,number)              put input bytes into a buffer char
  * *lgetw()                            get a whitespace ended word from
  * input char *lgetl()                         get a \n or EOF ended line
  */
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: io.c,v 1.12 2001/09/24 13:22:29 wiz Exp $");
+__RCSID("$NetBSD: io.c,v 1.19 2008/01/28 05:38:54 dholland Exp $");
 #endif /* not lint */
 
 #include "header.h"
 #include "extern.h"
 #include <string.h>
 #include <unistd.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <termcap.h>
 #include <fcntl.h>
@@ -116,11 +117,7 @@ static char     saveeof, saveeol;
 #endif /* not TERMIO or TERMIOS */
 
 #ifndef NOVARARGS      /* if we have varargs */
-#ifdef __STDC__
 #include <stdarg.h>
-#else
-#include <varargs.h>
-#endif
 #else  /* NOVARARGS */ /* if we don't have varargs */
 typedef char   *va_list;
 #define va_dcl int va_alist;
@@ -130,8 +127,8 @@ typedef char   *va_list;
 #endif /* NOVARARGS */
 
 #define LINBUFSIZE 128 /* size of the lgetw() and lgetl() buffer */
-int             lfd;   /* output file numbers */
-int             fd;    /* input file numbers */
+int             io_outfd; /* output file numbers */
+int             io_infd; /* input file numbers */
 static struct sgttyb ttx;/* storage for the tty modes */
 static int      ipoint = MAXIBUF, iepoint = MAXIBUF;   /* input buffering
                                                         * pointers    */
@@ -213,7 +210,7 @@ newgame()
        long  *p, *pe;
        for (p = c, pe = c + 100; p < pe; *p++ = 0);
        time(&initialtime);
-       srand(initialtime);
+       seedrand(initialtime);
        lcreat((char *) 0);     /* open buffering for output to terminal */
 }
 
@@ -230,144 +227,21 @@ newgame()
  *             are done beforehand if needed.
  *     Returns nothing of value.
  */
-#ifdef lint
-/* VARARGS */
-lprintf(str)
-       char           *str;
-{
-       char           *str2;
-       str2 = str;
-       str = str2;             /* to make lint happy */
-}
-/* VARARGS */
-sprintf(str)
-       char           *str;
-{
-       char           *str2;
-       str2 = str;
-       str = str2;             /* to make lint happy */
-}
-#else  /* lint */
-/* VARARGS */
-#ifdef __STDC__
-void lprintf(const char *fmt, ...)
-#else
 void
-lprintf(va_alist)
-va_dcl
-#endif
+lprintf(const char *fmt, ...)
 {
-       va_list         ap;     /* pointer for variable argument list */
-       char  *outb, *tmpb;
-       long   wide, left, cont, n;     /* data for lprintf      */
-       char            db[12]; /* %d buffer in lprintf  */
-#ifndef __STDC__
-       char  *fmt;
-
-       va_start(ap);           /* initialize the var args pointer */
-       fmt = va_arg(ap, char *);       /* pointer to format string */
-#else
+       va_list ap;
+       char buf[BUFBIG/2];
+
        va_start(ap, fmt);
-#endif
+       vsnprintf(buf, sizeof(buf), fmt, ap);
+       va_end(ap);
+
        if (lpnt >= lpend)
                lflush();
-       outb = lpnt;
-       for (;;) {
-               while (*fmt != '%')
-                       if (*fmt)
-                               *outb++ = *fmt++;
-                       else {
-                               lpnt = outb;
-                               va_end(ap);
-                               return;
-                       }
-               wide = 0;
-               left = 1;
-               cont = 1;
-               while (cont)
-                       switch (*(++fmt)) {
-                       case 'd':
-                               n = va_arg(ap, long);
-                               if (n < 0) {
-                                       n = -n;
-                                       *outb++ = '-';
-                                       if (wide)
-                                               --wide;
-                               }
-                               tmpb = db + 11;
-                               *tmpb = (char) (n % 10 + '0');
-                               while (n > 9)
-                                       *(--tmpb) = (char) ((n /= 10) % 10 + '0');
-                               if (wide == 0)
-                                       while (tmpb < db + 12)
-                                               *outb++ = *tmpb++;
-                               else {
-                                       wide -= db - tmpb + 12;
-                                       if (left)
-                                               while (wide-- > 0)
-                                                       *outb++ = ' ';
-                                       while (tmpb < db + 12)
-                                               *outb++ = *tmpb++;
-                                       if (left == 0)
-                                               while (wide-- > 0)
-                                                       *outb++ = ' ';
-                               }
-                               cont = 0;
-                               break;
-
-                       case 's':
-                               tmpb = va_arg(ap, char *);
-                               if (wide == 0) {
-                                       while ((*outb++ = *tmpb++) != '\0')
-                                               continue;
-                                       --outb;
-                               } else {
-                                       n = wide - strlen(tmpb);
-                                       if (left)
-                                               while (n-- > 0)
-                                                       *outb++ = ' ';
-                                       while ((*outb++ = *tmpb++) != '\0')
-                                               continue;
-                                       --outb;
-                                       if (left == 0)
-                                               while (n-- > 0)
-                                                       *outb++ = ' ';
-                               }
-                               cont = 0;
-                               break;
 
-                       case 'c':
-                               *outb++ = va_arg(ap, int);
-                               cont = 0;
-                               break;
-
-                       case '0':
-                       case '1':
-                       case '2':
-                       case '3':
-                       case '4':
-                       case '5':
-                       case '6':
-                       case '7':
-                       case '8':
-                       case '9':
-                               wide = 10 * wide + *fmt - '0';
-                               break;
-
-                       case '-':
-                               left = 0;
-                               break;
-
-                       default:
-                               *outb++ = *fmt;
-                               cont = 0;
-                               break;
-                       };
-               fmt++;
-       }
-       va_end(ap);
+       lprcat(buf);
 }
-#endif /* lint */
 
 /*
  *     lprint(long-integer)    send binary integer to output buffer
@@ -410,19 +284,21 @@ lwrite(buf, len)
        char  *buf;
        int             len;
 {
-       char  *str;
-       int    num2;
+       char *s;
+       u_char *t;
+       int num2;
+
        if (len > 399) {        /* don't copy data if can just write it */
 #ifdef EXTRA
                c[BYTESOUT] += len;
 #endif
 
 #ifndef VT100
-               for (str = buf; len > 0; --len)
-                       lprc(*str++);
+               for (s = buf; len > 0; --len)
+                       lprc(*s++);
 #else  /* VT100 */
                lflush();
-               write(lfd, buf, len);
+               write(io_outfd, buf, len);
 #endif /* VT100 */
        } else
                while (len) {
@@ -432,11 +308,11 @@ lwrite(buf, len)
                                                         * output buffer         */
                        if (num2 > len)
                                num2 = len;
-                       str = lpnt;
+                       t = lpnt;
                        len -= num2;
                        while (num2--)
-                               *str++ = *buf++;        /* copy in the bytes */
-                       lpnt = str;
+                               *t++ = *buf++;  /* copy in the bytes */
+                       lpnt = t;
                }
 }
 
@@ -453,7 +329,7 @@ lgetc()
                return (inbuffer[ipoint++]);
        if (iepoint != MAXIBUF)
                return (0);
-       if ((i = read(fd, inbuffer, MAXIBUF)) <= 0) {
+       if ((i = read(io_infd, inbuffer, MAXIBUF)) <= 0) {
                if (i != 0)
                        write(1, "error reading from input file\n", 30);
                iepoint = ipoint = 0;
@@ -478,7 +354,7 @@ lgetc()
  *     Returns the int read
  */
 long 
-lrint()
+larn_lrint()
 {
        unsigned long i;
        i = 255 & lgetc();
@@ -501,12 +377,13 @@ lrfill(adr, num)
        char  *adr;
        int             num;
 {
-       char  *pnt;
+       u_char  *pnt;
        int    num2;
+
        while (num) {
                if (iepoint == ipoint) {
                        if (num > 5) {  /* fast way */
-                               if (read(fd, adr, num) != num)
+                               if (read(io_infd, adr, num) != num)
                                        write(2, "error reading from input file\n", 30);
                                num = 0;
                        } else {
@@ -588,18 +465,19 @@ int
 lcreat(str)
        char *str;
 {
+       lflush();
        lpnt = lpbuf;
        lpend = lpbuf + BUFBIG;
        if (str == NULL)
-               return (lfd = 1);
-       if ((lfd = creat(str, 0644)) < 0) {
-               lfd = 1;
+               return (io_outfd = 1);
+       if ((io_outfd = creat(str, 0644)) < 0) {
+               io_outfd = 1;
                lprintf("error creating file <%s>: %s\n", str,
                        strerror(errno));
                lflush();
                return (-1);
        }
-       return (lfd);
+       return (io_outfd);
 }
 
 /*
@@ -615,14 +493,14 @@ lopen(str)
 {
        ipoint = iepoint = MAXIBUF;
        if (str == NULL)
-               return (fd = 0);
-       if ((fd = open(str, 0)) < 0) {
+               return (io_infd = 0);
+       if ((io_infd = open(str, O_RDONLY)) < 0) {
                lwclose();
-               lfd = 1;
+               io_outfd = 1;
                lpnt = lpbuf;
                return (-1);
        }
-       return (fd);
+       return (io_infd);
 }
 
 /*
@@ -639,13 +517,13 @@ lappend(str)
        lpnt = lpbuf;
        lpend = lpbuf + BUFBIG;
        if (str == NULL)
-               return (lfd = 1);
-       if ((lfd = open(str, 2)) < 0) {
-               lfd = 1;
+               return (io_outfd = 1);
+       if ((io_outfd = open(str, 2)) < 0) {
+               io_outfd = 1;
                return (-1);
        }
-       lseek(lfd, 0, 2);       /* seek to end of file */
-       return (lfd);
+       lseek(io_outfd, 0, SEEK_END);   /* seek to end of file */
+       return (io_outfd);
 }
 
 /*
@@ -656,8 +534,10 @@ lappend(str)
 void
 lrclose()
 {
-       if (fd > 0)
-               close(fd);
+       if (io_infd > 0) {
+               close(io_infd);
+               io_infd = 0;
+       }
 }
 
 /*
@@ -669,8 +549,10 @@ void
 lwclose()
 {
        lflush();
-       if (lfd > 2)
-               close(lfd);
+       if (io_outfd > 2) {
+               close(io_outfd);
+               io_outfd = 1;
+       }
 }
 
 /*
@@ -678,10 +560,9 @@ lwclose()
  *                             avoids calls to lprintf (time consuming)
  */
 void
-lprcat(str)
-       char  *str;
+lprcat(const char *str)
 {
-       char  *str2;
+       u_char  *str2;
        if (lpnt >= lpend)
                lflush();
        str2 = lpnt;
@@ -764,7 +645,6 @@ cursors()
  * obvious meanings.
  */
 
-static char     *cap;
 struct tinfo   *info;
 char           *CM, *CE, *CD, *CL, *SO, *SE, *AL, *DL; /* Termcap capabilities */
 static char    *outbuf = 0;    /* translated output buffer */
@@ -775,10 +655,8 @@ static char    *outbuf = 0;        /* translated output buffer */
 void
 init_term()
 {
-       char           *capptr;
        char           *term;
 
-       cap = NULL;
        switch (t_getent(&info, term = getenv("TERM"))) {
        case -1:
                write(2, "Cannot open termcap file.\n", 26);
@@ -790,16 +668,16 @@ init_term()
                exit(1);
        };
 
-       CM = t_agetstr(info, "cm", &cap, &capptr);      /* Cursor motion */
-       CE = t_agetstr(info, "ce", &cap, &capptr);      /* Clear to eoln */
-       CL = t_agetstr(info, "cl", &cap, &capptr);      /* Clear screen */
+       CM = t_agetstr(info, "cm");     /* Cursor motion */
+       CE = t_agetstr(info, "ce");     /* Clear to eoln */
+       CL = t_agetstr(info, "cl");     /* Clear screen */
 
        /* OPTIONAL */
-       AL = t_agetstr(info, "al", &cap, &capptr);      /* Insert line */
-       DL = t_agetstr(info, "dl", &cap, &capptr);      /* Delete line */
-       SO = t_agetstr(info, "so", &cap, &capptr);      /* Begin standout mode */
-       SE = t_agetstr(info, "se", &cap, &capptr);      /* End standout mode */
-       CD = t_agetstr(info, "cd", &cap, &capptr);      /* Clear to end of display */
+       AL = t_agetstr(info, "al");     /* Insert line */
+       DL = t_agetstr(info, "dl");     /* Delete line */
+       SO = t_agetstr(info, "so");     /* Begin standout mode */
+       SE = t_agetstr(info, "se");     /* End standout mode */
+       CD = t_agetstr(info, "cd");     /* Clear to end of display */
 
        if (!CM) {              /* can't find cursor motion entry */
                write(2, "Sorry, for a ", 13);
@@ -896,8 +774,7 @@ cl_dn(x, y)
  * standout(str)       Print the argument string in inverse video (standout mode).
  */
 void
-standout(str)
-       char  *str;
+standout(const char *str)
 {
 #ifdef VT100
        setbold();
@@ -946,7 +823,7 @@ lflush()
 #endif
                if (enable_scroll <= -1) {
                        flush_buf();
-                       if (write(lfd, lpbuf, lpoint) != lpoint)
+                       if (write(io_outfd, lpbuf, lpoint) != lpoint)
                                write(2, "error writing to output file\n", 29);
                        lpnt = lpbuf;   /* point back to beginning of buffer */
                        return;
@@ -1064,7 +941,7 @@ lflush()
 #ifdef EXTRA
                c[BYTESOUT] += lpoint;
 #endif
-               if (write(lfd, lpbuf, lpoint) != lpoint)
+               if (write(io_outfd, lpbuf, lpoint) != lpoint)
                        write(2, "error writing to output file\n", 29);
        }
        lpnt = lpbuf;           /* point back to beginning of buffer */
@@ -1077,10 +954,9 @@ static int      vindex = 0;
  * xputchar(ch)                Print one character in decoded output buffer.
  */
 int 
-xputchar(c)
-       int             c;
+xputchar(int ch)
 {
-       outbuf[vindex++] = c;
+       outbuf[vindex++] = ch;
        if (vindex >= BUFBIG)
                flush_buf();
        return (0);
@@ -1093,7 +969,7 @@ void
 flush_buf()
 {
        if (vindex)
-               write(lfd, outbuf, vindex);
+               write(io_outfd, outbuf, vindex);
        vindex = 0;
 }