Crossfire Server, Branches 1.12  R18729
porting.c File Reference
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/param.h>
#include <stdio.h>
#include <autoconf.h>
#include <stdarg.h>
#include "global.h"
+ Include dependency graph for porting.c:

Go to the source code of this file.

Macros

#define DIGIT(x)
 
#define MAXPATHLEN   4096
 
#define MAXTMPRETRY   10
 
#define MBASE   ('z'-'a'+1+10)
 

Functions

void close_and_delete (FILE *fp, int compressed)
 
int isqrt (int n)
 
void make_path_to_file (const char *filename)
 
FILE * open_and_uncompress (const char *name, int flag, int *compressed)
 
static FILE * open_and_uncompress_file (const char *ext, const char *uncompressor, const char *name, int flag, int *compressed)
 
void remove_directory (const char *path)
 
int snprintf (char *dest, int max, const char *format,...)
 
int strcasecmp (const char *s1, const char *s2)
 
const char * strcasestr_local (const char *s, const char *find)
 
char * strdup_local (const char *str)
 
char * strerror_local (int errnum, char *buf, size_t size)
 
int strncasecmp (const char *s1, const char *s2, int n)
 
long strtol (register char *str, char **ptr, register int base)
 
char * tempnam_local (const char *dir, const char *pfx)
 
FILE * tempnam_secure (const char *dir, const char *pfx, char **filename)
 

Variables

static unsigned int curtmp = 0
 
const char * uncomp [NROF_COMPRESS_METHODS][3]
 

Detailed Description

This file contains various functions that are not really unique for crossfire, but rather provides what should be standard functions for systems that do not have them. In this way, most of the nasty system dependent stuff is contained here, with the program calling these functions.

Definition in file porting.c.

Macro Definition Documentation

#define DIGIT (   x)
Value:
(isdigit(x) ? (x)-'0' : \
islower(x) ? (x)+10-'a' : (x)+10-'A')

Converts x to number

Definition at line 318 of file porting.c.

Referenced by strtol().

#define MAXPATHLEN   4096

Referenced by tempnam_local().

#define MAXTMPRETRY   10

Referenced by tempnam_secure().

#define MBASE   ('z'-'a'+1+10)

Definition at line 320 of file porting.c.

Referenced by strtol().

Function Documentation

void close_and_delete ( FILE *  fp,
int  compressed 
)
int isqrt ( int  n)

Computes the square root. Based on (n+1)^2 = n^2 + 2n + 1 given that 1^2 = 1, then 2^2 = 1 + (2 + 1) = 1 + 3 = 4 3^2 = 4 + (4 + 1) = 4 + 5 = 1 + 3 + 5 = 9 4^2 = 9 + (6 + 1) = 9 + 7 = 1 + 3 + 5 + 7 = 16 ... In other words, a square number can be express as the sum of the series n^2 = 1 + 3 + ... + (2n-1)

Parameters
nnumber of which to compute the root.
Returns
square root.

Definition at line 573 of file porting.c.

Referenced by expand_lighted_sight(), find_ingred_cost(), fix_flesh_item(), get_rangevector(), get_rangevector_from_mapcoord(), play_sound_map(), roguelike_place_room(), spell_failure(), stand_in_light(), and value_limit().

+ Here is the caller graph for this function:

void make_path_to_file ( const char *  filename)

Checks if any directories in the given path doesn't exist, and creates if necessary.

Parameters
filenamefile path we'll want to access. Can be NULL.
Note
will LOG() to debug and error.

Definition at line 764 of file porting.c.

References llevDebug, llevError, LOG(), MAX_BUF, mkdir, S_ISDIR, SAVE_DIR_MODE, and strerror_local().

Referenced by create_destination(), key_change_class(), process_map(), save_map(), save_player(), and write_map_page().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

FILE* open_and_uncompress ( const char *  name,
int  flag,
int *  compressed 
)

open_and_uncompress() first searches for the original filename. If it exist, then it opens it and returns the file-pointer.

If not, it does two things depending on the flag. If the flag is set, it tries to create the original file by appending a compression suffix to name and uncompressing it. If the flag is not set, it creates a pipe that is used for reading the file (NOTE - you can not use fseek on pipes).

The compressed pointer is set to nonzero if the file is compressed (and thus, fp is actually a pipe.) It returns 0 if it is a normal file.

Parameters
namethe base file name without compression extension
flagonly used for compressed files:
  • if set, uncompress and open the file
  • if unset, uncompress the file via pipe
[out]compressedset to zero if the file was uncompressed
Returns
pointer to opened file, NULL on failure.
Note
will set ::errno if an error occurs.

Definition at line 724 of file porting.c.

References NROF_COMPRESS_METHODS, and open_and_uncompress_file().

Referenced by add_score(), check_login(), display_high_score(), display_motd(), forbid_play(), init_artifacts(), init_attackmess(), init_book_archive(), init_experience(), init_formulae(), init_msgfile(), init_regions(), init_startup(), load_archetypes(), load_original_map(), load_overlay_map(), load_settings(), load_temporary_map(), load_treasures(), load_unique_objects(), metaserver2_init(), read_client_images(), send_news(), send_rules(), and verify_player().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static FILE* open_and_uncompress_file ( const char *  ext,
const char *  uncompressor,
const char *  name,
int  flag,
int *  compressed 
)
static

Open and possibly uncompress a file.

Parameters
extthe extension if the file is compressed.
uncompressorthe command to uncompress the file if the file is compressed.
namethe base file name without compression extension
flagonly used for compressed files:
  • if set, uncompress and open the file
  • if unset, uncompress the file via pipe
[out]compressedset to zero if the file was uncompressed
Returns
pointer to opened file, NULL on failure.
Note
will set ::errno if an error occurs.

Definition at line 621 of file porting.c.

References llevError, LOG(), MAX_BUF, popen, S_ISREG, snprintf(), unlink, WEXITSTATUS, and WIFEXITED.

Referenced by open_and_uncompress().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void remove_directory ( const char *  path)

This function removes everything in the directory, and the directory itself.

Errors are LOG() to error level.

Parameters
pathdirectory to remove.
Note
will fail if any file has a name starting by .

Definition at line 192 of file porting.c.

References closedir(), dirent::d_name, llevError, LOG(), MAX_BUF, opendir(), readdir(), remove_directory(), S_ISDIR, snprintf(), and unlink.

Referenced by delete_character(), and remove_directory().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int snprintf ( char *  dest,
int  max,
const char *  format,
  ... 
)

Formats to a string, in a size-safe way.

Parameters
destwhere to write.
maxmax length of dest.
formatformat specifier, and arguments.
Returns
number of chars written to dest.
Warning
this function will abort() if there is an overflow.
Todo:
try to do something better than abort()?

Definition at line 498 of file porting.c.

Referenced by add_author(), add_exit_to_item(), add_region_link(), add_required_parameter(), add_score(), adjust_sign_msg(), animate_weapon(), apply_anim_suffix(), apply_builder_floor(), apply_builder_wall(), apply_changes_to_player(), artifact_msg(), attempt_steal(), calc_alch_danger(), can_pay(), cast_consecrate(), cast_create_town_portal(), cfapi_map_set_map_property(), cfapi_object_get_property(), cfpython_globalEventListener(), cfpython_runPluginCommand(), change_book(), check_login(), check_path(), check_pick(), check_race_restrictions(), checkbanned(), checkdm(), clean_path(), command_banish(), command_bowmode(), command_cointoss(), command_help(), command_loadtest(), command_me(), command_party(), command_players(), command_rename_item(), command_reset(), command_title(), command_use(), communicate(), content_recipe_value(), convert_newline(), copy_message(), create_items_path(), create_overlay_pathname(), create_pathname(), create_singularity(), create_template_pathname(), dead_player(), deep_swamp_type_process(), delete_character(), describe_item(), describe_monster(), describe_resistance(), describe_shop(), display_high_score(), display_motd(), display_who_entry(), do_map_index(), do_parameters(), do_region_kills(), do_server(), do_talk_npc(), do_tell(), do_tiled_map_picture(), dragon_ability_gain(), dragon_eat_flesh(), draw_one_high_score(), dump_alchemy(), dump_alchemy_costs(), dump_gods(), dump_unused_maps(), enter_fixed_template_map(), enter_random_map(), enter_random_template_map(), enter_unique_map(), esrv_draw_look(), eventListener(), examine(), find_ingred_cost(), find_maps(), find_style(), fix_flesh_item(), fix_tiled_map(), flush_output_element(), food_type_apply(), forbid_play(), form_party(), format_time(), get_exit_seed(), get_levelnumber(), get_split_ob(), get_who_escape_code_value(), getPluginProperty(), give_artifact_abilities(), god_enchants_weapon(), god_examines_item(), god_info_msg(), gravestone_text(), help_topics(), i18n_init(), infect_object(), init_anim(), init_artifacts(), init_attackmess(), init_book_archive(), init_clocks(), init_dynamic(), init_emergency_mappath(), init_experience(), init_formulae(), init_msgfile(), init_races(), init_regions(), init_startup(), initPlugins(), is_defined_recipe(), key_change_class(), key_confirm_quit(), keyplace(), kill_object(), kill_player(), load_archetypes(), load_dir(), load_map_header(), load_materials(), load_original_map(), load_settings(), load_temporary_map(), load_treasures(), load_unique_objects(), lock_and_hide_doors(), magic_wall(), main(), make_formula_book(), make_map_floor(), make_map_walls(), map_remove_unique_files(), metaserver2_init(), metaserver2_updates(), metaserver_update(), mon_desc(), mon_info_msg(), move_symptom(), msgfile_msg(), npc_say(), nstrtok(), open_and_uncompress_file(), path_combine(), perceive_self(), pick_up_object(), place_chest(), place_exits(), place_monsters(), place_treasure(), player_lvl_adj(), poison_type_apply(), postInitPlugin(), pray(), print_los(), print_shop_string(), process_map(), put_decor(), put_doors(), put_score(), query_base_name(), query_short_name(), query_weight(), rangetostring(), read_bmap_names(), read_client_images(), read_face_data(), read_map_log(), read_smooth(), receive_party_password(), remove_directory(), replace(), reply_cmd(), resurrect_player(), ring_desc(), save_map(), save_player(), send_news(), send_rules(), set_dragon_name(), set_title(), show_matching_spells(), show_skills(), spellpath_msg(), ss_dump_statistics(), ss_dump_table(), START_TEST(), store_spell_expiry(), store_time(), strerror_local(), strtoktolin(), summon_golem(), tailor_god_spell(), tempnam_local(), tiled_map_need_pic(), unclean_path(), verify_player(), write_attribute_file(), write_book_archive(), write_equipment_index(), write_map_log(), write_map_page(), write_map_parameters_to_string(), write_maps_by_level(), write_note(), write_npc_list(), write_parameters_to_string(), write_quests_page(), write_race_index(), write_region_index(), write_regions_link(), write_rune(), write_slaying_info(), write_todclock(), write_type_file(), write_type_index(), write_world_info(), and write_world_map().

int strcasecmp ( const char *  s1,
const char *  s2 
)

Case-insensitive comparaison of strings.

This seems to be lacking on some system.

Parameters
s1
s2strings to compare.
Returns
  • -1 if s1 is less than s2
  • 0 if s1 equals s2
  • 1 if s1 is greater than s2

Definition at line 434 of file porting.c.

References tolower.

Referenced by cast_create_missile(), command_kill_pets(), compare_map_info(), dump_monster_treasure(), find_ingred_cost(), find_treasure_by_name(), get_god_for_race(), get_region_from_string(), item_matched_string(), load_settings(), metaserver2_init(), name_cmp(), shop_sort(), sort_archetypes(), sort_equipment(), sort_mapname(), sort_race(), sort_slaying(), sort_struct_map_in_quest(), and sortbyname().

+ Here is the caller graph for this function:

const char* strcasestr_local ( const char *  s,
const char *  find 
)

Finds a substring in a string, in a case-insensitive manner.

Parameters
sstring we're searching into.
findstring we're searching for.
Returns
pointer to first occurrence of find in s, NULL if not found.

Definition at line 461 of file porting.c.

References strncasecmp(), and tolower.

Referenced by display_high_score(), write_mark(), and write_note().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

char* strdup_local ( const char *  str)

A replacement of strdup(), since it's not defined at some unix variants.

Parameters
strstring to duplicate.
Returns
copy, needs to be freed by caller. NULL on memory allocation error.

Definition at line 310 of file porting.c.

Referenced by add_shop_item(), build_stringlist(), cast_spell(), cfapi_system_strdup_local(), check_login(), draw_ext_info(), fire_bow(), form_party(), generate_random_map(), init_attackmess(), init_connection(), init_emergency_mappath(), load_dir(), load_map_header(), load_settings(), metaserver2_init(), parse_regions(), parse_shop_string(), push(), read_bmap_names(), read_client_images(), and read_map_log().

+ Here is the caller graph for this function:

char* strerror_local ( int  errnum,
char *  buf,
size_t  size 
)

This takes an err number and returns a string with a description of the error.

Parameters
errnumerror we want the description of.
bufbuffer to contain the description.
sizebuf's length.
Returns
buf.

Definition at line 525 of file porting.c.

References snprintf().

Referenced by add_score(), command_help(), compile_info(), dead_player(), display_high_score(), do_server(), i18n_init(), init_anim(), init_races(), init_server(), load_original_map(), load_temporary_map(), make_path_to_file(), read_bmap_names(), read_face_data(), read_smooth(), save_map(), SockList_ReadPacket(), write_socket_buffer(), and Write_To_Socket().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int strncasecmp ( const char *  s1,
const char *  s2,
int  n 
)

Case-insensitive comparaison of strings.

This seems to be lacking on some system.

Parameters
s1
s2strings to compare.
nmaximum number of chars to compare.
Returns
  • -1 if s1 is less than s2
  • 0 if s1 equals s2
  • 1 if s1 is greater than s2

Definition at line 402 of file porting.c.

References tolower.

Referenced by basic_emote(), examine(), find_player_partial_name(), find_skill_by_name(), init_experience(), item_matched_string(), learn_skill(), strcasestr_local(), and use_skill().

+ Here is the caller graph for this function:

long strtol ( register char *  str,
char **  ptr,
register int  base 
)

Converts a string to long.

A replacement of strtol() since it's not defined at many unix systems.

Parameters
strstring to convert.
ptrwill point to first invalid character in str.
basebase to consider to convert to long.
Todo:
check weird -+ handling (missing break?)

Definition at line 339 of file porting.c.

References DIGIT, and MBASE.

Referenced by check_login(), find_multi_free_spot_within_radius(), and generate_monster().

+ Here is the caller graph for this function:

char* tempnam_local ( const char *  dir,
const char *  pfx 
)

A replacement for the tempnam() function since it's not defined at some unix variants. Do not use this function for new code, use tempnam_secure() instead.

Parameters
dirdirectory where to create the file. Can be NULL, in which case NULL is returned.
pfxprefix to create unique name. Can be NULL.
Returns
path to temporary file, or NULL if failure. Must be freed by caller.

Definition at line 87 of file porting.c.

References curtmp, F_OK, getpid, MAXPATHLEN, and snprintf().

Referenced by save_map(), and tempnam_secure().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

FILE* tempnam_secure ( const char *  dir,
const char *  pfx,
char **  filename 
)

A replacement for the tempnam_local() function since that one is not very secure. This one will open the file in an atomic way on platforms where it is possible.

Parameters
dirDirectory where to create the file. Can be NULL, in which case NULL is returned.
pfxPrefix to create unique name. Can be NULL.
filenameThis should be a pointer to a char*, the function will overwrite the char* with the name of the resulting file. Must be freed by caller. Value is unchanged if the function returns NULL.
Returns
A pointer to a FILE opened exclusively, or NULL if failure. It is up to the caller to properly close it.
Note
The file will be opened read-write.
Todo:
Maybe adding some #ifdef for non-UNIX? I don't have any such system around to test with.

Definition at line 143 of file porting.c.

References llevError, LOG(), MAXTMPRETRY, S_IRUSR, S_IWUSR, and tempnam_local().

Referenced by save_player().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Variable Documentation

unsigned int curtmp = 0
static

Used to generate temporary unique name.

Definition at line 69 of file porting.c.

Referenced by tempnam_local().

const char* uncomp[NROF_COMPRESS_METHODS][3]
Initial value:
= {
{ NULL, NULL, NULL },
{ ".Z", UNCOMPRESS, COMPRESS },
{ ".gz", GUNZIP, GZIP },
{ ".bz2", BUNZIP, BZIP }
}
#define COMPRESS
Definition: win32.h:116
#define UNCOMPRESS
Definition: win32.h:117
#define BUNZIP
Definition: win32.h:121
#define GZIP
Definition: win32.h:118
#define BZIP
Definition: win32.h:120
#define GUNZIP
Definition: win32.h:119

This is a list of the suffix, uncompress and compress functions. Thus, if you have some other compress program you want to use, the only thing that needs to be done is to extended this. The first entry must be NULL - this is what is used for non compressed files.

Definition at line 593 of file porting.c.

Referenced by check_path(), and save_map().