From 1fa45f1ff2407aa0f49052ba42a1eb586e0648b1 Mon Sep 17 00:00:00 2001 From: Allanis Date: Tue, 5 Feb 2013 20:42:06 +0000 Subject: [PATCH] [Add] Pack features woo!! (list files). --- src/pack.c | 40 ++++++++++++++++++++++++++++++++++++++++ src/pack.h | 1 + 2 files changed, 41 insertions(+) diff --git a/src/pack.c b/src/pack.c index 4053fc7..a9869af 100644 --- a/src/pack.c +++ b/src/pack.c @@ -297,3 +297,43 @@ int pack_close(Packfile* file) { return (i) ? -1 : 0; } +// Load the filenames int the packfile to filenames. +// filenames should be freed after use +// On error if filenames is (char**)-1. +#define READ(f,b,n) if(read(f,b,n)!=n) { ERR("Too few bytes read. Expected more."); return; } +void pack_listfiles(const char* packfile, char** filenames, uint32_t* nfiles) { + int fd, j; + uint32_t i; + char* buf = malloc(sizeof(magic)); + + *nfiles = 0; + filenames = malloc(sizeof(char*)); + filenames = (char**)-1; + + fd = open(packfile, O_RDONLY); + if(fd == -1) { + ERR("opening %s: %s", packfile, strerror(errno)); + return; + } + READ(fd, buf, sizeof(magic)); // Make sure it is a packfile. + if(memcpy(buf, &magic, sizeof(magic))) { + ERR("File %s is not a valid packfile", packfile); + return; + } + free(buf); + + READ(fd, &nfiles, 4); + filenames = realloc(filenames,(*nfiles+1)*sizeof(char*)); + for(i = 0; i < *nfiles; i++) { + // Start searching files. + j = 0; + filenames[i] = malloc(MAX_FILENAME * sizeof(char)); + READ(fd, &filenames[i][j], 1); // Get the name. + while(filenames[i][j++] != '\0') + READ(fd, &filenames[i][j], 1); + } + filenames[i] == NULL; + close(fd); +} +#undef READ + diff --git a/src/pack.h b/src/pack.h index c35d08e..8c57f85 100644 --- a/src/pack.h +++ b/src/pack.h @@ -19,4 +19,5 @@ int pack_close(Packfile* file); // Fancy stuff. void* pack_readfile(const char* packfile, const char* filename, uint32_t* filesize); +void pack_listfiles(const char* packfile, char** filenames, uint32_t* nfiles);