Freeciv-3.1: /fast/freeciv/nightly/S3_1/utility/shared.c File Reference

Freeciv-3.1

Loading...
Searching...
No Matches
Macros | Functions | Variables
shared.c File Reference
`#include "fc_prehdrs.h"`
`#include <errno.h>`
`#include <limits.h>`
`#include <stdarg.h>`
`#include <stdio.h>`
`#include <stdlib.h>`
`#include <sys/stat.h>`
`#include <sys/types.h>`
`#include "astring.h"`
`#include "fc_dirent.h"`
`#include "fciconv.h"`
`#include "fcintl.h"`
`#include "mem.h"`
`#include "rand.h"`
`#include "string_vector.h"`
`#include "shared.h"`

Go to the source code of this file.

## Macros

#define | DEFAULT_DATA_PATH
#define | DEFAULT_SAVE_PATH
#define | DEFAULT_SCENARIO_PATH
#define | FREECIV_DATA_PATH "FREECIV_DATA_PATH"
#define | FREECIV_SAVE_PATH "FREECIV_SAVE_PATH"
#define | FREECIV_SCENARIO_PATH "FREECIV_SCENARIO_PATH"
#define | HOMEVAR "HOME"
#define | default_data_path DEFAULT_DATA_PATH
#define | default_scenario_path DEFAULT_SCENARIO_PATH

## Functions

static int | compare_file_mtime_ptrs (const struct fileinfo *const *ppa, const struct fileinfo *const *ppb)
static char * | expand_dir (char *tok_in, bool ok_to_free)
enum fc_tristate | fc_tristate_and (enum fc_tristate one, enum fc_tristate two)
enum fc_tristate | fc_tristate_or (enum fc_tristate one, enum fc_tristate two)
const char * | big_int_to_text (unsigned int mantissa, unsigned int exponent)
const char * | int_to_text (unsigned int number)
static bool | is_ascii (char ch)
bool | is_safe_filename (const char *name)
bool | is_ascii_name (const char *name)
bool | is_base64url (const char *s)
void | randomize_base64url_string (char *s, size_t n)
int | compare_strings (const void *first, const void *second)
int | compare_strings_ptrs (const void *first, const void *second)
int | compare_strings_strvec (const char *const *first, const char *const *second)
char * | skip_leading_spaces (char *s)
void | remove_leading_spaces (char *s)
void | remove_trailing_spaces (char *s)
void | remove_leading_trailing_spaces (char *s)
static void | remove_trailing_char (char *s, char trailing)
char * | end_of_strn (char *str, int *nleft)
bool | check_strlen (const char *str, size_t len, const char *errmsg)
size_t | loud_strlcpy (char *buffer, const char *str, size_t len, const char *errmsg)
bool | str_to_int (const char *str, int *pint)
bool | str_to_uint (const char *str, unsigned int *pint)
bool | str_to_float (const char *str, float *pfloat)
char * | user_home_dir (void)
void | free_user_home_dir (void)
char * | freeciv_storage_dir (void)
void | free_freeciv_storage_dir (void)
char * | user_username (char *buf, size_t bufsz)
static struct strvec * | base_get_dirs (const char *dir_list)
void | free_data_dir_names (void)
const struct strvec * | get_data_dirs (void)
const struct strvec * | get_save_dirs (void)
const struct strvec * | get_scenario_dirs (void)
struct strvec * | fileinfolist (const struct strvec *dirs, const char *suffix)
const char * | fileinfoname (const struct strvec *dirs, const char *filename)
void | free_fileinfo_data (void)
static void | fileinfo_destroy (struct fileinfo *pfile)
static int | compare_file_name_ptrs (const struct fileinfo *const *ppa, const struct fileinfo *const *ppb)
static bool | compare_fileinfo_name (const struct fileinfo *pa, const struct fileinfo *pb)
struct fileinfo_list * | fileinfolist_infix (const struct strvec *dirs, const char *infix, bool nodups)
const char * | setup_langname (void)
void | switch_lang (const char *lang)
void | init_nls (void)
void | free_nls (void)
void | dont_run_as_root (const char *argv0, const char *fallback)
const char * | m_pre_description (enum m_pre_result result)
enum m_pre_result | match_prefix (m_pre_accessor_fn_t accessor_fn, size_t n_names, size_t max_len_name, m_pre_strncmp_fn_t cmp_fn, m_strlen_fn_t len_fn, const char *prefix, int *ind_result)
enum m_pre_result | match_prefix_full (m_pre_accessor_fn_t accessor_fn, size_t n_names, size_t max_len_name, m_pre_strncmp_fn_t cmp_fn, m_strlen_fn_t len_fn, const char *prefix, int *ind_result, int *matches, int max_matches, int *pnum_matches)
char * | get_multicast_group (bool ipv6_preferred)
void | free_multicast_group (void)
void | interpret_tilde (char *buf, size_t buf_size, const char *filename)
char * | interpret_tilde_alloc (const char *filename)
char * | skip_to_basename (char *filepath)
bool | make_dir (const char *pathname)
bool | make_dir_for_file (char *filename)
bool | path_is_absolute (const char *filename)
char | scanin (const char **buf, char *delimiters, char *dest, int size)
void | format_time_duration (time_t t, char *buf, int maxlen)
void | array_shuffle (int *array, int n)
static bool | wildcard_asterisk_fit (const char *pattern, const char *test)
static bool | wildcard_range_fit (const char **pattern, const char **test)
bool | wildcard_fit_string (const char *pattern, const char *test)
int | fc_vsnprintcf (char *buf, size_t buf_len, const char *format, const struct cf_sequence *sequences, size_t sequences_num)
int | fc_snprintcf (char *buf, size_t buf_len, const char *format,...)
static size_t | extract_escapes (const char *format, char *escapes, size_t max_escapes)
bool | formats_match (const char *format1, const char *format2)

## Variables

static char * | grouping = NULL
static char * | grouping_sep = NULL
static const char | base64url []
static struct strvec * | data_dir_names = NULL
static struct strvec * | save_dir_names = NULL
static struct strvec * | scenario_dir_names = NULL
static char * | mc_group = NULL
static char * | home_dir_user = NULL
static char * | storage_dir_freeciv = NULL
static struct astring | realfile = ASTRING_INIT

## Macro Definition Documentation

## ◆ DEFAULT_DATA_PATH

#define DEFAULT_DATA_PATH

Value:
"." PATH_SEPARATOR \
"data" PATH_SEPARATOR \
FREECIV_STORAGE_DIR DIR_SEPARATOR DATASUBDIR
DIR_SEPARATOR
#define DIR_SEPARATOR
Definition shared.h:127
PATH_SEPARATOR
#define PATH_SEPARATOR
Definition shared.h:116

Definition at line 69 of file shared.c.

## ◆ default_data_path

#define default_data_path DEFAULT_DATA_PATH

## ◆ DEFAULT_SAVE_PATH

#define DEFAULT_SAVE_PATH

Value:
"." PATH_SEPARATOR \
FREECIV_STORAGE_DIR DIR_SEPARATOR "saves"

Definition at line 74 of file shared.c.

## ◆ DEFAULT_SCENARIO_PATH

#define DEFAULT_SCENARIO_PATH

Value:
"." PATH_SEPARATOR \
"data" DIR_SEPARATOR "scenarios" PATH_SEPARATOR \
FREECIV_STORAGE_DIR DATASUBDIR DIR_SEPARATOR "scenarios" PATH_SEPARATOR \
FREECIV_STORAGE_DIR DIR_SEPARATOR "scenarios"

Definition at line 78 of file shared.c.

## ◆ default_scenario_path

#define default_scenario_path DEFAULT_SCENARIO_PATH

## ◆ FREECIV_DATA_PATH

#define FREECIV_DATA_PATH "FREECIV_DATA_PATH"

Definition at line 87 of file shared.c.

## ◆ FREECIV_SAVE_PATH

#define FREECIV_SAVE_PATH "FREECIV_SAVE_PATH"

Definition at line 90 of file shared.c.

## ◆ FREECIV_SCENARIO_PATH

#define FREECIV_SCENARIO_PATH "FREECIV_SCENARIO_PATH"

Definition at line 93 of file shared.c.

## ◆ HOMEVAR

#define HOMEVAR "HOME"

## Function Documentation

## ◆ array_shuffle()

void array_shuffle | ( | int * | array,
|  | int | n
| ) |  |

Randomize the elements of an array using the Fisher-Yates shuffle.

see: http://benpfaff.org/writings/clc/shuffle.html

Definition at line 1997 of file shared.c.

Referenced by trade_generator::calculate(), genlist_shuffle(), map_generate_fair_islands(), and shuffle_players().

## ◆ base_get_dirs()

static struct strvec * base_get_dirs | ( | const char * | dir_list | ) |

static

Returns a list of directory paths, in the order in which they should be searched. Base function for get_data_dirs(), get_save_dirs(), get_scenario_dirs()

Definition at line 832 of file shared.c.

Referenced by get_data_dirs(), get_save_dirs(), and get_scenario_dirs().

## ◆ big_int_to_text()

const char * big_int_to_text | ( | unsigned int | mantissa,
|  | unsigned int | exponent
| ) |  |

Returns a statically allocated string containing a nicely-formatted version of the given number according to the user's locale. (Only works for numbers >= zero.) The number is given in scientific notation as mantissa * 10^exponent.

Definition at line 159 of file shared.c.

Referenced by int_to_text(), and population_to_text().

## ◆ check_strlen()

bool check_strlen | ( | const char * | str,
|  | size_t | len,
|  | const char * | errmsg
| ) |  |

Check the length of the given string. If the string is too long, log errmsg, which should be a string in printf-format taking up to two arguments: the string and the length.

Definition at line 492 of file shared.c.

Referenced by load_ruleset_nations(), and loud_strlcpy().

## ◆ compare_file_mtime_ptrs()

static int compare_file_mtime_ptrs | ( | const struct fileinfo *const * | ppa,
|  | const struct fileinfo *const * | ppb
| ) |  |

static

Compare modification times.

Definition at line 1169 of file shared.c.

Referenced by fileinfolist_infix().

## ◆ compare_file_name_ptrs()

static int compare_file_name_ptrs | ( | const struct fileinfo *const * | ppa,
|  | const struct fileinfo *const * | ppb
| ) |  |

static

Compare names.

Definition at line 1181 of file shared.c.

Referenced by fileinfolist_infix().

## ◆ compare_fileinfo_name()

static bool compare_fileinfo_name | ( | const struct fileinfo * | pa,
|  | const struct fileinfo * | pb
| ) |  |

static

Compare names.

Definition at line 1190 of file shared.c.

Referenced by fileinfolist_infix().

## ◆ compare_strings()

int compare_strings | ( | const void * | first,
|  | const void * | second
| ) |  |

Compares two strings, in the collating order of the current locale, given pointers to the two strings (i.e., given "char *"s). Case-sensitive. Designed to be called from qsort().

Definition at line 358 of file shared.c.

Referenced by help_item_compar().

## ◆ compare_strings_ptrs()

int compare_strings_ptrs | ( | const void * | first,
|  | const void * | second
| ) |  |

Compares two strings, in the collating order of the current locale, given pointers to the two string pointers (i.e., given "char **"s). Case-sensitive. Designed to be called from qsort().

Definition at line 368 of file shared.c.

Referenced by packet_handlers_get().

## ◆ compare_strings_strvec()

int compare_strings_strvec | ( | const char *const * | first,
|  | const char *const * | second
| ) |  |

Compares two strings, in the collating order of the current locale, given pointers to the two string pointers. Case-sensitive. Designed to be called from strvec_sort().

Definition at line 378 of file shared.c.

Referenced by fileinfolist().

## ◆ dont_run_as_root()

void dont_run_as_root | ( | const char * | argv0,
|  | const char * | fallback
| ) |  |

If we have root privileges, die with an error. (Eg, for security reasons.) Param argv0 should be argv[0] or similar; fallback is used instead if argv0 is NULL. But don't die on systems where the user is always root... (a general test for this would be better). Doesn't use log_*() because gets called before logging is setup.

Definition at line 1539 of file shared.c.

Referenced by client_main(), and main().

## ◆ end_of_strn()

char * end_of_strn | ( | char * | str,
|  | int * | nleft
| ) |  |

Returns pointer to '\0' at end of string 'str', and decrements nleft by the length of 'str'. This is intended to be useful to allow strcat-ing without traversing the whole string each time, while still keeping track of the buffer length. Eg: char buf[128]; int n = sizeof(buf); char *p = buf;

fc_snprintf(p, n, "foo%p", p); p = end_of_strn(p, &n); fc_strlcpy(p, "yyy", n);

Definition at line 479 of file shared.c.

## ◆ expand_dir()

static char * expand_dir | ( | char * | tok_in,
|  | bool | ok_to_free
| ) |  |

static

Return tok_in directory name with "~/" expanded as user home directory. The function might return tok_in, or a new string. In either case caller should free() the returned string eventually. Also, tok_in should be something expand_dir() can free itself if it decides to return newly created string (so caller can always free() just the returned string, not to care if it's same as tok_in or not). If ok_to_free is FALSE, expand_dir() never frees original but can still return either it or a newly allocated string.

Definition at line 778 of file shared.c.

Referenced by base_get_dirs(), and freeciv_storage_dir().

## ◆ extract_escapes()

static size_t extract_escapes | ( | const char * | format,
|  | char * | escapes,
|  | size_t | max_escapes
| ) |  |

static

Extract the sequences of a format. Returns the number of extracted escapes.

Definition at line 2372 of file shared.c.

Referenced by formats_match().

## ◆ fc_snprintcf()

int fc_snprintcf | ( | char * | buf,
|  | size_t | buf_len,
|  | const char * | format,
|  |  | ...
| ) |  |

Print a string with a custom format. The additional arguments are a suite of cf_*_seq() finished by cf_end(). This return the number of printed characters (excluding the last '\0') or -1 if the buffer is full.

Example: char buf[256];

fc_snprintcf(buf, sizeof(buf), "%y %+06y", cf_int_seq('y', 2010), cf_end()); This will print "2010 +02010" into buf.

Definition at line 2338 of file shared.c.

## ◆ fc_tristate_and()

enum fc_tristate fc_tristate_and | ( | enum fc_tristate | one,
|  | enum fc_tristate | two
| ) |  |

An AND function for fc_tristate.

Definition at line 124 of file shared.c.

Referenced by action_enabled_local(), and action_prob().

## ◆ fc_tristate_or()

enum fc_tristate fc_tristate_or | ( | enum fc_tristate | one,
|  | enum fc_tristate | two
| ) |  |

An OR function for fc_tristate.

Definition at line 140 of file shared.c.

Referenced by is_diplrel_unitany_in_range().

## ◆ fc_vsnprintcf()

int fc_vsnprintcf | ( | char * | buf,
|  | size_t | buf_len,
|  | const char * | format,
|  | const struct cf_sequence * | sequences,
|  | size_t | sequences_num
| ) |  |

Print a string with a custom format. sequences is a pointer to an array of sequences, probably defined with CF_*_SEQ(). sequences_num is the number of the sequences, or -1 in the case the array is terminated with CF_END.

Example: static const struct cf_sequence sequences[] = { CF_INT_SEQ('y', 2010) }; char buf[256];

fc_vsnprintcf(buf, sizeof(buf), "%y %+06y", sequences, 1); This will print "2010 +02010" into buf.

Definition at line 2190 of file shared.c.

Referenced by fc_snprintcf(), and generate_save_name().

## ◆ fileinfo_destroy()

static void fileinfo_destroy | ( | struct fileinfo * | pfile | ) |

static

Destroys the file info structure.

Definition at line 1159 of file shared.c.

Referenced by fileinfolist_infix().

## ◆ fileinfolist()

struct strvec * fileinfolist | ( | const struct strvec * | dirs,
|  | const char * | suffix
| ) |  |

Returns a string vector storing the filenames in the data directories matching the given suffix.

The list is allocated when the function is called; it should either be stored permanently or destroyed (with strvec_destroy()).

The suffixes are removed from the filenames before the list is returned.

Definition at line 1020 of file shared.c.

Referenced by get_audio_speclist(), get_init_script_choices(), get_theme_list(), get_tileset_list(), themespec_try_read(), and tilespec_try_read().

## ◆ fileinfolist_infix()

struct fileinfo_list * fileinfolist_infix | ( | const struct strvec * | dirs,
|  | const char * | infix,
|  | bool | nodups
| ) |  |

Search for filenames with the "infix" substring in the "subpath" subdirectory of the data path. "nodups" removes duplicate names. The returned list will be sorted by name first and modification time second. Returned "name"s will be truncated starting at the "infix" substring. The returned list must be freed with fileinfo_list_destroy().

Definition at line 1204 of file shared.c.

Referenced by popup_load_game_dialog(), save_dialog_mapimg_list(), save_dialog_savegame_list(), save_dialog_scenario_list(), show_scenarios(), update_load_page(), fc_client::update_load_page(), update_scenario_page(), and fc_client::update_scenarios_page().

## ◆ fileinfoname()

const char * fileinfoname | ( | const struct strvec * | dirs,
|  | const char * | filename
| ) |  |

Returns a filename to access the specified file from a directory by searching all specified directories for the file.

If the specified 'filename' is NULL, the returned string contains the effective path. (But this should probably only be used for debug output.)

Returns NULL if the specified filename cannot be found in any of the data directories. (A file is considered "found" if it can be read-opened.) The returned pointer points to static memory, so this function can only supply one filename at a time. Don't free that pointer.

TODO: Make this re-entrant

Definition at line 1094 of file shared.c.

Referenced by audio_play_tag(), audiospec_fullname(), boot_help_texts(), comments_load(), datafilename(), fc_icons::get_icon(), fc_icons::get_path(), fc_icons::get_pixmap(), get_token_value(), get_ui_filename(), load_command(), load_gfx_file(), load_gfx_file(), lua_command(), main(), read_init_script_real(), ruledit_main::ruledit_main(), set_rulesetdir(), sg_load_savefile(), srv_prepare(), theme_read_toplevel(), themespec_gfx_filename(), tileset_read_toplevel(), tilespec_fullname(), tilespec_gfx_filename(), and valid_ruleset_filename().

## ◆ format_time_duration()

void format_time_duration | ( | time_t | t,
|  | char * | buf,
|  | int | maxlen
| ) |  |

Convenience function to nicely format a time_t seconds value in to a string with hours, minutes, etc.

Definition at line 1954 of file shared.c.

Referenced by unit_can_do_action_now().

## ◆ formats_match()

bool formats_match | ( | const char * | format1,
|  | const char * | format2
| ) |  |

Returns TRUE iff both formats are compatible (if 'format1' can be used instead 'format2' and reciprocally).

Definition at line 2430 of file shared.c.

Referenced by ruler_title_check(), and sanity_check_ruleset_data().

## ◆ free_data_dir_names()

void free_data_dir_names | ( | void |  | ) |

Free data dir name vectors.

Definition at line 859 of file shared.c.

Referenced by libfreeciv_free().

## ◆ free_fileinfo_data()

void free_fileinfo_data | ( | void |  | ) |

Free resources allocated for fileinfoname service

Definition at line 1151 of file shared.c.

Referenced by libfreeciv_free().

## ◆ free_freeciv_storage_dir()

void free_freeciv_storage_dir | ( | void |  | ) |

Free freeciv storage directory information

Definition at line 690 of file shared.c.

Referenced by libfreeciv_free().

## ◆ free_multicast_group()

void free_multicast_group | ( | void |  | ) |

Free multicast group resources

Definition at line 1693 of file shared.c.

Referenced by libfreeciv_free().

## ◆ free_nls()

void free_nls | ( | void |  | ) |

Free memory allocated by Native Language Support

Definition at line 1522 of file shared.c.

Referenced by libfreeciv_free().

## ◆ free_user_home_dir()

void free_user_home_dir | ( | void |  | ) |

Free user home directory information

Definition at line 659 of file shared.c.

Referenced by libfreeciv_free().

## ◆ freeciv_storage_dir()

char * freeciv_storage_dir | ( | void |  | ) |

Returns string which gives freeciv storage dir. Gets value once, and then caches result. Note the caller should not mess with the returned string.

Definition at line 672 of file shared.c.

Referenced by client_start_server(), fcmp_parse_cmdline(), get_challenge_fullname(), mr_menu::save_image(), send_client_wants_hack(), server_sniff_all_input(), and tab_misc::tab_misc().

## ◆ get_data_dirs()

const struct strvec * get_data_dirs | ( | void |  | ) |

Returns a list of data directory paths, in the order in which they should be searched. These paths are specified internally or may be set as the environment variable $FREECIV_DATA PATH (a separated list of directories, where the separator itself is specified internally, platform-dependent). '~' at the start of a component (provided followed by '/' or '\0') is expanded as $HOME.

The returned pointer is static and shouldn't be modified, nor destroyed by the user caller.

Definition at line 886 of file shared.c.

Referenced by audio_play_tag(), audiospec_fullname(), boot_help_texts(), comments_load(), datafilename(), get_audio_speclist(), get_gui_specific_themes_directories(), fc_icons::get_icon(), get_init_script_choices(), fc_icons::get_path(), fc_icons::get_pixmap(), get_theme_list(), get_tileset_list(), get_token_value(), get_ui_filename(), load_gfx_file(), load_gfx_file(), lua_command(), main(), qtg_get_gui_specific_themes_directories(), read_init_script_real(), ruledit_main::ruledit_main(), set_rulesetdir(), srv_prepare(), theme_read_toplevel(), themespec_gfx_filename(), themespec_try_read(), tileset_read_toplevel(), tilespec_fullname(), tilespec_gfx_filename(), tilespec_try_read(), and valid_ruleset_filename().

## ◆ get_multicast_group()

char * get_multicast_group | ( | bool | ipv6_preferred | ) |

Returns string which gives the multicast group IP address for finding servers on the LAN, as specified by $FREECIV_MULTICAST_GROUP. Gets value once, and then caches result.

Definition at line 1662 of file shared.c.

Referenced by begin_lanserver_scan(), send_lanserver_response(), and server_open_socket().

## ◆ get_save_dirs()

const struct strvec * get_save_dirs | ( | void |  | ) |

Returns a list of save directory paths, in the order in which they should be searched. These paths are specified internally or may be set as the environment variable $FREECIV_SAVE_PATH (a separated list of directories, where the separator itself is specified internally, platform-dependent). '~' at the start of a component (provided followed by '/' or '\0') is expanded as $HOME.

The returned pointer is static and shouldn't be modified, nor destroyed by the user caller.

Definition at line 934 of file shared.c.

Referenced by load_command(), popup_load_game_dialog(), save_dialog_mapimg_list(), save_dialog_savegame_list(), mr_menu::save_game_as(), update_load_page(), and fc_client::update_load_page().

## ◆ get_scenario_dirs()

const struct strvec * get_scenario_dirs | ( | void |  | ) |

Returns a list of scenario directory paths, in the order in which they should be searched. These paths are specified internally or may be set as the environment variable $FREECIV_SCENARIO_PATH (a separated list of directories, where the separator itself is specified internally, platform-dependent). '~' at the start of a component (provided followed by '/' or '\0') is expanded as $HOME.

The returned pointer is static and shouldn't be modified, nor destroyed by the user caller.

Definition at line 971 of file shared.c.

Referenced by load_command(), save_dialog_scenario_list(), sg_load_savefile(), show_scenarios(), update_scenario_page(), and fc_client::update_scenarios_page().

## ◆ init_nls()

void init_nls | ( | void |  | ) |

Setup for Native Language Support, if configured to use it. (Call this only once, or it may leak memory.)

Definition at line 1463 of file shared.c.

Referenced by libfreeciv_init().

## ◆ int_to_text()

const char * int_to_text | ( | unsigned int | number | ) |

Return a prettily formatted string containing the given number.

Definition at line 233 of file shared.c.

Referenced by value_units().

## ◆ interpret_tilde()

void interpret_tilde | ( | char * | buf,
|  | size_t | buf_size,
|  | const char * | filename
| ) |  |

Interpret ~/ in filename as home dir New path is returned in buf of size buf_size

This may fail if the path is too long. It is better to use interpret_tilde_alloc.

Definition at line 1708 of file shared.c.

Referenced by lua_command(), read_init_script_real(), secfile_load_section(), secfile_save(), and write_init_script().

## ◆ interpret_tilde_alloc()

char * interpret_tilde_alloc | ( | const char * | filename | ) |

Interpret ~/ in filename as home dir

The new path is returned in buf, as a newly allocated buffer. The new path will always be allocated and written, even if there is no ~ present.

Definition at line 1725 of file shared.c.

Referenced by make_dir().

## ◆ is_ascii()

static bool is_ascii | ( | char | ch | ) |

static

Check whether or not the given char is a valid ascii character. The character can be in any charset so long as it is a superset of ascii.

Definition at line 242 of file shared.c.

Referenced by is_ascii_name().

## ◆ is_ascii_name()

bool is_ascii_name | ( | const char * | name | ) |

This is used in sundry places to make sure that names of cities, players etc. do not contain yucky characters of various sorts. Returns TRUE iff the name is acceptable. FIXME: Not internationalised.

Definition at line 282 of file shared.c.

Referenced by is_allowed_city_name(), is_good_password(), is_valid_username(), server_player_name_is_allowed(), and user_username().

## ◆ is_base64url()

bool is_base64url | ( | const char * | s | ) |

Check for valid base64url.

Definition at line 317 of file shared.c.

Referenced by sg_load_sanitycheck(), and sg_load_sanitycheck().

## ◆ is_safe_filename()

bool is_safe_filename | ( | const char * | name | ) |

Check if the name is safe security-wise. This is intended to be used to make sure an untrusted filename is safe to be used.

Definition at line 252 of file shared.c.

Referenced by api_server_save(), load_command(), lua_command(), read_init_script_real(), savename_validate(), scorefile_validate(), send_client_wants_hack(), and set_rulesetdir().

## ◆ loud_strlcpy()

size_t loud_strlcpy | ( | char * | buffer,
|  | const char * | str,
|  | size_t | len,
|  | const char * | errmsg
| ) |  |

Call check_strlen() on str and then strlcpy() it into buffer.

Definition at line 501 of file shared.c.

## ◆ m_pre_description()

const char * m_pre_description | ( | enum m_pre_result | result | ) |

Return a description string of the result. In English, form of description is suitable to substitute in, eg: prefix is (N.B.: The description is always in English, but they have all been marked for translation. If you want a localized version, use _() on the return.)

Definition at line 1561 of file shared.c.

Referenced by cmd_reply_no_such_conn(), and cmd_reply_no_such_player().

## ◆ make_dir()

bool make_dir | ( | const char * | pathname | ) |

If the directory "pathname" does not exist, recursively create all directories until it does.

TODO: Make errno available after a failure, preferably via fc_get_errno(). Currently there's things potentially messing errno between failed mkdir() and the function return.

Definition at line 1769 of file shared.c.

Referenced by create_mpdb(), img_save(), make_dir_for_file(), save_game(), save_ruleset(), send_client_wants_hack(), and server_sniff_all_input().

## ◆ make_dir_for_file()

bool make_dir_for_file | ( | char * | filename | ) |

If the directory part of the "filename" does not exist, recursively create all directories until it does.

Definition at line 1848 of file shared.c.

Referenced by download_modpack_recursive().

## ◆ match_prefix()

enum m_pre_result match_prefix | ( | m_pre_accessor_fn_t | accessor_fn,
|  | size_t | n_names,
|  | size_t | max_len_name,
|  | m_pre_strncmp_fn_t | cmp_fn,
|  | m_strlen_fn_t | len_fn,
|  | const char * | prefix,
|  | int * | ind_result
| ) |  |

See match_prefix_full().

Definition at line 1578 of file shared.c.

Referenced by command_named(), conn_by_user_prefix(), delegate_command(), fcdb_command(), lookup_option(), lua_command(), mapimg_command(), player_by_name_prefix(), reset_command(), show_list(), and vote_command().

## ◆ match_prefix_full()

enum m_pre_result match_prefix_full | ( | m_pre_accessor_fn_t | accessor_fn,
|  | size_t | n_names,
|  | size_t | max_len_name,
|  | m_pre_strncmp_fn_t | cmp_fn,
|  | m_strlen_fn_t | len_fn,
|  | const char * | prefix,
|  | int * | ind_result,
|  | int * | matches,
|  | int | max_matches,
|  | int * | pnum_matches
| ) |  |

Given n names, with maximum length max_len_name, accessed by accessor_fn(0) to accessor_fn(n-1), look for matching prefix according to given comparison function. Returns type of match or fail, and for return <= M_PRE_AMBIGUOUS sets *ind_result with matching index (or for ambiguous, first match). If max_len_name == 0, treat as no maximum. If the int array 'matches' is non-NULL, up to 'max_matches' ambiguous matching names indices will be inserted into it. If 'pnum_matches' is non-NULL, it will be set to the number of indices inserted into 'matches'.

Definition at line 1601 of file shared.c.

Referenced by check_player_or_user_name(), match_prefix(), and show_help().

## ◆ path_is_absolute()

bool path_is_absolute | ( | const char * | filename | ) |

Returns TRUE if the filename's path is absolute.

Definition at line 1870 of file shared.c.

Referenced by img_save(), and save_game().

## ◆ randomize_base64url_string()

void randomize_base64url_string | ( | char * | s,
|  | size_t | n
| ) |  |

generate a random string meeting criteria such as is_ascii_name(), is_base64url(), and is_safe_filename().

Definition at line 338 of file shared.c.

Referenced by init_new_game(), sg_load_sanitycheck(), and sg_load_sanitycheck().

## ◆ remove_leading_spaces()

void remove_leading_spaces | ( | char * | s | ) |

Removes leading spaces in string pointed to by 's'. Note 's' must point to writeable memory!

Definition at line 402 of file shared.c.

Referenced by handle_stdin_input_real(), and remove_leading_trailing_spaces().

## ◆ remove_leading_trailing_spaces()

void remove_leading_trailing_spaces | ( | char * | s | ) |

Removes leading and trailing spaces in string pointed to by 's'. Note 's' must point to writeable memory!

Definition at line 443 of file shared.c.

Referenced by cancelvote_command(), conn_pattern_from_string(), explain_option(), handle_login_request(), kick_command(), load_city_name_list(), lookup_string(), scanin(), secfile_lookup_bitwise_enum_default_full(), secfile_lookup_bitwise_enum_full(), secfile_lookup_enum_data(), secfile_lookup_enum_default_data(), server_player_set_name_full(), setting_bitwise_validate_base(), setting_bool_validate_base(), setting_enum_validate_base(), show_list(), show_settings(), strvec_remove_empty(), and unignore_command().

## ◆ remove_trailing_char()

static void remove_trailing_char | ( | char * | s,
|  | char | trailing
| ) |  |

static

As remove_trailing_spaces(), for specified char.

Definition at line 452 of file shared.c.

Referenced by expand_dir().

## ◆ remove_trailing_spaces()

void remove_trailing_spaces | ( | char * | s | ) |

Terminates string pointed to by 's' to remove trailing spaces; Note 's' must point to writeable memory!

Definition at line 420 of file shared.c.

Referenced by expand_dir(), handle_stdin_input_real(), and remove_leading_trailing_spaces().

## ◆ scanin()

char scanin | ( | const char ** | buf,
|  | char * | delimiters,
|  | char * | dest,
|  | int | size
| ) |  |

Scan in a word or set of words from start to but not including any of the given delimiters. The buf pointer will point past delimiter, or be set to NULL if there is nothing there. Removes excess white space.

This function should be safe, and dest will contain "\0" and buf == NULL on failure. We always fail gently.

Due to the way the scanning is performed, looking for a space will give you the first word even if it comes before multiple spaces.

Returns delimiter found.

Pass in NULL for dest and -1 for size to just skip ahead. Note that if nothing is found, dest will contain the whole string, minus leading and trailing whitespace. You can scan for "" to conveniently grab the remainder of a string.

Definition at line 1909 of file shared.c.

Referenced by sg_load_map_owner(), sg_load_map_owner(), sg_load_map_worked(), sg_load_map_worked(), sg_load_player_vision(), and sg_load_player_vision().

## ◆ setup_langname()

const char * setup_langname | ( | void |  | ) |

Language environmental variable (with emulation).

Definition at line 1281 of file shared.c.

Referenced by init_nls(), and theme_read_toplevel().

## ◆ skip_leading_spaces()

char * skip_leading_spaces | ( | char * | s | ) |

Returns 's' incremented to first non-space character.

Definition at line 387 of file shared.c.

Referenced by aicmd_command(), chat_msg_to_all(), chat_msg_to_allies(), chat_msg_to_conn(), chat_msg_to_global_observers(), chat_msg_to_player(), expand_dir(), fcdb_command(), handle_stdin_input_real(), lua_command(), and remove_leading_spaces().

## ◆ skip_to_basename()

char * skip_to_basename | ( | char * | filepath | ) |

Return a pointer to the start of the file basename in filepath. If the string contains no dir separator, it is returned itself.

Definition at line 1748 of file shared.c.

Referenced by scenario_list_callback().

## ◆ str_to_float()

bool str_to_float | ( | const char * | str,
|  | float * | pfloat
| ) |  |

Convert 'str' to its float representation if possible. 'pfloat' can be NULL, then it will only test 'str' only contains a floating point number.

Definition at line 578 of file shared.c.

Referenced by entry_from_token().

## ◆ str_to_int()

bool str_to_int | ( | const char * | str,
|  | int * | pint
| ) |  |

Convert 'str' to its int representation if possible. 'pint' can be NULL, then it will only test 'str' only contains an integer number.

Definition at line 512 of file shared.c.

Referenced by cancelvote_command(), client_main(), debug_command(), entry_from_token(), get_compression_level(), main(), main(), option_widget_apply(), re_parse_cmdline(), rup_parse_cmdline(), set_command(), sg_load_map_owner(), sg_load_map_owner(), sg_load_map_worked(), sg_load_map_worked(), sg_load_player_vision(), sg_load_player_vision(), team_command(), text_tag_init_from_sequence(), timeout_command(), unignore_command(), and vote_command().

## ◆ str_to_uint()

bool str_to_uint | ( | const char * | str,
|  | unsigned int * | pint
| ) |  |

Convert 'str' to its unsigned int representation if possible. 'pint' can be NULL, then it will only test 'str' only contains an unsigned integer number.

Definition at line 545 of file shared.c.

Referenced by log_parse_level_str(), and parse_options().

## ◆ switch_lang()

void switch_lang | ( | const char * | lang | ) |

Switch to specified LANG

Definition at line 1434 of file shared.c.

## ◆ user_home_dir()

char * user_home_dir | ( | void |  | ) |

Returns string which gives users home dir, as specified by $HOME. Gets value once, and then caches result. If $HOME is not set, give a log message and returns NULL. Note the caller should not mess with the returned string.

Definition at line 628 of file shared.c.

Referenced by expand_dir(), get_gui_specific_themes_directories(), interpret_tilde(), and interpret_tilde_alloc().

## ◆ user_username()

char * user_username | ( | char * | buf,
|  | size_t | bufsz
| ) |  |

Returns string which gives user's username, as specified by $USER or as given in password file for this user's uid, or a made up name if we can't get either of the above. Gets value once, and then caches result. Note the caller should not mess with returned string.

Definition at line 705 of file shared.c.

Referenced by client_main().

## ◆ wildcard_asterisk_fit()

static bool wildcard_asterisk_fit | ( | const char * | pattern,
|  | const char * | test
| ) |  |

static

Test an asterisk in the pattern against test. Returns TRUE if test fit the pattern. May be recursive, as it calls wildcard_fit_string() itself (if many asterisks).

Definition at line 2015 of file shared.c.

Referenced by wildcard_fit_string().

## ◆ wildcard_fit_string()

bool wildcard_fit_string | ( | const char * | pattern,
|  | const char * | test
| ) |  |

Returns TRUE if test fit the pattern. The pattern can contain special characters: '*': to specify a substitute for any zero or more characters. '?': to specify a substitute for any one character. '[...]': to specify a range of characters: '!': at the begenning of the range means that the matching result will be inverted 'A-Z': means any character between 'A' and 'Z'. 'agr': means 'a', 'g' or 'r'.

Definition at line 2141 of file shared.c.

Referenced by conn_pattern_match(), and wildcard_asterisk_fit().

## ◆ wildcard_range_fit()

static bool wildcard_range_fit | ( | const char ** | pattern,
|  | const char ** | test
| ) |  |

static

Test a range in the pattern against test. Returns TRUE if **test fit the first range in *pattern.

Definition at line 2075 of file shared.c.

Referenced by wildcard_fit_string().

## Variable Documentation

## ◆ base64url

const char base64url[]

static

Initial value:
=
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"

Definition at line 103 of file shared.c.

Referenced by is_base64url(), is_safe_filename(), and randomize_base64url_string().

## ◆ data_dir_names

struct strvec* data_dir_names = NULL

static

Definition at line 106 of file shared.c.

Referenced by free_data_dir_names(), and get_data_dirs().

## ◆ grouping

char* grouping = NULL

static

Definition at line 98 of file shared.c.

Referenced by big_int_to_text(), free_nls(), init_nls(), menu_entry_group_set_sensitive(), and menu_entry_group_set_sensitive().

## ◆ grouping_sep

char* grouping_sep = NULL

static

Definition at line 99 of file shared.c.

Referenced by big_int_to_text(), free_nls(), and init_nls().

## ◆ home_dir_user

char* home_dir_user = NULL

static

Definition at line 111 of file shared.c.

Referenced by free_user_home_dir(), and user_home_dir().

## ◆ mc_group

char* mc_group = NULL

static

Definition at line 110 of file shared.c.

Referenced by free_multicast_group(), and get_multicast_group().

## ◆ realfile

struct astring realfile = ASTRING_INIT

static

Definition at line 114 of file shared.c.

Referenced by fileinfoname(), and free_fileinfo_data().

## ◆ save_dir_names

struct strvec* save_dir_names = NULL

static

Definition at line 107 of file shared.c.

Referenced by free_data_dir_names(), and get_save_dirs().

## ◆ scenario_dir_names

struct strvec* scenario_dir_names = NULL

static

Definition at line 108 of file shared.c.

Referenced by free_data_dir_names(), and get_scenario_dirs().

## ◆ storage_dir_freeciv

char* storage_dir_freeciv = NULL

static

Definition at line 112 of file shared.c.

Referenced by free_freeciv_storage_dir(), and freeciv_storage_dir().