commit 780fd613ebbdb59f183ec93778ee0c31d6bcede3
parent 923773f9d888a8c72efc93cbeb46fbd5992e2125
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Fri, 14 Feb 2014 14:35:01 +0100
truncate: match coreutils truncate behaviour
improvements:
- when truncate on a file failed proceed with the rest.
- when truncate on a file failed exit with EXIT_FAILURE.
Signed-off-by: Hiltjo Posthuma <hiltjo@codemadness.org>
Diffstat:
1 file changed, 15 insertions(+), 6 deletions(-)
diff --git a/truncate.c b/truncate.c
@@ -4,6 +4,8 @@
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
#include "util.h"
static void
@@ -16,7 +18,7 @@ int
main(int argc, char *argv[])
{
int cflag = 0, sflag = 0;
- int fd, i;
+ int fd, i, ret = EXIT_SUCCESS;
long size;
ARGBEGIN {
@@ -36,11 +38,18 @@ main(int argc, char *argv[])
for (i = 0; i < argc; i++) {
fd = open(argv[i], O_WRONLY | (cflag ? 0 : O_CREAT), 0644);
- if (fd < 0)
- eprintf("open %s:", argv[i]);
- if (ftruncate(fd, size) < 0)
- eprintf("ftruncate: %s:", argv[i]);
+ if (fd < 0) {
+ fprintf(stderr, "open: cannot open `%s' for writing: %s\n",
+ argv[i], strerror(errno));
+ ret = EXIT_FAILURE;
+ continue;
+ }
+ if (ftruncate(fd, size) < 0) {
+ fprintf(stderr, "truncate: cannot open `%s' for writing: %s\n",
+ argv[i], strerror(errno));
+ ret = EXIT_FAILURE;
+ }
close(fd);
}
- return EXIT_SUCCESS;
+ return ret;
}