ignore & before array
[unix-history] / usr / src / old / more / more.c
index cdcfbdc..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.6 (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,13 +30,16 @@ static char sccsid[] = "@(#)more.c  5.6 (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 <sgtty.h>
 #include <setjmp.h>
 #include <sys/stat.h>
 #include <ctype.h>
 #include <signal.h>
 #include <errno.h>
 #include <sgtty.h>
 #include <setjmp.h>
 #include <sys/stat.h>
+#include <sys/file.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"
@@ -46,7 +55,7 @@ static char sccsid[] = "@(#)more.c    5.6 (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'
@@ -170,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);
     }
@@ -238,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
@@ -255,7 +267,7 @@ char *argv[];
                    if(clreol) cleareol();
                    printf("%s\n", fnames[fnum]);
                    if(clreol) cleareol();
                    if(clreol) cleareol();
                    printf("%s\n", fnames[fnum]);
                    if(clreol) cleareol();
-                   printf("::::::::::::::\n", fnames[fnum]);
+                   printf("::::::::::::::\n");
                    if (left > Lpp - 4)
                        left = Lpp - 4;
                }
                    if (left > Lpp - 4)
                        left = Lpp - 4;
                }
@@ -283,14 +295,18 @@ char *argv[];
 argscan(s)
 char *s;
 {
 argscan(s)
 char *s;
 {
-       for (dlines = 0; *s != '\0'; s++)
-       {
-               switch (*s)
-               {
+       int seen_num = 0;
+
+       while (*s != '\0') {
+               switch (*s) {
                  case '0': case '1': case '2':
                  case '3': case '4': case '5':
                  case '6': case '7': case '8':
                  case '9':
                  case '0': case '1': case '2':
                  case '3': case '4': case '5':
                  case '6': case '7': case '8':
                  case '9':
+                       if (!seen_num) {
+                               dlines = 0;
+                               seen_num = 1;
+                       }
                        dlines = dlines*10 + *s - '0';
                        break;
                  case 'd':
                        dlines = dlines*10 + *s - '0';
                        break;
                  case 'd':
@@ -315,6 +331,7 @@ char *s;
                        ul_opt = 0;
                        break;
                }
                        ul_opt = 0;
                        break;
                }
+               s++;
        }
 }
 
        }
 }
 
@@ -326,55 +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);
-    }
-    c = Getc(f);
-
-    /* Try to see whether it is an ASCII file */
-
-    switch ((c | *f->_ptr << 8) & 0177777) {
-    case 0405:
-    case 0407:
-    case 0410:
-    case 0411:
-    case 0413:
-    case 0177545:
-       printf("\n******** %s: Not a text file ********\n\n", fs);
-       fclose (f);
-       return (NULL);
-    default:
-       break;
-    }
-    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);
+}
+
+/*
+ * 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?
+ */
+static
+magic(f, fs)
+       FILE *f;
+       char *fs;
+{
+       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);
 }
 
 /*
 }
 
 /*
@@ -547,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')
@@ -566,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':
@@ -581,8 +607,8 @@ int args;
                default:
                        break;
                }
                default:
                        break;
                }
-               ++argp;
        }
        }
+       va_end(ap);
        return (ccount);
 
 }
        return (ccount);
 
 }
@@ -626,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;
@@ -969,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;
 
@@ -1015,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':
@@ -1344,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 ();
@@ -1360,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);
@@ -1558,9 +1589,10 @@ readch ()
        char ch;
        extern int errno;
 
        char ch;
        extern int errno;
 
+       errno = 0;
        if (read (2, &ch, 1) <= 0)
                if (errno != EINTR)
        if (read (2, &ch, 1) <= 0)
                if (errno != EINTR)
-                       exit(0);
+                       end_it();
                else
                        ch = otty.sg_kill;
        return (ch);
                else
                        ch = otty.sg_kill;
        return (ch);
@@ -1744,6 +1776,8 @@ set_tty ()
 
 reset_tty ()
 {
 
 reset_tty ()
 {
+    if (no_tty)
+       return;
     if (pstate) {
        tputs(ULexit, 1, putch);
        fflush(stdout);
     if (pstate) {
        tputs(ULexit, 1, putch);
        fflush(stdout);