]> git.ipfire.org Git - thirdparty/util-linux.git/blob - disk-utils/delpart.c
hexdump: check blocksize when display data
[thirdparty/util-linux.git] / disk-utils / delpart.c
1 /*
2 * SPDX-License-Identifier: GPL-2.0-or-later
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * Copyright (C) 2012-2023 Karel Zak <kzak@redhat.com>
10 */
11 #include <getopt.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <fcntl.h>
15
16 #include "c.h"
17 #include "nls.h"
18 #include "partx.h"
19 #include "strutils.h"
20
21 static void __attribute__((__noreturn__)) usage(void)
22 {
23 FILE *out = stdout;
24 fputs(USAGE_HEADER, out);
25 fprintf(out, _(" %s <disk device> <partition number>\n"),
26 program_invocation_short_name);
27
28 fputs(USAGE_SEPARATOR, out);
29 fputs(_("Tell the kernel to forget about a specified partition.\n"), out);
30
31 fputs(USAGE_OPTIONS, out);
32 fprintf(out, USAGE_HELP_OPTIONS(16));
33 fprintf(out, USAGE_MAN_TAIL("delpart(8)"));
34 exit(EXIT_SUCCESS);
35 }
36
37 int main(int argc, char **argv)
38 {
39 int c, fd;
40
41 static const struct option longopts[] = {
42 {"help", no_argument, NULL, 'h'},
43 {"version", no_argument, NULL, 'V'},
44 {NULL, 0, NULL, 0},
45 };
46
47 setlocale(LC_ALL, "");
48 bindtextdomain(PACKAGE, LOCALEDIR);
49 textdomain(PACKAGE);
50
51 while ((c = getopt_long(argc, argv, "Vh", longopts, NULL)) != -1)
52 switch (c) {
53 case 'V':
54 print_version(EXIT_SUCCESS);
55 case 'h':
56 usage();
57 default:
58 errtryhelp(EXIT_FAILURE);
59 }
60
61 if (argc != 3) {
62 warnx(_("not enough arguments"));
63 errtryhelp(EXIT_FAILURE);
64 }
65
66
67 if ((fd = open(argv[1], O_RDONLY)) < 0)
68 err(EXIT_FAILURE, _("cannot open %s"), argv[1]);
69
70 if (partx_del_partition(fd,
71 strtou32_or_err(argv[2], _("invalid partition number argument"))))
72 err(EXIT_FAILURE, _("failed to remove partition"));
73
74 return EXIT_SUCCESS;
75 }