ubase

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

commit 604b66ae8b4005d89eed1cbab45a64cb57e75390
parent 4f5837147a14e0fb8ec1a2a46a9c4045bdb75696
Author: sin <sin@2f30.org>
Date:   Mon,  2 Jul 2018 14:03:49 +0100

Add blkdiscard(8)

Diffstat:
MMakefile | 1+
Ablkdiscard.8 | 12++++++++++++
Ablkdiscard.c | 48++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 61 insertions(+), 0 deletions(-)

diff --git a/Makefile b/Makefile @@ -37,6 +37,7 @@ LIBUTILSRC = \ LIB = $(LIBUTIL) BIN = \ + blkdiscard \ chvt \ clear \ ctrlaltdel \ diff --git a/blkdiscard.8 b/blkdiscard.8 @@ -0,0 +1,12 @@ +.Dd July 2, 2018 +.Dt BLKDISCARD 8 +.Os ubase +.Sh NAME +.Nm blkdiscard +.Nd discard sectors on a device +.Sh SYNOPSIS +.Nm +.Ar device +.Sh DESCRIPTION +.Nm +is used to discard all device sectors on solid-state devices. diff --git a/blkdiscard.c b/blkdiscard.c @@ -0,0 +1,48 @@ +/* See LICENSE file for copyright and license details. */ +#include <sys/ioctl.h> +#include <sys/mount.h> +#include <sys/stat.h> +#include <sys/types.h> + +#include <fcntl.h> +#include <stdint.h> +#include <unistd.h> + +#include "util.h" + +#define OFFSET_IDX 0 +#define LENGTH_IDX 1 + +#define BLKDISCARD _IO(0x12, 119) + +static void +usage(void) +{ + eprintf("usage: %s device\n", argv0); +} + +int +main(int argc, char *argv[]) +{ + uint64_t range[2]; + int fd; + + ARGBEGIN { + default: + usage(); + } ARGEND + + if (argc != 1) + usage(); + + fd = open(argv[0], O_RDWR); + if (fd < 0) + eprintf("open: %s:", argv[0]); + range[OFFSET_IDX] = 0; + if (ioctl(fd, BLKGETSIZE64, &range[LENGTH_IDX]) < 0) + eprintf("BLKGETSIZE64: %s:", argv[0]); + if (ioctl(fd, BLKDISCARD, range) < 0) + eprintf("BLKDISCARD: %s:", argv[0]); + close(fd); + return 0; +}