tarw.h
Minimalist header-only library for generating TAR files
01 02 03 04 05 06 07 08 09 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
#include <stdlib.h> #include <stdio.h> #define TARW_IMPLEMENTATION #include "tarw.h" int main(void) { const char *output_path = "./test.tar"; FILE *output = fopen(output_path, "wb"); if (output == NULL) { fprintf(stderr, "ERROR: could not open \"%s\"\n", output_path); return EXIT_FAILURE; } TarWriter w = { .owner = "user", .group = "group", .owner_id = 1000, .group_id = 1000, }; if (!tarw_add_directory(&w, "test/", 0755)) goto not_enought_memmory; const char *msg = "Hello from C!\n"; if (!tarw_add_file(&w, "test/a", 0644, msg, strlen(msg))) goto not_enought_memmory; if (!tarw_write(w, output)) { fprintf(stderr, "ERROR: could write all bytes to \"%s\"\n", output_path); return EXIT_FAILURE; } return EXIT_SUCCESS; not_enought_memmory: fprintf(stderr, "ERROR: could not allocate enough memmory\n"); return EXIT_FAILURE; }