sort: fix hang with sort --compress
[platform/upstream/coreutils.git] / src / truncate.c
index f353067..faa83f1 100644 (file)
@@ -1,5 +1,5 @@
 /* truncate -- truncate or extend the length of files.
-   Copyright (C) 2008 Free Software Foundation, Inc.
+   Copyright (C) 2008-2010 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
 
    This is backwards compatible with the FreeBSD utility, but is more
    flexible wrt the size specifications and the use of long options,
-   to better fit the "GNU" environment.
-
-   Note if !defined(HAVE_FTRUNCATE) then the --skip-ftruncate configure flag
-   was specified or we're in a mingw environment. In these cases gnulib
-   emulation will be used and GNULIB_FTRUNCATE is defined. Note if emulation
-   can't even be provided ftruncate() will return EIO.  */
+   to better fit the "GNU" environment.  */
 
 #include <config.h>             /* sets _FILE_OFFSET_BITS=64 etc. */
 #include <stdio.h>
@@ -50,7 +45,8 @@ static bool block_mode;
 /* (-r) Reference file to use size from */
 static char const *ref_file;
 
-static struct option const longopts[] = {
+static struct option const longopts[] =
+{
   {"no-create", no_argument, NULL, 'c'},
   {"io-blocks", no_argument, NULL, 'o'},
   {"reference", required_argument, NULL, 'r'},
@@ -73,13 +69,24 @@ static int
 parse_len (char const *str, off_t *size)
 {
   enum strtol_error e;
-  /* OFF_T_MAX = INTMAX_MAX */
-  e = xstrtoimax (str, NULL, 10, size, "EgGkKmMPtTYZ0");
-  errno = (e == LONGINT_OVERFLOW) ? EOVERFLOW : 0;
-  return (e == LONGINT_OK) ? 0 : -1;
+  intmax_t tmp_size;
+  e = xstrtoimax (str, NULL, 10, &tmp_size, "EgGkKmMPtTYZ0");
+  if (e == LONGINT_OK
+      && !(OFF_T_MIN <= tmp_size && tmp_size <= OFF_T_MAX))
+    e = LONGINT_OVERFLOW;
+
+  if (e == LONGINT_OK)
+    {
+      errno = 0;
+      *size = tmp_size;
+      return 0;
+    }
+
+  errno = (e == LONGINT_OVERFLOW ? EOVERFLOW : 0);
+  return -1;
 }
 
-static void
+void
 usage (int status)
 {
   if (status != EXIT_SUCCESS)
@@ -105,80 +112,75 @@ Mandatory arguments to long options are mandatory for short options too.\n\
   -c, --no-create        do not create any files\n\
 "), stdout);
       fputs (_("\
-  -o, --io-blocks        Treat SIZE as number of IO blocks instead of bytes\n\
+  -o, --io-blocks        treat SIZE as number of IO blocks instead of bytes\n\
 "), stdout);
       fputs (_("\
-  -r, --reference=FILE   use this FILE's size\n\
-  -s, --size=SIZE        use this SIZE\n"), stdout);
+  -r, --reference=RFILE  base size on RFILE\n\
+  -s, --size=SIZE        set or adjust the file size by SIZE\n"), stdout);
       fputs (HELP_OPTION_DESCRIPTION, stdout);
       fputs (VERSION_OPTION_DESCRIPTION, stdout);
-      fputs (_("\n\
-SIZE is a number which may be followed by one of the following suffixes:\n\
-KB 1000, K 1024, MB 1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y.\n\
-"), stdout);
+      emit_size_note ();
       fputs (_("\n\
 SIZE may also be prefixed by one of the following modifying characters:\n\
 `+' extend by, `-' reduce by, `<' at most, `>' at least,\n\
 `/' round down to multiple of, `%' round up to multiple of.\n"), stdout);
-      fputs (_("\
-\n\
-Note that the -r and -s options are mutually exclusive.\n\
-"), stdout);
-      emit_bug_reporting_address ();
+      emit_ancillary_info ();
     }
   exit (status);
 }
 
-/* return 1 on error, 0 on success */
-static int
-do_ftruncate (int fd, char const *fname, off_t ssize, rel_mode_t rel_mode)
+/* return true on success, false on error.  */
+static bool
+do_ftruncate (int fd, char const *fname, off_t ssize, off_t rsize,
+              rel_mode_t rel_mode)
 {
   struct stat sb;
   off_t nsize;
 
-  if ((block_mode || rel_mode) && fstat (fd, &sb) != 0)
+  if ((block_mode || (rel_mode && rsize < 0)) && fstat (fd, &sb) != 0)
     {
       error (0, errno, _("cannot fstat %s"), quote (fname));
-      return 1;
+      return false;
     }
   if (block_mode)
     {
-      size_t const blksize = ST_BLKSIZE (sb);
+      off_t const blksize = ST_BLKSIZE (sb);
       if (ssize < OFF_T_MIN / blksize || ssize > OFF_T_MAX / blksize)
         {
           error (0, 0,
                  _("overflow in %" PRIdMAX
-                   " * %zu byte blocks for file %s"), ssize, blksize,
+                   " * %" PRIdMAX " byte blocks for file %s"),
+                 (intmax_t) ssize, (intmax_t) blksize,
                  quote (fname));
-          return 1;
+          return false;
         }
       ssize *= blksize;
     }
   if (rel_mode)
     {
-      uintmax_t const fsize = sb.st_size;
+      uintmax_t const fsize = rsize < 0 ? sb.st_size : rsize;
 
-      if (sb.st_size < 0)
+      if (rsize < 0) /* fstat used above to get size.  */
         {
-          /* Complain only for a regular file, a directory,
-             or a shared memory object, as POSIX 1003.1-2004 specifies
-             ftruncate's behavior only for these file types.  */
-          if (S_ISREG (sb.st_mode) || S_ISDIR (sb.st_mode)
-              || S_TYPEISSHM (&sb))
+          if (!S_ISREG (sb.st_mode) && !S_TYPEISSHM (&sb))
+            {
+              error (0, 0, _("cannot get the size of %s"), quote (fname));
+              return false;
+            }
+          if (sb.st_size < 0)
             {
-              /* overflow is the only reason I can think
-                 this would ever go negative for the above types */
+              /* Sanity check. Overflow is the only reason I can think
+                 this would ever go negative. */
               error (0, 0, _("%s has unusable, apparently negative size"),
                      quote (fname));
-              return 1;
+              return false;
             }
-          return 0;
         }
 
       if (rel_mode == rm_min)
-        nsize = MAX (fsize, ssize);
+        nsize = MAX (fsize, (uintmax_t) ssize);
       else if (rel_mode == rm_max)
-        nsize = MIN (fsize, ssize);
+        nsize = MIN (fsize, (uintmax_t) ssize);
       else if (rel_mode == rm_rdn)
         /* 0..ssize-1 -> 0 */
         nsize = (fsize / ssize) * ssize;
@@ -191,7 +193,7 @@ do_ftruncate (int fd, char const *fname, off_t ssize, rel_mode_t rel_mode)
             {
               error (0, 0, _("overflow rounding up size of file %s"),
                      quote (fname));
-              return 1;
+              return false;
             }
           nsize = overflow;
         }
@@ -201,7 +203,7 @@ do_ftruncate (int fd, char const *fname, off_t ssize, rel_mode_t rel_mode)
             {
               error (0, 0, _("overflow extending size of file %s"),
                      quote (fname));
-              return 1;
+              return false;
             }
           nsize = fsize + ssize;
         }
@@ -213,39 +215,25 @@ do_ftruncate (int fd, char const *fname, off_t ssize, rel_mode_t rel_mode)
 
   if (ftruncate (fd, nsize) == -1)      /* note updates mtime & ctime */
     {
-      /* Complain only when ftruncate fails on a regular file, a
-         directory, or a shared memory object, as POSIX 1003.1-2004
-         specifies ftruncate's behavior only for these file types.
-         For example, do not complain when Linux 2.4 ftruncate
-         fails on /dev/fd0.  */
-      int const ftruncate_errno = errno;
-      if (fstat (fd, &sb) != 0)
-        {
-          error (0, errno, _("cannot fstat %s"), quote (fname));
-          return 1;
-        }
-      else if (S_ISREG (sb.st_mode) || S_ISDIR (sb.st_mode)
-               || S_TYPEISSHM (&sb))
-        {
-          error (0, ftruncate_errno,
-                 _("truncating %s at %" PRIdMAX " bytes"), quote (fname),
-                 nsize);
-          return 1;
-        }
-      return 0;
+      error (0, errno,
+             _("failed to truncate %s at %" PRIdMAX " bytes"), quote (fname),
+             (intmax_t) nsize);
+      return false;
     }
 
-  return 0;
+  return true;
 }
 
 int
 main (int argc, char **argv)
 {
   bool got_size = false;
-  off_t size;
+  bool errors = false;
+  off_t size IF_LINT ( = 0);
+  off_t rsize = -1;
   rel_mode_t rel_mode = rm_abs;
   mode_t omode;
-  int c, errors = 0, fd = -1, oflags;
+  int c, fd = -1, oflags;
   char const *fname;
 
   initialize_main (&argc, &argv);
@@ -273,6 +261,9 @@ main (int argc, char **argv)
           break;
 
         case 's':
+          /* skip any whitespace */
+          while (isspace (to_uchar (*optarg)))
+            optarg++;
           switch (*optarg)
             {
             case '<':
@@ -292,6 +283,9 @@ main (int argc, char **argv)
               optarg++;
               break;
             }
+          /* skip any whitespace */
+          while (isspace (to_uchar (*optarg)))
+            optarg++;
           if (*optarg == '+' || *optarg == '-')
             {
               if (rel_mode)
@@ -324,9 +318,16 @@ main (int argc, char **argv)
   argc -= optind;
 
   /* must specify either size or reference file */
-  if ((ref_file && got_size) || (!ref_file && !got_size))
+  if (!ref_file && !got_size)
+    {
+      error (0, 0, _("you must specify either %s or %s"),
+             quote_n (0, "--size"), quote_n (1, "--reference"));
+      usage (EXIT_FAILURE);
+    }
+  /* must specify a relative size with a reference file */
+  if (ref_file && got_size && !rel_mode)
     {
-      error (0, 0, _("You must specify one of %s or %s"),
+      error (0, 0, _("you must specify a relative %s with %s"),
              quote_n (0, "--size"), quote_n (1, "--reference"));
       usage (EXIT_FAILURE);
     }
@@ -346,10 +347,17 @@ main (int argc, char **argv)
 
   if (ref_file)
     {
+      /* FIXME: Maybe support getting size of block devices.  */
       struct stat sb;
       if (stat (ref_file, &sb) != 0)
         error (EXIT_FAILURE, errno, _("cannot stat %s"), quote (ref_file));
-      size = sb.st_size;
+      if (!S_ISREG (sb.st_mode) && !S_TYPEISSHM (&sb))
+        error (EXIT_FAILURE, 0, _("cannot get the size of %s"),
+               quote (ref_file));
+      if (!got_size)
+        size = sb.st_size;
+      else
+        rsize = sb.st_size;
     }
 
   oflags = O_WRONLY | (no_create ? 0 : O_CREAT) | O_NONBLOCK;
@@ -365,20 +373,9 @@ main (int argc, char **argv)
              `truncate -s0 .` should gen EISDIR error */
           if (!(no_create && errno == ENOENT))
             {
-              int const open_errno = errno;
-              struct stat sb;
-              if (stat (fname, &sb) == 0)
-                {
-                  /* Complain only for a regular file, a directory,
-                     or a shared memory object, as POSIX 1003.1-2004 specifies
-                     ftruncate's behavior only for these file types.  */
-                  if (!S_ISREG (sb.st_mode) && !S_ISDIR (sb.st_mode)
-                      && !S_TYPEISSHM (&sb))
-                    continue;
-                }
-              error (0, open_errno, _("cannot open %s for writing"),
+              error (0, errno, _("cannot open %s for writing"),
                      quote (fname));
-              errors++;
+              errors = true;
             }
           continue;
         }
@@ -386,20 +383,14 @@ main (int argc, char **argv)
 
       if (fd != -1)
         {
-          errors += do_ftruncate (fd, fname, size, rel_mode);
+          errors |= !do_ftruncate (fd, fname, size, rsize, rel_mode);
           if (close (fd) != 0)
             {
               error (0, errno, _("closing %s"), quote (fname));
-              errors++;
+              errors = true;
             }
         }
     }
 
   return errors ? EXIT_FAILURE : EXIT_SUCCESS;
 }
-
-/*
- * Local variables:
- *  indent-tabs-mode: nil
- * End:
- */