diff options
Diffstat (limited to 'src/truncate.c')
-rw-r--r-- | src/truncate.c | 24 |
1 files changed, 14 insertions, 10 deletions
diff --git a/src/truncate.c b/src/truncate.c index e37ab380..8349cb61 100644 --- a/src/truncate.c +++ b/src/truncate.c @@ -1,5 +1,5 @@ /* truncate -- truncate or extend the length of files. - Copyright (C) 2008-2012 Free Software Foundation, Inc. + Copyright (C) 2008-2013 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 @@ -102,11 +102,10 @@ A FILE argument that does not exist is created.\n\ If a FILE is larger than the specified size, the extra data is lost.\n\ If a FILE is shorter, it is extended and the extended part (hole)\n\ reads as zero bytes.\n\ -\n\ -"), stdout); - fputs (_("\ -Mandatory arguments to long options are mandatory for short options too.\n\ "), stdout); + + emit_mandatory_arg_note (); + fputs (_("\ -c, --no-create do not create any files\n\ "), stdout); @@ -244,7 +243,6 @@ main (int argc, char **argv) off_t size IF_LINT ( = 0); off_t rsize = -1; rel_mode_t rel_mode = rm_abs; - mode_t omode; int c, fd = -1, oflags; char const *fname; @@ -371,8 +369,15 @@ main (int argc, char **argv) if (0 <= ref_fd) { off_t file_end = lseek (ref_fd, 0, SEEK_END); - if (0 <= file_end && close (ref_fd) == 0) + int saved_errno = errno; + close (ref_fd); /* ignore failure */ + if (0 <= file_end) file_size = file_end; + else + { + /* restore, in case close clobbered it. */ + errno = saved_errno; + } } } if (file_size < 0) @@ -385,11 +390,10 @@ main (int argc, char **argv) } oflags = O_WRONLY | (no_create ? 0 : O_CREAT) | O_NONBLOCK; - omode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; while ((fname = *argv++) != NULL) { - if ((fd = open (fname, oflags, omode)) == -1) + if ((fd = open (fname, oflags, MODE_RW_UGO)) == -1) { /* 'truncate -s0 -c no-such-file' shouldn't gen error 'truncate -s0 no-such-dir/file' should gen ENOENT error @@ -410,7 +414,7 @@ main (int argc, char **argv) errors |= !do_ftruncate (fd, fname, size, rsize, rel_mode); if (close (fd) != 0) { - error (0, errno, _("closing %s"), quote (fname)); + error (0, errno, _("failed to close %s"), quote (fname)); errors = true; } } |