ubase

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

blkdiscard.c (809B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <sys/ioctl.h>
      3 #include <sys/mount.h>
      4 #include <sys/stat.h>
      5 #include <sys/types.h>
      6 
      7 #include <fcntl.h>
      8 #include <stdint.h>
      9 #include <unistd.h>
     10 
     11 #include "util.h"
     12 
     13 #define OFFSET_IDX	0
     14 #define LENGTH_IDX	1
     15 
     16 #define BLKDISCARD	_IO(0x12, 119)
     17 
     18 static void
     19 usage(void)
     20 {
     21 	eprintf("usage: %s device\n", argv0);
     22 }
     23 
     24 int
     25 main(int argc, char *argv[])
     26 {
     27 	uint64_t range[2];
     28 	int fd;
     29 
     30 	ARGBEGIN {
     31 	default:
     32 		usage();
     33 	} ARGEND
     34 
     35 	if (argc != 1)
     36 		usage();
     37 
     38 	fd = open(argv[0], O_RDWR);
     39 	if (fd < 0)
     40 		eprintf("open: %s:", argv[0]);
     41 	range[OFFSET_IDX] = 0;
     42 	if (ioctl(fd, BLKGETSIZE64, &range[LENGTH_IDX]) < 0)
     43 		eprintf("BLKGETSIZE64: %s:", argv[0]);
     44 	if (ioctl(fd, BLKDISCARD, range) < 0)
     45 		eprintf("BLKDISCARD: %s:", argv[0]);
     46 	close(fd);
     47 	return 0;
     48 }