1 #include <getopt.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <fcntl.h>
5 
6 #include "c.h"
7 #include "nls.h"
8 #include "partx.h"
9 #include "strutils.h"
10 
usage(void)11 static void __attribute__((__noreturn__)) usage(void)
12 {
13 	FILE *out = stdout;
14 	fputs(USAGE_HEADER, out);
15 	fprintf(out, _(" %s <disk device> <partition number>\n"),
16 		program_invocation_short_name);
17 
18 	fputs(USAGE_SEPARATOR, out);
19 	fputs(_("Tell the kernel to forget about a specified partition.\n"), out);
20 
21 	fputs(USAGE_OPTIONS, out);
22 	printf(USAGE_HELP_OPTIONS(16));
23 	printf(USAGE_MAN_TAIL("delpart(8)"));
24 	exit(EXIT_SUCCESS);
25 }
26 
main(int argc,char ** argv)27 int main(int argc, char **argv)
28 {
29 	int c, fd;
30 
31 	static const struct option longopts[] = {
32 		{"help",    no_argument, NULL, 'h'},
33 		{"version", no_argument, NULL, 'V'},
34 		{NULL, 0, NULL, 0},
35 	};
36 
37 	setlocale(LC_ALL, "");
38 	bindtextdomain(PACKAGE, LOCALEDIR);
39 	textdomain(PACKAGE);
40 
41 	while ((c = getopt_long(argc, argv, "Vh", longopts, NULL)) != -1)
42 		switch (c) {
43 		case 'V':
44 			print_version(EXIT_SUCCESS);
45 		case 'h':
46 			usage();
47 		default:
48 			errtryhelp(EXIT_FAILURE);
49 		}
50 
51 	if (argc != 3) {
52 		warnx(_("not enough arguments"));
53 		errtryhelp(EXIT_FAILURE);
54 	}
55 
56 
57 	if ((fd = open(argv[1], O_RDONLY)) < 0)
58 		err(EXIT_FAILURE, _("cannot open %s"), argv[1]);
59 
60 	if (partx_del_partition(fd,
61 			strtou32_or_err(argv[2], _("invalid partition number argument"))))
62 		err(EXIT_FAILURE, _("failed to remove partition"));
63 
64 	return EXIT_SUCCESS;
65 }
66