diff options
author | quou <quou@disroot.org> | 2023-05-02 21:02:04 +1000 |
---|---|---|
committer | quou <quou@disroot.org> | 2023-05-02 21:02:04 +1000 |
commit | c1efdf9b0875f2a39488a86cd838947a24fab9fc (patch) | |
tree | b459d024fa99029758f8d2f8630470fe6060122e /pack.c |
Initial commit.
Diffstat (limited to 'pack.c')
-rw-r--r-- | pack.c | 67 |
1 files changed, 67 insertions, 0 deletions
@@ -0,0 +1,67 @@ +#include <stdio.h> +#include <string.h> + +#define buffer_size 256 + +typedef struct { + int size; + int offset; + char name[56]; +} Entry; + +int main(int argc, char** argv) { + int i, j, file_count, blob_offset, read, head_size; + char buffer[buffer_size]; + Entry entry; + FILE* outfile, * infile; + + outfile = fopen("pack", "wb"); + if (!outfile) { + fprintf(stderr, "Failed to open 'pack' for writing."); + return 1; + } + + file_count = argc - 1; + head_size = 8; + blob_offset = head_size + file_count * sizeof entry; + + fwrite(&file_count, 4, 1, outfile); + fwrite(&blob_offset, 4, 1, outfile); + + entry.offset = blob_offset; + + for (i = 0; i < file_count; i++) { + strcpy(entry.name, argv[i + 1]); + + infile = fopen(entry.name, "rb"); + if (!infile) { + fprintf( + stderr, + "Failed to open '%s' for reading.", + entry.name + ); + return 1; + } + + fseek(infile, 0, SEEK_END); + entry.size = ftell(infile); + rewind(infile); + + printf("Adding %s\n", entry.name); + + fseek(outfile, head_size + i * sizeof entry, SEEK_SET); + fwrite(&entry, 1, sizeof entry, outfile); + + fseek(outfile, entry.offset, SEEK_SET); + for (j = 0; j < entry.size; j += buffer_size) { + read = fread(buffer, 1, buffer_size, infile); + fwrite(buffer, 1, read, outfile); + } + + entry.offset += entry.size; + + fclose(infile); + } + + fclose(outfile); +} |