Merge pull request #442 from yenatch/fix-fread

fix fread warnings
This commit is contained in:
yenatch 2017-12-28 01:31:00 -05:00 committed by GitHub
commit e2b378f5e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 48 additions and 16 deletions

View File

@ -18,5 +18,6 @@ all: $(tools)
clean:
rm -f $(tools)
gfx md5: common.h
%: %.c
$(CC) $(CFLAGS) -o $@ $<

View File

@ -21,7 +21,10 @@ uint8_t *read_u8(char *filename, int *size) {
*size = ftell(f);
rewind(f);
uint8_t *data = malloc(*size);
fread(data, 1, *size, f);
if (*size != (int)fread(data, 1, *size, f)) {
fprintf(stderr, "Could not read file: \"%s\"\n", filename);
exit(1);
}
fclose(f);
return data;
}

View File

@ -230,8 +230,13 @@ int png_get_width(char *filename) {
const int OFFSET_WIDTH = 16;
uint8_t bytes[4];
fseek(f, OFFSET_WIDTH, SEEK_SET);
fread(bytes, 1, 4, f);
size_t size = 4;
size_t result = fread(bytes, 1, size, f);
fclose(f);
if (result != size) {
fprintf(stderr, "Could not read file at offset 0x%x: \"%s\"\n", OFFSET_WIDTH, filename);
exit(1);
}
int width = 0;
for (int i = 0; i < 4; i++) {

View File

@ -29,7 +29,11 @@ void print_pokemon_palette(char* palette_filename) {
}
fseek(f, 2, SEEK_SET);
fread(bytes, 1, 4, f);
size_t size = 4;
if (size != fread(bytes, 1, size, f)) {
fprintf(stderr, "failed to read file %s\n", palette_filename);
exit(1);
}
fclose(f);
print_rgb((bytes[1] << 8) | bytes[0]);
@ -39,7 +43,7 @@ void print_pokemon_palette(char* palette_filename) {
void print_palette(char* palette_filename) {
FILE* f;
uint8_t* bytes;
long size;
size_t size;
int i;
f = fopen(palette_filename, "rb");
@ -63,10 +67,13 @@ void print_palette(char* palette_filename) {
}
fseek(f, 0, SEEK_SET);
fread(bytes, 1, size, f);
if (size != fread(bytes, 1, size, f)) {
fprintf(stderr, "failed to read file %s\n", palette_filename);
exit(1);
}
fclose(f);
for (i = 0; i + 1 < size; i += 2) {
for (i = 0; i + 1 < (int)size; i += 2) {
print_rgb((bytes[i + 1] << 8) | bytes[i]);
}
}

View File

@ -22,8 +22,12 @@ void output_dimensions(char* png_filename, char* out_filename) {
// width
fseek(f, 16, SEEK_SET);
fread(bytes, 1, 4, f);
int size = fread(bytes, 1, 4, f);
fclose(f);
if (size != 4) {
fprintf(stderr, "failed to read at offset 0x10 in file %s\n", png_filename);
exit(1);
}
width = 0;
for (i = 0; i < 4; i++) {

View File

@ -38,7 +38,7 @@ void make_frames(struct Frames* frames, struct Bitmasks* bitmasks, char* tilemap
uint8_t* tilemap;
uint8_t* this_frame;
FILE* f;
long size;
size_t size;
int width;
int height;
uint8_t byte;
@ -48,7 +48,7 @@ void make_frames(struct Frames* frames, struct Bitmasks* bitmasks, char* tilemap
f = fopen(tilemap_filename, "rb");
if (f == NULL) {
fprintf(stderr, "could not open file %s", tilemap_filename);
fprintf(stderr, "could not open file %s\n", tilemap_filename);
exit(1);
}
@ -65,15 +65,21 @@ void make_frames(struct Frames* frames, struct Bitmasks* bitmasks, char* tilemap
fprintf(stderr, "malloc failure\n");
exit(1);
}
fread(tilemap, 1, size, f);
if (size != fread(tilemap, 1, size, f)) {
fprintf(stderr, "failed to read file %s\n", tilemap_filename);
exit(1);
}
fclose(f);
f = fopen(dimensions_filename, "rb");
if (f == NULL) {
fprintf(stderr, "could not open file %s", dimensions_filename);
fprintf(stderr, "could not open file %s\n", dimensions_filename);
exit(1);
}
if (1 != fread(&byte, 1, 1, f)) {
fprintf(stderr, "failed to read file %s\n", dimensions_filename);
exit(1);
}
fread(&byte, 1, 1, f);
fclose(f);
width = byte & 0xf;
@ -137,7 +143,7 @@ void make_frames(struct Frames* frames, struct Bitmasks* bitmasks, char* tilemap
//}
//free(frames->frames);
//fprintf(stderr, "num bitmasks: %d", bitmasks->num_bitmasks);
//fprintf(stderr, "num bitmasks: %d\n", bitmasks->num_bitmasks);
//for (i = 0; i < bitmasks->num_bitmasks; i++) {
// free(bitmasks->bitmasks[i].data);
// fprintf(stderr, "freed bitmask %d\n", i);
@ -263,7 +269,7 @@ int main(int argc, char* argv[]) {
//ext = strrchr(argv[3], '.');
//if (!ext || ext == argv[3]) {
// fprintf(stderr, "need a file extension to determine what to write to %s", argv[3]);
// fprintf(stderr, "need a file extension to determine what to write to %s\n", argv[3]);
//}
make_frames(&frames, &bitmasks, tilemap_filename, dimensions_filename);

View File

@ -100,7 +100,10 @@ void create_tilemap(struct Tilemap* tilemap, struct Graphic* graphic, char* grap
fprintf(stderr, "malloc failure\n");
exit(1);
}
fread(graphics, 1, graphics_size, f);
if (graphics_size != (long)fread(graphics, 1, graphics_size, f)) {
fprintf(stderr, "failed to read file %s\n", graphics_filename);
exit(1);
}
fclose(f);
int num_tiles_per_frame = width * height;
@ -213,7 +216,10 @@ int main(int argc, char* argv[]) {
if (!f) {
exit(1);
}
fread(bytes, 1, 1, f);
if (1 != fread(bytes, 1, 1, f)) {
fprintf(stderr, "failed to read file %s\n", dimensions_filename);
exit(1);
}
fclose(f);
width = bytes[0] & 0xf;
height = bytes[0] >> 4;