Moved scope of 'buffer' to main.

This commit is contained in:
_ 2018-04-01 17:19:39 -07:00
parent 37ce23db73
commit a8c704e428

23
main.c
View File

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