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>
This commit is contained in:
Hiltjo Posthuma 2014-02-14 14:35:01 +01:00 committed by sin
parent 923773f9d8
commit 780fd613eb
1 changed files with 15 additions and 6 deletions

View File

@ -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;
}