]> git.ipfire.org Git - thirdparty/util-linux.git/blob - disk-utils/addpart.c
Merge branch 'meson-more-build-options' of https://github.com/jwillikers/util-linux
[thirdparty/util-linux.git] / disk-utils / addpart.c
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
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> <start> <length>\n"),
16 program_invocation_short_name);
17
18 fputs(USAGE_SEPARATOR, out);
19 fputs(_("Tell the kernel about the existence of a specified partition.\n"), out);
20
21 fputs(USAGE_OPTIONS, out);
22 printf(USAGE_HELP_OPTIONS(16));
23 printf(USAGE_MAN_TAIL("addpart(8)"));
24 exit(EXIT_SUCCESS);
25 }
26
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 != 5) {
52 warnx(_("not enough arguments"));
53 errtryhelp(EXIT_FAILURE);
54 }
55
56 if ((fd = open(argv[1], O_RDONLY)) < 0)
57 err(EXIT_FAILURE, _("cannot open %s"), argv[1]);
58
59 if (partx_add_partition(fd,
60 strtou32_or_err(argv[2], _("invalid partition number argument")),
61 strtou64_or_err(argv[3], _("invalid start argument")),
62 strtou64_or_err(argv[4], _("invalid length argument"))))
63 err(EXIT_FAILURE, _("failed to add partition"));
64
65 return EXIT_SUCCESS;
66 }