diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..0dc2c5d --- /dev/null +++ b/.clang-format @@ -0,0 +1,12 @@ +--- +BasedOnStyle: Microsoft +CompactNamespaces: 'false' +FixNamespaceComments: 'false' +NamespaceIndentation: All +AllowShortBlocksOnASingleLine: 'true' +AllowShortCaseLabelsOnASingleLine: 'true' +AllowShortFunctionsOnASingleLine: None +AllowShortIfStatementsOnASingleLine: Always +AllowShortLambdasOnASingleLine: All +AllowShortLoopsOnASingleLine: 'true' +PointerAlignment: Left \ No newline at end of file diff --git a/src/tar.c b/src/tar.c index c0f5b0c..b833203 100644 --- a/src/tar.c +++ b/src/tar.c @@ -1,6 +1,6 @@ -#include -#include "minitar.h" #include "tar.h" +#include "minitar.h" +#include int minitar_read_header(struct minitar* mp, struct tar_header* hdr); int minitar_validate_header(struct tar_header* hdr); @@ -11,9 +11,13 @@ char* minitar_read_file(struct minitar_entry_metadata* metadata, struct minitar* struct minitar* minitar_open(const char* path) { FILE* fp = fopen(path, "rb"); // On some systems, this might be necessary to read the file properly. - if(!fp) return NULL; + if (!fp) return NULL; struct minitar* mp = malloc(sizeof(struct minitar)); - if(!mp) { fclose(fp); return NULL; } + if (!mp) + { + fclose(fp); + return NULL; + } mp->stream = fp; return mp; } @@ -22,7 +26,7 @@ int minitar_close(struct minitar* mp) { int rc = fclose(mp->stream); free(mp); - if(rc) return rc; + if (rc) return rc; return 0; } @@ -30,12 +34,12 @@ static struct minitar_entry* minitar_attempt_read_entry(struct minitar* mp, int* { struct minitar_entry entry; struct tar_header hdr; - if(!minitar_read_header(mp, &hdr)) + if (!minitar_read_header(mp, &hdr)) { *valid = 1; // we are at end-of-file return NULL; } - if(!minitar_validate_header(&hdr)) + if (!minitar_validate_header(&hdr)) { *valid = 0; return NULL; @@ -43,7 +47,7 @@ static struct minitar_entry* minitar_attempt_read_entry(struct minitar* mp, int* *valid = 1; minitar_parse_tar_header(&hdr, &entry.metadata); char* buf = minitar_read_file(&entry.metadata, mp); - if(!buf) return NULL; + if (!buf) return NULL; entry.ptr = buf; return minitar_dup_entry(&entry); } @@ -54,7 +58,7 @@ struct minitar_entry* minitar_read_entry(struct minitar* mp) struct minitar_entry* result; do { result = minitar_attempt_read_entry(mp, &valid); - } while(!valid); + } while (!valid); return result; } diff --git a/src/tar.h b/src/tar.h index e83652c..004ba77 100644 --- a/src/tar.h +++ b/src/tar.h @@ -1,24 +1,25 @@ #ifndef MINITAR_TAR_H #define MINITAR_TAR_H -struct tar_header { - char name[100]; - char mode[8]; - char uid[8]; - char gid[8]; - char size[12]; - char mtime[12]; - char chksum[8]; - char typeflag; - char linkname[100]; - char magic[6]; - char version[2]; - char uname[32]; - char gname[32]; - char devmajor[8]; - char devminor[8]; - char prefix[155]; - char padding[12]; // to make the structure 512 bytes +struct tar_header +{ + char name[100]; + char mode[8]; + char uid[8]; + char gid[8]; + char size[12]; + char mtime[12]; + char chksum[8]; + char typeflag; + char linkname[100]; + char magic[6]; + char version[2]; + char uname[32]; + char gname[32]; + char devmajor[8]; + char devminor[8]; + char prefix[155]; + char padding[12]; // to make the structure 512 bytes } __attribute__((packed)); #endif \ No newline at end of file diff --git a/src/util.c b/src/util.c index a6291a1..dd9747b 100644 --- a/src/util.c +++ b/src/util.c @@ -1,11 +1,11 @@ #define _POSIX_C_SOURCE 200809L +#include "minitar.h" +#include "tar.h" +#include #include #include #include -#include #include -#include "minitar.h" -#include "tar.h" noreturn void minitar_panic(const char* message) { @@ -15,11 +15,13 @@ noreturn void minitar_panic(const char* message) void minitar_parse_tar_header(struct tar_header* hdr, struct minitar_entry_metadata* metadata) { - if(!strlen(hdr->prefix)) + if (!strlen(hdr->prefix)) { strncpy(metadata->name, hdr->name, 100); metadata->name[100] = '\0'; - } else { + } + else + { strcpy(metadata->name, hdr->prefix); strcat(metadata->name, "/"); strncat(metadata->name, hdr->name, 100); @@ -31,38 +33,26 @@ void minitar_parse_tar_header(struct tar_header* hdr, struct minitar_entry_metad metadata->gid = (gid_t)strtoul(hdr->gid, NULL, 8); char* sizeptr = strndup(hdr->size, 12); - if(!sizeptr) minitar_panic("Failed to allocate memory to duplicate a tar header's size field"); + if (!sizeptr) minitar_panic("Failed to allocate memory to duplicate a tar header's size field"); metadata->size = (size_t)strtoull(sizeptr, NULL, 8); free(sizeptr); char* timeptr = strndup(hdr->mtime, 12); - if(!timeptr) minitar_panic("Failed to allocate memory to duplicate a tar header's mtime field"); + if (!timeptr) minitar_panic("Failed to allocate memory to duplicate a tar header's mtime field"); metadata->mtime = (time_t)strtol(timeptr, NULL, 8); free(timeptr); - switch(hdr->typeflag) + switch (hdr->typeflag) { - case '\0': - case '0': - metadata->type = MTAR_REGULAR; - break; - case '1': - minitar_panic("Links to other files within a tar archive are unsupported"); - case '2': - minitar_panic("Symbolic links are unsupported"); - case '3': - metadata->type = MTAR_CHRDEV; - break; - case '4': - metadata->type = MTAR_BLKDEV; - break; - case '5': - metadata->type = MTAR_DIRECTORY; - break; - case '6': - minitar_panic("FIFOs are unsupported"); - default: - minitar_panic("Unknown entry type in tar header"); + case '\0': + case '0': metadata->type = MTAR_REGULAR; break; + case '1': minitar_panic("Links to other files within a tar archive are unsupported"); + case '2': minitar_panic("Symbolic links are unsupported"); + case '3': metadata->type = MTAR_CHRDEV; break; + case '4': metadata->type = MTAR_BLKDEV; break; + case '5': metadata->type = MTAR_DIRECTORY; break; + case '6': minitar_panic("FIFOs are unsupported"); + default: minitar_panic("Unknown entry type in tar header"); } strcpy(metadata->uname, hdr->uname); @@ -77,14 +67,14 @@ int minitar_validate_header(struct tar_header* hdr) int minitar_read_header(struct minitar* mp, struct tar_header* hdr) { size_t rc = fread(hdr, sizeof *hdr, 1, mp->stream); - if(rc == 0 && feof(mp->stream)) return 0; + if (rc == 0 && feof(mp->stream)) return 0; return 1; } struct minitar_entry* minitar_dup_entry(struct minitar_entry* original) { struct minitar_entry* new = malloc(sizeof *original); - if(!new) return NULL; + if (!new) return NULL; memcpy(new, original, sizeof *new); return new; } @@ -92,14 +82,14 @@ struct minitar_entry* minitar_dup_entry(struct minitar_entry* original) char* minitar_read_file(struct minitar_entry_metadata* metadata, struct minitar* mp) { char* buf = malloc(metadata->size + 1); - if(!buf) return NULL; + if (!buf) return NULL; size_t nread = fread(buf, 1, metadata->size, mp->stream); - if(!nread) + if (!nread) { free(buf); - if(feof(mp->stream)) return NULL; - if(ferror(mp->stream)) minitar_panic("Error while reading file data from tar archive"); + if (feof(mp->stream)) return NULL; + if (ferror(mp->stream)) minitar_panic("Error while reading file data from tar archive"); __builtin_unreachable(); }