-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.c
121 lines (98 loc) · 2.4 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
113
114
115
116
117
118
119
120
121
#include <stdlib.h> // size_t, strtol
#include <string.h> // strcasecmp
#include <limits.h> // LONG_MAX, LONG_MIN
#include <stdio.h>
#include "split.h"
byte buffer[BUFFER_SIZE];
#define USAGE(p) printf("USAGE:\n%s [-j|-s -sz <splitsize (gb|mb|kb)>] source dest\n",p)
#define MODE_UNDEFINED 0
#define MODE_SPLIT 1
#define MODE_JOIN 2
size_t strtosize(char *sizestring);
int main(int argc, char *argv[])
{
char *source = NULL;
char *destination = NULL;
char *strsplitsize = NULL;
size_t splitsize;
int mode = MODE_UNDEFINED;
// Parse the input array and retrive the values provided by the user.
char *c;
int i;
for(i = 1; i < argc;i++)
{
c = argv[i];
if (c[0] == '-'){
if (c[1] == 's'){
if (c[2] == 'z')
strsplitsize = argv[++i];
else
mode = MODE_SPLIT;
}
else if (c[1] == 'j')
mode = MODE_JOIN;
}
else{
if (source == NULL)
source = c;
else
destination = c;
}
}
// Print some debug information is program build with the DEBUG defination
// turned on.
#ifdef DEBUG
printf("Mode: %u\nSplit size: %s\nSource: %s\nDestination: %s\n",
mode,
strsplitsize,
source,
destination);
#endif
// Validate the user input and print USAGE if found wrong.
if (
(mode == MODE_UNDEFINED) ||
(mode == MODE_SPLIT && ((splitsize = strtosize(strsplitsize)) == 0)) ||
(source == NULL) ||
(destination == NULL)
){
USAGE(argv[0]);
exit(3);
}
// Perform split or join based on user input
int filecount;
if (mode == MODE_SPLIT){
filecount = split(source,destination,splitsize,1);
printf("%s splitted into %u files.\n",source,filecount);
}
else{
filecount = join(source,destination);
printf("Joined %u files into %s.\n",filecount,destination);
}
return 0;
}
size_t strtosize(char *sizestring)
{
if (sizestring == NULL)
return 0;
/* strtol:
If an underflow occurs, strtol() returns LONG_MIN.
If an overflow occurs, strtol() returns LONG_MAX.*/
char *rest_of_string;
size_t size = strtol(sizestring,&rest_of_string,10);
if (size == LONG_MIN || size == LONG_MAX)
return 0;
if (strcasecmp(rest_of_string,"GB") == 0)
size *= 1024 * 1024 * 1024;
else if (strcasecmp(rest_of_string,"MB") == 0)
size *= 1024 * 1024;
else if (strcasecmp(rest_of_string,"KB") == 0)
size *= 1024;
else if (strcmp(rest_of_string,"") == 0)
;
else
{
printf("Invalid unit '%s'\n",rest_of_string);
return 0;
}
return size;
}