Remove various compilation warnings

With the removal of these warnings we slightly improve the robustness of
the highscore parsing, handling some more cases of bad parsing/writing
and failure to open files.
master
tiehuis 9 years ago
parent 28b4a46336
commit 4520781f25
  1. 2
      Makefile
  2. 3
      src/engine.h
  3. 24
      src/highscore.c
  4. 2
      src/highscore.h
  5. 2
      src/main.c

@ -1,5 +1,5 @@
CC ?= clang
CFLAGS += -Wno-visibility -Wno-incompatible-pointer-types -Wall -Wextra -O2 -DINVERT_COLORS -DVT100
CFLAGS += -Wno-visibility -Wno-incompatible-pointer-types -Wall -Wextra -O2 -DINVERT_COLORS -DVT100 -O2
LFLAGS +=
PROGRAM := 2048

@ -2,7 +2,6 @@
#define ENGINE_H
#include <stdio.h>
#include "gfx.h"
#include "options.h"
#define fatal(msg)\
@ -34,6 +33,8 @@ enum {
dir_up
};
struct gfx_state;
int gamestate_end_condition(struct gamestate*);
void gamestate_new_block(struct gamestate*);
int gamestate_tick(struct gfx_state*, struct gamestate*, int, void (*callback)(struct gfx_state*, struct gamestate*));

@ -57,7 +57,9 @@ void highscore_reset(void)
while (1) {
/* fgets is used to avoid queuing that may occur with getchar */
fgets(resp, resp_length, stdin);
if (fgets(resp, resp_length, stdin) == NULL)
return;
string_to_lower(resp);
const size_t sl = strlen(resp);
@ -87,7 +89,16 @@ long highscore_load(struct gamestate *g)
if (fd == NULL)
fd = fopen(hsfile, "w+");
fscanf(fd, "%ld", &result);
if (fd == NULL) {
fprintf(stderr, "load: Failed to open highscore file\n");
return 0;
}
if (fscanf(fd, "%ld", &result) != 1) {
fprintf(stderr, "load: Failed to parse highscore file\n");
result = 0;
}
fclose(fd);
if (g) g->score_high = result;
@ -105,6 +116,13 @@ void highscore_save(struct gamestate *g)
const char *hsfile = highscore_retrieve_file();
FILE *fd = fopen(hsfile, "w");
fprintf(fd, "%ld", g->score);
if (fd == NULL) {
fprintf(stderr, "save: Failed to open highscore file\n");
return;
}
if (fprintf(fd, "%ld", g->score) < 0) {
fprintf(stderr, "save: Failed to write highscore file\n");
}
fclose(fd);
}

@ -1,6 +1,8 @@
#ifndef HIGHSCORE_H
#define HIGHSCORE_H
#include "engine.h"
void highscore_reset(void);
long highscore_load(struct gamestate *g);
void highscore_save(struct gamestate *g);

@ -14,7 +14,7 @@ void draw_then_sleep(struct gfx_state *s, struct gamestate *g)
int main(int argc, char **argv)
{
struct gamestate *g = gamestate_init(argc, argv);
struct gfx_state *s;
struct gfx_state *s = NULL;
if (g->opts->interactive)
s = gfx_init(g);

Loading…
Cancel
Save