aboutsummaryrefslogtreecommitdiff
path: root/coreutils/truncate.c
diff options
context:
space:
mode:
authorAri Sundholm <ari@tuxera.com>2015-05-25 15:16:11 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2015-05-25 15:17:03 +0200
commitfc3e40ee8102c8b17fc98fbc93417bed4d878723 (patch)
treec81cf708a85dab603f85626988585a8b833d76aa /coreutils/truncate.c
parent7f4a49a96c4e6626f01a09a801d79c591ad0b1db (diff)
downloadbusybox-w32-fc3e40ee8102c8b17fc98fbc93417bed4d878723.tar.gz
busybox-w32-fc3e40ee8102c8b17fc98fbc93417bed4d878723.tar.bz2
busybox-w32-fc3e40ee8102c8b17fc98fbc93417bed4d878723.zip
truncate: do not die when a file doesn't exist and no-create flag is on
Additionally, open(2) failures do not make the program die immediately. This makes the behavior of the program match coreutils more closely. function old new delta truncate_main 161 221 +60 Signed-off-by: Ari Sundholm <ari@tuxera.com> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'coreutils/truncate.c')
-rw-r--r--coreutils/truncate.c20
1 files changed, 15 insertions, 5 deletions
diff --git a/coreutils/truncate.c b/coreutils/truncate.c
index 0e36daba3..e5fa656c8 100644
--- a/coreutils/truncate.c
+++ b/coreutils/truncate.c
@@ -64,12 +64,22 @@ int truncate_main(int argc UNUSED_PARAM, char **argv)
64 64
65 argv += optind; 65 argv += optind;
66 while (*argv) { 66 while (*argv) {
67 int fd = xopen(*argv, flags); 67 int fd = open(*argv, flags);
68 if (ftruncate(fd, size) == -1) { 68 if (fd < 0) {
69 bb_perror_msg("%s: ftruncate", *argv); 69 if (errno != ENOENT || !(opts & OPT_NOCREATE)) {
70 ret = EXIT_FAILURE; 70 bb_perror_msg("%s: open", *argv);
71 ret = EXIT_FAILURE;
72 }
73 /* else: ENOENT && OPT_NOCREATE:
74 * do not report error, exitcode is also 0.
75 */
76 } else {
77 if (ftruncate(fd, size) == -1) {
78 bb_perror_msg("%s: truncate", *argv);
79 ret = EXIT_FAILURE;
80 }
81 xclose(fd);
71 } 82 }
72 xclose(fd);
73 ++argv; 83 ++argv;
74 } 84 }
75 85