From 290b2c51401c8c76e04b4d66036b62d8f1b1d596 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Fri, 21 Sep 2018 17:07:15 +0200 Subject: V 3.2 - Group make output lines - Print files to a string which can be used in many ways - Use snprintf instead of sprintf, strcpy & strcat - Unify about.c functions - Add site to README --- Makefile | 16 +++-- README.txt | 4 ++ bin/Makefile | 6 +- libalx/inc/alx_file.h | 2 +- libalx/inc/alx_ncur.h | 3 +- libalx/obj/Makefile | 44 ++++++------- libalx/src/alx_file.c | 56 +++++++++++++++-- libalx/src/alx_ncur.c | 16 ++--- modules/about/inc/about.h | 23 ++++--- modules/about/obj/Makefile | 7 +-- modules/about/src/about.c | 92 ++++++++++++--------------- modules/ctrl/obj/Makefile | 7 +-- modules/game/obj/Makefile | 12 ++-- modules/game/src/game_iface.c | 21 ++++++- modules/menu/inc/menu_iface.h | 2 + modules/menu/obj/Makefile | 22 +++---- modules/menu/src/menu_clui.c | 4 +- modules/menu/src/menu_iface.c | 25 +++++++- modules/menu/src/menu_tui.c | 12 +++- modules/menu/src/parser.c | 18 +++--- modules/player/inc/player_clui.h | 4 +- modules/player/inc/player_iface.h | 4 +- modules/player/inc/player_tui.h | 4 +- modules/player/obj/Makefile | 20 +++--- modules/player/src/player_clui.c | 10 +-- modules/player/src/player_iface.c | 64 +++++++++---------- modules/player/src/player_tui.c | 6 +- modules/save/inc/score.h | 2 +- modules/save/obj/Makefile | 12 ++-- modules/save/src/save.c | 48 +++++--------- modules/save/src/score.c | 128 ++++++++++++++++++++------------------ modules/xyzzy/obj/Makefile | 7 +-- obj/Makefile | 4 +- share/README.txt | 4 ++ src/main.c | 28 ++++++--- 35 files changed, 404 insertions(+), 333 deletions(-) diff --git a/Makefile b/Makefile index d8c9629..3974868 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,10 @@ #!/usr/bin/make -f -VERSION = 3 -PATCHLEVEL = .1 +VERSION = 4 +PATCHLEVEL = ~a3 SUBLEVEL = EXTRAVERSION = -NAME = instalable +NAME = graphic export VERSION export PATCHLEVEL @@ -141,23 +141,25 @@ export LD ################################################################################ CFLAGS = -std=c11 +CFLAGS += `pkg-config --cflags gtk+-2.0` CFLAGS += -D PROG_VERSION=\"$(PROGRAMVERSION)\" CFLAGS += -D 'INSTALL_SHARE_DIR="$(INSTALL_SHARE_DIR)"' CFLAGS += -D SHARE_DIR=\"$(SHARE_DIR)\" CFLAGS += -D 'INSTALL_VAR_DIR="$(INSTALL_VAR_DIR)"' CFLAGS += -D VAR_DIR=\"$(VAR_DIR)\" +GTK_LIBS = `pkg-config --libs gtk+-2.0` ifeq ($(OS), linux) CFLAGS += -D OS_LINUX - LIBS = -l m -l ncursesw + LIBS = -l m -l ncursesw $(GTK_LIBS) else ifeq ($(OS), win) CFLAGS += -D OS_WIN # curses CFLAGS += -D _XOPEN_SOURCE=500 -I /mingw/include/ncursesw -I /mingw/include CURSES_LIBS = -L /mingw/lib -l ncursesw -l psapi - LIBS = -static -l m $(CURSES_LIBS) + LIBS = -static -l m $(CURSES_LIBS) $(GTK_LIBS) endif export CFLAGS @@ -264,10 +266,6 @@ help: -################################################################################ -######## End of file ########################################################### -################################################################################ -######## End of file ########################################################### ################################################################################ ######## End of file ########################################################### ################################################################################ diff --git a/README.txt b/README.txt index edd8a2b..ebdf9c3 100644 --- a/README.txt +++ b/README.txt @@ -39,6 +39,10 @@ If you make a version, please tell me. I would like to see it. Thank you. +site: + https://github.com/AlejandroColomar/mine-sweeper + + COMPILE: - linux: $ make diff --git a/bin/Makefile b/bin/Makefile index fafbfef..5f4b88c 100644 --- a/bin/Makefile +++ b/bin/Makefile @@ -60,7 +60,7 @@ all: $(ALL) $(BIN_NAME): $(MAIN_OBJS) $(Q)$(CC) $^ -o $@ $(LIBS) - @echo "\tLD $@" + @echo "\tBIN $@" @echo "" @@ -71,7 +71,3 @@ clean: ################################################################################ ######## End of file ########################################################### ################################################################################ -######## End of file ########################################################### -################################################################################ -######## End of file ########################################################### -################################################################################ diff --git a/libalx/inc/alx_file.h b/libalx/inc/alx_file.h index 951d98c..352d422 100644 --- a/libalx/inc/alx_file.h +++ b/libalx/inc/alx_file.h @@ -13,7 +13,7 @@ /****************************************************************************** ******* functions ************************************************************ ******************************************************************************/ -void alx_prn_file (const char *filepath); +void alx_snprint_file (char *dest, int destsize, const char *filepath); /****************************************************************************** diff --git a/libalx/inc/alx_ncur.h b/libalx/inc/alx_ncur.h index bf5ed43..f1a774f 100644 --- a/libalx/inc/alx_ncur.h +++ b/libalx/inc/alx_ncur.h @@ -65,7 +65,8 @@ const char *format, ...); - void alx_w_getstr (char *str, + void alx_w_getstr (char *dest, + int destsize, int w, int r, const char *title, diff --git a/libalx/obj/Makefile b/libalx/obj/Makefile index cd410ee..717c7af 100644 --- a/libalx/obj/Makefile +++ b/libalx/obj/Makefile @@ -10,7 +10,8 @@ SRC_DIR = $(LIBALX_DIR)/src/ # dependencies -ALL = alx_cmp.o alx_file.o alx_getnum.o alx_mask.o alx_math.o alx_ncur.o alx_seed.o +_ALL = alx_cmp.o alx_file.o alx_getnum.o alx_mask.o alx_math.o alx_ncur.o alx_seed.o +ALL = $(_ALL) alx_lib.o CMP_INC = alx_cmp.h CMP_DEPS = $(SRC_DIR)/alx_cmp.c $(patsubst %,$(INC_DIR)/%,$(CMP_INC)) @@ -46,40 +47,39 @@ SEED_INC_DIRS = -I $(INC_DIR) all: $(ALL) -alx_cmp.o: $(CMP_DEPS) - $(Q)$(CC) $(CFLAGS) $(CMP_INC_DIRS) -c $< -o $@ $(LIBS) - @echo "\tCC $<" +alx_lib.o: $(_ALL) + $(Q)$(LD) -r $^ -o $@ + @echo "\tLD $@" @echo "" + +alx_cmp.o: $(CMP_DEPS) + $(Q)$(CC) $(CFLAGS) $(CMP_INC_DIRS) -c $< -o $@ + @echo "\tCC $@" + alx_file.o: $(FILE_DEPS) - $(Q)$(CC) $(CFLAGS) $(FILE_INC_DIRS) -c $< -o $@ $(LIBS) - @echo "\tCC $<" - @echo "" + $(Q)$(CC) $(CFLAGS) $(FILE_INC_DIRS) -c $< -o $@ + @echo "\tCC $@" alx_getnum.o: $(GETN_DEPS) - $(Q)$(CC) $(CFLAGS) $(GETN_INC_DIRS) -c $< -o $@ $(LIBS) - @echo "\tCC $<" - @echo "" + $(Q)$(CC) $(CFLAGS) $(GETN_INC_DIRS) -c $< -o $@ + @echo "\tCC $@" alx_mask.o: $(MASK_DEPS) - $(Q)$(CC) $(CFLAGS) $(MASK_INC_DIRS) -c $< -o $@ $(LIBS) - @echo "\tCC $<" - @echo "" + $(Q)$(CC) $(CFLAGS) $(MASK_INC_DIRS) -c $< -o $@ + @echo "\tCC $@" alx_math.o: $(MATH_DEPS) - $(Q)$(CC) $(CFLAGS) $(MATH_INC_DIRS) -c $< -o $@ $(LIBS) - @echo "\tCC $<" - @echo "" + $(Q)$(CC) $(CFLAGS) $(MATH_INC_DIRS) -c $< -o $@ + @echo "\tCC $@" alx_ncur.o: $(NCUR_DEPS) - $(Q)$(CC) $(CFLAGS) $(NCUR_INC_DIRS) -c $< -o $@ $(LIBS) - @echo "\tCC $<" - @echo "" + $(Q)$(CC) $(CFLAGS) $(NCUR_INC_DIRS) -c $< -o $@ + @echo "\tCC $@" alx_seed.o: $(SEED_DEPS) - $(Q)$(CC) $(CFLAGS) $(SEED_INC_DIRS) -c $< -o $@ $(LIBS) - @echo "\tCC $<" - @echo "" + $(Q)$(CC) $(CFLAGS) $(SEED_INC_DIRS) -c $< -o $@ + @echo "\tCC $@" clean: diff --git a/libalx/src/alx_file.c b/libalx/src/alx_file.c index fa0d6e3..26ab468 100644 --- a/libalx/src/alx_file.c +++ b/libalx/src/alx_file.c @@ -10,6 +10,7 @@ * * * Standard * * * * * * * * * * * * * * * */ #include + #include #include #include @@ -22,32 +23,75 @@ /****************************************************************************** ******* macros *************************************************************** ******************************************************************************/ + # define BUFF_SIZE_TEXT (1048576) + +#if defined OS_LINUX # define BEGINNING "\n┌──────────────────────────────────────────────────────────────────────────────┐\n" # define ENDING "└──────────────────────────────────────────────────────────────────────────────┘\n\n" +#elif defined OS_WIN + # define BEGINNING "\n________________________________________________________________________________\n" + # define ENDING "________________________________________________________________________________\n\n" +#endif # define ERR_FPTR_MSG "¡ FILE error !" + # define ERR_FSIZE_MSG "¡ FILE is too big !" /****************************************************************************** ******* main ***************************************************************** ******************************************************************************/ -void alx_prn_file (const char *filepath) +void alx_snprint_file (char *dest, int destsize, const char *filepath) { - int64_t c; + char buff [BUFF_SIZE_TEXT]; FILE *fp; + /* Open file */ fp = fopen(filepath, "r"); - printf(BEGINNING); + +#if 0 + /* Implemented using getc instead of fread */ + char *str; + int64_t c; + if (fp) { - while ((c = getc(fp)) != EOF){ - putchar(c); + str = buff; + while (((c = getc(fp)) != EOF) && str < BUFF_SIZE_TEXT) { + sprintf(p, "%c", (char)c); + str++; } fclose(fp); } else { puts(ERR_FPTR_MSG); printf(" errno = %i;\n", errno); } - printf(ENDING); +#else + long len; + if (fp) { + /* File lenght */ + fseek(fp, 0, SEEK_END); + len = ftell(fp); + fseek(fp, 0, SEEK_SET); + + /* Read file into buff and append '\0' */ + if (len < BUFF_SIZE_TEXT) { + fread(buff, sizeof(char), len, fp); + buff[len] = '\0'; + } else { + /* Prevent buffer overflow */ + puts(ERR_FSIZE_MSG); + } + + /* Close file */ + fclose(fp); + } else { + /* File error */ + puts(ERR_FPTR_MSG); + printf(" errno = %i;\n", errno); + } +#endif + + /* Print into dest */ + snprintf(dest, destsize, "%s", buff); } diff --git a/libalx/src/alx_ncur.c b/libalx/src/alx_ncur.c index 116a3bb..78de144 100644 --- a/libalx/src/alx_ncur.c +++ b/libalx/src/alx_ncur.c @@ -14,6 +14,7 @@ #include #include #include + /* strlen() */ #include /* wchar_t */ #include @@ -54,7 +55,7 @@ static double loop_w_getdbl (WINDOW *win, double m, double def, double M); static int64_t loop_w_getint (WINDOW *win, double m, int64_t def, double M); -static void loop_w_getstr (char *str, WINDOW *win); +static void loop_w_getstr (char *dest, int destsize, WINDOW *win); static void loop_w_getfname (const char *fpath, char *fname, bool exist, WINDOW *win); @@ -289,7 +290,7 @@ int64_t alx_w_getint (int w, int r, const char *title, return Z; } -void alx_w_getstr (char *str, +void alx_w_getstr (char *dest, int destsize, int w, int r, const char *title, const char *format, ...) { @@ -346,7 +347,7 @@ void alx_w_getstr (char *str, win3 = newwin(h3, w3, r3, c3); wbkgd(win3, A_REVERSE); wrefresh(win3); - loop_w_getstr(str, win3); + loop_w_getstr(dest, destsize, win3); /* Delete window */ alx_win_del(win3); @@ -612,7 +613,7 @@ static int64_t loop_w_getint (WINDOW *win, return Z; } -static void loop_w_getstr (char *str, WINDOW *win) +static void loop_w_getstr (char *dest, int destsize, WINDOW *win) { int i; char buff [BUFF_SIZE]; @@ -637,7 +638,7 @@ static void loop_w_getstr (char *str, WINDOW *win) } if (!err) { - strcpy(str, buff); + snprintf(dest, destsize, buff); } } @@ -665,8 +666,7 @@ static void loop_w_getfname (const char *fpath, char *fname, bool exist, } else if (sscanf(buff, " %s ", buff2) != 1) { err = ERR_SSCANF; } else { - strcpy(file_path, fpath); - strcat(file_path, buff2); + snprintf(file_path, FILENAME_MAX, "%s%s", fpath, buff2); fp = fopen(file_path, "r"); @@ -691,7 +691,7 @@ static void loop_w_getfname (const char *fpath, char *fname, bool exist, } if (!err) { - strcpy(fname, buff2); + snprintf(fname, FILENAME_MAX, buff2); } } diff --git a/modules/about/inc/about.h b/modules/about/inc/about.h index b51eee9..7118577 100644 --- a/modules/about/inc/about.h +++ b/modules/about/inc/about.h @@ -24,6 +24,18 @@ # define PROG_YEAR "2015" +/****************************************************************************** + ******* enums **************************************************************** + ******************************************************************************/ + enum Share_File { + SHARE_COPYRIGHT, + SHARE_DISCLAIMER, + SHARE_HELP, + SHARE_LICENSE, + SHARE_USAGE + }; + + /****************************************************************************** ******* variables ************************************************************ ******************************************************************************/ @@ -33,13 +45,10 @@ /****************************************************************************** ******* functions ************************************************************ ******************************************************************************/ - void about_init (void); - void print_cpright (void); - void print_disclaim (void); - void print_help (void); - void print_license (void); - void print_usage (void); - void print_version (void); + void about_init (void); + void snprint_share_file (char *dest, int destsize, int share_file); + void print_share_file (int share_file); + void print_version (void); /****************************************************************************** diff --git a/modules/about/obj/Makefile b/modules/about/obj/Makefile index 701c308..2c1886b 100644 --- a/modules/about/obj/Makefile +++ b/modules/about/obj/Makefile @@ -30,14 +30,13 @@ all: $(ALL) about_mod.o: $(_ALL) $(Q)$(LD) -r $^ -o $@ - @echo "\tLD $@" + @echo "\tLD $@" @echo "" about.o: $(ABOU_DEPS) - $(Q)$(CC) $(CFLAGS) $(ABOU_INC_DIRS) -c $< -o $@ $(LIBS) - @echo "\tCC $<" - @echo "" + $(Q)$(CC) $(CFLAGS) $(ABOU_INC_DIRS) -c $< -o $@ + @echo "\tCC $@" clean: diff --git a/modules/about/src/about.c b/modules/about/src/about.c index 0f5ed88..a9fe94a 100644 --- a/modules/about/src/about.c +++ b/modules/about/src/about.c @@ -11,8 +11,6 @@ * * * * * * * * * */ /* printf() */ #include - /* strcpy() & strcat() */ - #include /* * * * * * * * * * * * * Other * * * * * * * @@ -23,6 +21,20 @@ #include "about.h" +/****************************************************************************** + ******* macros *************************************************************** + ******************************************************************************/ + # define BUFF_SIZE_TEXT (1048576) + +#if defined OS_LINUX + # define BEGINNING "\n┌──────────────────────────────────────────────────────────────────────────────┐\n" + # define ENDING "└──────────────────────────────────────────────────────────────────────────────┘\n\n" +#elif defined OS_WIN + # define BEGINNING "\n________________________________________________________________________________\n" + # define ENDING "________________________________________________________________________________\n\n" +#endif + + /****************************************************************************** ******* variables ************************************************************ ******************************************************************************/ @@ -34,65 +46,43 @@ char share_path [FILENAME_MAX]; ******************************************************************************/ void about_init (void) { - strcpy(share_path, INSTALL_SHARE_DIR); - strcat(share_path, "/"); - strcat(share_path, SHARE_DIR); - strcat(share_path, "/"); + snprintf(share_path, FILENAME_MAX, "%s/%s/", INSTALL_SHARE_DIR, SHARE_DIR); } -void print_cpright (void) +void snprint_share_file (char *dest, int destsize, int share_file) { char file_name [FILENAME_MAX]; - strcpy(file_name, share_path); - strcat(file_name, "/"); - strcat(file_name, "COPYRIGHT.txt"); - - alx_prn_file(file_name); + switch (share_file) { + case SHARE_COPYRIGHT: + snprintf(file_name, FILENAME_MAX, "%s/%s", share_path, "COPYRIGHT.txt"); + break; + case SHARE_DISCLAIMER: + snprintf(file_name, FILENAME_MAX, "%s/%s", share_path, "DISCLAIMER.txt"); + break; + case SHARE_HELP: + snprintf(file_name, FILENAME_MAX, "%s/%s", share_path, "HELP.txt"); + break; + case SHARE_LICENSE: + snprintf(file_name, FILENAME_MAX, "%s/%s", share_path, "LICENSE.txt"); + break; + case SHARE_USAGE: + snprintf(file_name, FILENAME_MAX, "%s/%s", share_path, "USAGE.txt"); + break; + } + + alx_snprint_file(dest, destsize, file_name); } -void print_disclaim (void) +void print_share_file (int share_file) { - char file_name [FILENAME_MAX]; - - strcpy(file_name, share_path); - strcat(file_name, "/"); - strcat(file_name, "DISCLAIMER.txt"); - - alx_prn_file(file_name); -} - -void print_help (void) -{ - char file_name [FILENAME_MAX]; - - strcpy(file_name, share_path); - strcat(file_name, "/"); - strcat(file_name, "HELP.txt"); - - alx_prn_file(file_name); -} - -void print_license (void) -{ - char file_name [FILENAME_MAX]; - - strcpy(file_name, share_path); - strcat(file_name, "/"); - strcat(file_name, "LICENSE.txt"); - - alx_prn_file(file_name); -} - -void print_usage (void) -{ - char file_name [FILENAME_MAX]; + char str [BUFF_SIZE_TEXT]; - strcpy(file_name, share_path); - strcat(file_name, "/"); - strcat(file_name, "USAGE.txt"); + snprint_share_file(str, BUFF_SIZE_TEXT, share_file); - alx_prn_file(file_name); + printf(BEGINNING); + printf("%s", str); + printf(ENDING); } void print_version (void) diff --git a/modules/ctrl/obj/Makefile b/modules/ctrl/obj/Makefile index 79c85a6..a0e4537 100644 --- a/modules/ctrl/obj/Makefile +++ b/modules/ctrl/obj/Makefile @@ -39,14 +39,13 @@ all: $(ALL) ctrl_mod.o: $(_ALL) $(Q)$(LD) -r $^ -o $@ - @echo "\tLD $@" + @echo "\tLD $@" @echo "" start.o: $(STRT_DEPS) - $(Q)$(CC) $(CFLAGS) $(STRT_INC_DIRS) -c $< -o $@ $(LIBS) - @echo "\tCC $<" - @echo "" + $(Q)$(CC) $(CFLAGS) $(STRT_INC_DIRS) -c $< -o $@ + @echo "\tCC $@" clean: diff --git a/modules/game/obj/Makefile b/modules/game/obj/Makefile index c0053c8..5cd2974 100644 --- a/modules/game/obj/Makefile +++ b/modules/game/obj/Makefile @@ -51,19 +51,17 @@ all: $(ALL) game_mod.o: $(_ALL) $(Q)$(LD) -r $^ -o $@ - @echo "\tLD $@" + @echo "\tLD $@" @echo "" game.o: $(GAME_DEPS) - $(Q)$(CC) $(CFLAGS) $(GAME_INC_DIRS) -c $< -o $@ $(LIBS) - @echo "\tCC $<" - @echo "" + $(Q)$(CC) $(CFLAGS) $(GAME_INC_DIRS) -c $< -o $@ + @echo "\tCC $@" game_iface.o: $(GAMEI_DEPS) - $(Q)$(CC) $(CFLAGS) $(GAMEI_INC_DIRS) -c $< -o $@ $(LIBS) - @echo "\tCC $<" - @echo "" + $(Q)$(CC) $(CFLAGS) $(GAMEI_INC_DIRS) -c $< -o $@ + @echo "\tCC $@" clean: diff --git a/modules/game/src/game_iface.c b/modules/game/src/game_iface.c index 511c573..9841d7c 100644 --- a/modules/game/src/game_iface.c +++ b/modules/game/src/game_iface.c @@ -424,7 +424,6 @@ static void game_iface_update_vis (int r, int c) int field_vis; switch (game_iface_out.state) { - case GAME_IFACE_STATE_SAFE: case GAME_IFACE_STATE_CHEATED: case GAME_IFACE_STATE_XYZZY: case GAME_IFACE_STATE_PLAYING: @@ -447,6 +446,26 @@ static void game_iface_update_vis (int r, int c) } break; + case GAME_IFACE_STATE_SAFE: + switch (game_board.usr[r][c]) { + case GAME_USR_HIDDEN: + field_vis = GAME_IFACE_VIS_SAFE_MINE; + break; + + case GAME_USR_CLEAR: + field_vis = GAME_IFACE_VIS_0 + game_board.gnd[r][c]; + break; + + case GAME_USR_FLAG: + field_vis = GAME_IFACE_VIS_FLAG; + break; + + case GAME_USR_POSSIBLE: + field_vis = GAME_IFACE_VIS_POSSIBLE; + break; + } + break; + case GAME_IFACE_STATE_GAMEOVER: switch (game_board.usr[r][c]) { case GAME_USR_KBOOM: diff --git a/modules/menu/inc/menu_iface.h b/modules/menu/inc/menu_iface.h index a7ac23b..481e6cb 100644 --- a/modules/menu/inc/menu_iface.h +++ b/modules/menu/inc/menu_iface.h @@ -50,6 +50,8 @@ extern struct Menu_Iface_Variables menu_iface_variables; ******* functions ************************************************************ ******************************************************************************/ void menu_iface_init (void); +void menu_iface_init_iface (void); +void menu_iface_cleanup (void); void menu_iface_board (int *level, int *rows, int *cols, int *mines); void menu_iface (void); diff --git a/modules/menu/obj/Makefile b/modules/menu/obj/Makefile index de999f8..c32d449 100644 --- a/modules/menu/obj/Makefile +++ b/modules/menu/obj/Makefile @@ -102,29 +102,25 @@ all: $(ALL) menu_mod.o: $(_ALL) $(Q)$(LD) -r $^ -o $@ - @echo "\tLD $@" + @echo "\tLD $@" @echo "" parser.o: $(PARS_DEPS) - $(Q)$(CC) $(CFLAGS) $(PARS_INC_DIRS) -c $< -o $@ $(LIBS) - @echo "\tCC $<" - @echo "" + $(Q)$(CC) $(CFLAGS) $(PARS_INC_DIRS) -c $< -o $@ + @echo "\tCC $@" menu_iface.o: $(MENUI_DEPS) - $(Q)$(CC) $(CFLAGS) $(MENUI_INC_DIRS) -c $< -o $@ $(LIBS) - @echo "\tCC $<" - @echo "" + $(Q)$(CC) $(CFLAGS) $(MENUI_INC_DIRS) -c $< -o $@ + @echo "\tCC $@" menu_clui.o: $(MENUCLUI_DEPS) - $(Q)$(CC) $(CFLAGS) $(MENUCLUI_INC_DIRS) -c $< -o $@ $(LIBS) - @echo "\tCC $<" - @echo "" + $(Q)$(CC) $(CFLAGS) $(MENUCLUI_INC_DIRS) -c $< -o $@ + @echo "\tCC $@" menu_tui.o: $(MENUTUI_DEPS) - $(Q)$(CC) $(CFLAGS) $(MENUTUI_INC_DIRS) -c $< -o $@ $(LIBS) - @echo "\tCC $<" - @echo "" + $(Q)$(CC) $(CFLAGS) $(MENUTUI_INC_DIRS) -c $< -o $@ + @echo "\tCC $@" clean: diff --git a/modules/menu/src/menu_clui.c b/modules/menu/src/menu_clui.c index 03c92d9..6fccbe0 100644 --- a/modules/menu/src/menu_clui.c +++ b/modules/menu/src/menu_clui.c @@ -68,7 +68,7 @@ void menu_clui (void) sscanf(buff, " %c", &ch); if (ch == 'y' || ch == 'Y') { puts (" >yes"); - print_disclaim(); + print_share_file(SHARE_DISCLAIMER); } else { puts (" >NO"); } @@ -79,7 +79,7 @@ void menu_clui (void) sscanf(buff, " %c", &ch); if (ch == 'y' || ch == 'Y') { puts (" >yes"); - print_license(); + print_share_file(SHARE_LICENSE); } else { puts (" >NO"); } diff --git a/modules/menu/src/menu_iface.c b/modules/menu/src/menu_iface.c index 3394293..ebe3872 100644 --- a/modules/menu/src/menu_iface.c +++ b/modules/menu/src/menu_iface.c @@ -43,6 +43,28 @@ void menu_iface_init (void) menu_iface_variables.p = 0.16; } +void menu_iface_init_iface (void) +{ + switch (menu_iface_mode) { + case MENU_IFACE_CLUI: + break; + + case MENU_IFACE_TUI: + break; + } +} + +void menu_iface_cleanup (void) +{ + switch (menu_iface_mode) { + case MENU_IFACE_CLUI: + break; + + case MENU_IFACE_TUI: + break; + } +} + void menu_iface_board (int *level, int *rows, int *cols, int *mines) { *level = menu_iface_variables.level; @@ -101,9 +123,6 @@ void menu_iface (void) case MENU_IFACE_TUI: menu_tui(); break; - - case MENU_IFACE_GUI: - break; } } } diff --git a/modules/menu/src/menu_tui.c b/modules/menu/src/menu_tui.c index 43e4b92..c59a094 100644 --- a/modules/menu/src/menu_tui.c +++ b/modules/menu/src/menu_tui.c @@ -45,6 +45,8 @@ # error "cols max (tui)" #endif + # define BUFF_SIZE_TEXT (1048576) + /****************************************************************************** ******* static functions ***************************************************** @@ -98,14 +100,14 @@ void menu_tui (void) case 2: alx_pause_curses(); - print_disclaim(); + print_share_file(SHARE_DISCLAIMER); getchar(); alx_resume_curses(); break; case 3: alx_pause_curses(); - print_license(); + print_share_file(SHARE_LICENSE); getchar(); alx_resume_curses(); break; @@ -121,6 +123,9 @@ void menu_tui (void) ******************************************************************************/ static void menu_tui_continue (void) { + /* Hi scores tmp string */ + char str [BUFF_SIZE_TEXT]; + /* Menu dimensions & options */ WINDOW *win; int h; @@ -196,7 +201,8 @@ static void menu_tui_continue (void) case 5: alx_win_del(win); alx_pause_curses(); - read_scores(); + snprint_scores(str, BUFF_SIZE_TEXT); + printf("%s", str); getchar(); alx_resume_curses(); break; diff --git a/modules/menu/src/parser.c b/modules/menu/src/parser.c index 8449e59..2a20afb 100644 --- a/modules/menu/src/parser.c +++ b/modules/menu/src/parser.c @@ -10,10 +10,10 @@ * * * Standard * * * * * * * * * * * * * * * */ #include + /* FILE & fopen() & snprintf() & FILENAME_MAX */ #include /* exit() */ #include - #include /* * * * * * * * * * * * * Other * * * * * * * @@ -86,15 +86,15 @@ void parser (int argc, char *argv[]) break; case 'h': - print_help(); + print_share_file(SHARE_HELP); exit(EXIT_SUCCESS); case 'L': - print_license(); + print_share_file(SHARE_LICENSE); exit(EXIT_SUCCESS); case 'u': - print_usage(); + print_share_file(SHARE_USAGE); exit(EXIT_SUCCESS); case 'v': @@ -134,7 +134,7 @@ void parser (int argc, char *argv[]) /* getopt_long already printed an error message. */ default: - print_usage(); + print_share_file(SHARE_USAGE); exit(EXIT_FAILURE); } } @@ -176,8 +176,8 @@ static void parse_file (char* argument) } else { fclose(fp); - strcpy(saved_path, ""); - strcpy(saved_name, argument); + sprintf(saved_path, ""); + snprintf(saved_name, FILENAME_MAX, argument); } } @@ -185,9 +185,9 @@ static void parse_iface (char* argument) { menu_iface_mode = atoi(argument); player_iface_mode = menu_iface_mode; - if (menu_iface_mode < MENU_IFACE_CLUI || menu_iface_mode > MENU_IFACE_TUI) { + if (menu_iface_mode < MENU_IFACE_CLUI || menu_iface_mode > MENU_IFACE_GUI) { printf("--iface argument not valid\n"); - printf("It must be an integer [%i U %i]\n", MENU_IFACE_CLUI, MENU_IFACE_TUI); + printf("It must be an integer [%i U %i]\n", MENU_IFACE_CLUI, MENU_IFACE_GUI); exit(EXIT_FAILURE); } } diff --git a/modules/player/inc/player_clui.h b/modules/player/inc/player_clui.h index 6492054..2d92a38 100644 --- a/modules/player/inc/player_clui.h +++ b/modules/player/inc/player_clui.h @@ -58,8 +58,8 @@ void player_clui (const struct Game_Iface_Out *board, const char *subtitle, int *action); -void player_clui_save_name (const char *filepath, char *filename); -void player_clui_score_name (char *player_name); +void player_clui_save_name (const char *filepath, char *filename, int destsize); +void player_clui_score_name (char *player_name, int destsize); /****************************************************************************** diff --git a/modules/player/inc/player_iface.h b/modules/player/inc/player_iface.h index 2430002..a60a0ac 100644 --- a/modules/player/inc/player_iface.h +++ b/modules/player/inc/player_iface.h @@ -77,8 +77,8 @@ int player_iface_start (int *pos_row, int *pos_col); void player_iface (const struct Game_Iface_Out *game_iface_out, const struct Game_Iface_Score *game_iface_score, struct Game_Iface_In *game_iface_in); -void player_iface_save_name (const char *filepath, char *filename); -void player_iface_score_name (char *player_name); +void player_iface_save_name (const char *filepath, char *filename, int destsize); +void player_iface_score_name (char *player_name, int destsize); void player_iface_cleanup (void); diff --git a/modules/player/inc/player_tui.h b/modules/player/inc/player_tui.h index 4070147..6c0e7e2 100644 --- a/modules/player/inc/player_tui.h +++ b/modules/player/inc/player_tui.h @@ -103,8 +103,8 @@ int player_tui (const struct Game_Iface_Out *board, const char *subtitle, int *action); -void player_tui_save_name (const char *filepath, char *filename); -void player_tui_score_name (char *player_name); +void player_tui_save_name (const char *filepath, char *filename, int destsize); +void player_tui_score_name (char *player_name, int destsize); void player_tui_cleanup (void); diff --git a/modules/player/obj/Makefile b/modules/player/obj/Makefile index 4cef87b..73c71c4 100644 --- a/modules/player/obj/Makefile +++ b/modules/player/obj/Makefile @@ -7,6 +7,7 @@ LIBALX_INC_DIR = $(LIBALX_DIR)/inc/ GAME_INC_DIR = $(GAME_DIR)/inc/ +MENU_INC_DIR = $(MENU_DIR)/inc/ INC_DIR = $(PLAY_DIR)/inc/ SRC_DIR = $(PLAY_DIR)/src/ @@ -38,7 +39,7 @@ PLAYTUI_INC = player_tui.h player_iface.h PLAYTUI_DEPS = $(SRC_DIR)/player_tui.c \ $(patsubst %,$(INC_DIR)/%,$(PLAYTUI_INC)) \ $(patsubst %,$(LIBALX_INC_DIR)/%,$(PLAYTUI_INC_LIBALX)) \ - $(patsubst %,$(GAME_INC_DIR)/%,$(PLAYCLUI_INC_GAME)) + $(patsubst %,$(GAME_INC_DIR)/%,$(PLAYTUI_INC_GAME)) PLAYTUI_INC_DIRS = -I $(INC_DIR) \ -I $(LIBALX_INC_DIR) \ -I $(GAME_INC_DIR) @@ -51,24 +52,21 @@ all: $(ALL) player_mod.o: $(_ALL) $(Q)$(LD) -r $^ -o $@ - @echo "\tLD $@" + @echo "\tLD $@" @echo "" player_iface.o: $(PLAYI_DEPS) - $(Q)$(CC) $(CFLAGS) $(PLAYI_INC_DIRS) -c $< -o $@ $(LIBS) - @echo "\tCC $<" - @echo "" + $(Q)$(CC) $(CFLAGS) $(PLAYI_INC_DIRS) -c $< -o $@ + @echo "\tCC $@" player_clui.o: $(PLAYCLUI_DEPS) - $(Q)$(CC) $(CFLAGS) $(PLAYCLUI_INC_DIRS) -c $< -o $@ $(LIBS) - @echo "\tCC $<" - @echo "" + $(Q)$(CC) $(CFLAGS) $(PLAYCLUI_INC_DIRS) -c $< -o $@ + @echo "\tCC $@" player_tui.o: $(PLAYTUI_DEPS) - $(Q)$(CC) $(CFLAGS) $(PLAYTUI_INC_DIRS) -c $< -o $@ $(LIBS) - @echo "\tCC $<" - @echo "" + $(Q)$(CC) $(CFLAGS) $(PLAYTUI_INC_DIRS) -c $< -o $@ + @echo "\tCC $@" clean: diff --git a/modules/player/src/player_clui.c b/modules/player/src/player_clui.c index 7e47d2f..f1d890c 100644 --- a/modules/player/src/player_clui.c +++ b/modules/player/src/player_clui.c @@ -10,7 +10,7 @@ * * * Standard * * * * * * * * * * * * * * * */ #include - /* sprintf() */ + /* printf() & sscanf() */ #include #include @@ -104,16 +104,16 @@ void player_clui (const struct Game_Iface_Out *board, oldaction = *action; } -void player_clui_save_name (const char *filepath, char *filename) +void player_clui_save_name (const char *filepath, char *filename, int destsize) { puts("File name:"); - scanf(" %100c ", filename); + fgets(filename, destsize, stdin); } -void player_clui_score_name (char *player_name) +void player_clui_score_name (char *player_name, int destsize) { puts("Your name:"); - scanf(" %100c ", player_name); + fgets(player_name, destsize, stdin); } diff --git a/modules/player/src/player_iface.c b/modules/player/src/player_iface.c index 83edc99..83c82c4 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 /* * * * * * * * * * @@ -44,22 +44,19 @@ int player_iface_mode; * * * Static * * * * * * * * * * * * * * * */ static struct Player_Iface_Position player_iface_position; +static int player_action; /****************************************************************************** ******* static functions ***************************************************** ******************************************************************************/ /* Actions */ -static void player_iface_act (struct Game_Iface_In *game_iface_in, - int action); +static void player_iface_act (struct Game_Iface_In *game_iface_in); /* Actions: game iface */ -static void player_iface_act_start (int action); - -static void player_iface_act_play (struct Game_Iface_In *game_iface_in, - int action); -static void player_iface_act_game (struct Game_Iface_In *game_iface_in, - int action); +static void player_iface_act_start (void); +static void player_iface_act_play (struct Game_Iface_In *game_iface_in); +static void player_iface_act_game (struct Game_Iface_In *game_iface_in); /* Actions: player iface */ static void player_iface_move_up (void); @@ -91,10 +88,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; @@ -102,7 +99,6 @@ int player_iface_start (int *pos_row, int *pos_col) player_iface_position.highlight = false; /* Loop until first step */ - int player_action; do { switch (player_iface_mode) { case PLAYER_IFACE_CLUI: @@ -116,7 +112,7 @@ int player_iface_start (int *pos_row, int *pos_col) break; } - player_iface_act_start(player_action); + player_iface_act_start(); } while (player_action != PLAYER_IFACE_ACT_STEP && player_action != PLAYER_IFACE_ACT_QUIT); @@ -147,15 +143,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 */ @@ -169,16 +165,15 @@ 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 */ - int player_action; switch (player_iface_mode) { case PLAYER_IFACE_CLUI: player_clui(game_iface_out, &player_iface_position, @@ -191,31 +186,31 @@ void player_iface (const struct Game_Iface_Out *game_iface_out, break; } - player_iface_act(game_iface_in, player_action); + player_iface_act(game_iface_in); } -void player_iface_save_name (const char *filepath, char *filename) +void player_iface_save_name (const char *filepath, char *filename, int destsize) { switch (player_iface_mode) { case PLAYER_IFACE_CLUI: - player_clui_save_name(filepath, filename); + player_clui_save_name(filepath, filename, destsize); break; case PLAYER_IFACE_TUI: - player_tui_save_name(filepath, filename); + player_tui_save_name(filepath, filename, destsize); break; } } -void player_iface_score_name (char *player_name) +void player_iface_score_name (char *player_name, int destsize) { switch (player_iface_mode) { case PLAYER_IFACE_CLUI: - player_clui_score_name(player_name); + player_clui_score_name(player_name, destsize); break; case PLAYER_IFACE_TUI: - player_tui_score_name(player_name); + player_tui_score_name(player_name, destsize); break; } } @@ -240,15 +235,14 @@ void player_iface_cleanup (void) /* * * * * * * * * * * * * Actions * * * * * * * * * * * * * * * */ -static void player_iface_act (struct Game_Iface_In *game_iface_in, - int player_action) +static void player_iface_act (struct Game_Iface_In *game_iface_in) { switch (player_action) { case PLAYER_IFACE_ACT_STEP: case PLAYER_IFACE_ACT_FLAG: case PLAYER_IFACE_ACT_FLAG_POSSIBLE: case PLAYER_IFACE_ACT_RM_FLAG: - player_iface_act_play(game_iface_in, player_action); + player_iface_act_play(game_iface_in); break; case PLAYER_IFACE_ACT_PAUSE: @@ -259,7 +253,7 @@ static void player_iface_act (struct Game_Iface_In *game_iface_in, case PLAYER_IFACE_ACT_XYZZY_P: case PLAYER_IFACE_ACT_XYZZY_NP: case PLAYER_IFACE_ACT_QUIT: - player_iface_act_game(game_iface_in, player_action); + player_iface_act_game(game_iface_in); break; case PLAYER_IFACE_ACT_MOVE_UP: @@ -284,7 +278,7 @@ static void player_iface_act (struct Game_Iface_In *game_iface_in, } } -static void player_iface_act_start (int player_action) +static void player_iface_act_start (void) { switch (player_action) { case PLAYER_IFACE_ACT_STEP: @@ -316,8 +310,7 @@ static void player_iface_act_start (int player_action) /* * * * * * * * * * * * * Actions: game iface * * * * * * * * * * * * * * */ -static void player_iface_act_play (struct Game_Iface_In *game_iface_in, - int player_action) +static void player_iface_act_play (struct Game_Iface_In *game_iface_in) { switch (player_action) { case PLAYER_IFACE_ACT_STEP: @@ -344,8 +337,7 @@ static void player_iface_act_play (struct Game_Iface_In *game_iface_in, game_iface_in->action = GAME_IFACE_ACT_PLAY; } -static void player_iface_act_game (struct Game_Iface_In *game_iface_in, - int player_action) +static void player_iface_act_game (struct Game_Iface_In *game_iface_in) { switch (player_action) { case PLAYER_IFACE_ACT_PAUSE: diff --git a/modules/player/src/player_tui.c b/modules/player/src/player_tui.c index f6ea181..aaaeb3a 100644 --- a/modules/player/src/player_tui.c +++ b/modules/player/src/player_tui.c @@ -169,7 +169,7 @@ int player_tui (const struct Game_Iface_Out *board, *action = usr_input(); } -void player_tui_save_name (const char *filepath, char *filename) +void player_tui_save_name (const char *filepath, char *filename, int destsize) { /* Input box */ int w; @@ -181,7 +181,7 @@ void player_tui_save_name (const char *filepath, char *filename) alx_w_getfname(filepath, filename, false, w, r, "File name:", NULL); } -void player_tui_score_name (char *player_name) +void player_tui_score_name (char *player_name, int destsize) { /* Input box */ int w; @@ -190,7 +190,7 @@ void player_tui_score_name (char *player_name) r = 10; /* Request name */ - alx_w_getstr(player_name, w, r, "Your name:", NULL); + alx_w_getstr(player_name, destsize, w, r, "Your name:", NULL); } void player_tui_cleanup (void) diff --git a/modules/save/inc/score.h b/modules/save/inc/score.h index 3680abe..e81c6d1 100644 --- a/modules/save/inc/score.h +++ b/modules/save/inc/score.h @@ -56,7 +56,7 @@ extern char var_hiscores_expert_name [FILENAME_MAX]; ******************************************************************************/ void score_init (void); void save_score (const struct Game_Iface_Score *game_iface_score); -void read_scores (void); +void snprint_scores (char *dest, int destsize); /****************************************************************************** diff --git a/modules/save/obj/Makefile b/modules/save/obj/Makefile index e4cbf5d..016a57b 100644 --- a/modules/save/obj/Makefile +++ b/modules/save/obj/Makefile @@ -45,19 +45,17 @@ all: $(ALL) save_mod.o: $(_ALL) $(Q)$(LD) -r $^ -o $@ - @echo "\tLD $@" + @echo "\tLD $@" @echo "" save.o: $(SAVE_DEPS) - $(Q)$(CC) $(CFLAGS) $(SAVE_INC_DIRS) -c $< -o $@ $(LIBS) - @echo "\tCC $<" - @echo "" + $(Q)$(CC) $(CFLAGS) $(SAVE_INC_DIRS) -c $< -o $@ + @echo "\tCC $@" score.o: $(SCORE_DEPS) - $(Q)$(CC) $(CFLAGS) $(SCORE_INC_DIRS) -c $< -o $@ $(LIBS) - @echo "\tCC $<" - @echo "" + $(Q)$(CC) $(CFLAGS) $(SCORE_INC_DIRS) -c $< -o $@ + @echo "\tCC $@" clean: diff --git a/modules/save/src/save.c b/modules/save/src/save.c index af18609..c25010f 100644 --- a/modules/save/src/save.c +++ b/modules/save/src/save.c @@ -13,12 +13,10 @@ #include /* bool */ #include - /* fscanf() & fprintf() & FILE & FILENAME_MAX */ + /* fscanf() & fprintf() & FILE & FILENAME_MAX & snprintf() */ #include /* getenv() */ #include - /* strcpy() & strcat() */ - #include /* mkdir */ #if defined OS_LINUX #include @@ -51,19 +49,10 @@ char saved_name [FILENAME_MAX]; ******************************************************************************/ void save_init (void) { - strcpy(home_path, getenv(ENV_HOME)); - - strcpy(user_game_path, home_path); - strcat(user_game_path, "/"); - strcat(user_game_path, USER_GAME_DIR); - strcat(user_game_path, "/"); - - strcpy(saved_path, home_path); - strcat(saved_path, "/"); - strcat(saved_path, USER_SAVED_DIR); - strcat(saved_path, "/"); - - strcpy(saved_name, ""); + snprintf(home_path, FILENAME_MAX, "%s/", getenv(ENV_HOME)); + snprintf(user_game_path, FILENAME_MAX, "%s/%s/", home_path, USER_GAME_DIR); + snprintf(saved_path, FILENAME_MAX, "%s/%s/", home_path, USER_SAVED_DIR); + sprintf(saved_name, ""); int err; #if defined OS_LINUX @@ -99,10 +88,7 @@ void save_init (void) void save_clr (void) { - strcpy(saved_path, home_path); - strcat(saved_path, "/"); - strcat(saved_path, USER_SAVED_DIR); - strcat(saved_path, "/"); + snprintf(saved_path, FILENAME_MAX, "%s/%s/", home_path, USER_SAVED_DIR); } void load_game_file (void) @@ -113,8 +99,7 @@ void load_game_file (void) int i; int j; - strcpy(file_name, saved_path); - strcat(file_name, saved_name); + snprintf(file_name, FILENAME_MAX, "%s/%s", saved_path, saved_name); fp = fopen(file_name, "r"); if (fp) { @@ -146,6 +131,7 @@ void load_game_file (void) void save_game_file (char *filepath) { char file_name [FILENAME_MAX]; + char tmp [FILENAME_MAX]; char file_num [6] = ""; FILE *fp; @@ -160,23 +146,22 @@ void save_game_file (char *filepath) /* Default path & name */ save_clr(); - strcpy(saved_name, SAVED_NAME_DEFAULT); + snprintf(saved_name, FILENAME_MAX, "%s", SAVED_NAME_DEFAULT); /* Request file name */ - player_iface_save_name(filepath, saved_name); + player_iface_save_name(filepath, saved_name, FILENAME_MAX); /* Look for an unused name of the type 'name_XXX.mine'. */ bool x; x = true; for (i = 0; x; i++) { if (filepath == NULL) { - strcpy(file_name, saved_path); + snprintf(file_name, FILENAME_MAX, "%s/%s%s%s", + saved_path, saved_name, file_num, FILE_EXTENSION); } else { - strcpy(file_name, filepath); + snprintf(file_name, FILENAME_MAX, "%s/%s%s%s", + filepath, saved_name, file_num, FILE_EXTENSION); } - strcat(file_name, saved_name); - strcat(file_name, file_num); - strcat(file_name, FILE_EXTENSION); fp = fopen(file_name, "r"); if (fp) { @@ -188,8 +173,9 @@ void save_game_file (char *filepath) file_num[4] = '\0'; } else { x = false; - strcat(saved_name, file_num); - strcat(saved_name, FILE_EXTENSION); + snprintf(tmp, FILENAME_MAX, "%s%s%s", + saved_name, file_num, FILE_EXTENSION); + snprintf(saved_name, FILENAME_MAX, "%s", tmp); } } diff --git a/modules/save/src/score.c b/modules/save/src/score.c index 337cdcf..8aa837e 100644 --- a/modules/save/src/score.c +++ b/modules/save/src/score.c @@ -9,12 +9,10 @@ /* * * * * * * * * * * * * Standard * * * * * * * * * * * * * * * */ - /* fscanf() & fprintf() & FILE & FILENAME_MAX */ + /* fscanf() & fprintf() & FILE & FILENAME_MAX & snprintf() */ #include /* exit() */ #include - /* strcpy() & strcat() */ - #include /* time_t & time() & struct tm & localtime() */ #include @@ -37,6 +35,7 @@ ******* macros *************************************************************** ******************************************************************************/ # define BUFF_SIZE (1024) + # define BUFF_SIZE_TEXT (1048576) /****************************************************************************** @@ -57,7 +56,7 @@ char var_hiscores_expert_name [FILENAME_MAX]; /****************************************************************************** ******* static functions ***************************************************** ******************************************************************************/ -static void read_scores_file (char *file_name); +static void snprint_scores_file (char *dest, int destsize, char *file_name); /****************************************************************************** @@ -65,57 +64,44 @@ static void read_scores_file (char *file_name); ******************************************************************************/ void score_init (void) { - strcpy(var_path, INSTALL_VAR_DIR); - strcat(var_path, "/"); - strcat(var_path, VAR_DIR); - strcat(var_path, "/"); - - strcpy(var_hiscores_path, var_path); - strcat(var_hiscores_path, "/"); - strcat(var_hiscores_path, HISCORES_DIR); - strcat(var_hiscores_path, "/"); - - strcpy(var_boards_beginner_path, var_path); - strcat(var_boards_beginner_path, "/"); - strcat(var_boards_beginner_path, BOARDS_BEGINNER_DIR); - strcat(var_boards_beginner_path, "/"); - - strcpy(var_boards_intermediate_path, var_path); - strcat(var_boards_intermediate_path, "/"); - strcat(var_boards_intermediate_path, BOARDS_INTERMEDIATE_DIR); - strcat(var_boards_intermediate_path, "/"); - - strcpy(var_boards_expert_path, var_path); - strcat(var_boards_expert_path, "/"); - strcat(var_boards_expert_path, BOARDS_EXPERT_DIR); - strcat(var_boards_expert_path, "/"); - - strcpy(var_boards_custom_path, var_path); - strcat(var_boards_custom_path, "/"); - strcat(var_boards_custom_path, BOARDS_CUSTOM_DIR); - strcat(var_boards_custom_path, "/"); - - strcpy(var_hiscores_beginner_name, HISCORES_BEGINNER_NAME); - strcpy(var_hiscores_intermediate_name, HISCORES_INTERMEDIATE_NAME); - strcpy(var_hiscores_expert_name, HISCORES_EXPERT_NAME); + snprintf(var_path, FILENAME_MAX, "%s/%s/", + INSTALL_VAR_DIR, VAR_DIR); + snprintf(var_hiscores_path, FILENAME_MAX, "%s/%s/", + var_path, HISCORES_DIR); + snprintf(var_boards_beginner_path, FILENAME_MAX, "%s/%s/", + var_path, BOARDS_BEGINNER_DIR); + snprintf(var_boards_intermediate_path, FILENAME_MAX, "%s/%s/", + var_path, BOARDS_INTERMEDIATE_DIR); + snprintf(var_boards_expert_path, FILENAME_MAX, "%s/%s/", + var_path, BOARDS_EXPERT_DIR); + snprintf(var_boards_custom_path, FILENAME_MAX, "%s/%s/", + var_path, BOARDS_CUSTOM_DIR); + snprintf(var_hiscores_beginner_name, FILENAME_MAX, "%s", + HISCORES_BEGINNER_NAME); + snprintf(var_hiscores_intermediate_name, FILENAME_MAX, "%s", + HISCORES_INTERMEDIATE_NAME); + snprintf(var_hiscores_expert_name, FILENAME_MAX, "%s", + HISCORES_EXPERT_NAME); } void save_score (const struct Game_Iface_Score *game_iface_score) { /* File name */ char file_name [FILENAME_MAX]; - strcpy(file_name, var_hiscores_path); switch (game_iface_score->level) { case GAME_IFACE_LEVEL_BEGINNER: - strcat(file_name, var_hiscores_beginner_name); + snprintf(file_name, FILENAME_MAX, "%s/%s", + var_hiscores_path, var_hiscores_beginner_name); break; case GAME_IFACE_LEVEL_INTERMEDIATE: - strcat(file_name, var_hiscores_intermediate_name); + snprintf(file_name, FILENAME_MAX, "%s/%s", + var_hiscores_path, var_hiscores_intermediate_name); break; case GAME_IFACE_LEVEL_EXPERT: - strcat(file_name, var_hiscores_expert_name); + snprintf(file_name, FILENAME_MAX, "%s/%s", + var_hiscores_path, var_hiscores_expert_name); break; } @@ -127,7 +113,7 @@ void save_score (const struct Game_Iface_Score *game_iface_score) /* Player name (foo is default) */ char player_name [BUFF_SIZE]; - player_iface_score_name(player_name); + player_iface_score_name(player_name, BUFF_SIZE); /* Write to file (append) */ FILE *fp; @@ -157,37 +143,48 @@ void save_score (const struct Game_Iface_Score *game_iface_score) } } -void read_scores (void) +void snprint_scores (char *dest, int destsize) { /* File */ char file_name [FILENAME_MAX]; + /* Tmp strings */ + char tmp1 [BUFF_SIZE_TEXT]; + char tmp2 [BUFF_SIZE_TEXT]; + /* Beginner */ - strcpy(file_name, var_hiscores_path); - strcat(file_name, var_hiscores_beginner_name); - read_scores_file(file_name); + snprintf(file_name, FILENAME_MAX, "%s/%s", + var_hiscores_path, var_hiscores_beginner_name); + snprint_scores_file(dest, destsize, file_name); /* Intermediate */ - strcpy(file_name, var_hiscores_path); - strcat(file_name, var_hiscores_intermediate_name); - read_scores_file(file_name); + snprintf(file_name, FILENAME_MAX, "%s/%s", + var_hiscores_path, var_hiscores_intermediate_name); + snprint_scores_file(tmp1, destsize, file_name); + snprintf(tmp2, destsize, "%s%s", dest, tmp1); + snprintf(dest, destsize, "%s", tmp2); /* Expert */ - strcpy(file_name, var_hiscores_path); - strcat(file_name, var_hiscores_expert_name); - read_scores_file(file_name); + snprintf(file_name, FILENAME_MAX, "%s/%s", + var_hiscores_path, var_hiscores_expert_name); + snprint_scores_file(tmp1, destsize, file_name); + snprintf(tmp2, destsize, "%s%s", dest, tmp1); + snprintf(dest, destsize, "%s", tmp2); } /****************************************************************************** ******* static functions ***************************************************** ******************************************************************************/ -static void read_scores_file (char *file_name) +static void snprint_scores_file (char *dest, int destsize, char *file_name) { /* File */ FILE *fp; int c; + /* Tmp string */ + char tmp [BUFF_SIZE_TEXT]; + /* Score variables */ char title [BUFF_SIZE]; char name [BUFF_SIZE]; @@ -210,9 +207,11 @@ static void read_scores_file (char *file_name) fscanf(fp, " "); /* Print */ - printf("_______________________________________________________\n"); - printf("%s\n\n", title); - printf("name date clicks time file\n\n"); + snprintf(dest, destsize, + "_______________________________________________________\n" + "%s\n\n" + "name date clicks time file\n\n", + title); while ((c = getc(fp)) != EOF){ ungetc(c, fp); @@ -242,12 +241,17 @@ static void read_scores_file (char *file_name) secs = (time % 60); /* Print */ - printf("%s\n\t", name); - printf("%4i/%2i/%2i %i %i:%02i:%02i \t%s\n\n", - year, 1 + mon, day, - clicks, - hours, mins, secs, - file); + snprintf(tmp, BUFF_SIZE_TEXT, + "%s" + "%s\n" + " %4i/%2i/%2i %i %i:%02i:%02i \t%s\n\n", + dest, + name, + year, 1 + mon, day, + clicks, + hours, mins, secs, + file); + snprintf(dest, BUFF_SIZE_TEXT, "%s", tmp); } fclose(fp); diff --git a/modules/xyzzy/obj/Makefile b/modules/xyzzy/obj/Makefile index 0a79bda..2d96e2b 100644 --- a/modules/xyzzy/obj/Makefile +++ b/modules/xyzzy/obj/Makefile @@ -30,14 +30,13 @@ all: $(ALL) xyzzy_mod.o: $(_ALL) $(Q)$(LD) -r $^ -o $@ - @echo "\tLD $@" + @echo "\tLD $@" @echo "" xyzzy.o: $(XYZZY_DEPS) - $(Q)$(CC) $(CFLAGS) $(XYZZY_INC_DIRS) -c $< -o $@ $(LIBS) - @echo "\tCC $<" - @echo "" + $(Q)$(CC) $(CFLAGS) $(XYZZY_INC_DIRS) -c $< -o $@ + @echo "\tCC $@" clean: diff --git a/obj/Makefile b/obj/Makefile index 3a6f482..70823a4 100644 --- a/obj/Makefile +++ b/obj/Makefile @@ -60,8 +60,8 @@ MAIN_INC_DIRS = -I $(LIBALX_INC_DIR) \ all: $(ALL) main.o: $(MAIN_DEPS) - $(Q)$(CC) $(CFLAGS) $(MAIN_INC_DIRS) -c $< -o $@ $(LIBS) - @echo "\tCC $<" + $(Q)$(CC) $(CFLAGS) $(MAIN_INC_DIRS) -c $< -o $@ + @echo "\tCC $@" @echo "" diff --git a/share/README.txt b/share/README.txt index edd8a2b..ebdf9c3 100644 --- a/share/README.txt +++ b/share/README.txt @@ -39,6 +39,10 @@ If you make a version, please tell me. I would like to see it. Thank you. +site: + https://github.com/AlejandroColomar/mine-sweeper + + COMPILE: - linux: $ make diff --git a/src/main.c b/src/main.c index 3559ef7..fbb8561 100644 --- a/src/main.c +++ b/src/main.c @@ -9,6 +9,7 @@ /* * * * * * * * * * * * * Standard * * * * * * * * * * * * * * * */ + #include /* getchar() */ #include @@ -31,7 +32,7 @@ /****************************************************************************** ******* static functions ***************************************************** ******************************************************************************/ -void init_all (void); +void init_all (int *argc, char *(*argv[])); void cleanup (void); @@ -40,13 +41,10 @@ void cleanup (void); ******************************************************************************/ int main (int argc, char *argv[]) { - init_all(); - - /* Parse command line options */ - parser(argc, argv); + init_all(&argc, &argv); /* Print copyright () and wait for any key to continue */ - print_cpright(); + print_share_file(SHARE_COPYRIGHT); getchar(); /* Start () */ @@ -64,12 +62,15 @@ int main (int argc, char *argv[]) /****************************************************************************** ******* static functions ***************************************************** ******************************************************************************/ -void init_all (void) +void init_all (int *argc, char *(*argv[])) { - /* Init modules */ + /* Init gtk & curses */ + gtk_init_check(argc, argv); alx_start_curses(); - menu_iface_init(); alx_pause_curses(); + + /* Init modules */ + menu_iface_init(); game_init(); about_init(); save_init(); @@ -80,10 +81,19 @@ void init_all (void) flag_exit = false; menu_iface_mode = MENU_IFACE_TUI; player_iface_mode = PLAYER_IFACE_TUI; + + /* Parse command line options */ + parser(*argc, *argv); + + /* Init iface */ + menu_iface_init_iface(); } void cleanup (void) { + /* Clean iface */ + menu_iface_cleanup(); + /* End curses */ alx_resume_curses(); alx_end_curses(); -- cgit v1.2.3 From c34944eb025257e7d9d23c097681599f1e43af2e Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Fri, 21 Sep 2018 17:08:00 +0200 Subject: V 3.2 Version number --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 3974868..a84bb9a 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ #!/usr/bin/make -f -VERSION = 4 -PATCHLEVEL = ~a3 +VERSION = 3 +PATCHLEVEL = .2 SUBLEVEL = EXTRAVERSION = NAME = graphic -- cgit v1.2.3 From 14e49e4aa0a6e8e682c1388024d3ad65137f6137 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Fri, 21 Sep 2018 17:10:13 +0200 Subject: V 3.2 --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index a84bb9a..fee3fc3 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ VERSION = 3 PATCHLEVEL = .2 SUBLEVEL = EXTRAVERSION = -NAME = graphic +NAME = installable export VERSION export PATCHLEVEL -- cgit v1.2.3 From 790c3ee817396122c630ca82b48856fc0bc798e0 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Fri, 21 Sep 2018 17:14:14 +0200 Subject: V 3.2 --- Makefile | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index fee3fc3..f8dc20a 100644 --- a/Makefile +++ b/Makefile @@ -141,25 +141,23 @@ export LD ################################################################################ CFLAGS = -std=c11 -CFLAGS += `pkg-config --cflags gtk+-2.0` CFLAGS += -D PROG_VERSION=\"$(PROGRAMVERSION)\" CFLAGS += -D 'INSTALL_SHARE_DIR="$(INSTALL_SHARE_DIR)"' CFLAGS += -D SHARE_DIR=\"$(SHARE_DIR)\" CFLAGS += -D 'INSTALL_VAR_DIR="$(INSTALL_VAR_DIR)"' CFLAGS += -D VAR_DIR=\"$(VAR_DIR)\" -GTK_LIBS = `pkg-config --libs gtk+-2.0` ifeq ($(OS), linux) CFLAGS += -D OS_LINUX - LIBS = -l m -l ncursesw $(GTK_LIBS) + LIBS = -l m -l ncursesw else ifeq ($(OS), win) CFLAGS += -D OS_WIN # curses CFLAGS += -D _XOPEN_SOURCE=500 -I /mingw/include/ncursesw -I /mingw/include CURSES_LIBS = -L /mingw/lib -l ncursesw -l psapi - LIBS = -static -l m $(CURSES_LIBS) $(GTK_LIBS) + LIBS = -static -l m $(CURSES_LIBS) endif export CFLAGS -- cgit v1.2.3