Skip to content

Commit

Permalink
checks return value for scanf, aprintf, ...
Browse files Browse the repository at this point in the history
  • Loading branch information
Judro committed Dec 17, 2024
1 parent c6e788e commit f1c5ed2
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
6 changes: 5 additions & 1 deletion src/controls.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ int cmove(GameInstance g, WINDOW **window) {
break;
case 'm':
endwin();
system("man mines-tui");
if (system("man mines-tui") != 0) {
printf("Please press enter to continue.\n");
while (getchar() == 0)
;
}
*window = create_window();
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void print_scrollable(char **text, unsigned terminal_width,
while (*tmp != NULL && index < highscore_window_height) {
print_left_margin(terminal_width, text_width);
printw("│");
printw(*tmp);
printw("%s", *tmp);
for (int i = 0; i < text_width - strlen(*tmp); i++)
printw(" ");
printw("│\n");
Expand Down
12 changes: 8 additions & 4 deletions src/highscore.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ FILE *init_state_files() {
char *username = getenv("USER");
if (strcmp(username, "root") == 0) {
printf("Please enter your username ($USER): ");
scanf("%s[^\n]", username_n);
while (scanf("%s[^\n]", username_n) != 1)
;
username = username_n;
}
char save_path[strlen(save_directory) + strlen(username) + 5];
Expand Down Expand Up @@ -150,9 +151,12 @@ char **userHighscores2string(UserHighscore *highscores) {
char time[32];
struct tm *local_time = localtime(&highscores[index].highscore.date);
strftime(time, 32, "%Y-%m-%d", local_time);
asprintf(&tmp, " %02u:%02u %s %s ", highscores[index].highscore.time / 60,
highscores[index].highscore.time % 60, time,
highscores[index].user);
if (asprintf(&tmp, " %02u:%02u %s %s ",
highscores[index].highscore.time / 60,
highscores[index].highscore.time % 60, time,
highscores[index].user) == -1)
exit(EXIT_FAILURE);

if (tmp == NULL)
exit(EXIT_FAILURE);
free(highscores[index].user);
Expand Down
6 changes: 5 additions & 1 deletion src/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ GameInstance select_mode(WINDOW **window) {
return get_custom_game(window);
case 4:
endwin();
system("man mines-tui");
if (system("man mines-tui") != 0) {
printf("Please press enter to continue.\n");
while (getchar() == 0)
;
}
*window = create_window();
return select_mode(window);
default:
Expand Down

0 comments on commit f1c5ed2

Please sign in to comment.