-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsplit.c
More file actions
56 lines (45 loc) · 1.25 KB
/
Copy pathsplit.c
File metadata and controls
56 lines (45 loc) · 1.25 KB
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
#include <stdio.h>
#include <math.h> // ceil()
#include <stdlib.h> // exit()
int main(int argc, char *argv[]) {
char part_filename[200];
FILE *ptr_source, *ptr_part;
size_t parts, i;
unsigned long int part_size, written_bytes;
int byte;
if (argc != 3) {
printf("Usage: %s <maximum part size in MiB> <file to split>\n", argv[0]);
exit(1);
}
if ((ptr_source = fopen(argv[2], "rb")) == NULL) {
perror("Could not open file to split");
exit(1);
}
fseek(ptr_source, 0, SEEK_END);
part_size = atol(argv[1]) * 1024 * 1024;
if (part_size == 0) {
printf("Invalid part size: %s\n", argv[1]);
exit(1);
}
parts = (size_t) ceil((double) ftell(ptr_source) / part_size);
rewind(ptr_source);
printf("File will be split into %u parts.\n", parts);
for (i = 0; i < parts; i++) {
printf("\nWriting part%02d... ", i);
sprintf(part_filename, "%s.part%02d", argv[2], i);
if ((ptr_part = fopen(part_filename, "wb")) == NULL) {
perror("Could not open part for writing");
exit(1);
}
for (written_bytes = 0; written_bytes < part_size &&
(byte = fgetc(ptr_source)) != EOF; written_bytes++)
{
fputc(byte, ptr_part);
}
printf("(%lu bytes)", written_bytes);
fclose(ptr_part);
}
printf("\n\nDone!\n");
fclose(ptr_source);
return 0;
}