bug-coreutils
[Top][All Lists]
Advanced

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

coreutils int fixes for rmdir, sum, tee, remove.h


From: Paul Eggert
Subject: coreutils int fixes for rmdir, sum, tee, remove.h
Date: Tue, 03 Aug 2004 13:08:47 -0700
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux)

The only bug fix here is for the bug where 'sum' dumps core if argc ==
0.  (Is this something we're supposed to worry about?  Other coreutils
apps do.)  Everything else is just a code cleanup.

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

        * src/remove.h: Add copyright notice.
        (struct rm_options): Use bool for booleans.
        * src/rmdir.c (empty_paths, ignore_fail_on_non_empty, verbose,
        errno_rmdir_non_empty, remove_parents, main): Likewise.
        * src/sum.c (have_read_stdin, bsd_sum_file, sysv_sum_file,
        main): Likewise.
        (main): Don't dump core if invoked with argv[0]==NULL.
        * src/tee.c (tee, append, ignore_interrupts, main, tee):
        Use bool for booleans.
        (tee): Use ssize_t for read returns.

Index: src/remove.h
===================================================================
RCS file: /home/eggert/coreutils/cu/src/remove.h,v
retrieving revision 1.11
diff -p -u -r1.11 remove.h
--- src/remove.h        29 May 2004 22:05:26 -0000      1.11
+++ src/remove.h        3 Aug 2004 20:04:25 -0000
@@ -1,3 +1,22 @@
+/* Remove directory entries.
+
+   Copyright (C) 1998, 2000, 2002, 2003, 2004 Free Software
+   Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software Foundation,
+   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+
 #ifndef REMOVE_H
 # define REMOVE_H
 
@@ -5,29 +24,29 @@
 
 struct rm_options
 {
-  /* If nonzero, ignore nonexistent files.  */
-  int ignore_missing_files;
+  /* If true, ignore nonexistent files.  */
+  bool ignore_missing_files;
 
-  /* If nonzero, query the user about whether to remove each file.  */
-  int interactive;
+  /* If true, query the user about whether to remove each file.  */
+  bool interactive;
 
-  /* If nonzero, recursively remove directories.  */
-  int recursive;
+  /* If true, recursively remove directories.  */
+  bool recursive;
 
   /* Pointer to the device and inode numbers of `/', when --recursive.
      Otherwise NULL.  */
   struct dev_ino *root_dev_ino;
 
   /* If nonzero, stdin is a tty.  */
-  int stdin_tty;
+  bool stdin_tty;
 
-  /* If nonzero, remove directories with unlink instead of rmdir, and don't
+  /* If true, remove directories with unlink instead of rmdir, and don't
      require a directory to be empty before trying to unlink it.
      Only works for the super-user.  */
-  int unlink_dirs;
+  bool unlink_dirs;
 
-  /* If nonzero, display the name of each file removed.  */
-  int verbose;
+  /* If true, display the name of each file removed.  */
+  bool verbose;
 
   /* If true, treat the failure by the rm function to restore the
      current working directory as a fatal error.  I.e., if this field
Index: src/rmdir.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/rmdir.c,v
retrieving revision 1.76
diff -p -u -r1.76 rmdir.c
--- src/rmdir.c 21 Jun 2004 15:03:35 -0000      1.76
+++ src/rmdir.c 19 Jul 2004 03:58:12 -0000
@@ -48,15 +48,15 @@
 /* The name this program was run with. */
 char *program_name;
 
-/* If nonzero, remove empty parent directories. */
-static int empty_paths;
+/* If true, remove empty parent directories.  */
+static bool empty_paths;
 
-/* If nonzero, don't treat failure to remove a nonempty directory
+/* If true, don't treat failure to remove a nonempty directory
    as an error.  */
-static int ignore_fail_on_non_empty;
+static bool ignore_fail_on_non_empty;
 
-/* If nonzero, output a diagnostic for every directory processed.  */
-static int verbose;
+/* If true, output a diagnostic for every directory processed.  */
+static bool verbose;
 
 /* For long options that have no equivalent short option, use a
    non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
@@ -80,10 +80,10 @@ static struct option const longopts[] =
   {NULL, 0, NULL, 0}
 };
 
-/* Return nonzero if ERROR_NUMBER is one of the values associated
+/* Return true if ERROR_NUMBER is one of the values associated
    with a failed rmdir due to non-empty target directory.  */
 
-static int
+static bool
 errno_rmdir_non_empty (int error_number)
 {
   return (error_number == RMDIR_ERRNO_NOT_EMPTY);
@@ -92,13 +92,13 @@ errno_rmdir_non_empty (int error_number)
 /* Remove any empty parent directories of PATH.
    If PATH contains slash characters, at least one of them
    (beginning with the rightmost) is replaced with a NUL byte.
-   Return zero if successful.  */
+   Return true if successful.  */
 
-static int
+static bool
 remove_parents (char *path)
 {
   char *slash;
-  int fail = 0;
+  bool ok = true;
 
   strip_trailing_slashes (path);
   while (1)
@@ -116,15 +116,15 @@ remove_parents (char *path)
       if (verbose)
        error (0, 0, _("removing directory, %s"), path);
 
-      fail = (rmdir (path) != 0);
+      ok = (rmdir (path) == 0);
 
-      if (fail)
+      if (!ok)
        {
          /* Stop quietly if --ignore-fail-on-non-empty. */
          if (ignore_fail_on_non_empty
              && errno_rmdir_non_empty (errno))
            {
-             fail = 0;
+             ok = true;
            }
          else
            {
@@ -133,7 +133,7 @@ remove_parents (char *path)
          break;
        }
     }
-  return fail;
+  return ok;
 }
 
 void
@@ -168,7 +168,7 @@ Remove the DIRECTORY(ies), if they are e
 int
 main (int argc, char **argv)
 {
-  int errors = 0;
+  bool ok = true;
   int optc;
 
   initialize_main (&argc, &argv);
@@ -179,7 +179,7 @@ main (int argc, char **argv)
 
   atexit (close_stdout);
 
-  empty_paths = 0;
+  empty_paths = false;
 
   while ((optc = getopt_long (argc, argv, "pv", longopts, NULL)) != -1)
     {
@@ -188,13 +188,13 @@ main (int argc, char **argv)
        case 0:                 /* Long option. */
          break;
        case 'p':
-         empty_paths = 1;
+         empty_paths = true;
          break;
        case IGNORE_FAIL_ON_NON_EMPTY_OPTION:
-         ignore_fail_on_non_empty = 1;
+         ignore_fail_on_non_empty = true;
          break;
        case 'v':
-         verbose = 1;
+         verbose = true;
          break;
        case_GETOPT_HELP_CHAR;
        case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
@@ -211,29 +211,26 @@ main (int argc, char **argv)
 
   for (; optind < argc; ++optind)
     {
-      int fail;
       char *dir = argv[optind];
 
       /* Give a diagnostic for each attempted removal if --verbose.  */
       if (verbose)
        error (0, 0, _("removing directory, %s"), dir);
 
-      fail = rmdir (dir);
-
-      if (fail)
+      if (rmdir (dir) != 0)
        {
          if (ignore_fail_on_non_empty
              && errno_rmdir_non_empty (errno))
            continue;
 
          error (0, errno, "%s", quote (dir));
-         errors = 1;
+         ok = false;
        }
       else if (empty_paths)
        {
-         errors |= remove_parents (dir);
+         ok &= remove_parents (dir);
        }
     }
 
-  exit (errors == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
+  exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
 }
Index: src/sum.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/sum.c,v
retrieving revision 1.73
diff -p -u -r1.73 sum.c
--- src/sum.c   21 Jan 2004 23:45:43 -0000      1.73
+++ src/sum.c   19 Jul 2004 16:28:47 -0000
@@ -37,8 +37,8 @@
 /* The name this program was run with. */
 char *program_name;
 
-/* Nonzero if any of the files read were the standard input. */
-static int have_read_stdin;
+/* True if any of the files read were the standard input. */
+static bool have_read_stdin;
 
 static struct option const longopts[] =
 {
@@ -80,10 +80,10 @@ With no FILE, or when FILE is -, read st
 /* Calculate and print the rotated checksum and the size in 1K blocks
    of file FILE, or of the standard input if FILE is "-".
    If PRINT_NAME is >1, print FILE next to the checksum and size.
-   The checksum varies depending on sizeof(int).
-   Return 0 if successful, -1 if an error occurs. */
+   The checksum varies depending on sizeof (int).
+   Return true if successful.  */
 
-static int
+static bool
 bsd_sum_file (const char *file, int print_name)
 {
   register FILE *fp;
@@ -95,7 +95,7 @@ bsd_sum_file (const char *file, int prin
   if (STREQ (file, "-"))
     {
       fp = stdin;
-      have_read_stdin = 1;
+      have_read_stdin = true;
     }
   else
     {
@@ -103,7 +103,7 @@ bsd_sum_file (const char *file, int prin
       if (fp == NULL)
        {
          error (0, errno, "%s", file);
-         return -1;
+         return false;
        }
     }
   /* Need binary I/O, or else byte counts and checksums are incorrect.  */
@@ -122,13 +122,13 @@ bsd_sum_file (const char *file, int prin
       error (0, errno, "%s", file);
       if (!STREQ (file, "-"))
        fclose (fp);
-      return -1;
+      return false;
     }
 
   if (!STREQ (file, "-") && fclose (fp) == EOF)
     {
       error (0, errno, "%s", file);
-      return -1;
+      return false;
     }
 
   printf ("%05d %5s", checksum,
@@ -137,15 +137,15 @@ bsd_sum_file (const char *file, int prin
     printf (" %s", file);
   putchar ('\n');
 
-  return 0;
+  return true;
 }
 
 /* Calculate and print the checksum and the size in 512-byte blocks
    of file FILE, or of the standard input if FILE is "-".
    If PRINT_NAME is >0, print FILE next to the checksum and size.
-   Return 0 if successful, -1 if an error occurs. */
+   Return true if successful.  */
 
-static int
+static bool
 sysv_sum_file (const char *file, int print_name)
 {
   int fd;
@@ -161,7 +161,7 @@ sysv_sum_file (const char *file, int pri
   if (STREQ (file, "-"))
     {
       fd = 0;
-      have_read_stdin = 1;
+      have_read_stdin = true;
     }
   else
     {
@@ -169,7 +169,7 @@ sysv_sum_file (const char *file, int pri
       if (fd == -1)
        {
          error (0, errno, "%s", file);
-         return -1;
+         return false;
        }
     }
   /* Need binary I/O, or else byte counts and checksums are incorrect.  */
@@ -188,7 +188,7 @@ sysv_sum_file (const char *file, int pri
          error (0, errno, "%s", file);
          if (!STREQ (file, "-"))
            close (fd);
-         return -1;
+         return false;
        }
 
       for (i = 0; i < bytes_read; i++)
@@ -199,7 +199,7 @@ sysv_sum_file (const char *file, int pri
   if (!STREQ (file, "-") && close (fd) == -1)
     {
       error (0, errno, "%s", file);
-      return -1;
+      return false;
     }
 
   r = (s & 0xffff) + ((s & 0xffffffff) >> 16);
@@ -211,16 +211,16 @@ sysv_sum_file (const char *file, int pri
     printf (" %s", file);
   putchar ('\n');
 
-  return 0;
+  return true;
 }
 
 int
 main (int argc, char **argv)
 {
-  int errors = 0;
+  bool ok;
   int optc;
   int files_given;
-  int (*sum_func) (const char *, int) = bsd_sum_file;
+  bool (*sum_func) (const char *, int) = bsd_sum_file;
 
   initialize_main (&argc, &argv);
   program_name = argv[0];
@@ -230,7 +230,7 @@ main (int argc, char **argv)
 
   atexit (close_stdout);
 
-  have_read_stdin = 0;
+  have_read_stdin = false;
 
   while ((optc = getopt_long (argc, argv, "rs", longopts, NULL)) != -1)
     {
@@ -257,17 +257,13 @@ main (int argc, char **argv)
     }
 
   files_given = argc - optind;
-  if (files_given == 0)
-    {
-      if ((*sum_func) ("-", files_given) < 0)
-       errors = 1;
-    }
+  if (files_given <= 0)
+    ok = sum_func ("-", files_given);
   else
-    for (; optind < argc; optind++)
-      if ((*sum_func) (argv[optind], files_given) < 0)
-       errors = 1;
+    for (ok = true; optind < argc; optind++)
+      ok &= sum_func (argv[optind], files_given);
 
   if (have_read_stdin && fclose (stdin) == EOF)
     error (EXIT_FAILURE, errno, "-");
-  exit (errors == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
+  exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
 }
Index: src/tee.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/tee.c,v
retrieving revision 1.73
diff -p -u -r1.73 tee.c
--- src/tee.c   21 Apr 2004 14:26:09 -0000      1.73
+++ src/tee.c   19 Jul 2004 23:53:08 -0000
@@ -31,13 +31,13 @@
 
 #define AUTHORS "Mike Parker", "Richard M. Stallman", "David MacKenzie"
 
-static int tee (int nfiles, const char **files);
+static bool tee (int nfiles, const char **files);
 
-/* If nonzero, append to output files rather than truncating them. */
-static int append;
+/* If true, append to output files rather than truncating them. */
+static bool append;
 
-/* If nonzero, ignore interrupts. */
-static int ignore_interrupts;
+/* If true, ignore interrupts. */
+static bool ignore_interrupts;
 
 /* The name that this program was run with. */
 char *program_name;
@@ -76,7 +76,7 @@ Copy standard input to each FILE, and al
 int
 main (int argc, char **argv)
 {
-  int errs;
+  bool ok;
   int optc;
 
   initialize_main (&argc, &argv);
@@ -87,8 +87,8 @@ main (int argc, char **argv)
 
   atexit (close_stdout);
 
-  append = 0;
-  ignore_interrupts = 0;
+  append = false;
+  ignore_interrupts = false;
 
   while ((optc = getopt_long (argc, argv, "ai", long_options, NULL)) != -1)
     {
@@ -98,11 +98,11 @@ main (int argc, char **argv)
          break;
 
        case 'a':
-         append = 1;
+         append = true;
          break;
 
        case 'i':
-         ignore_interrupts = 1;
+         ignore_interrupts = true;
          break;
 
        case_GETOPT_HELP_CHAR;
@@ -120,24 +120,25 @@ main (int argc, char **argv)
   /* Do *not* warn if tee is given no file arguments.
      POSIX requires that it work when given no arguments.  */
 
-  errs = tee (argc - optind, (const char **) &argv[optind]);
+  ok = tee (argc - optind, (const char **) &argv[optind]);
   if (close (STDIN_FILENO) != 0)
     error (EXIT_FAILURE, errno, _("standard input"));
 
-  exit (errs);
+  exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
 }
 
 /* Copy the standard input into each of the NFILES files in FILES
    and into the standard output.
-   Return 0 if successful, 1 if any errors occur. */
+   Return true if successful.  */
 
-static int
+static bool
 tee (int nfiles, const char **files)
 {
   FILE **descriptors;
   char buffer[BUFSIZ];
-  int bytes_read, i;
-  int ret = 0;
+  ssize_t bytes_read;
+  int i;
+  bool ok = true;
   const char *mode_string = (append ? "a" : "w");
 
   descriptors = xnmalloc (nfiles + 1, sizeof *descriptors);
@@ -161,7 +162,7 @@ tee (int nfiles, const char **files)
       if (descriptors[i] == NULL)
        {
          error (0, errno, "%s", files[i]);
-         ret = 1;
+         ok = false;
        }
       else
        {
@@ -188,14 +189,14 @@ tee (int nfiles, const char **files)
          {
            error (0, errno, "%s", files[i]);
            descriptors[i] = NULL;
-           ret = 1;
+           ok = false;
          }
     }
 
   if (bytes_read == -1)
     {
       error (0, errno, _("read error"));
-      ret = 1;
+      ok = false;
     }
 
   /* Close the files, but not standard output.  */
@@ -203,10 +204,10 @@ tee (int nfiles, const char **files)
     if (descriptors[i] && fclose (descriptors[i]) != 0)
       {
        error (0, errno, "%s", files[i]);
-       ret = 1;
+       ok = false;
       }
 
   free (descriptors);
 
-  return ret;
+  return ok;
 }




reply via email to

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