ignore & before array
[unix-history] / usr / src / old / more / more.c
index 0d8d671..f7b6689 100644 (file)
@@ -1,18 +1,24 @@
 /*
  * Copyright (c) 1980 Regents of the University of California.
 /*
  * Copyright (c) 1980 Regents of the University of California.
- * All rights reserved.  The Berkeley software License Agreement
- * specifies the terms and conditions for redistribution.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that this notice is preserved and that due credit is given
+ * to the University of California at Berkeley. The name of the University
+ * may not be used to endorse or promote products derived from this
+ * software without specific written prior permission. This software
+ * is provided ``as is'' without express or implied warranty.
  */
 
 #ifndef lint
 char copyright[] =
 "@(#) Copyright (c) 1980 Regents of the University of California.\n\
  All rights reserved.\n";
  */
 
 #ifndef lint
 char copyright[] =
 "@(#) Copyright (c) 1980 Regents of the University of California.\n\
  All rights reserved.\n";
-#endif not lint
+#endif /* not lint */
 
 #ifndef lint
 
 #ifndef lint
-static char sccsid[] = "@(#)more.c     5.13 (Berkeley) %G%";
-#endif not lint
+static char sccsid[] = "@(#)more.c     5.18 (Berkeley) %G%";
+#endif /* not lint */
 
 /*
 ** more.c - General purpose tty output filter and file perusal program
 
 /*
 ** more.c - General purpose tty output filter and file perusal program
@@ -24,7 +30,7 @@ static char sccsid[] = "@(#)more.c    5.13 (Berkeley) %G%";
 */
 
 #include <stdio.h>
 */
 
 #include <stdio.h>
-#include <sys/types.h>
+#include <sys/param.h>
 #include <ctype.h>
 #include <signal.h>
 #include <errno.h>
 #include <ctype.h>
 #include <signal.h>
 #include <errno.h>
@@ -32,7 +38,8 @@ static char sccsid[] = "@(#)more.c    5.13 (Berkeley) %G%";
 #include <setjmp.h>
 #include <sys/stat.h>
 #include <sys/file.h>
 #include <setjmp.h>
 #include <sys/stat.h>
 #include <sys/file.h>
-#include <sys/exec.h>
+#include <a.out.h>
+#include <varargs.h>
 
 #define HELPFILE       "/usr/lib/more.help"
 #define VI             "/usr/ucb/vi"
 
 #define HELPFILE       "/usr/lib/more.help"
 #define VI             "/usr/ucb/vi"
@@ -48,7 +55,7 @@ static char sccsid[] = "@(#)more.c    5.13 (Berkeley) %G%";
 
 #define TBUFSIZ        1024
 #define LINSIZ 256
 
 #define TBUFSIZ        1024
 #define LINSIZ 256
-#define ctrl(letter)   ('letter' & 077)
+#define ctrl(letter)   (letter & 077)
 #define RUBOUT '\177'
 #define ESC    '\033'
 #define QUIT   '\034'
 #define RUBOUT '\177'
 #define ESC    '\033'
 #define QUIT   '\034'
@@ -172,8 +179,11 @@ char *argv[];
     if (nfiles > 1)
        prnames++;
     if (!no_intty && nfiles == 0) {
     if (nfiles > 1)
        prnames++;
     if (!no_intty && nfiles == 0) {
-       fputs("Usage: ",stderr);
-       fputs(argv[0],stderr);
+       char *rindex();
+
+       p = rindex(argv[0], '/');
+       fputs("usage: ",stderr);
+       fputs(p ? p + 1 : argv[0],stderr);
        fputs(" [-dfln] [+linenum | +/pattern] name1 name2 ...\n",stderr);
        exit(1);
     }
        fputs(" [-dfln] [+linenum | +/pattern] name1 name2 ...\n",stderr);
        exit(1);
     }
@@ -240,7 +250,7 @@ char *argv[];
                left = command (fnames[fnum], f);
            }
            if (left != 0) {
                left = command (fnames[fnum], f);
            }
            if (left != 0) {
-               if ((noscroll || clearit) && (file_size != 0x7fffffffffffffffL))
+               if ((noscroll || clearit) && (file_size != LONG_MAX))
                    if (clreol)
                        home ();
                    else
                    if (clreol)
                        home ();
                    else
@@ -333,64 +343,65 @@ char *s;
 
 FILE *
 checkf (fs, clearfirst)
 
 FILE *
 checkf (fs, clearfirst)
-register char *fs;
-int *clearfirst;
+       register char *fs;
+       int *clearfirst;
 {
 {
-    struct stat stbuf;
-    register FILE *f;
-    char c;
+       struct stat stbuf;
+       register FILE *f;
+       char c;
 
 
-    if (stat (fs, &stbuf) == -1) {
-       fflush(stdout);
-       if (clreol)
-           cleareol ();
-       perror(fs);
-       return (NULL);
-    }
-    if ((stbuf.st_mode & S_IFMT) == S_IFDIR) {
-       printf("\n*** %s: directory ***\n\n", fs);
-       return (NULL);
-    }
-    if ((f=Fopen(fs, "r")) == NULL) {
-       fflush(stdout);
-       perror(fs);
-       return (NULL);
-    }
-    /* Try to see whether it is an ASCII file */
-    if (magic(f)) {
-       printf("\n******** %s: Not a text file ********\n\n", fs);
-       fclose (f);
-       return (NULL);
-    }
-    c = Getc(f);
-    if (c == '\f')
-       *clearfirst = 1;
-    else {
-       *clearfirst = 0;
+       if (stat (fs, &stbuf) == -1) {
+               (void)fflush(stdout);
+               if (clreol)
+                       cleareol ();
+               perror(fs);
+               return((FILE *)NULL);
+       }
+       if ((stbuf.st_mode & S_IFMT) == S_IFDIR) {
+               printf("\n*** %s: directory ***\n\n", fs);
+               return((FILE *)NULL);
+       }
+       if ((f = Fopen(fs, "r")) == NULL) {
+               (void)fflush(stdout);
+               perror(fs);
+               return((FILE *)NULL);
+       }
+       if (magic(f, fs))
+               return((FILE *)NULL);
+       c = Getc(f);
+       *clearfirst = c == '\f';
        Ungetc (c, f);
        Ungetc (c, f);
-    }
-    if ((file_size = stbuf.st_size) == 0)
-       file_size = 0x7fffffffffffffffL;
-    return (f);
+       if ((file_size = stbuf.st_size) == 0)
+               file_size = LONG_MAX;
+       return(f);
 }
 
 /*
 }
 
 /*
- * Check for file magic numbers. This code would best
- * be shared with the file(1) program or, perhaps, more
- * should not try and be so smart?
+ * magic --
+ *     check for file magic numbers.  This code would best be shared with
+ *     the file(1) program or, perhaps, more should not try and be so smart?
  */
  */
-magic(f)
-    FILE *f;
+static
+magic(f, fs)
+       FILE *f;
+       char *fs;
 {
 {
-    long magic;
-
-    magic = getw(f);
-    if (ftell(f) < sizeof magic)
-       rewind(f);                              /* reset file position */
-    else
-       fseek(f, -sizeof (magic), L_INCR);      /* reset file position */
-    return (magic == 0405 || magic == OMAGIC || magic == NMAGIC ||
-       magic == 0411 || magic == ZMAGIC || magic == 0177545);
+       struct exec ex;
+
+       if (fread(&ex, sizeof(ex), 1, f) == 1)
+               switch(ex.a_magic) {
+               case OMAGIC:
+               case NMAGIC:
+               case ZMAGIC:
+               case 0405:
+               case 0411:
+               case 0177545:
+                       printf("\n******** %s: Not a text file ********\n\n", fs);
+                       (void)fclose(f);
+                       return(1);
+               }
+       (void)fseek(f, 0L, L_SET);              /* rewind() not necessary */
+       return(0);
 }
 
 /*
 }
 
 /*
@@ -563,16 +574,16 @@ register FILE *f;
 
 /* Simplified printf function */
 
 
 /* Simplified printf function */
 
-printf (fmt, args)
+printf (fmt, va_alist)
 register char *fmt;
 register char *fmt;
-int args;
+va_dcl
 {
 {
-       register int *argp;
+       va_list ap;
        register char ch;
        register int ccount;
 
        ccount = 0;
        register char ch;
        register int ccount;
 
        ccount = 0;
-       argp = &args;
+       va_start(ap);
        while (*fmt) {
                while ((ch = *fmt++) != '%') {
                        if (ch == '\0')
        while (*fmt) {
                while ((ch = *fmt++) != '%') {
                        if (ch == '\0')
@@ -582,14 +593,13 @@ int args;
                }
                switch (*fmt++) {
                case 'd':
                }
                switch (*fmt++) {
                case 'd':
-                       ccount += printd (*argp);
+                       ccount += printd (va_arg(ap, int));
                        break;
                case 's':
                        break;
                case 's':
-                       ccount += pr ((char *)*argp);
+                       ccount += pr (va_arg(ap, char *));
                        break;
                case '%':
                        ccount++;
                        break;
                case '%':
                        ccount++;
-                       argp--;
                        putchar ('%');
                        break;
                case '0':
                        putchar ('%');
                        break;
                case '0':
@@ -597,8 +607,8 @@ int args;
                default:
                        break;
                }
                default:
                        break;
                }
-               ++argp;
        }
        }
+       va_end(ap);
        return (ccount);
 
 }
        return (ccount);
 
 }
@@ -642,7 +652,7 @@ Sprintf (n)
     *sptr++ = n % 10 + '0';
 }
 
     *sptr++ = n % 10 + '0';
 }
 
-static char bell = ctrl(G);
+static char bell = ctrl('G');
 
 strlen (s)
 char *s;
 
 strlen (s)
 char *s;
@@ -985,7 +995,7 @@ register FILE *f;
                done++;
            break;
        case 'b':
                done++;
            break;
        case 'b':
-       case ctrl(B):
+       case ctrl('B'):
            {
                register int initline;
 
            {
                register int initline;
 
@@ -1031,7 +1041,7 @@ register FILE *f;
            else if (comchar == 'z') dlines = nlines;
            ret (nlines);
        case 'd':
            else if (comchar == 'z') dlines = nlines;
            ret (nlines);
        case 'd':
-       case ctrl(D):
+       case ctrl('D'):
            if (nlines != 0) nscroll = nlines;
            ret (nscroll);
        case 'q':
            if (nlines != 0) nscroll = nlines;
            ret (nscroll);
        case 'q':
@@ -1360,12 +1370,15 @@ register int n;
     }
 }
 
     }
 }
 
-execute (filename, cmd, args)
+/*VARARGS2*/
+execute (filename, cmd, va_alist)
 char *filename;
 char *filename;
-char *cmd, *args;
+char *cmd;
+va_dcl
 {
        int id;
        int n;
 {
        int id;
        int n;
+       va_list argp;
 
        fflush (stdout);
        reset_tty ();
 
        fflush (stdout);
        reset_tty ();
@@ -1376,9 +1389,11 @@ char *cmd, *args;
                close(0);
                open("/dev/tty", 0);
            }
                close(0);
                open("/dev/tty", 0);
            }
-           execv (cmd, &args);
+           va_start(argp);
+           execv (cmd, argp);
            write (2, "exec failed\n", 12);
            exit (1);
            write (2, "exec failed\n", 12);
            exit (1);
+           va_end(argp);       /* balance {}'s for some UNIX's */
        }
        if (id > 0) {
            signal (SIGINT, SIG_IGN);
        }
        if (id > 0) {
            signal (SIGINT, SIG_IGN);