diff --git a/main.c b/main.c index f63897c..6dd2931 100644 --- a/main.c +++ b/main.c @@ -28,6 +28,9 @@ void disp_pldheader(struct PldHeader *x) { } bool read_pldheader(const char *buffer) { + if (buffer == NULL) { + return false; + } int32_t *n = (int32_t*)buffer; struct PldHeader *ph = (struct PldHeader*)malloc( sizeof(struct PldHeader) @@ -50,9 +53,8 @@ bool read_pldheader(const char *buffer) { return true; } -bool readpld(const char *fname) { +bool readpld(const char *fname, char *buf) { FILE *f = fopen(fname, "rb"); - char *buffer = NULL; unsigned int size = 0; // number of elements to buffer; unsigned int rcnt = 0; // number of char's read by fread(...) if (f == NULL) { @@ -63,31 +65,32 @@ bool readpld(const char *fname) { fseek(f, 0, SEEK_END); size = ftell(f); rewind(f); - buffer = (char*)malloc(sizeof(char) * size); - if (buffer == NULL) { + buf = (char*)malloc(sizeof(char) * size); + if (buf == NULL) { perror("Error 2: "); - free(buffer); + free(buf); return false; } - rcnt = fread(buffer, sizeof(char), size, f); + rcnt = fread(buf, sizeof(char), size, f); if (rcnt < size) { perror("Error 3: "); - free(buffer); + free(buf); return false; } - read_pldheader(buffer); + read_pldheader(buf); fclose(f); - free(buffer); return true; } int main(int argc, char ** argv) { - char *filename = argv[1]; - bool status = readpld(filename); + char *filename = argv[1]; + char *filedata = NULL; + bool status = readpld(filename, filedata); if (status) { printf("Read OK"); } else { printf("Read not-OK"); } + free(filedata); return 0; }