-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmkgpt.c
85 lines (80 loc) · 1.87 KB
/
mkgpt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <string.h>
#include "device.h"
#include "gpt.h"
#include "mbr.h"
void usage()
{
printf("mkgpt [--force] [device]\n");
}
int main(int argc, char *argv[])
{
int force = 0;
char *devname = NULL;
struct device dev;
if (argc > 3)
{
printf("Too many arguments.\n");
usage();
return -1;
}
else if(argc == 1)
{
printf("Missing parameters.\n");
usage();
return -1;
}
else
{
for(int i = 1; i < argc; i++)
{
if(argv[i][0] == '-')
{
if(strcmp("--force", argv[i]) == 0)
{
if(argc == 2)
{
printf("You must specify a device.\n");
usage();
return -1;
}
force = 1;
}
else
{
printf("Unknown parameter %s\n", argv[i]);
usage();
return -1;
}
}
else
{
devname = argv[i];
}
}
}
if(open_device(devname, &dev) < 0)
{
printf("Unable to open device: %s\n", strerror(errno));
return -1;
}
if(!force)
{
if(has_partition_scheme(&dev) != PARTSCHEME_IS_NOTHING)
{
printf("An existing partition scheme was found on this device. If you really want to proceed and DESTROY YOUR PARTITIONS, use the --force parameter.\n");
close_device(&dev);
return -1;
}
}
if(initialize_gpt(&dev) == -1)
{
printf("Error initializing GPT: %s\n", strerror(errno));
close_device(&dev);
return -1;
}
close_device(&dev);
return 0;
}