You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
959 B
49 lines
959 B
2 years ago
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <errno.h>
|
||
|
#include <string.h>
|
||
|
|
||
|
#define MAX(a,b) ((a)>(b)?(a):(b))
|
||
|
#define MIN(a,b) ((a)<(b)?(a):(b))
|
||
|
|
||
|
int main(int argc, char *argv[]) {
|
||
|
for (int i = 1; i < argc; i++) {
|
||
|
if (0==strcmp(argv[i], "-h") || 0==strcmp(argv[i], "--help")) {
|
||
|
fprintf(stderr, "binary split\nArguments: file start end\nStart and end are byte indexes. End is optional\n");
|
||
|
return 1;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (argc < 3) {
|
||
|
fprintf(stderr, "Bad argc\n");
|
||
|
return 1;
|
||
|
}
|
||
|
size_t pos = atol(argv[2]);
|
||
|
|
||
|
size_t take = argc==4?atol(argv[3]):0;
|
||
|
|
||
|
FILE * f = fopen(argv[1], "rb");
|
||
|
fseek(f, pos, SEEK_SET);
|
||
|
|
||
|
#define CHUNKSZ 8192
|
||
|
|
||
|
unsigned char chunk[CHUNKSZ];
|
||
|
|
||
|
while (1) {
|
||
|
size_t maxtake = take==0?CHUNKSZ : MIN(take, CHUNKSZ);
|
||
|
size_t len = fread(chunk, 1, maxtake, f);
|
||
|
if (errno != 0 || len == 0) {
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
fwrite(chunk, 1, len, stdout);
|
||
|
|
||
|
if (take > 0) {
|
||
|
take -= len;
|
||
|
if (take == 0) break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|