[hackers] [ubase] truncate: match coreutils truncate behaviour || Hiltjo Posthuma

From: <git_AT_suckless.org>
Date: Fri, 14 Feb 2014 15:11:13 +0100

commit 780fd613ebbdb59f183ec93778ee0c31d6bcede3
Author: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
Date: Fri Feb 14 14:35:01 2014 +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_AT_codemadness.org>

diff --git a/truncate.c b/truncate.c
index 56793e5..b34fd9a 100644
--- a/truncate.c
+++ b/truncate.c
_AT_@ -4,6 +4,8 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+#include <errno.h>
 #include "util.h"
 
 static void
_AT_@ -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 {
_AT_@ -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
",
+ argv[i], strerror(errno));
+ ret = EXIT_FAILURE;
+ continue;
+ }
+ if (ftruncate(fd, size) < 0) {
+ fprintf(stderr, "truncate: cannot open `%s' for writing: %s
",
+ argv[i], strerror(errno));
+ ret = EXIT_FAILURE;
+ }
                 close(fd);
         }
- return EXIT_SUCCESS;
+ return ret;
 }
Received on Fri Feb 14 2014 - 15:11:13 CET

This archive was generated by hypermail 2.3.0 : Fri Feb 14 2014 - 15:12:26 CET