ubase/truncate.c

58 lines
1.0 KiB
C
Raw Normal View History

2013-08-16 06:34:52 -04:00
/* See LICENSE file for copyright and license details. */
#include <sys/stat.h>
2014-06-30 14:03:41 -04:00
#include <errno.h>
2013-08-16 06:34:52 -04:00
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2014-06-30 14:03:41 -04:00
#include <unistd.h>
2013-08-16 06:34:52 -04:00
#include "util.h"
static void
usage(void)
{
eprintf("usage: %s [-c] -s size file...\n", argv0);
}
int
main(int argc, char *argv[])
{
int cflag = 0, sflag = 0;
int fd, i, ret = EXIT_SUCCESS;
long size = 0;
2013-08-16 06:34:52 -04:00
ARGBEGIN {
case 's':
sflag = 1;
size = estrtol(EARGF(usage()), 10);
break;
case 'c':
cflag = 1;
break;
default:
usage();
} ARGEND;
if (argc < 1 || sflag == 0)
2013-08-16 06:34:52 -04:00
usage();
for (i = 0; i < argc; i++) {
fd = open(argv[i], O_WRONLY | (cflag ? 0 : O_CREAT), 0644);
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;
}
2013-08-16 06:34:52 -04:00
close(fd);
}
return ret;
2013-08-16 06:34:52 -04:00
}