-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
112 lines (93 loc) · 2.51 KB
/
main.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* Copyright (C) 2009-2012 Przemyslaw Pawelczyk <[email protected]>
*
* This software is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2.
* See <http://www.gnu.org/licenses/gpl-2.0.txt>.
*
* This software is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include "common.h"
#include "ui.h"
#include "vdi.h"
const char vidma_header_string[] =
"vidma - Virtual Disks Manipulator, " VIDMA_VERSION "\n"
"(C) 2009-2012 Przemyslaw Pawelczyk\n";
const char vidma_usage_string[] =
"Usage: %s INPUT_FILE [NEW_SIZE_IN_MB [OUTPUT_FILE]]\n"
"\n"
"USE AT YOUR OWN RISK! NO WARRANTY!\n";
ui_ops_t *ui = &ui_cli;
int litle_endian_test()
{
uint32_t endianness_test = 0x00000001;
return ((char*)&endianness_test)[0];
}
int main(int argc, char *argv[])
{
int fin, fout, result;
char *tmp;
vd_type_t *types[] = {
&vd_vdi,
NULL
};
vd_type_t **type = types;
uint32_t new_msize = 0;
if (!litle_endian_test()) {
fprintf(stderr, "This program requires little-endian machine. Sorry!");
exit(FAILURE);
}
if (argc == 1) {
puts(vidma_header_string);
printf(vidma_usage_string, argv[0]);
exit(SUCCESS);
} else if (argc == 3 || argc == 4) {
new_msize = strtoll(argv[2], &tmp, 10);
if (*argv[2] == '\0' || new_msize <= 0 || *tmp != '\0') {
fprintf(stderr, "Incorrect second argument!\n");
exit(FAILURE);
}
} else if (argc > 4) {
fprintf(stderr, "Too many arguments!\n");
exit(FAILURE);
}
fin = open(argv[1], O_RDONLY | O_BINARY);
if (fin < 0) {
perror(argv[1]);
exit(FAILURE);
}
for (; *type != NULL; type++) {
if ((*type)->ops.detect(fin) == SUCCESS) {
fprintf(stderr, "Recognized file format:\n"
" %s (%s)\n\n", (*type)->name, (*type)->ext);
break;
}
}
if (*type == NULL) {
fprintf(stderr, "Unrecognized file format!\n");
exit(FAILURE);
}
fout = open(argc == 4 ? argv[3] : argv[1],
O_CREAT | O_WRONLY | O_BINARY,
S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
if (fout < 0) {
perror(argc == 4 ? argv[3] : argv[1]);
exit(FAILURE);
}
if (argc == 2) {
(*type)->ops.info(fin);
return 0;
}
result = (*type)->ops.resize(fin, fout, new_msize);
close(fout);
close(fin);
return result;
}