bug-coreutils
[Top][All Lists]
Advanced

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

coreutils int patches for nohup, paste, pathchk, printenv


From: Paul Eggert
Subject: coreutils int patches for nohup, paste, pathchk, printenv
Date: Tue, 03 Aug 2004 08:36:29 -0700
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux)

I installed this mostly-just-a-cleanup patch.  There is one
theoretical bug fix in printenv, to remove a possible integer overflow
if there are lots of arguments and lots of dups in the environment.

2004-08-03  Paul Eggert  <address@hidden>

        * src/nohup.c (main): Use bool for booleans.
        * src/paste.c (paste_parallel, paste_serial, main): Likewise.
        * src/pathchk.c (validate-path, main, portable_chars_only): Likewise.
        (portable_chars_only): Use to_uchar rather than a cast.
        * src/printenv.c (main): Use bool for booleans.
        Do not assume that the environ has at most one matching entry
        for each option (integer overflow was possible otherwise).

Index: src/nohup.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/nohup.c,v
retrieving revision 1.16
diff -p -u -r1.16 nohup.c
--- src/nohup.c 4 Jul 2004 18:01:04 -0000       1.16
+++ src/nohup.c 17 Jul 2004 17:05:47 -0000
@@ -73,7 +73,7 @@ main (int argc, char **argv)
 {
   int fd;
   int saved_stderr_fd = -1;
-  int stderr_isatty;
+  bool stderr_isatty;
 
   initialize_main (&argc, &argv);
   program_name = argv[0];
Index: src/paste.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/paste.c,v
retrieving revision 1.72
diff -p -u -r1.72 paste.c
--- src/paste.c 23 Jan 2004 09:26:24 -0000      1.72
+++ src/paste.c 18 Jul 2004 03:53:45 -0000
@@ -143,13 +143,13 @@ collapse_escapes (char const *strptr)
 }
 
 /* Perform column paste on the NFILES files named in FNAMPTR.
-   Return 0 if no errors, 1 if one or more files could not be
+   Return true if successful, false if one or more files could not be
    opened or read. */
 
-static int
+static bool
 paste_parallel (size_t nfiles, char **fnamptr)
 {
-  int errors = 0;              /* 1 if open or read errors occur. */
+  bool ok = true;
   /* If all files are just ready to be closed, or will be on this
      round, the string of delimiters must be preserved.
      delbuf[0] through delbuf[nfiles]
@@ -232,14 +232,14 @@ paste_parallel (size_t nfiles, char **fn
                  if (ferror (fileptr[i]))
                    {
                      error (0, errno, "%s", fnamptr[i]);
-                     errors = 1;
+                     ok = false;
                    }
                  if (fileptr[i] == stdin)
                    clearerr (fileptr[i]); /* Also clear EOF. */
                  else if (fclose (fileptr[i]) == EOF)
                    {
                      error (0, errno, "%s", fnamptr[i]);
-                     errors = 1;
+                     ok = false;
                    }
 
                  fileptr[i] = CLOSED;
@@ -298,17 +298,17 @@ paste_parallel (size_t nfiles, char **fn
     }
   free (fileptr);
   free (delbuf);
-  return errors;
+  return ok;
 }
 
 /* Perform serial paste on the NFILES files named in FNAMPTR.
-   Return 0 if no errors, 1 if one or more files could not be
+   Return true if no errors, false if one or more files could not be
    opened or read. */
 
-static int
+static bool
 paste_serial (size_t nfiles, char **fnamptr)
 {
-  int errors = 0;              /* 1 if open or read errors occur. */
+  bool ok = true;      /* false if open or read errors occur. */
   int charnew, charold; /* Current and previous char read. */
   char const *delimptr;        /* Current delimiter char. */
   FILE *fileptr;       /* Open for reading current file. */
@@ -327,7 +327,7 @@ paste_serial (size_t nfiles, char **fnam
          if (fileptr == NULL)
            {
              error (0, errno, "%s", *fnamptr);
-             errors = 1;
+             ok = false;
              continue;
            }
        }
@@ -372,17 +372,17 @@ paste_serial (size_t nfiles, char **fnam
       if (ferror (fileptr))
        {
          error (0, saved_errno, "%s", *fnamptr);
-         errors = 1;
+         ok = false;
        }
       if (fileptr == stdin)
        clearerr (fileptr);     /* Also clear EOF. */
       else if (fclose (fileptr) == EOF)
        {
          error (0, errno, "%s", *fnamptr);
-         errors = 1;
+         ok = false;
        }
     }
-  return errors;
+  return ok;
 }
 
 void
@@ -421,7 +421,8 @@ Mandatory arguments to long options are 
 int
 main (int argc, char **argv)
 {
-  int optc, exit_status;
+  int optc;
+  bool ok;
   char const *delim_arg = "\t";
 
   initialize_main (&argc, &argv);
@@ -468,13 +469,13 @@ main (int argc, char **argv)
   collapse_escapes (delim_arg);
 
   if (!serial_merge)
-    exit_status = paste_parallel (argc - optind, &argv[optind]);
+    ok = paste_parallel (argc - optind, &argv[optind]);
   else
-    exit_status = paste_serial (argc - optind, &argv[optind]);
+    ok = paste_serial (argc - optind, &argv[optind]);
 
   free (delims);
 
   if (have_read_stdin && fclose (stdin) == EOF)
     error (EXIT_FAILURE, errno, "-");
-  exit (exit_status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
+  exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
 }
Index: src/pathchk.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/pathchk.c,v
retrieving revision 1.79
diff -p -u -r1.79 pathchk.c
--- src/pathchk.c       25 Jul 2004 07:38:39 -0000      1.79
+++ src/pathchk.c       25 Jul 2004 22:37:08 -0000
@@ -100,7 +100,7 @@
 # define NAME_MAX_FOR(p) NAME_MAX
 #endif
 
-static int validate_path (char *path, int portability);
+static bool validate_path (char *path, bool portability);
 
 /* The name this program was run with. */
 char *program_name;
@@ -154,8 +154,8 @@ Diagnose unportable constructs in NAME.\
 int
 main (int argc, char **argv)
 {
-  int exit_status = 0;
-  int check_portability = 0;
+  bool ok = true;
+  bool check_portability = false;
   int optc;
 
   initialize_main (&argc, &argv);
@@ -177,7 +177,7 @@ main (int argc, char **argv)
          break;
 
        case 'p':
-         check_portability = 1;
+         check_portability = true;
          break;
 
        default:
@@ -192,9 +192,9 @@ main (int argc, char **argv)
     }
 
   for (; optind < argc; ++optind)
-    exit_status |= validate_path (argv[optind], check_portability);
+    ok &= validate_path (argv[optind], check_portability);
 
-  exit (exit_status);
+  exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
 }
 
 /* Each element is nonzero if the corresponding ASCII character is
@@ -220,21 +220,21 @@ static char const portable_chars[256] =
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
 };
 
-/* If PATH contains only portable characters, return 1, else 0.  */
+/* If PATH contains only portable characters, return true, else false.  */
 
-static int
+static bool
 portable_chars_only (const char *path)
 {
   const char *p;
 
   for (p = path; *p; ++p)
-    if (portable_chars[(unsigned char) *p] == 0)
+    if (portable_chars[to_uchar (*p)] == 0)
       {
        error (0, 0, _("path `%s' contains nonportable character `%c'"),
               path, *p);
-       return 0;
+       return false;
       }
-  return 1;
+  return true;
 }
 
 /* Return 1 if PATH is a usable leading directory, 0 if not,
@@ -273,7 +273,7 @@ dir_ok (const char *path)
    strlen (PATH) <= PATH_MAX
    && strlen (each-existing-directory-in-PATH) <= NAME_MAX
 
-   If PORTABILITY is nonzero, compare against _POSIX_PATH_MAX and
+   If PORTABILITY is true, compare against _POSIX_PATH_MAX and
    _POSIX_NAME_MAX instead, and make sure that PATH contains no
    characters not in the POSIX portable filename character set, which
    consists of A-Z, a-z, 0-9, ., _, -.
@@ -281,10 +281,10 @@ dir_ok (const char *path)
    Make sure that all leading directories along PATH that exist have
    `x' permission.
 
-   Return 0 if all of these tests are successful, 1 if any fail. */
+   Return true if all of these tests are successful, false if any fail.  */
 
-static int
-validate_path (char *path, int portability)
+static bool
+validate_path (char *path, bool portability)
 {
   long int path_max;
   int last_elem;               /* Nonzero if checking last element of path. */
@@ -293,10 +293,10 @@ validate_path (char *path, int portabili
   char *parent;                        /* Last existing leading directory so 
far.  */
 
   if (portability && !portable_chars_only (path))
-    return 1;
+    return false;
 
   if (*path == '\0')
-    return 0;
+    return true;
 
   /* Figure out the parent of the first element in PATH.  */
   parent = xstrdup (*path == '/' ? "/" : ".");
@@ -329,7 +329,7 @@ validate_path (char *path, int portabili
          if (exists == 0)
            {
              free (parent);
-             return 1;
+             return false;
            }
        }
 
@@ -350,7 +350,7 @@ validate_path (char *path, int portabili
          error (0, 0, _("name `%s' has length %ld; exceeds limit of %ld"),
                 start, length, name_max);
          free (parent);
-         return 1;
+         return false;
        }
 
       if (last_elem)
@@ -374,9 +374,9 @@ validate_path (char *path, int portabili
   if (strlen (path) > (size_t) path_max)
     {
       error (0, 0, _("path `%s' has length %lu; exceeds limit of %ld"),
-            path, (unsigned long) strlen (path), path_max);
-      return 1;
+            path, (unsigned long int) strlen (path), path_max);
+      return false;
     }
 
-  return 0;
+  return true;
 }
Index: src/printenv.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/printenv.c,v
retrieving revision 1.50
diff -p -u -r1.50 printenv.c
--- src/printenv.c      22 Jan 2004 20:44:15 -0000      1.50
+++ src/printenv.c      18 Jul 2004 06:00:22 -0000
@@ -83,9 +83,8 @@ main (int argc, char **argv)
   char **env;
   char *ep, *ap;
   int i;
-  int matches = 0;
   int c;
-  int exit_status;
+  bool ok;
 
   initialize_main (&argc, &argv);
   program_name = argv[0];
@@ -115,12 +114,16 @@ main (int argc, char **argv)
     {
       for (env = environ; *env != NULL; ++env)
        puts (*env);
-      exit_status = EXIT_SUCCESS;
+      ok = true;
     }
   else
     {
+      int matches = 0;
+
       for (i = optind; i < argc; ++i)
        {
+         bool matched = false;
+
          for (env = environ; *env; ++env)
            {
              ep = *env;
@@ -130,14 +133,17 @@ main (int argc, char **argv)
                  if (*ep == '=' && *ap == '\0')
                    {
                      puts (ep + 1);
-                     ++matches;
+                     matched = true;
                      break;
                    }
                }
            }
+
+         matches += matched;
        }
-      exit_status = (matches != argc - optind);
+
+      ok = (matches == argc - optind);
     }
 
-  exit (exit_status);
+  exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
 }




reply via email to

[Prev in Thread] Current Thread [Next in Thread]