master
Ondřej Hruška 2 years ago
commit 776dd74325
  1. 4
      .gitignore
  2. 48
      main.c

4
.gitignore vendored

@ -0,0 +1,4 @@
.idea/
*.o
bsplit

@ -0,0 +1,48 @@
#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;
}
Loading…
Cancel
Save