summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoralex <alex@ASUS>2018-09-19 14:14:49 +0200
committeralex <alex@ASUS>2018-09-19 14:14:49 +0200
commitdb072808ecf073168422ca908dc329d4122c29e1 (patch)
tree38da4c31ebbb9dc2a02b19774421fe23790349f6
parent545b9dabd4e72c291884367621d0cdfe2da469d2 (diff)
Use snprintf instead of sprintf
-rw-r--r--modules/menu/src/menu_gui.c78
-rw-r--r--modules/player/src/player_clui.c2
-rw-r--r--modules/player/src/player_gui.c23
-rw-r--r--modules/player/src/player_iface.c18
4 files changed, 64 insertions, 57 deletions
diff --git a/modules/menu/src/menu_gui.c b/modules/menu/src/menu_gui.c
index 0743f6f..38387b9 100644
--- a/modules/menu/src/menu_gui.c
+++ b/modules/menu/src/menu_gui.c
@@ -14,7 +14,7 @@
#include <math.h>
/* true & false */
#include <stdbool.h>
- /* sprintf() */
+ /* snprintf() */
#include <stdio.h>
/* srand() */
#include <stdlib.h>
@@ -34,18 +34,24 @@
/******************************************************************************
+ ******* macros ***************************************************************
+ ******************************************************************************/
+ # define LINE_SIZE (80)
+
+
+/******************************************************************************
******* structs **************************************************************
******************************************************************************/
struct Button_Data {
GtkWidget *ptr;
- char text [80];
+ char text [LINE_SIZE];
int num;
int *sw;
};
struct Label_Data {
GtkWidget *ptr;
- char text [80];
+ char text [LINE_SIZE];
};
@@ -89,8 +95,8 @@ void menu_gui_init (void)
g_signal_connect(window_gui, "destroy", G_CALLBACK(destroy_window), NULL);
/* Title. PROG_VERSION defined in Makefile */
- char title [80];
- sprintf(title, "mine-sweeper %s", PROG_VERSION);
+ char title [LINE_SIZE];
+ snprintf(title, LINE_SIZE, "mine-sweeper %s", PROG_VERSION);
gtk_window_set_title(GTK_WINDOW(window_gui), title);
/* Border */
@@ -113,11 +119,11 @@ void menu_gui (void)
struct Button_Data button [4];
/* Text */
- sprintf(label.text, "Main menu");
- sprintf(button[1].text, "[_1] Continue");
- sprintf(button[2].text, "[_2] Disclaimer of warranty");
- sprintf(button[3].text, "[_3] Terms and conditions");
- sprintf(button[0].text, "[_0] Exit program");
+ snprintf(label.text, LINE_SIZE, "Main menu");
+ snprintf(button[1].text, LINE_SIZE, "[_1] Continue");
+ snprintf(button[2].text, LINE_SIZE, "[_2] Disclaimer of warranty");
+ snprintf(button[3].text, LINE_SIZE, "[_3] Terms and conditions");
+ snprintf(button[0].text, LINE_SIZE, "[_0] Exit program");
/* Data */
button[1].num = 1;
@@ -250,13 +256,13 @@ static void menu_gui_continue (void)
struct Button_Data button [7];
/* Text */
- sprintf(label.text, "Game menu");
- sprintf(button[1].text, "[_1] Start");
- sprintf(button[2].text, "[_2] Select map");
- sprintf(button[3].text, "[_3] Change difficulty");
- sprintf(button[5].text, "[_5] Hi scores");
- sprintf(button[6].text, "[_6] DEVEL");
- sprintf(button[0].text, "[_0] Back");
+ snprintf(label.text, LINE_SIZE, "Game menu");
+ snprintf(button[1].text, LINE_SIZE, "[_1] Start");
+ snprintf(button[2].text, LINE_SIZE, "[_2] Select map");
+ snprintf(button[3].text, LINE_SIZE, "[_3] Change difficulty");
+ snprintf(button[5].text, LINE_SIZE, "[_5] Hi scores");
+ snprintf(button[6].text, LINE_SIZE, "[_6] DEVEL");
+ snprintf(button[0].text, LINE_SIZE, "[_0] Back");
/* Data */
button[1].num = 1;
@@ -278,7 +284,7 @@ static void menu_gui_continue (void)
wh = true;
while (wh) {
/* Text */
- sprintf(button[4].text, "[_4] Change file name (File: \"%s\")", saved_name);
+ snprintf(button[4].text, LINE_SIZE, "[_4] Change file name (File: \"%s\")", saved_name);
/* Generate widgets */
box = gtk_vbox_new(false, 0);
@@ -380,10 +386,10 @@ static void menu_gui_select (void)
struct Button_Data button [3];
/* Text */
- sprintf(label.text, "Select map");
- sprintf(button[1].text, "[_1] New map");
- sprintf(button[2].text, "[_2] Load map (File: \"%s\")", saved_name);
- sprintf(button[0].text, "[_0] Back");
+ snprintf(label.text, LINE_SIZE, "Select map");
+ snprintf(button[1].text, LINE_SIZE, "[_1] New map");
+ snprintf(button[2].text, LINE_SIZE, "[_2] Load map (File: \"%s\")", saved_name);
+ snprintf(button[0].text, LINE_SIZE, "[_0] Back");
/* Data */
button[1].num = 1;
@@ -455,12 +461,12 @@ static void menu_gui_level (void)
struct Button_Data button [5];
/* Text */
- sprintf(label.text, "Select level");
- sprintf(button[1].text, "[_1] Beginner");
- sprintf(button[2].text, "[_2] Intermediate");
- sprintf(button[3].text, "[_3] Expert");
- sprintf(button[4].text, "[_4] Custom");
- sprintf(button[0].text, "[_0] Back");
+ snprintf(label.text, LINE_SIZE, "Select level");
+ snprintf(button[1].text, LINE_SIZE, "[_1] Beginner");
+ snprintf(button[2].text, LINE_SIZE, "[_2] Intermediate");
+ snprintf(button[3].text, LINE_SIZE, "[_3] Expert");
+ snprintf(button[4].text, LINE_SIZE, "[_4] Custom");
+ snprintf(button[0].text, LINE_SIZE, "[_0] Back");
/* Data */
button[1].num = 1;
@@ -550,8 +556,8 @@ static void menu_gui_custom (void)
struct Button_Data button [4];
/* Text */
- sprintf(label.text, "Custom");
- sprintf(button[0].text, "[_0] Back");
+ snprintf(label.text, LINE_SIZE, "Custom");
+ snprintf(button[0].text, LINE_SIZE, "[_0] Back");
/* Data */
button[1].num = 1;
@@ -567,9 +573,9 @@ static void menu_gui_custom (void)
wh = true;
while (wh) {
/* Text */
- sprintf(button[1].text, "[_1] Change rows: rows\t\t(%i)", menu_iface_variables.rows);
- sprintf(button[2].text, "[_2] Change columns: cols\t(%i)", menu_iface_variables.cols);
- sprintf(button[3].text, "[_3] Change proportion of mines: p\t(%lf)", menu_iface_variables.p);
+ snprintf(button[1].text, LINE_SIZE, "[_1] Change rows: rows\t\t(%i)", menu_iface_variables.rows);
+ snprintf(button[2].text, LINE_SIZE, "[_2] Change columns: cols\t(%i)", menu_iface_variables.cols);
+ snprintf(button[3].text, LINE_SIZE, "[_3] Change proportion of mines: p\t(%lf)", menu_iface_variables.p);
/* Generate widgets */
box = gtk_vbox_new(false, 0);
@@ -646,9 +652,9 @@ static void menu_gui_devel (void)
struct Button_Data button [2];
/* Text */
- sprintf(label.text, "DEVELOPER OPTIONS");
- sprintf(button[1].text, "[_1] Change seed (srand)");
- sprintf(button[0].text, "[_0] Back");
+ snprintf(label.text, LINE_SIZE, "DEVELOPER OPTIONS");
+ snprintf(button[1].text, LINE_SIZE, "[_1] Change seed (srand)");
+ snprintf(button[0].text, LINE_SIZE, "[_0] Back");
/* Data */
button[1].num = 1;
diff --git a/modules/player/src/player_clui.c b/modules/player/src/player_clui.c
index 7e47d2f..6d6ade6 100644
--- a/modules/player/src/player_clui.c
+++ b/modules/player/src/player_clui.c
@@ -10,7 +10,7 @@
* * * Standard * * * * * *
* * * * * * * * * */
#include <stdbool.h>
- /* sprintf() */
+ /* printf() & sscanf() */
#include <stdio.h>
#include <wchar.h>
diff --git a/modules/player/src/player_gui.c b/modules/player/src/player_gui.c
index 517bde4..b93eafb 100644
--- a/modules/player/src/player_gui.c
+++ b/modules/player/src/player_gui.c
@@ -32,6 +32,7 @@
/******************************************************************************
******* macros ***************************************************************
******************************************************************************/
+ # define LINE_SIZE (80)
# define REFRESH_TIME_MS (100)
@@ -68,15 +69,15 @@ struct Field_Data {
struct Button_Data {
GtkWidget *ptr;
- char text [80];
+ char text [LINE_SIZE];
int val;
int *act;
};
struct TButton_Data {
GtkWidget *ptr;
- char text [80];
- char text2 [80];
+ char text [LINE_SIZE];
+ char text2 [LINE_SIZE];
int val;
int val2;
int *act;
@@ -90,7 +91,7 @@ struct EvBox_Data {
struct Label_Data {
GtkWidget *ptr;
- char text [80];
+ char text [LINE_SIZE];
};
@@ -495,13 +496,13 @@ static void help_init (int *action)
gtk_box_pack_start(GTK_BOX(box_help), box_help_in, false, false, 5);
/* Text */
- sprintf(button[BTN_CH_OFF].text, "[_0] Cheats off");
- sprintf(button[BTN_CH_1].text, "Cheat _1");
- sprintf(button[BTN_CH_2].text, "Cheat _2");
- sprintf(tbutton[TBTN_PAUSE].text, "_Pause");
- sprintf(tbutton[TBTN_PAUSE].text2, "[_P] Continue");
- sprintf(button[BTN_SAVE].text, "_Save");
- sprintf(button[BTN_QUIT].text, "_Quit");
+ snprintf(button[BTN_CH_OFF].text, LINE_SIZE, "[_0] Cheats off");
+ snprintf(button[BTN_CH_1].text, LINE_SIZE, "Cheat _1");
+ snprintf(button[BTN_CH_2].text, LINE_SIZE, "Cheat _2");
+ snprintf(tbutton[TBTN_PAUSE].text, LINE_SIZE, "_Pause");
+ snprintf(tbutton[TBTN_PAUSE].text2, LINE_SIZE, "[_P] Continue");
+ snprintf(button[BTN_SAVE].text, LINE_SIZE, "_Save");
+ snprintf(button[BTN_QUIT].text, LINE_SIZE, "_Quit");
/* Data */
/* L click */
diff --git a/modules/player/src/player_iface.c b/modules/player/src/player_iface.c
index cf75da4..ea32b4f 100644
--- a/modules/player/src/player_iface.c
+++ b/modules/player/src/player_iface.c
@@ -9,7 +9,7 @@
/* * * * * * * * * *
* * * Standard * * * * * *
* * * * * * * * * */
- /* sprintf() & fflush() */
+ /* snprintf() & fflush() */
#include <stdio.h>
/* * * * * * * * * *
@@ -98,10 +98,10 @@ int player_iface_start (int *pos_row, int *pos_col)
{
/* Title */
char title[TITLE_SIZE];
- sprintf(title, "Start:");
+ snprintf(title, TITLE_SIZE, "Start:");
/* Subtitle */
char subtitle[TITLE_SIZE];
- sprintf(subtitle, "00:00 | 0");
+ snprintf(subtitle, TITLE_SIZE, "00:00 | 0");
/* Start position */
player_iface_position.row = 0;
@@ -158,15 +158,15 @@ void player_iface (const struct Game_Iface_Out *game_iface_out,
case GAME_IFACE_STATE_CHEATED:
case GAME_IFACE_STATE_PLAYING:
case GAME_IFACE_STATE_PAUSE:
- sprintf(title, "Mines: %i/%i", game_iface_out->flags, game_iface_out->mines);
+ snprintf(title, TITLE_SIZE, "Mines: %i/%i", game_iface_out->flags, game_iface_out->mines);
break;
case GAME_IFACE_STATE_GAMEOVER:
- sprintf(title, "GAME OVER");
+ snprintf(title, TITLE_SIZE, "GAME OVER");
break;
case GAME_IFACE_STATE_SAFE:
- sprintf(title, "You win!");
+ snprintf(title, TITLE_SIZE, "You win!");
break;
}
/* Subtitle */
@@ -180,12 +180,12 @@ void player_iface (const struct Game_Iface_Out *game_iface_out,
secs = ((int)game_iface_score->time % 60);
if (game_iface_score->time >= 3600) {
- sprintf(subtitle, "%02i:%02i:%02i | %i", hours, mins, secs, game_iface_score->clicks);
+ snprintf(subtitle, TITLE_SIZE, "%02i:%02i:%02i | %i", hours, mins, secs, game_iface_score->clicks);
} else {
- sprintf(subtitle, "%02i:%02i | %i", mins, secs, game_iface_score->clicks);
+ snprintf(subtitle, TITLE_SIZE, "%02i:%02i | %i", mins, secs, game_iface_score->clicks);
}
} else {
- sprintf(subtitle, "N/A");
+ snprintf(subtitle, TITLE_SIZE, "N/A");
}
/* Request player action */