From: Christian Heller Date: Wed, 23 Jul 2014 03:16:12 +0000 (+0200) Subject: Use not f_name variable but __func__, standardize function name writing. X-Git-Tag: tce~700 X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/static/%7B%7Byoutube_prefix%7D%7D%7B%7Bvideo_id%7D%7D?a=commitdiff_plain;h=1cb57a35a3b3cc4ec8870531ca254a655c0bdda2;p=plomrogue Use not f_name variable but __func__, standardize function name writing. --- diff --git a/TODO b/TODO index 42a2cad..14006bf 100644 --- a/TODO +++ b/TODO @@ -4,8 +4,6 @@ IN GENERAL: - expand use of hardcoded_strings module(s) -- use __func__ instead of f_name - BOTH SERVER/CLIENT: - make server and client communicate by specific world state info requests diff --git a/src/client/array_append.c b/src/client/array_append.c index f4391c7..37fde0e 100644 --- a/src/client/array_append.c +++ b/src/client/array_append.c @@ -12,10 +12,9 @@ extern void array_append(uint32_t old_n, size_t region_size, void * new_region, void ** ptr_old_array) { - char * f_name = "array_append()"; uint32_t old_size = old_n * region_size; uint32_t new_size = old_size + region_size; - char * new_array = try_malloc(new_size, f_name); + char * new_array = try_malloc(new_size, __func__); memcpy(new_array, * ptr_old_array, old_size); memcpy(new_array + (old_n * region_size), new_region, region_size); free(* ptr_old_array); diff --git a/src/client/cleanup.c b/src/client/cleanup.c index 7611139..3499246 100644 --- a/src/client/cleanup.c +++ b/src/client/cleanup.c @@ -18,7 +18,6 @@ static uint32_t cleanup_flags = 0x0000; extern void cleanup() { - char * f_name = "cleanup()"; free(world.map.cells); free(world.log); free(world.player_inventory); @@ -36,11 +35,11 @@ extern void cleanup() } if (cleanup_flags & CLEANUP_SERVER_IN) { - try_fclose(world.file_server_in, f_name); + try_fclose(world.file_server_in, __func__); } if (cleanup_flags & CLEANUP_SERVER_OUT) { - try_fclose(world.file_server_out, f_name); + try_fclose(world.file_server_out, __func__); } } diff --git a/src/client/control.c b/src/client/control.c index 17a7632..3d9c490 100644 --- a/src/client/control.c +++ b/src/client/control.c @@ -179,16 +179,15 @@ static uint8_t set_string_if_char_match(char c, char c_to_match, static char * build_server_message_with_argument(struct Command * cmd) { - char * f_name = "build_server_message_with_argument()"; uint8_t command_size = strlen(cmd->server_msg); char * arg_str = ""; uint8_t arg_size = 0; if ('i' == cmd->arg) { arg_size = 3; - arg_str = try_malloc(arg_size + 1, f_name); + arg_str = try_malloc(arg_size + 1, __func__); int test = sprintf(arg_str, "%d",world.player_inventory_select); - exit_trouble(test < 0, f_name, "sprintf()"); + exit_trouble(test < 0, __func__, "sprintf"); } else if ( set_string_if_char_match(cmd->arg, 'd', &arg_str, "east") || set_string_if_char_match(cmd->arg, 'c', &arg_str, "south-east") @@ -203,9 +202,9 @@ static char * build_server_message_with_argument(struct Command * cmd) { exit_err(1, "Illegal server command argument."); } - char * msg = try_malloc(command_size + 1 + arg_size + 1, f_name); + char * msg = try_malloc(command_size + 1 + arg_size + 1, __func__); int test = sprintf(msg, "%s %s", cmd->server_msg, arg_str); - exit_trouble(test < 0, f_name, "sprintf()"); + exit_trouble(test < 0, __func__, "sprintf"); if ('i' == cmd->arg) { free(arg_str); diff --git a/src/client/draw_wins.c b/src/client/draw_wins.c index f6ac7e2..bb86bb9 100644 --- a/src/client/draw_wins.c +++ b/src/client/draw_wins.c @@ -69,7 +69,6 @@ static void draw_titled_keybinding_list(char * title, struct Win * win, static void try_resize_winmap(struct Win * win, int new_size_y, int new_size_x) { - char * f_name = "try_resize_winmap()"; if (win->winmap_size.y >= new_size_y && win->winmap_size.x >= new_size_x) { return; @@ -84,7 +83,7 @@ static void try_resize_winmap(struct Win * win, int new_size_y, int new_size_x) } chtype * old_winmap = win->winmap; uint32_t new_size = sizeof(chtype) * new_size_y * new_size_x; - win->winmap = try_malloc(new_size, f_name); + win->winmap = try_malloc(new_size, __func__); uint16_t y, x; for (y = 0; y < new_size_y; y++) { @@ -178,15 +177,14 @@ static void add_line_long(struct Win * win, char * line, attr_t attri) static void add_line_compact(struct Win * win, char * line, attr_t attri, uint16_t * offset, uint8_t last) { - char * f_name = "add_line_compact()"; uint16_t y_start = win->winmap_size.y - (win->winmap_size.y > 0); try_resize_winmap(win, y_start + 1, win->frame_size.x); uint16_t len_line = strlen(line); char * separator = last ? "" : " / "; uint32_t len_line_new = len_line + strlen(separator); - char * line_new = try_malloc(len_line_new, f_name); + char * line_new = try_malloc(len_line_new, __func__); int test = sprintf(line_new, "%s%s", line, separator); - exit_trouble(test < 0, f_name, "sprintf()"); + exit_trouble(test < 0, __func__, "sprintf"); uint16_t x = 0; uint16_t y; uint32_t z; @@ -260,12 +258,11 @@ static void draw_text_from_bottom(struct Win * win, char * text) static char * get_kb_line(struct KeyBinding * kb) { - char * f_name = "get_kb_line()"; char * keyname = get_keyname_to_keycode(kb->keycode); uint16_t size = strlen(keyname) + 3 + strlen(kb->command->dsc_long) + 1; - char * kb_line = try_malloc(size, f_name); + char * kb_line = try_malloc(size, __func__); int test = sprintf(kb_line, "%s - %s", keyname, kb->command->dsc_long); - exit_trouble(test < 0, f_name, "sprintf()"); + exit_trouble(test < 0, __func__, "sprintf"); free(keyname); return kb_line; } @@ -359,14 +356,13 @@ extern void draw_win_map(struct Win * win) extern void draw_win_info(struct Win * win) { - char * f_name = "draw_win_info()"; char * dsc_turn = "Turn: "; char * dsc_hitpoints = "\nHitpoints: "; uint16_t maxl = strlen(dsc_turn) + 5 + strlen(dsc_hitpoints) + 3; - char * text = try_malloc(maxl + 1, f_name); + char * text = try_malloc(maxl + 1, __func__); int test = sprintf(text, "%s%d%s%d", dsc_turn, world.turn, dsc_hitpoints, world.player_lifepoints); - exit_trouble(test < 0, f_name, "sprintf()"); + exit_trouble(test < 0, __func__, "sprintf"); add_text_with_linebreaks(win, text); free(text); } @@ -462,19 +458,18 @@ extern void draw_winconf_keybindings(struct Win * win) extern void draw_winconf_geometry(struct Win * win) { - char * f_name = "draw_winconf_geometry()"; char * title = "Window's geometry:\n\n"; char * h_title = "Height to save: "; char h_value[6 + 1]; /* 6: int16_t value max strlen */ int test = sprintf(h_value, "%d", win->target_height); - exit_trouble(test < 0, f_name, "sprintf()"); + exit_trouble(test < 0, __func__, "sprintf"); char * h_plus = " (width in cells)\n\n"; char * h_minus = " (negative diff: cells to screen width)\n\n"; char * h_type = (1 == win->target_height_type) ? h_minus : h_plus; char * w_title = "Width to save: "; char w_value[6 + 1]; test = sprintf(w_value, "%d", win->target_width); - exit_trouble(test < 0, f_name, "sprintf()"); + exit_trouble(test < 0, __func__, "sprintf"); char * w_plus = "(height in cells)\n\n"; char * w_minus = " (negative diff: cells to screen height)\n\n"; char * w_type = (1 == win->target_width_type) ? w_minus : w_plus; @@ -485,10 +480,10 @@ extern void draw_winconf_geometry(struct Win * win) + strlen(h_title) + strlen(h_value) + strlen(h_type) + strlen(w_title) + strlen(w_value) + strlen(w_type) + strlen(breaks_title) + strlen(breaks_type); - char * text = try_malloc(text_size + 1, f_name); + char * text = try_malloc(text_size + 1, __func__); test = sprintf(text, "%s%s%s%s%s%s%s%s%s", title, h_title, h_value, h_type, w_title, w_value, w_type, breaks_title, breaks_type); - exit_trouble(test < 0, f_name, "sprintf()"); + exit_trouble(test < 0, __func__, "sprintf"); add_text_with_linebreaks(win, text); free(text); } diff --git a/src/client/interface_conf.c b/src/client/interface_conf.c index aba5e3b..65debb0 100644 --- a/src/client/interface_conf.c +++ b/src/client/interface_conf.c @@ -118,7 +118,6 @@ static void set_keybindings(char * token1, uint8_t flags, static void write_keybindings(FILE * file, struct KeyBindingDB * kbdb) { - char * f_name = "write_keybindings()"; char * sep = " "; char * tok0 = "KEY"; uint8_t i_kb; @@ -126,12 +125,12 @@ static void write_keybindings(FILE * file, struct KeyBindingDB * kbdb) { size_t size = strlen(tok0) + strlen(sep) + 3 + strlen(sep) + strlen(kbdb->kbs[i_kb].command->dsc_short) + 1 + 1; - char * line = try_malloc(size, f_name); + char * line = try_malloc(size, __func__); int test = snprintf(line, size, "%s%s%d%s%s\n", tok0, sep, kbdb->kbs[i_kb].keycode, sep, kbdb->kbs[i_kb].command->dsc_short); - exit_trouble(test < 0, f_name, "snprintf()"); - try_fwrite(line, sizeof(char), strlen(line), file, f_name); + exit_trouble(test < 0, __func__, "snprintf"); + try_fwrite(line, sizeof(char), strlen(line), file, __func__); free(line); } } @@ -141,7 +140,6 @@ static void write_keybindings(FILE * file, struct KeyBindingDB * kbdb) static void write_def(FILE * file, char * prefix, uint8_t quotes, char * val, char type) { - char * f_name = "write_def()"; char * val_str = NULL; int test_val_str = 1; if ('s' == type) @@ -151,26 +149,26 @@ static void write_def(FILE * file, char * prefix, uint8_t quotes, char * val, if ('i' == type) { size_t size_val_str = 6 + 1; - val_str = try_malloc(size_val_str, f_name); + val_str = try_malloc(size_val_str, __func__); test_val_str = snprintf(val_str, size_val_str, "%d", (int16_t) *val); } else if ('c' == type) { size_t size_val_str = 1 + 1; - val_str = try_malloc(size_val_str, f_name); + val_str = try_malloc(size_val_str, __func__); test_val_str = snprintf(val_str, size_val_str, "%c", * val); } - exit_trouble(test_val_str < 0, f_name, "snprintf()"); + exit_trouble(test_val_str < 0, __func__, "snprintf"); char * quote = quotes ? "'": ""; char * affix = "\n"; size_t size = strlen(prefix) + strlen(val_str) + (2 * strlen(quote)) + strlen(affix) + 1; - char * line = try_malloc(size, f_name); + char * line = try_malloc(size, __func__); int test = snprintf(line, size, "%s%s%s%s%s", prefix, quote, val_str, quote, affix); - exit_trouble(test < 0, f_name, "snprintf()"); + exit_trouble(test < 0, __func__, "snprintf"); free(val_str); - try_fwrite(line, sizeof(char), strlen(line), file, f_name); + try_fwrite(line, sizeof(char), strlen(line), file, __func__); free(line); } @@ -216,16 +214,15 @@ static void tokens_into_entries(char * token0, char * token1) static void write_if_win(struct Win ** win) { - char * f_name = "write_if_win()"; if (*win) { (*win)->target_height_type = (0 >= (*win)->target_height); (*win)->target_width_type = (0 >= (*win)->target_width);; size_t old_ids_size = strlen(world.winDB.ids); size_t new_size = old_ids_size + 1 + 1; - char * new_ids = try_malloc(new_size, f_name); + char * new_ids = try_malloc(new_size, __func__); int test = snprintf(new_ids,new_size,"%s%c",world.winDB.ids,(*win)->id); - exit_trouble(test < 0, f_name, "snprintf()"); + exit_trouble(test < 0, __func__, "snprintf"); free(world.winDB.ids); world.winDB.ids = new_ids; array_append(old_ids_size, sizeof(struct Win), *win, @@ -432,11 +429,10 @@ extern void save_interface_conf() extern void load_interface_conf() { - char * f_name = "load_interface_conf()"; - world.winDB.ids = try_malloc(1, f_name); + world.winDB.ids = try_malloc(1, __func__); world.winDB.ids[0] = '\0'; world.winDB.wins = NULL; - tmp_order = try_malloc(1, f_name); + tmp_order = try_malloc(1, __func__); tmp_order[0] = '\0'; tmp_active = '\0'; detect_atomic_leftover(world.path_interface); @@ -444,7 +440,7 @@ extern void load_interface_conf() char * err = "Not all expected windows defined in config file."; exit_err(strlen(world.winDB.legal_ids) != strlen(world.winDB.ids), err); make_v_screen_and_init_win_sizes(); - world.winDB.order = try_malloc(1, f_name); + world.winDB.order = try_malloc(1, __func__); world.winDB.order[0] = '\0'; uint8_t i; for (i = 0; i < strlen(tmp_order); toggle_window(tmp_order[i]), i++); diff --git a/src/client/io.c b/src/client/io.c index c047f69..d0b9dc0 100644 --- a/src/client/io.c +++ b/src/client/io.c @@ -87,13 +87,12 @@ static void test_server_activity(time_t * last_server_answer_time); static void read_inventory(char * read_buf, uint32_t linemax, FILE * file) { - char * f_name = "read_inventory()"; char * delimiter = "%\n"; free(world.player_inventory); world.player_inventory = NULL; /* Avoids illegal strlen() below. */ while (1) { - try_fgets(read_buf, linemax + 1, file, f_name); + try_fgets(read_buf, linemax + 1, file, __func__); if (!(strcmp(read_buf, delimiter))) { break; @@ -104,10 +103,10 @@ static void read_inventory(char * read_buf, uint32_t linemax, FILE * file) old_size = strlen(world.player_inventory); } int new_size = strlen(read_buf); - char * new_inventory = try_malloc(old_size + new_size + 1, f_name); + char * new_inventory = try_malloc(old_size + new_size + 1, __func__); memcpy(new_inventory, world.player_inventory, old_size); int test = sprintf(new_inventory + old_size, "%s", read_buf); - exit_trouble(test < 0, f_name, "sprintf()"); + exit_trouble(test < 0, __func__, "sprintf"); free(world.player_inventory); world.player_inventory = new_inventory; } @@ -119,18 +118,17 @@ static void read_inventory(char * read_buf, uint32_t linemax, FILE * file) static void read_map_cells(FILE * file) { - char * f_name = "read_map_cells()"; free(world.map.cells); - world.map.cells = try_malloc(world.map.length * world.map.length, f_name); + world.map.cells = try_malloc(world.map.length * world.map.length, __func__); uint16_t y, x; for (y = 0; y < world.map.length; y++) { for (x = 0; x < world.map.length; x++) { - char c = try_fgetc(file, f_name); + char c = try_fgetc(file, __func__); world.map.cells[(y * world.map.length) + x] = c; } - try_fgetc(file, f_name); + try_fgetc(file, __func__); } } @@ -138,10 +136,9 @@ static void read_map_cells(FILE * file) static void read_log(char * read_buf, uint32_t linemax, FILE * file) { - char * f_name = "read_log()"; free(world.log); world.log = NULL; - while (try_fgets(read_buf, linemax + 1, file, f_name)) + while (try_fgets(read_buf, linemax + 1, file, __func__)) { int old_size = 0; if (NULL != world.log) @@ -149,10 +146,10 @@ static void read_log(char * read_buf, uint32_t linemax, FILE * file) old_size = strlen(world.log); } int new_size = strlen(read_buf); - char * new_log = try_malloc(old_size + new_size + 1, f_name); + char * new_log = try_malloc(old_size + new_size + 1, __func__); memcpy(new_log, world.log, old_size); int test = sprintf(new_log + old_size, "%s", read_buf); - exit_trouble(test < 0, f_name, "sprintf()"); + exit_trouble(test < 0, __func__, "sprintf"); free(world.log); world.log = new_log; } @@ -163,8 +160,7 @@ static void read_log(char * read_buf, uint32_t linemax, FILE * file) static uint16_t read_value_from_line(char * read_buf, uint32_t linemax, FILE * file) { - char * f_name = "read_value_from_line()"; - try_fgets(read_buf, linemax + 1, file, f_name); + try_fgets(read_buf, linemax + 1, file, __func__); return atoi(read_buf); } @@ -172,23 +168,22 @@ static uint16_t read_value_from_line(char * read_buf, uint32_t linemax, static FILE * changed_worldstate_file(char * path) { - char * f_name = "changed_worldstate_file()"; struct stat stat_buf; - exit_trouble(stat(path, &stat_buf), f_name, "stat()"); + exit_trouble(stat(path, &stat_buf), __func__, "stat"); if (stat_buf.st_mtime != world.last_update) { world.last_update = stat_buf.st_mtime; - return try_fopen(path, "r", f_name); + return try_fopen(path, "r", __func__); } - FILE * file = try_fopen(path, "r", f_name); + FILE * file = try_fopen(path, "r", __func__); char turn_string[6]; - try_fgets(turn_string, 6, file, f_name); + try_fgets(turn_string, 6, file, __func__); if (world.turn == atoi(turn_string)) { - try_fclose(file, f_name); + try_fclose(file, __func__); return NULL; } - exit_trouble(fseek(file, 0, SEEK_SET), f_name, "fseek()"); + exit_trouble(fseek(file, 0, SEEK_SET), __func__, "fseek"); return file; } @@ -196,7 +191,6 @@ static FILE * changed_worldstate_file(char * path) static uint8_t read_world() { - char * f_name = "read_world()"; char * path = "server/worldstate"; char * quit_msg = "No worldstate file found to read. Server may be down."; static uint8_t first_read = 1; @@ -207,7 +201,7 @@ static uint8_t read_world() return 0; } uint32_t linemax = textfile_width(file); - char * read_buf = try_malloc(linemax + 1, f_name); + char * read_buf = try_malloc(linemax + 1, __func__); world.turn = read_value_from_line(read_buf, linemax, file); world.player_lifepoints = read_value_from_line(read_buf, linemax, file); read_inventory(read_buf, linemax, file); @@ -222,7 +216,7 @@ static uint8_t read_world() read_map_cells(file); read_log(read_buf, linemax, file); free(read_buf); - try_fclose(file, f_name); + try_fclose(file, __func__); return 1; } @@ -249,8 +243,7 @@ static void test_ping_pong(time_t last_server_answer_time) static void test_server_activity(time_t * last_server_answer_time) { - char * f_name = "test_server_activity()"; - int test = try_fgetc(world.file_server_out, f_name); + int test = try_fgetc(world.file_server_out, __func__); if (EOF == test) { return; @@ -259,7 +252,7 @@ static void test_server_activity(time_t * last_server_answer_time) { ; } - while (EOF != (test = try_fgetc(world.file_server_out, f_name))); + while (EOF != (test = try_fgetc(world.file_server_out, __func__))); * last_server_answer_time = time(0); } @@ -267,12 +260,11 @@ static void test_server_activity(time_t * last_server_answer_time) extern void send(char * msg) { - char * f_name = "send()"; uint32_t msg_size = strlen(msg) + 1; char * err = "send() tries to send message larger than PIPE_BUF bytes."; exit_err(msg_size > PIPE_BUF, err); - try_fwrite(msg, strlen(msg), 1, world.file_server_in, f_name); - try_fputc('\n', world.file_server_in, f_name); + try_fwrite(msg, strlen(msg), 1, world.file_server_in, __func__); + try_fputc('\n', world.file_server_in, __func__); fflush(world.file_server_in); } diff --git a/src/client/keybindings.c b/src/client/keybindings.c index b0b7c7a..ba8dc4f 100644 --- a/src/client/keybindings.c +++ b/src/client/keybindings.c @@ -50,11 +50,10 @@ static struct KeyBindingDB * char_selected_kb_db(char c) static uint8_t try_keycode(uint16_t keycode_given, char * keyname, uint16_t keycode_match, char * keyname_match) { - char * f_name = "try_keycode()"; if (keycode_given == keycode_match) { int test = sprintf(keyname, "%s", keyname_match); - exit_trouble(test < 0, f_name, "sprintf()"); + exit_trouble(test < 0, __func__, "sprintf"); return 1; } return 0; @@ -80,16 +79,15 @@ extern struct Command * get_command_to_keycode(struct KeyBindingDB * kbdb, extern char * get_keyname_to_keycode(uint16_t keycode) { - char * f_name = "get_name_to_keycode()"; - char * keyname = try_malloc(10, f_name); /* max keyname length + 1 */ + char * keyname = try_malloc(10, __func__); /* max keyname length + 1 */ if (32 < keycode && keycode < 127) { - exit_trouble(sprintf(keyname, "%c", keycode) < 0, f_name, "sprintf()"); + exit_trouble(sprintf(keyname, "%c", keycode) < 0, __func__, "sprintf"); } else if (keycode >= KEY_F0 && keycode <= KEY_F(63)) { uint16_t f = keycode - KEY_F0; - exit_trouble(sprintf(keyname, "F%d", f) < 0, f_name, "sprintf()");; + exit_trouble(sprintf(keyname, "F%d", f) < 0, __func__, "sprintf");; } else if ( try_keycode(keycode, keyname, 9, "TAB") || try_keycode(keycode, keyname, 10, "RETURN") @@ -111,7 +109,7 @@ extern char * get_keyname_to_keycode(uint16_t keycode) } else { - exit_trouble(sprintf(keyname, "(unknown)") < 0, f_name, "sprintf()"); + exit_trouble(sprintf(keyname, "(unknown)") < 0, __func__, "sprintf"); } return keyname; } diff --git a/src/client/main.c b/src/client/main.c index 344df69..9c6a72a 100644 --- a/src/client/main.c +++ b/src/client/main.c @@ -25,8 +25,6 @@ struct World world; int main(int argc, char * argv[]) { - char * f_name = "main()"; - /* Declare hard-coded paths and values here. */ world.path_commands = "confclient/commands"; world.path_interface = "confclient/interface_conf"; @@ -58,13 +56,13 @@ int main(int argc, char * argv[]) struct sigaction act; memset(&act, 0, sizeof(act)); act.sa_handler = &winch_called; - exit_trouble(sigaction(SIGWINCH, &act, NULL), f_name, "sigaction()"); + exit_trouble(sigaction(SIGWINCH, &act, NULL), __func__, "sigaction"); /* Open file streams for communicating with the server. */ exit_err(access(path_server_in, F_OK), "No server input file found."); - world.file_server_in = try_fopen(path_server_in, "a", f_name); + world.file_server_in = try_fopen(path_server_in, "a", __func__); set_cleanup_flag(CLEANUP_SERVER_IN); - world.file_server_out = try_fopen(path_server_out, "r", f_name); + world.file_server_out = try_fopen(path_server_out, "r", __func__); set_cleanup_flag(CLEANUP_SERVER_OUT); /* This is where most everything happens. */ @@ -72,6 +70,6 @@ int main(int argc, char * argv[]) /* Leave properly. */ cleanup(); - exit_trouble(printf("%s\n", quit_msg) < 0, f_name, "printf()"); + exit_trouble(printf("%s\n", quit_msg) < 0, __func__, "printf"); exit(EXIT_SUCCESS); } diff --git a/src/client/parse.c b/src/client/parse.c index 4f47aaa..5efbaf9 100644 --- a/src/client/parse.c +++ b/src/client/parse.c @@ -19,24 +19,23 @@ extern void parse_file(char * path, void (* token_to_entry) (char *, char *)) { - char * f_name = "read_new_config_file()"; char * prefix = "Failed reading config file: \""; char * affix = "\". "; size_t size = strlen(prefix) + strlen(path) + strlen(affix) + 1; - char * errline_intro = try_malloc(size, f_name); + char * errline_intro = try_malloc(size, __func__); int test = snprintf(errline_intro, size, "%s%s%s", prefix, path, affix); - exit_trouble(test < 0, f_name, "snprintf()"); + exit_trouble(test < 0, __func__, "snprintf"); exit_err(access(path, F_OK), errline_intro); - FILE * file = try_fopen(path, "r", f_name); + FILE * file = try_fopen(path, "r", __func__); uint32_t linemax = textfile_width(file); - char * errline_line = try_malloc(linemax + 1, f_name); + char * errline_line = try_malloc(linemax + 1, __func__); set_err_line_options(errline_intro, errline_line, 1); err_line_zero(); err_line(0 == linemax, "File is empty."); char * token0 = NULL; /* For final token_to_entry() if while() stagnates. */ char * token1 = NULL; char * err_val = "No value given."; - while (try_fgets(errline_line, linemax + 1, file, f_name)) + while (try_fgets(errline_line, linemax + 1, file, __func__)) { err_line_inc(); char * line_copy = strdup(errline_line); @@ -50,7 +49,7 @@ extern void parse_file(char * path, void (* token_to_entry) (char *, char *)) free(line_copy); } token_to_entry(token0, token1); - try_fclose(file, f_name); + try_fclose(file, __func__); free(errline_line); free(errline_intro); } @@ -87,9 +86,8 @@ extern void parse_unknown_arg() extern char * parse_init_entry(uint8_t * flags, size_t size) { - char * f_name = "parse_init_entry()"; *flags = EDIT_STARTED; - char * p = try_malloc(size, f_name); + char * p = try_malloc(size, __func__); memset(p, 0, size); return p; } diff --git a/src/client/wincontrol.c b/src/client/wincontrol.c index c42516f..85750ec 100644 --- a/src/client/wincontrol.c +++ b/src/client/wincontrol.c @@ -177,9 +177,8 @@ static void set_win_target_size(struct Win * w) static void append_win(struct Win * w) { - char * f_name = "append_win()"; uint8_t old_size = strlen(world.winDB.order) + 1; - char * new_order = try_malloc(old_size + 1, f_name); + char * new_order = try_malloc(old_size + 1, __func__); memcpy(new_order, world.winDB.order, old_size - 1); new_order[old_size - 1] = w->id; new_order[old_size] = '\0'; @@ -193,15 +192,14 @@ static void append_win(struct Win * w) static void suspend_win(struct Win * w) { - char * f_name = "suspend_win()"; uint8_t new_size = strlen(world.winDB.order); - char * new_order = try_malloc(new_size, f_name); + char * new_order = try_malloc(new_size, __func__); uint8_t i = get_win_pos_in_order(w->id); char next_char = world.winDB.order[i + 1]; world.winDB.order[i] = '\0'; char * second_part = &world.winDB.order[i + 1]; int test = sprintf(new_order, "%s%s", world.winDB.order, second_part); - exit_trouble(test < 0, f_name, "sprintf()"); + exit_trouble(test < 0, __func__, "sprintf"); free(world.winDB.order); world.winDB.order = new_order; world.winDB.active = world.winDB.order[i]; @@ -329,11 +327,10 @@ extern void resize_active_win(char change) extern void shift_active_win(char dir) { - char * f_name = "shift_active_win()"; uint8_t len_order = strlen(world.winDB.order); if (1 < len_order) { - char * tmp = try_malloc(len_order + 1, f_name); + char * tmp = try_malloc(len_order + 1, __func__); tmp[len_order] = '\0'; uint8_t pos = get_win_pos_in_order(world.winDB.active); if ('f' == dir) diff --git a/src/client/windows.c b/src/client/windows.c index 0fee3e3..3881393 100644 --- a/src/client/windows.c +++ b/src/client/windows.c @@ -147,8 +147,6 @@ static char get_next_win_id() static void scroll_hint(struct yx_uint16 fsize, char dir, uint16_t dist, char * unit, struct yx_uint16 start) { - char * f_name = "sprintf()"; - /* Decide on alignment (vertical/horizontal?), thereby hint text space. */ char * more = "more"; uint16_t dsc_space = fsize.x; @@ -157,9 +155,9 @@ static void scroll_hint(struct yx_uint16 fsize, char dir, uint16_t dist, dsc_space = fsize.y; } /* vv-- 10 = max strlen for uint16_t */ uint16_t size = 1 + strlen(more) + 1 + 10 + 1 + strlen(unit) + 1 + 1; - char * scrolldsc = try_malloc(size, f_name); + char * scrolldsc = try_malloc(size, __func__); int test = sprintf(scrolldsc, " %d %s %s ", dist, more, unit); - exit_trouble(test < 0, f_name, "sprintf()"); + exit_trouble(test < 0, __func__, "sprintf"); /* Decide on offset of the description text inside the scroll hint line. */ uint16_t dsc_offset = 1; @@ -236,7 +234,7 @@ static void draw_win_borderlines(struct Win * w) offset = (w->frame_size.x - (strlen(w->title) + 2)) / 2; } /* +2 is for padding/decoration */ uint16_t length_visible = strnlen(w->title, w->frame_size.x - 2); - char * title = try_malloc(length_visible + 3, "draw_win_borderlines()"); + char * title = try_malloc(length_visible + 3, "draw_win_borderlines"); char decoration = ' '; if (w->id == world.winDB.active) { @@ -394,16 +392,15 @@ extern struct Win * get_win_by_id(char id) extern void make_v_screen_and_init_win_sizes() { - char * f_name = "make_v_screen_and_init_win_sizes()"; char * err_s = "creating an illegaly large virtual screen"; - char * err_m = "triggering a memory allocation error via newpad()"; + char * err_m = "triggering a memory allocation error via newpad"; uint32_t maxy_test = getmaxy(world.winDB.t_screen); uint32_t maxx_test = getmaxx(world.winDB.t_screen); - exit_trouble(maxy_test>UINT16_MAX || maxx_test>UINT16_MAX, f_name, err_s); + exit_trouble(maxy_test>UINT16_MAX || maxx_test>UINT16_MAX, __func__, err_s); world.winDB.v_screen_size.y = maxy_test; world.winDB.v_screen_size.x = maxx_test; world.winDB.v_screen = newpad(world.winDB.v_screen_size.y, 1); - exit_trouble(NULL == world.winDB.v_screen, f_name, err_m); + exit_trouble(NULL == world.winDB.v_screen, __func__, err_m); char id; while (0 != (id = get_next_win_id())) { @@ -438,12 +435,11 @@ extern void winch_called() extern void reset_windows_on_winch() { - char * f_name = "reset_windows_on_winch()"; endwin(); /* "[S]tandard way" to recalibrate ncurses post SIGWINCH, says */ refresh(); /* . */ - char * tmp_order = try_malloc(strlen(world.winDB.order) + 1, f_name); + char * tmp_order = try_malloc(strlen(world.winDB.order) + 1, __func__); int test = sprintf(tmp_order, "%s", world.winDB.order); - exit_trouble(test < 0, f_name, "sprintf()"); + exit_trouble(test < 0, __func__, "sprintf"); uint8_t i; char tmp_active = world.winDB.active; for (i = 0; i < strlen(tmp_order); toggle_window(tmp_order[i]), i++); diff --git a/src/common/parse_file.c b/src/common/parse_file.c index 82ba4e3..fd5b9a8 100644 --- a/src/common/parse_file.c +++ b/src/common/parse_file.c @@ -77,22 +77,21 @@ extern uint8_t err_line(uint8_t test, char * msg) { return 0; } - char * f_name = "err_line()"; char * prefix = " Offending line "; char * affix = ": "; size_t size = strlen(err_line_intro) + strlen(msg) + strlen(prefix) + 10 /* strlen for uint32_t representations */ + strlen(affix) + strlen(err_line_line) + 1; - char * err = try_malloc(size, f_name); + char * err = try_malloc(size, __func__); int ret = snprintf(err, size, "%s%s%s%d%s%s", err_line_intro, msg, prefix, err_line_count, affix, err_line_line); - exit_trouble(ret < 0, f_name, "snprintf()"); + exit_trouble(ret < 0, __func__, "snprintf"); if (err_line_exit) { exit_err(1, err); } - exit_trouble(0 > printf("%s\n", err), f_name, "printf()"); - exit_trouble(EOF == fflush(stdout), f_name, "fflush()"); + exit_trouble(0 > printf("%s\n", err), __func__, "printf"); + exit_trouble(EOF == fflush(stdout), __func__, "fflush"); free(err); return 1; } diff --git a/src/common/readwrite.c b/src/common/readwrite.c index c9e22d7..f05c128 100644 --- a/src/common/readwrite.c +++ b/src/common/readwrite.c @@ -21,29 +21,27 @@ static char * build_temp_path(char * path); static char * build_temp_path(char * path) { - char * f_name = "build_temp_path"; char * suffix_tmp = "_tmp"; uint16_t size = strlen(path) + strlen(suffix_tmp) + 1; - char * path_tmp = try_malloc(size, f_name); + char * path_tmp = try_malloc(size, __func__); int test = sprintf(path_tmp, "%s%s", path, suffix_tmp); - exit_trouble(test < 0, f_name, "sprintf()"); + exit_trouble(test < 0, __func__, "sprintf"); return path_tmp; } -extern FILE * try_fopen(char * path, char * mode, char * f) +extern FILE * try_fopen(char * path, char * mode, const char * f) { - char * f_name = "try_fopen()"; char * msg1 = "Trouble in "; - char * msg2 = " with fopen() (mode '"; + char * msg2 = " with fopen (mode '"; char * msg3 = "') on path '"; char * msg4 = "'."; uint16_t size = strlen(msg1) + strlen(msg2) + strlen(msg3) + strlen(msg4) + strlen(f) + strlen(path) + strlen(mode) + 1; - char * msg = try_malloc(size, f_name); + char * msg = try_malloc(size, __func__); int test = sprintf(msg, "%s%s%s%s%s%s%s", msg1,f,msg2,mode,msg3,path,msg4); - exit_trouble(test < 0, f_name, "sprintf()"); + exit_trouble(test < 0, __func__, "sprintf"); FILE * file_p = fopen(path, mode); exit_err(NULL == file_p, msg); free(msg); @@ -52,42 +50,42 @@ extern FILE * try_fopen(char * path, char * mode, char * f) -extern void try_fclose(FILE * file, char * f) +extern void try_fclose(FILE * file, const char * f) { - exit_trouble(fclose(file), f, "fclose()"); + exit_trouble(fclose(file), f, "fclose"); } extern void try_fwrite(void * ptr, size_t size, size_t nmemb, FILE * stream, - char * f) + const char * f) { - exit_trouble(0 == fwrite(ptr, size, nmemb, stream), f, "fwrite()"); + exit_trouble(0 == fwrite(ptr, size, nmemb, stream), f, "fwrite"); } -extern void try_fputc(uint8_t c, FILE * file, char * f) +extern void try_fputc(uint8_t c, FILE * file, const char * f) { - exit_trouble(EOF == fputc(c, file), f, "fputc()"); + exit_trouble(EOF == fputc(c, file), f, "fputc"); } -extern int try_fgetc(FILE * file, char * f) +extern int try_fgetc(FILE * file, const char * f) { clearerr(file); /* OSX' (BSD?) fgetc() needs this to undo previous EOFs. */ int test = fgetc(file); - exit_trouble(EOF == test && ferror(file), f, "fgetc()"); + exit_trouble(EOF == test && ferror(file), f, "fgetc"); return test; } -extern char * try_fgets(char * line, int linemax, FILE * file, char * f) +extern char * try_fgets(char * line, int linemax, FILE * file, const char * f) { char * test = fgets(line, linemax, file); - exit_trouble(NULL == test && ferror(file), f, "fgets()"); + exit_trouble(NULL == test && ferror(file), f, "fgets"); return test; } @@ -95,39 +93,37 @@ extern char * try_fgets(char * line, int linemax, FILE * file, char * f) extern FILE * atomic_write_start(char * path, char ** path_tmp) { - char * f_name = "atomic_write_start()"; *path_tmp = build_temp_path(path); - return try_fopen(*path_tmp, "w", f_name); + return try_fopen(*path_tmp, "w", __func__); } extern void atomic_write_finish(FILE * file, char * path, char * path_tmp) { - char * f_name = "atomic_write_finish()"; - try_fclose(file, f_name); + try_fclose(file, __func__); char * msg1 = "Trouble in "; char * msg4 = "'."; if (!access(path, F_OK)) { - char * msg2 = " with unlink() on path '"; + char * msg2 = " with unlink on path '"; uint16_t size = strlen(msg1) + strlen(msg2) + strlen(msg4) - + strlen(f_name) + strlen(path) + 1; - char * msg = try_malloc(size, f_name); - int test = sprintf(msg, "%s%s%s%s%s", msg1, f_name, msg2, path, msg4); - exit_trouble(test < 0, f_name, "sprintf()"); + + strlen(__func__) + strlen(path) + 1; + char * msg = try_malloc(size, __func__); + int test = sprintf(msg, "%s%s%s%s%s", msg1, __func__, msg2, path, msg4); + exit_trouble(test < 0, __func__, "sprintf"); exit_err(unlink(path), msg); free(msg); } - char * msg2 = " with rename() from '"; + char * msg2 = " with rename from '"; char * msg3 = "' to '"; - uint16_t size = strlen(msg1) + strlen(f_name) + strlen(msg2) + + uint16_t size = strlen(msg1) + strlen(__func__) + strlen(msg2) + + strlen(path_tmp) + strlen(msg3) + strlen(path) + strlen(msg4) + 1; - char * msg = try_malloc(size, f_name); + char * msg = try_malloc(size, __func__); int test = sprintf(msg, "%s%s%s%s%s%s%s", - msg1, f_name, msg2, path_tmp, msg3, path, msg4); - exit_trouble(test < 0, f_name, "sprintf()"); + msg1, __func__, msg2, path_tmp, msg3, path, msg4); + exit_trouble(test < 0, __func__, "sprintf"); exit_err(rename(path_tmp, path), msg); free(msg); free(path_tmp); @@ -137,7 +133,6 @@ extern void atomic_write_finish(FILE * file, char * path, char * path_tmp) extern void detect_atomic_leftover(char * path) { - char * f_name = "detect_atomic_leftover()"; char * path_tmp = build_temp_path(path); char * part1 = "Found file '"; char * part2 = "' that may be a leftover from an aborted previous attempt " @@ -145,9 +140,9 @@ extern void detect_atomic_leftover(char * path) char * part3 = "'. Aborting until the matter is solved by (re-)moving it."; uint32_t size = strlen(part1) + strlen(path_tmp) + strlen(part2) + strlen(path) + strlen(part3) + 1; - char * msg = try_malloc(size, f_name); + char * msg = try_malloc(size, __func__); int test = sprintf(msg, "%s%s%s%s%s", part1, path_tmp, part2, path, part3); - exit_trouble(test < 0, f_name, "sprintf()"); + exit_trouble(test < 0, __func__, "sprintf"); exit_err(!access(path_tmp, F_OK), msg); free(msg); free(path_tmp); @@ -157,19 +152,18 @@ extern void detect_atomic_leftover(char * path) extern uint32_t textfile_width(FILE * file) { - char * f_name = "textfile_width()"; int c = 0; uint32_t c_count = 0; uint32_t linemax = 0; while (1) { - c = try_fgetc(file, f_name); + c = try_fgetc(file, __func__); if (EOF == c) { break; } c_count++; - exit_trouble(UINT32_MAX == c_count, f_name, "too large text file line"); + exit_trouble(UINT32_MAX==c_count, __func__, "too large text file line"); if ('\n' == c) { if (c_count > linemax) @@ -183,6 +177,6 @@ extern uint32_t textfile_width(FILE * file) { /* line / lack newline chars. */ linemax = c_count; } - exit_trouble(-1 == fseek(file, 0, SEEK_SET), f_name, "fseek()"); + exit_trouble(-1 == fseek(file, 0, SEEK_SET), __func__, "fseek"); return linemax; } diff --git a/src/common/readwrite.h b/src/common/readwrite.h index eddd0e6..cdbd386 100644 --- a/src/common/readwrite.h +++ b/src/common/readwrite.h @@ -14,11 +14,11 @@ /* Wrappers to fopen(), fclose(), fgets() and fwrite() from function called "f", * calling exit_err() upon error with appropriate error messages. */ -extern FILE * try_fopen(char * path, char * mode, char * f); -extern void try_fclose(FILE * file, char * f); +extern FILE * try_fopen(char * path, char * mode, const char * f); +extern void try_fclose(FILE * file, const char * f); extern void try_fwrite(void * ptr, size_t size, size_t nmemb, FILE * stream, - char * f); -extern void try_fputc(uint8_t c, FILE * file, char * f); + const char * f); +extern void try_fputc(uint8_t c, FILE * file, const char * f); /* Wrapper to calling fgetc() and fgets() from function "f". The return code is * returned unless ferror() indicates an error (i.e. to signify an end of file, @@ -26,8 +26,8 @@ extern void try_fputc(uint8_t c, FILE * file, char * f); * on "file" before fgetc(), because some Unixes fgetc() remember old EOFs and * only return those until explicitely cleared. */ -extern int try_fgetc(FILE * file, char * f); -extern char * try_fgets(char * line, int size, FILE * file, char * f); +extern int try_fgetc(FILE * file, const char * f); +extern char * try_fgets(char * line, int size, FILE * file, const char * f); /* Write to "path_tmp" "path" + "_tmp" and return a new file at that "path_tmp" * open for writing. "path_tmp" is malloc()'d, must be freed externally. diff --git a/src/common/rexit.c b/src/common/rexit.c index 7e16f12..a3e18b2 100644 --- a/src/common/rexit.c +++ b/src/common/rexit.c @@ -21,7 +21,7 @@ extern void set_cleanup_func(void (* f)()) -extern void exit_err(int err, char * msg) +extern void exit_err(int err, const char * msg) { if (0 == err) { @@ -42,17 +42,16 @@ extern void exit_err(int err, char * msg) -extern void exit_trouble(int err, char * parent, char * child) +extern void exit_trouble(int err, const char * parent, const char * child) { - char * f_name = "exit_trouble()"; char * p1 = "Trouble in "; char * p2 = " with "; char * p3 = "."; uint16_t size = strlen(p1) + strlen(parent) + strlen(p2) + strlen(child) + strlen(p3) + 1; - char * msg = try_malloc(size, f_name); + char * msg = try_malloc(size, __func__); int test = sprintf(msg, "%s%s%s%s%s", p1, parent, p2, child, p3); - exit_err(test < 0, "Trouble in exit_trouble() with sprintf()"); + exit_err(test < 0, "Trouble in exit_trouble with sprintf."); exit_err(err, msg); free(msg); } diff --git a/src/common/rexit.h b/src/common/rexit.h index 15c39b9..460cde0 100644 --- a/src/common/rexit.h +++ b/src/common/rexit.h @@ -16,10 +16,10 @@ extern void set_cleanup_func(void (* f)()); * consists of "msg" or (if "msg" is NULL pointer) a generic "Details unknown" * and of errno's content (if it is non-zero). */ -extern void exit_err(int err, char * msg); +extern void exit_err(int err, const char * msg); /* Do exit_err() with "msg" as: "Trouble in ".parent." with ".child."." */ -extern void exit_trouble(int err, char * parent, char * child); +extern void exit_trouble(int err, const char * parent, const char * child); diff --git a/src/common/try_malloc.c b/src/common/try_malloc.c index 70392be..81e75bf 100644 --- a/src/common/try_malloc.c +++ b/src/common/try_malloc.c @@ -9,14 +9,14 @@ -extern void * try_malloc(size_t size, char * f) +extern void * try_malloc(size_t size, const char * f) { - char * prefix = "Trouble with malloc() in "; + char * prefix = "Trouble with malloc in "; char * msg = malloc(strlen(prefix) + strlen(f) + 1 + 1); exit_err(NULL == msg, - "Trouble in try_malloc() with malloc() for error message string."); + "Trouble in try_malloc with malloc for error message string."); int test = sprintf(msg, "%s%s.", prefix, f); - exit_err(test < 0, "Trouble in try_malloc() with sprintf()."); + exit_err(test < 0, "Trouble in try_malloc with sprintf."); void * p = malloc(size); exit_err(NULL == p, msg); /* Bypass exit_trouble() calling try_malloc(). */ free(msg); diff --git a/src/common/try_malloc.h b/src/common/try_malloc.h index 9cf76f2..19c4d4b 100644 --- a/src/common/try_malloc.h +++ b/src/common/try_malloc.h @@ -11,7 +11,7 @@ /* Call malloc("size") from function called "f"; exit_trouble() on error. */ -extern void * try_malloc(size_t size, char * f); +extern void * try_malloc(size_t size, const char * f); diff --git a/src/server/ai.c b/src/server/ai.c index 497d35d..94f212e 100644 --- a/src/server/ai.c +++ b/src/server/ai.c @@ -122,15 +122,13 @@ static void dijkstra_map(uint16_t * score_map, uint16_t max_score) static char get_dir_to_nearest_enemy(struct Thing * t_origin) { - char * f_name = "get_dir_to_nearest_enemy()"; - /* Calculate for each cell the distance to the visibly nearest map actor not * "t_origin", with movement only possible in the directions of "dir". * (Actors' own cells start with a distance of 0 towards themselves.) */ uint32_t map_size = world.map.length * world.map.length; uint16_t max_score = UINT16_MAX - 1; - uint16_t * score_map = try_malloc(map_size * sizeof(uint16_t), f_name); + uint16_t * score_map = try_malloc(map_size * sizeof(uint16_t), __func__); uint32_t i; for (i = 0; i < map_size; i++) { diff --git a/src/server/cleanup.c b/src/server/cleanup.c index 2e7ab83..1eebbaf 100644 --- a/src/server/cleanup.c +++ b/src/server/cleanup.c @@ -20,7 +20,6 @@ static uint32_t cleanup_flags = 0x0000; extern void cleanup() { - char * f_name = "cleanup()"; free(world.queue); free(world.log); free(world.map.cells); @@ -42,12 +41,12 @@ extern void cleanup() } if (cleanup_flags & CLEANUP_IN) { - try_fclose(world.file_in, f_name); + try_fclose(world.file_in, __func__); unlink(s[S_PATH_IN]); } if (cleanup_flags & CLEANUP_OUT) { - try_fclose(world.file_out, f_name); + try_fclose(world.file_out, __func__); free(world.server_test); unlink(s[S_PATH_OUT]); } diff --git a/src/server/field_of_view.c b/src/server/field_of_view.c index 177143a..278cc8b 100644 --- a/src/server/field_of_view.c +++ b/src/server/field_of_view.c @@ -42,8 +42,8 @@ static void mv_yx_in_hex_dir(char d, struct yx_uint8 * yx); */ static uint8_t mv_yx_in_dir_wrap(char d, struct yx_uint8 * yx, uint8_t unwrap); -/* Wrapper to "mv_yx_in_dir_wrap()", returns 1 if the wrapped function moved - * "yx" within the wrap borders and the map size, else 0. +/* Wrapper to mv_yx_in_dir_wrap(), returns 1 if the wrapped function moved "yx" + * within the wrap borders and the map size, else 0. */ static uint8_t mv_yx_in_dir_legal(char dir, struct yx_uint8 * yx); @@ -255,7 +255,6 @@ static void set_shadow(uint32_t left_angle, uint32_t right_angle, struct shadow_angle ** shadows, uint16_t pos_in_map, uint8_t * fov_map) { - char * f_name = "set_shadow()"; struct shadow_angle * shadow_i; if (fov_map[pos_in_map] & VISIBLE) { @@ -274,7 +273,7 @@ static void set_shadow(uint32_t left_angle, uint32_t right_angle, if (!try_merging_angles(left_angle, right_angle, shadows)) { struct shadow_angle * shadow; - shadow = try_malloc(sizeof(struct shadow_angle), f_name); + shadow = try_malloc(sizeof(struct shadow_angle), __func__); shadow->left_angle = left_angle; shadow->right_angle = right_angle; shadow->next = NULL; @@ -331,9 +330,8 @@ static void eval_position(uint16_t dist, uint16_t hex_i, uint8_t * fov_map, extern uint8_t * build_fov_map(struct Thing * eye) { - char * f_name = "build_fov_map()"; uint32_t map_size = world.map.length * world.map.length; - uint8_t * fov_map = try_malloc(map_size, f_name); + uint8_t * fov_map = try_malloc(map_size, __func__); memset(fov_map, VISIBLE, map_size); struct yx_uint8 test_pos = eye->pos; struct shadow_angle * shadows = NULL; diff --git a/src/server/god_commands.c b/src/server/god_commands.c index 4ed9f73..674ff62 100644 --- a/src/server/god_commands.c +++ b/src/server/god_commands.c @@ -332,7 +332,6 @@ static uint8_t world_may_be_set_active() static uint8_t parse_world_active(char * tok0, char * tok1) { - char * f_name = "parse_world_active()"; if (!strcmp(tok0, s[S_CMD_WORLD_ACTIVE]) && !parsetest_int(tok1, '8')) { if (!parsetest_int(tok1, '8')) @@ -343,7 +342,7 @@ static uint8_t parse_world_active(char * tok0, char * tok1) if (!access(s[S_PATH_WORLDSTATE], F_OK)) { int test = unlink(s[S_PATH_WORLDSTATE]); - exit_trouble(-1 == test, f_name, "unlink()"); + exit_trouble(-1 == test, __func__, "unlink"); } world.exists = 0; } diff --git a/src/server/hardcoded_strings.c b/src/server/hardcoded_strings.c index 4094478..6ad856b 100644 --- a/src/server/hardcoded_strings.c +++ b/src/server/hardcoded_strings.c @@ -35,7 +35,7 @@ extern void init_strings() s[S_CMD_PICKUP] = "pick_up"; s[S_CMD_DROP] = "drop"; s[S_CMD_USE] = "use"; - s[S_FCN_SPRINTF] = "sprintf()"; + s[S_FCN_SPRINTF] = "sprintf"; s[S_CMD_THINGTYPE] = "THING_TYPE"; s[S_CMD_TT_CONSUM] = "TT_CONSUMABLE"; s[S_CMD_TT_STARTN] = "TT_START_NUMBER"; diff --git a/src/server/init.c b/src/server/init.c index 16ab4df..01950bc 100644 --- a/src/server/init.c +++ b/src/server/init.c @@ -52,11 +52,10 @@ static uint8_t world_cannot_be_made(); static void obey_lines_from_file(char * path, uint8_t record) { - char * f_name = "obey_lines_from_file()"; - FILE * file = try_fopen(path, "r", f_name); + FILE * file = try_fopen(path, "r", __func__); uint32_t linemax = textfile_width(file); - char * line = try_malloc(linemax + 1, f_name); - while (NULL != try_fgets(line, linemax + 1, file, f_name)) + char * line = try_malloc(linemax + 1, __func__); + while (NULL != try_fgets(line, linemax + 1, file, __func__)) { if (strlen(line)) { @@ -73,20 +72,19 @@ static void obey_lines_from_file(char * path, uint8_t record) } } free(line); - try_fclose(file, f_name); + try_fclose(file, __func__); } static void replay_game() { - char * f_name = "replay_game()"; exit_err(access(s[S_PATH_RECORD], F_OK), "No record found to replay."); - FILE * file = try_fopen(s[S_PATH_RECORD], "r", f_name); + FILE * file = try_fopen(s[S_PATH_RECORD], "r", __func__); uint32_t linemax = textfile_width(file); - char * line = try_malloc(linemax + 1, f_name); + char * line = try_malloc(linemax + 1, __func__); while ( world.turn < world.replay - && NULL != try_fgets(line, linemax + 1, file, f_name)) + && NULL != try_fgets(line, linemax + 1, file, __func__)) { obey_msg(line, 0, 1); err_line_inc(); @@ -96,7 +94,7 @@ static void replay_game() { if (!end) { - end = (NULL == try_fgets(line, linemax + 1, file, f_name)); + end = (NULL == try_fgets(line, linemax + 1, file, __func__)); if (!end) { obey_msg(line, 0, 1); @@ -105,7 +103,7 @@ static void replay_game() } } free(line); - try_fclose(file, f_name); + try_fclose(file, __func__); } @@ -163,15 +161,14 @@ extern void obey_argv(int argc, char * argv[]) extern void setup_server_io() { - char * f_name = "setup_server_io()"; int test = mkdir("server", 0700); - exit_trouble(test && EEXIST != errno, f_name, "mkdir()"); - world.file_out = try_fopen(s[S_PATH_OUT], "w", f_name); - world.server_test = try_malloc(10 + 1 + 10 + 1 + 1, f_name); + exit_trouble(test && EEXIST != errno, __func__, "mkdir"); + world.file_out = try_fopen(s[S_PATH_OUT], "w", __func__); + world.server_test = try_malloc(10 + 1 + 10 + 1 + 1, __func__); test = sprintf(world.server_test, "%d %d\n", getpid(), (int) time(0)); - exit_trouble(test < 0, f_name, s[S_FCN_SPRINTF]); + exit_trouble(test < 0, __func__, s[S_FCN_SPRINTF]); try_fwrite(world.server_test, strlen(world.server_test), 1, - world.file_out, f_name); + world.file_out, __func__); fflush(world.file_out); set_cleanup_flag(CLEANUP_OUT); char * path_in = s[S_PATH_IN]; @@ -179,9 +176,9 @@ extern void setup_server_io() { /* file streams of clients */ unlink(path_in) ; /* communicating with server processes */ } /* superseded by this current one. */ - world.file_in = try_fopen(path_in, "w", f_name); - try_fclose(world.file_in, f_name); - world.file_in = try_fopen(path_in, "r", f_name); + world.file_in = try_fopen(path_in, "w", __func__); + try_fclose(world.file_in, __func__); + world.file_in = try_fopen(path_in, "r", __func__); set_cleanup_flag(CLEANUP_IN); } @@ -230,7 +227,6 @@ extern uint8_t remake_world() extern void run_game() { - char * f_name = "run_game()"; detect_atomic_leftover(s[S_PATH_SAVE]); detect_atomic_leftover(s[S_PATH_RECORD]); err_line_zero(); @@ -250,9 +246,9 @@ extern void run_game() obey_lines_from_file(s[S_PATH_CONFIG], 1); err_line_zero(); char * command = s[S_CMD_MAKE_WORLD]; - char * msg = try_malloc(strlen(command) + 1 + 11 + 1, f_name); + char * msg = try_malloc(strlen(command) + 1 + 11 + 1, __func__); int test = sprintf(msg, "%s %d", command, (int) time(NULL)); - exit_trouble(test < 0, f_name, s[S_FCN_SPRINTF]); + exit_trouble(test < 0, __func__, s[S_FCN_SPRINTF]); obey_msg(msg, 1, 1); free(msg); } diff --git a/src/server/io.c b/src/server/io.c index 317c088..6228415 100644 --- a/src/server/io.c +++ b/src/server/io.c @@ -71,41 +71,38 @@ static void write_map(struct Thing * player, FILE * file); static void write_key_value(FILE * file, char * key, uint32_t value) { - char * f_name = "write_key_value()"; - try_fwrite(key, strlen(key), 1, file, f_name); - try_fputc(' ', file, f_name); - char * line = try_malloc(11, f_name); - exit_trouble(-1 == sprintf(line, "%u", value), f_name, s[S_FCN_SPRINTF]); - try_fwrite(line, strlen(line), 1, file, f_name); + try_fwrite(key, strlen(key), 1, file, __func__); + try_fputc(' ', file, __func__); + char * line = try_malloc(11, __func__); + exit_trouble(-1 == sprintf(line, "%u", value), __func__, s[S_FCN_SPRINTF]); + try_fwrite(line, strlen(line), 1, file, __func__); free(line); - try_fputc('\n', file, f_name); + try_fputc('\n', file, __func__); } static void write_key_string(FILE * file, char * key, char * string) { - char * f_name = "write_key_string()"; - try_fwrite(key, strlen(key), 1, file, f_name); - try_fputc(' ', file, f_name); + try_fwrite(key, strlen(key), 1, file, __func__); + try_fputc(' ', file, __func__); uint8_t contains_space = NULL != strchr(string, ' '); if (contains_space) { - try_fputc('\'', file, f_name); + try_fputc('\'', file, __func__); } - try_fwrite(string, strlen(string), 1, file, f_name); + try_fwrite(string, strlen(string), 1, file, __func__); if (contains_space) { - try_fputc('\'', file, f_name); + try_fputc('\'', file, __func__); } - try_fputc('\n', file, f_name); + try_fputc('\n', file, __func__); } static void write_thing(FILE * file, struct Thing * t) { - char * f_name = "write_thing()"; struct Thing * o; for (o = t->owns; o; o = o->next) { @@ -123,14 +120,13 @@ static void write_thing(FILE * file, struct Thing * t) { write_key_value(file, s[S_CMD_T_CARRIES], o->id); } - try_fputc('\n', file, f_name); + try_fputc('\n', file, __func__); } static char * get_message_from_queue() { - char * f_name = "get_message_from_queue()"; char * message = NULL; if (world.queue_size) { @@ -138,7 +134,7 @@ static char * get_message_from_queue() if (0 < cutout_len) { cutout_len++; - message = try_malloc(cutout_len, f_name); + message = try_malloc(cutout_len, __func__); memcpy(message, world.queue, cutout_len); } for (; @@ -152,7 +148,7 @@ static char * get_message_from_queue() } /* un-allocated first. */ else { - char * new_queue = try_malloc(world.queue_size, f_name); + char * new_queue = try_malloc(world.queue_size, __func__); memcpy(new_queue, &(world.queue[cutout_len]), world.queue_size); free(world.queue); world.queue = new_queue; @@ -165,14 +161,13 @@ static char * get_message_from_queue() static void read_file_into_queue() { - char * f_name = "read_file_into_queue()"; uint8_t wait_seconds = 5; time_t now = time(0); struct timespec dur; dur.tv_sec = 0; dur.tv_nsec = 33333333; int test; - while (EOF == (test = try_fgetc(world.file_in, f_name))) + while (EOF == (test = try_fgetc(world.file_in, __func__))) { nanosleep(&dur, NULL); if (time(0) > now + wait_seconds) @@ -187,7 +182,7 @@ static void read_file_into_queue() { c = '\0'; } - char * new_queue = try_malloc(world.queue_size + 1, f_name); + char * new_queue = try_malloc(world.queue_size + 1, __func__); memcpy(new_queue, world.queue, world.queue_size); char * new_pos = new_queue + world.queue_size; * new_pos = c; @@ -195,14 +190,13 @@ static void read_file_into_queue() free(world.queue); world.queue = new_queue; } - while (EOF != (test = try_fgetc(world.file_in, f_name))); + while (EOF != (test = try_fgetc(world.file_in, __func__))); } static void update_worldstate_file() { - char * f_name = "update_worldstate_file()"; char * path_tmp; FILE * file = atomic_write_start(s[S_PATH_WORLDSTATE], &path_tmp); struct Thing * player = get_player(); @@ -215,12 +209,12 @@ static void update_worldstate_file() write_map(player, file); if (world.log) { - try_fwrite(world.log, strlen(world.log), 1, file, f_name); + try_fwrite(world.log, strlen(world.log), 1, file, __func__); } atomic_write_finish(file, s[S_PATH_WORLDSTATE], path_tmp); set_cleanup_flag(CLEANUP_WORLDSTATE); char * dot = ".\n"; - try_fwrite(dot, strlen(dot), 1, world.file_out, f_name); + try_fwrite(dot, strlen(dot), 1, world.file_out, __func__); fflush(world.file_out); } @@ -228,22 +222,20 @@ static void update_worldstate_file() static void write_value_as_line(uint32_t value, FILE * file) { - char * f_name = "write_value_as_line()"; char write_buf[12]; /* Holds 10 digits of uint32_t maximum + \n + \0. */ - exit_trouble(sprintf(write_buf, "%u\n",value) < 0, f_name,s[S_FCN_SPRINTF]); - try_fwrite(write_buf, strlen(write_buf), 1, file, f_name); + exit_trouble(sprintf(write_buf,"%u\n",value) < 0,__func__,s[S_FCN_SPRINTF]); + try_fwrite(write_buf, strlen(write_buf), 1, file, __func__); } static void write_inventory(struct Thing * player, FILE * file) { - char * f_name = "write_inventory()"; struct Thing * owned = player->owns; if (NULL == owned) { char * empty = "(none)\n"; - try_fwrite(empty, strlen(empty), 1, file, f_name); + try_fwrite(empty, strlen(empty), 1, file, __func__); } else { @@ -251,22 +243,21 @@ static void write_inventory(struct Thing * player, FILE * file) for (q = 0; NULL != owned; q++) { struct ThingType * tt = get_thing_type(owned->type); - try_fwrite(tt->name, strlen(tt->name), 1, file, f_name); - try_fputc('\n', file, f_name); + try_fwrite(tt->name, strlen(tt->name), 1, file, __func__); + try_fputc('\n', file, __func__); owned = owned->next; } } - try_fputc('%', file, f_name); - try_fputc('\n', file, f_name); + try_fputc('%', file, __func__); + try_fputc('\n', file, __func__); } static char * build_visible_map(struct Thing * player) { - char * f_name = "build_visible_map()"; uint32_t map_size = world.map.length * world.map.length; - char * visible_map = try_malloc(map_size, f_name); + char * visible_map = try_malloc(map_size, __func__); memset(visible_map, ' ', map_size); if (player->fov_map) /* May fail if player thing was created / positioned */ { /* by god command after turning off FOV building. */ @@ -304,16 +295,15 @@ static char * build_visible_map(struct Thing * player) static void write_map(struct Thing * player, FILE * file) { - char * f_name = "write_map()"; char * visible_map = build_visible_map(player); uint16_t x, y; for (y = 0; y < world.map.length; y++) { for (x = 0; x < world.map.length; x++) { - try_fputc(visible_map[(y * world.map.length) + x], file, f_name); + try_fputc(visible_map[(y * world.map.length) + x], file, __func__); } - try_fputc('\n', file, f_name); + try_fputc('\n', file, __func__); } free(visible_map); } @@ -322,7 +312,6 @@ static void write_map(struct Thing * player, FILE * file) extern char * io_round() { - char * f_name = "io_round()"; if (0 < world.queue_size) { return get_message_from_queue(); @@ -335,7 +324,7 @@ extern char * io_round() read_file_into_queue(); if (world.queue_size && '\0' != world.queue[world.queue_size - 1]) { - char * new_queue = try_malloc(world.queue_size + 1, f_name); + char * new_queue = try_malloc(world.queue_size + 1, __func__); memcpy(new_queue, world.queue, world.queue_size); new_queue[world.queue_size] = '\0'; world.queue_size++; @@ -349,19 +338,18 @@ extern char * io_round() extern void save_world() { - char * f_name = "save_world()"; char * path_tmp; FILE * file = atomic_write_start(s[S_PATH_SAVE], &path_tmp); write_key_value(file, s[S_CMD_MAPLENGTH], world.map.length); write_key_value(file, s[S_CMD_PLAYTYPE], world.player_type); - try_fputc('\n', file, f_name); + try_fputc('\n', file, __func__); struct ThingAction * ta; for (ta = world.thing_actions; ta; ta = ta->next) { write_key_value(file, s[S_CMD_THINGACTION], ta->id); write_key_value(file, s[S_CMD_TA_EFFORT], ta->effort); write_key_string(file, s[S_CMD_TA_NAME], ta->name); - try_fputc('\n', file, f_name); + try_fputc('\n', file, __func__); } struct ThingType * tt; for (tt = world.thing_types; tt; tt = tt->next) @@ -370,21 +358,21 @@ extern void save_world() write_key_value(file, s[S_CMD_TT_STARTN], tt->start_n); write_key_value(file, s[S_CMD_TT_HP], tt->lifepoints); int test = fprintf(file, "%s %c\n", s[S_CMD_TT_SYMB], tt->char_on_map); - exit_trouble(test < 0, f_name, "fprintf()"); + exit_trouble(test < 0, __func__, "fprintf"); write_key_string(file, s[S_CMD_TT_NAME], tt->name); write_key_value(file, s[S_CMD_TT_CONSUM], tt->consumable); - try_fputc('\n', file, f_name); + try_fputc('\n', file, __func__); } for (tt = world.thing_types; tt; tt = tt->next) { write_key_value(file, s[S_CMD_THINGTYPE], tt->id); write_key_value(file, s[S_CMD_TT_CORPS], tt->corpse_id); } - try_fputc('\n', file, f_name); + try_fputc('\n', file, __func__); write_key_value(file, s[S_CMD_SEED_MAP], world.seed_map); write_key_value(file, s[S_CMD_SEED_RAND], world.seed); write_key_value(file, s[S_CMD_TURN], world.turn); - try_fputc('\n', file, f_name); + try_fputc('\n', file, __func__); struct Thing * t; for (t = world.things; t; t = t->next) { diff --git a/src/server/map.c b/src/server/map.c index 059a713..7eececa 100644 --- a/src/server/map.c +++ b/src/server/map.c @@ -139,9 +139,8 @@ static void make_trees() extern void remake_map() { - char * f_name = "init_map()"; free(world.map.cells); - world.map.cells = try_malloc(world.map.length * world.map.length, f_name); + world.map.cells = try_malloc(world.map.length * world.map.length, __func__); uint32_t store_seed = world.seed; world.seed = world.seed_map; make_sea(); diff --git a/src/server/run.c b/src/server/run.c index 8251d06..70a663c 100644 --- a/src/server/run.c +++ b/src/server/run.c @@ -159,11 +159,10 @@ static uint8_t parse_command_1arg(char * tok0, char * tok1) static void server_test() { - char * f_name = "server_test()"; char test[10 + 1 + 10 + 1 + 1]; - FILE * file = try_fopen(s[S_PATH_OUT], "r", f_name); - try_fgets(test, 10 + 10 + 1 + 1, file, f_name); - try_fclose(file, f_name); + FILE * file = try_fopen(s[S_PATH_OUT], "r", __func__); + try_fgets(test, 10 + 10 + 1 + 1, file, __func__); + try_fclose(file, __func__); if (strcmp(test, world.server_test)) { unset_cleanup_flag(CLEANUP_WORLDSTATE); @@ -219,23 +218,22 @@ static void turn_over() static void record_msg(char * msg) { - char * f_name = "record_msg()"; char * path_tmp; FILE * file_tmp = atomic_write_start(s[S_PATH_RECORD], &path_tmp); if (!access(s[S_PATH_RECORD], F_OK)) { - FILE * file_read = try_fopen(s[S_PATH_RECORD], "r", f_name); + FILE * file_read = try_fopen(s[S_PATH_RECORD], "r", __func__); uint32_t linemax = textfile_width(file_read); - char * line = try_malloc(linemax + 1, f_name); - while (try_fgets(line, linemax + 1, file_read, f_name)) + char * line = try_malloc(linemax + 1, __func__); + while (try_fgets(line, linemax + 1, file_read, __func__)) { - try_fwrite(line, strlen(line), 1, file_tmp, f_name); + try_fwrite(line, strlen(line), 1, file_tmp, __func__); } free(line); - try_fclose(file_read, f_name); + try_fclose(file_read, __func__); } - try_fwrite(msg, strlen(msg), 1, file_tmp, f_name); - try_fputc('\n', file_tmp, f_name); + try_fwrite(msg, strlen(msg), 1, file_tmp, __func__); + try_fputc('\n', file_tmp, __func__); atomic_write_finish(file_tmp, s[S_PATH_RECORD], path_tmp); } @@ -243,10 +241,9 @@ static void record_msg(char * msg) extern void obey_msg(char * msg, uint8_t do_record, uint8_t do_verbose) { - char * f_name = "obey_msg()"; if (world.is_verbose && do_verbose) { - exit_trouble(-1 == printf("Input: %s\n", msg), f_name, "printf()"); + exit_trouble(-1 == printf("Input: %s\n", msg), __func__, "printf"); } set_err_line_options("Trouble with message: ", msg, 0); char * msg_copy = strdup(msg); @@ -283,7 +280,6 @@ extern void obey_msg(char * msg, uint8_t do_record, uint8_t do_verbose) extern uint8_t io_loop() { - char * f_name = "io_loop()"; while (1) { server_test(); @@ -294,7 +290,7 @@ extern uint8_t io_loop() } if (world.is_verbose) { - exit_trouble(-1 == printf("Input: %s\n", msg), f_name, "printf()"); + exit_trouble(-1 == printf("Input: %s\n", msg), __func__, "printf"); } if (!strcmp("QUIT", msg)) { @@ -305,7 +301,7 @@ extern uint8_t io_loop() { free(msg); char * pong = "PONG\n"; - try_fwrite(pong, strlen(pong), 1, world.file_out, f_name); + try_fwrite(pong, strlen(pong), 1, world.file_out, __func__); fflush(world.file_out); continue; } diff --git a/src/server/thing_actions.c b/src/server/thing_actions.c index e91f576..bcf9e45 100644 --- a/src/server/thing_actions.c +++ b/src/server/thing_actions.c @@ -85,7 +85,6 @@ static uint8_t text_equals_log_end(char * log, char * text) static void update_log(char * text) { - char * f_name = "update_log()"; uint16_t len_new = strlen(text); uint16_t len_old = 0; uint16_t offset = 0; @@ -103,10 +102,10 @@ static void update_log(char * text) } } uint16_t len_whole = len_old + len_new + 1; - char * new_text = try_malloc(len_whole, f_name); + char * new_text = try_malloc(len_whole, __func__); memcpy(new_text, world.log + offset, len_old); int test = sprintf(new_text + len_old, "%s", text); - exit_trouble(test < 0, f_name, s[S_FCN_SPRINTF]); + exit_trouble(test < 0, __func__, s[S_FCN_SPRINTF]); free(world.log); world.log = new_text; } @@ -115,7 +114,6 @@ static void update_log(char * text) static void actor_hits_actor(struct Thing * hitter, struct Thing * hitted) { - char * f_name = "actor_hits_actor()"; struct ThingType * tt_hitter = get_thing_type(hitter->type); struct ThingType * tt_hitted = get_thing_type(hitted->type); struct Thing * player = get_player(); @@ -132,9 +130,9 @@ static void actor_hits_actor(struct Thing * hitter, struct Thing * hitted) msg3 = tt_hitted->name; } uint8_t len = 1 + strlen(msg1) + 1 + strlen(msg2) + 1 + strlen(msg3) + 2; - char * msg = try_malloc(len, f_name); + char * msg = try_malloc(len, __func__); int test = sprintf(msg, "\n%s %s %s.", msg1, msg2, msg3); - exit_trouble(test < 0, f_name, s[S_FCN_SPRINTF]); + exit_trouble(test < 0, __func__, s[S_FCN_SPRINTF]); update_log(msg); free(msg); hitted->lifepoints--; @@ -173,7 +171,6 @@ static uint8_t match_dir(char d, char ** dsc_d, char match, char * dsc_match) static void playerbonus_move(char d, uint8_t passable) { - char * f_name = "playerbonus_move()"; char * dsc_dir = "north-east"; if ( match_dir(d, &dsc_dir, 'd', "east") || match_dir(d, &dsc_dir, 'c', "south-east") @@ -188,9 +185,9 @@ static void playerbonus_move(char d, uint8_t passable) { dsc_move = "You fail to move "; } - char * msg = try_malloc(strlen(dsc_move) + strlen (dsc_dir) + 3, f_name); + char * msg = try_malloc(strlen(dsc_move) + strlen (dsc_dir) + 3, __func__); int test = sprintf(msg, "\n%s%s.", dsc_move, dsc_dir); - exit_trouble(test < 0, f_name, s[S_FCN_SPRINTF]); + exit_trouble(test < 0, __func__, s[S_FCN_SPRINTF]); update_log(msg); free(msg); } diff --git a/src/server/things.c b/src/server/things.c index 09000ed..2cb7db9 100644 --- a/src/server/things.c +++ b/src/server/things.c @@ -66,8 +66,7 @@ static struct NextAndId * add_to_struct_list(size_t n_size, uint8_t start_id, int16_t id, uint8_t struct_id, struct NextAndId ** start) { - char * f_name = "add_to_struct_list()"; - struct NextAndId * nai = try_malloc(n_size, f_name); + struct NextAndId * nai = try_malloc(n_size, __func__); memset(nai, 0, n_size); nai->id = (start_id<=id && id<=UINT8_MAX) ? id : get_unused_id(struct_id); struct NextAndId ** nai_ptr_ptr = start;