=/^G show total bytes now; document may not always work
[unix-history] / usr / src / usr.bin / more / os.c
index 24528bb..fd8da13 100644 (file)
@@ -3,15 +3,13 @@
  * Copyright (c) 1988 Regents of the University of California.
  * All rights reserved.
  *
  * Copyright (c) 1988 Regents of the University of California.
  * All rights reserved.
  *
- * This code is derived from software contributed to Berkeley by
- * Mark Nudleman.
- * 
  * Redistribution and use in source and binary forms are permitted
  * provided that the above copyright notice and this paragraph are
  * duplicated in all such forms and that any documentation,
  * advertising materials, and other materials related to such
  * distribution and use acknowledge that the software was developed
  * Redistribution and use in source and binary forms are permitted
  * provided that the above copyright notice and this paragraph are
  * duplicated in all such forms and that any documentation,
  * advertising materials, and other materials related to such
  * distribution and use acknowledge that the software was developed
- * by the University of California, Berkeley.  The name of the
+ * by Mark Nudleman and the University of California, Berkeley.  The
+ * name of Mark Nudleman or the
  * University may not be used to endorse or promote products derived
  * from this software without specific prior written permission.
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  * University may not be used to endorse or promote products derived
  * from this software without specific prior written permission.
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
@@ -20,7 +18,7 @@
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)os.c       5.3 (Berkeley) %G%";
+static char sccsid[] = "@(#)os.c       5.9 (Berkeley) %G%";
 #endif /* not lint */
 
 /*
 #endif /* not lint */
 
 /*
@@ -35,14 +33,15 @@ static char sccsid[] = "@(#)os.c    5.3 (Berkeley) %G%";
  * Unix features are present.
  */
 
  * Unix features are present.
  */
 
-#include <stdio.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/file.h>
 #include <signal.h>
 #include <setjmp.h>
 #include <signal.h>
 #include <setjmp.h>
-#include "less.h"
-
-char *getenv();
+#include <stdio.h>
+#include <less.h>
 
 
-public int reading;
+int reading;
 
 extern int screen_trashed;
 
 
 extern int screen_trashed;
 
@@ -52,13 +51,12 @@ static jmp_buf read_label;
  * Pass the specified command to a shell to be executed.
  * Like plain "system()", but handles resetting terminal modes, etc.
  */
  * Pass the specified command to a shell to be executed.
  * Like plain "system()", but handles resetting terminal modes, etc.
  */
-       public void
 lsystem(cmd)
        char *cmd;
 {
        int inp;
        char cmdbuf[256];
 lsystem(cmd)
        char *cmd;
 {
        int inp;
        char cmdbuf[256];
-       char *shell;
+       char *shell, *getenv();
 
        /*
         * Print the command which is to be executed,
 
        /*
         * Print the command which is to be executed,
@@ -92,9 +90,9 @@ lsystem(cmd)
         * even if less's standard input is coming from a pipe.
         */
        inp = dup(0);
         * even if less's standard input is coming from a pipe.
         */
        inp = dup(0);
-       close(0);
-       if (open("/dev/tty", 0) < 0)
-               dup(inp);
+       (void)close(0);
+       if (open("/dev/tty", O_RDONLY, 0) < 0)
+               (void)dup(inp);
 
        /*
         * Pass the command to the system to be executed.
 
        /*
         * Pass the command to the system to be executed.
@@ -108,21 +106,21 @@ lsystem(cmd)
                        cmd = shell;
                else
                {
                        cmd = shell;
                else
                {
-                       sprintf(cmdbuf, "%s -c \"%s\"", shell, cmd);
+                       (void)sprintf(cmdbuf, "%s -c \"%s\"", shell, cmd);
                        cmd = cmdbuf;
                }
        }
        if (*cmd == '\0')
                cmd = "sh";
 
                        cmd = cmdbuf;
                }
        }
        if (*cmd == '\0')
                cmd = "sh";
 
-       system(cmd);
+       (void)system(cmd);
 
        /*
         * Restore standard input, reset signals, raw mode, etc.
         */
 
        /*
         * Restore standard input, reset signals, raw mode, etc.
         */
-       close(0);
-       dup(inp);
-       close(inp);
+       (void)close(0);
+       (void)dup(inp);
+       (void)close(inp);
 
        init_signals(1);
        raw_mode(1);
 
        init_signals(1);
        raw_mode(1);
@@ -142,7 +140,6 @@ lsystem(cmd)
  * A call to intread() from a signal handler will interrupt
  * any pending iread().
  */
  * A call to intread() from a signal handler will interrupt
  * any pending iread().
  */
-       public int
 iread(fd, buf, len)
        int fd;
        char *buf;
 iread(fd, buf, len)
        int fd;
        char *buf;
@@ -165,44 +162,28 @@ iread(fd, buf, len)
        return (n);
 }
 
        return (n);
 }
 
-       public void
 intread()
 {
 intread()
 {
-#if SIGSETMASK
-       sigsetmask(0);
-#endif
+       (void)sigsetmask(0L);
        longjmp(read_label, 1);
 }
 
        longjmp(read_label, 1);
 }
 
-#if GET_TIME
-       public long
-get_time()
-{
-       long t;
-
-       time(&t);
-       return (t);
-}
-#endif
-
 /*
  * Expand a filename, substituting any environment variables, etc.
  * The implementation of this is necessarily very operating system
  * dependent.  This implementation is unabashedly only for Unix systems.
  */
 /*
  * Expand a filename, substituting any environment variables, etc.
  * The implementation of this is necessarily very operating system
  * dependent.  This implementation is unabashedly only for Unix systems.
  */
-#if GLOB
-
 FILE *popen();
 
 FILE *popen();
 
-       public char *
+char *
 glob(filename)
        char *filename;
 {
        FILE *f;
        char *p;
        int ch;
 glob(filename)
        char *filename;
 {
        FILE *f;
        char *p;
        int ch;
-       char *cmd;
-       static char buffer[FILENAME];
+       char *cmd, *malloc(), *getenv();
+       static char buffer[MAXPATHLEN];
 
        if (filename[0] == '#')
                return (filename);
 
        if (filename[0] == '#')
                return (filename);
@@ -217,19 +198,19 @@ glob(filename)
                /*
                 * Read the output of <echo filename>.
                 */
                /*
                 * Read the output of <echo filename>.
                 */
-               cmd = calloc(strlen(filename)+8, sizeof(char));
+               cmd = malloc((u_int)(strlen(filename)+8));
                if (cmd == NULL)
                        return (filename);
                if (cmd == NULL)
                        return (filename);
-               sprintf(cmd, "echo \"%s\"", filename);
+               (void)sprintf(cmd, "echo \"%s\"", filename);
        } else
        {
                /*
                 * Read the output of <$SHELL -c "echo filename">.
                 */
        } else
        {
                /*
                 * Read the output of <$SHELL -c "echo filename">.
                 */
-               cmd = calloc(strlen(p)+12);
+               cmd = malloc((u_int)(strlen(p)+12));
                if (cmd == NULL)
                        return (filename);
                if (cmd == NULL)
                        return (filename);
-               sprintf(cmd, "%s -c \"echo %s\"", p, filename);
+               (void)sprintf(cmd, "%s -c \"echo %s\"", p, filename);
        }
 
        if ((f = popen(cmd, "r")) == NULL)
        }
 
        if ((f = popen(cmd, "r")) == NULL)
@@ -243,87 +224,45 @@ glob(filename)
                *p = ch;
        }
        *p = '\0';
                *p = ch;
        }
        *p = '\0';
-       pclose(f);
-       return (buffer);
-}
-
-#else
-
-       public char *
-glob(filename)
-       char *filename;
-{
-       return (filename);
+       (void)pclose(f);
+       return(buffer);
 }
 
 }
 
-#endif
-
-
-/*
- * Returns NULL if the file can be opened and
- * is an ordinary file, otherwise an error message
- * (if it cannot be opened or is a directory, etc.)
- */
-
-#include <sys/types.h>
-#include <sys/stat.h>
-
-       public char *
+char *
 bad_file(filename, message, len)
 bad_file(filename, message, len)
-       char *filename;
-       char *message;
-       unsigned int len;
+       char *filename, *message;
+       u_int len;
 {
 {
+       extern int errno;
        struct stat statbuf;
        struct stat statbuf;
+       char *strcat(), *strerror();
 
 
-       if (stat(filename, &statbuf) < 0)
-               return (errno_message(filename, message, len));
-
-       if ((statbuf.st_mode & S_IFMT) == S_IFDIR)
-       {
-               static char is_dir[] = " is a directory";
-               strtcpy(message, filename, len-sizeof(is_dir)-1);
-               strcat(message, is_dir);
-               return (message);
+       if (stat(filename, &statbuf) < 0) {
+               (void)sprintf(message, "%s: %s", filename, strerror(errno));
+               return(message);
        }
        }
-       if ((statbuf.st_mode & S_IFMT) != S_IFREG)
-       {
-               static char not_reg[] = " is not a regular file";
-               strtcpy(message, filename, len-sizeof(not_reg)-1);
-               strcat(message, not_reg);
-               return (message);
+       if ((statbuf.st_mode & S_IFMT) == S_IFDIR) {
+               static char is_dir[] = " is a directory";
+
+               strtcpy(message, filename, (int)(len-sizeof(is_dir)-1));
+               (void)strcat(message, is_dir);
+               return(message);
        }
        }
-       return (NULL);
+       return((char *)NULL);
 }
 
 /*
 }
 
 /*
- * errno_message: Return an error message based on the value of "errno".
- * okreadfail: Return true if the previous failure of a read
- *     (on the input tty) should be considered ok.
+ * Copy a string, truncating to the specified length if necessary.
+ * Unlike strncpy(), the resulting string is guaranteed to be null-terminated.
  */
  */
-
-extern char *sys_errlist[];
-extern int sys_nerr;
-extern int errno;
-
-       public char *
-errno_message(filename, message, len)
-       char *filename;
-       char *message;
-       unsigned int len;
+static
+strtcpy(to, from, len)
+       char *to, *from;
+       int len;
 {
 {
-       char *p;
-       char msg[16];
+       char *strncpy();
 
 
-       if (errno < sys_nerr)
-               p = sys_errlist[errno];
-       else
-       {
-               sprintf(msg, "Error %d", errno);
-               p = msg;
-       }
-       strtcpy(message, filename, len-strlen(p)-3);
-       strcat(message, ": ");
-       strcat(message, p);
-       return (message);
+       (void)strncpy(to, from, (int)len);
+       to[len-1] = '\0';
 }
 }
+