ubase

suckless linux base utils
git clone git://git.2f30.org/ubase.git
Log | Files | Refs | README | LICENSE

commit f885062b2241c9b008398c819cf42f3eba65ee1d
parent 9bbef8d06f1bf543099b23aae5d21aed4b73e1e0
Author: sin <sin@2f30.org>
Date:   Fri Aug 16 11:34:52 +0100

Add truncate(1)

Diffstat:
Makefile | 1+
truncate.c | 46++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 47 insertions(+), 0 deletions(-)
diff --git a/Makefile b/Makefile @@ -34,6 +34,7 @@ SRC = \ stat.c \ swapoff.c \ swapon.c \ + truncate.c \ umount.c \ unshare.c diff --git a/truncate.c b/truncate.c @@ -0,0 +1,46 @@ +/* See LICENSE file for copyright and license details. */ +#include <sys/stat.h> +#include <fcntl.h> +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#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; + long size; + + ARGBEGIN { + case 's': + sflag = 1; + size = estrtol(EARGF(usage()), 10); + break; + case 'c': + cflag = 1; + break; + default: + usage(); + } ARGEND; + + if (argc < 1 || !sflag) + usage(); + + 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]); + close(fd); + } + return 0; +}