2013-08-16 10:34:52 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <sys/stat.h>
|
2014-06-30 18:03:41 +00:00
|
|
|
|
2013-08-16 10:34:52 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2014-06-30 18:03:41 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2013-08-16 10:34:52 +00: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;
|
2014-10-02 22:45:25 +00:00
|
|
|
int fd, i, ret = 0;
|
2014-04-14 13:52:16 +00:00
|
|
|
long size = 0;
|
2013-08-16 10:34:52 +00:00
|
|
|
|
|
|
|
ARGBEGIN {
|
|
|
|
case 's':
|
|
|
|
sflag = 1;
|
|
|
|
size = estrtol(EARGF(usage()), 10);
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
cflag = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
2014-04-14 13:52:16 +00:00
|
|
|
if (argc < 1 || sflag == 0)
|
2013-08-16 10:34:52 +00:00
|
|
|
usage();
|
|
|
|
|
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
fd = open(argv[i], O_WRONLY | (cflag ? 0 : O_CREAT), 0644);
|
2014-02-14 13:35:01 +00:00
|
|
|
if (fd < 0) {
|
2014-06-30 21:42:56 +00:00
|
|
|
weprintf("open: cannot open `%s' for writing:", argv[i]);
|
2014-10-02 22:45:25 +00:00
|
|
|
ret = 1;
|
2014-02-14 13:35:01 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (ftruncate(fd, size) < 0) {
|
2014-06-30 21:42:56 +00:00
|
|
|
weprintf("ftruncate: cannot open `%s' for writing:", argv[i]);
|
2014-10-02 22:45:25 +00:00
|
|
|
ret = 1;
|
2014-02-14 13:35:01 +00:00
|
|
|
}
|
2013-08-16 10:34:52 +00:00
|
|
|
close(fd);
|
|
|
|
}
|
2014-02-14 13:35:01 +00:00
|
|
|
return ret;
|
2013-08-16 10:34:52 +00:00
|
|
|
}
|