MCPcopy Create free account
hub / github.com/ading2210/linuxpdf / main

Function main

tinyemu/splitimg.c:31–102  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

29#include <getopt.h>
30
31int main(int argc, char **argv)
32{
33 int blocksize, ret, i;
34 const char *infilename, *outpath;
35 FILE *f, *fo;
36 char buf1[1024];
37 uint8_t *buf;
38
39 if ((optind + 1) >= argc) {
40 printf("splitimg version " CONFIG_VERSION ", Copyright (c) 2011-2016 Fabrice Bellard\n"
41 "usage: splitimg infile outpath [blocksize]\n"
42 "Create a multi-file disk image for the RISCVEMU HTTP block device\n"
43 "\n"
44 "outpath must be a directory\n"
45 "blocksize is the block size in KB\n");
46 exit(1);
47 }
48
49 infilename = argv[optind++];
50 outpath = argv[optind++];
51 blocksize = 256;
52 if (optind < argc)
53 blocksize = strtol(argv[optind++], NULL, 0);
54
55 blocksize *= 1024;
56
57 buf = malloc(blocksize);
58
59 f = fopen(infilename, "rb");
60 if (!f) {
61 perror(infilename);
62 exit(1);
63 }
64 i = 0;
65 for(;;) {
66 ret = fread(buf, 1, blocksize, f);
67 if (ret < 0) {
68 perror("fread");
69 exit(1);
70 }
71 if (ret == 0)
72 break;
73 if (ret < blocksize) {
74 printf("warning: last block is not full\n");
75 memset(buf + ret, 0, blocksize - ret);
76 }
77 snprintf(buf1, sizeof(buf1), "%s/blk%09u.bin", outpath, i);
78 fo = fopen(buf1, "wb");
79 if (!fo) {
80 perror(buf1);
81 exit(1);
82 }
83 fwrite(buf, 1, blocksize, fo);
84 fclose(fo);
85 i++;
86 }
87 fclose(f);
88 printf("%d blocks\n", i);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected