X-Git-Url: https://git.artsoft.org/?a=blobdiff_plain;f=src%2Fmisc.c;h=a7e52dcc4a71aaf2273ae986ec1ff2551ed46a50;hb=40a487dcc5d3028343ff9123a72b8b3839a42861;hp=9c6d93b18618d4b6e87b9927f5fe40d5bb2a3586;hpb=c4b26cf489dcc65a00bfcc05f7898700d2f0c9e4;p=rocksndiamonds.git diff --git a/src/misc.c b/src/misc.c index 9c6d93b1..a7e52dcc 100644 --- a/src/misc.c +++ b/src/misc.c @@ -155,39 +155,59 @@ unsigned int SimpleRND(unsigned int max) gettimeofday(¤t_time,NULL); root = root * 4253261 + current_time.tv_sec + current_time.tv_usec; - return(root % max); + return (root % max); } unsigned int RND(unsigned int max) { - return(random_linux_libc() % max); + return (random_linux_libc() % max); } unsigned int InitRND(long seed) { struct timeval current_time; - if (seed==NEW_RANDOMIZE) + if (seed == NEW_RANDOMIZE) { gettimeofday(¤t_time,NULL); srandom_linux_libc((unsigned int) current_time.tv_usec); - return((unsigned int) current_time.tv_usec); + return (unsigned int)current_time.tv_usec; } else { srandom_linux_libc((unsigned int) seed); - return((unsigned int) seed); + return (unsigned int)seed; } } -char *GetLoginName() +char *getLoginName() { struct passwd *pwd; - if (!(pwd=getpwuid(getuid()))) - return("ANONYMOUS"); + if (!(pwd = getpwuid(getuid()))) + return "ANONYMOUS"; else - return(pwd->pw_name); + return pwd->pw_name; +} + +char *getHomeDir() +{ + static char *home_dir = NULL; + + if (!home_dir) + { + if (!(home_dir = getenv("HOME"))) + { + struct passwd *pwd; + + if ((pwd = getpwuid(getuid()))) + home_dir = pwd->pw_dir; + else + home_dir = "."; + } + } + + return home_dir; } void MarkTileDirty(int x, int y) @@ -196,11 +216,10 @@ void MarkTileDirty(int x, int y) int yy = redraw_y1 + y; if (!redraw[xx][yy]) - { - redraw[xx][yy] = TRUE; redraw_tiles++; - redraw_mask |= REDRAW_TILES; - } + + redraw[xx][yy] = TRUE; + redraw_mask |= REDRAW_TILES; } void GetOptions(char *argv[]) @@ -211,6 +230,8 @@ void GetOptions(char *argv[]) options.display_name = NULL; options.server_host = NULL; options.server_port = 0; + options.base_directory = BASE_PATH; + options.level_directory = BASE_PATH "/" LEVELS_DIRECTORY; options.serveronly = FALSE; options.network = FALSE; options.verbose = FALSE; @@ -254,6 +275,7 @@ void GetOptions(char *argv[]) printf("Usage: %s [options] [server.name [port]]\n" "Options:\n" " -d, --display machine:0 X server display\n" + " -b, --basepath directory alternative base directory\n" " -l, --levels directory alternative level directory\n" " -s, --serveronly only start network server\n" " -n, --network network multiplayer game\n" @@ -272,16 +294,33 @@ void GetOptions(char *argv[]) printf("--display == '%s'\n", options.display_name); } + else if (strncmp(option, "-basepath", option_len) == 0) + { + if (option_arg == NULL) + Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str); + + options.base_directory = option_arg; + if (option_arg == next_option) + options_left++; + + printf("--basepath == '%s'\n", options.base_directory); + + /* adjust path for level directory accordingly */ + options.level_directory = checked_malloc(strlen(options.base_directory) + + strlen(LEVELS_DIRECTORY) + 2); + sprintf(options.level_directory, "%s/%s", + options.base_directory, LEVELS_DIRECTORY); + } else if (strncmp(option, "-levels", option_len) == 0) { if (option_arg == NULL) Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str); - level_directory = option_arg; + options.level_directory = option_arg; if (option_arg == next_option) options_left++; - printf("--levels == '%s'\n", level_directory); + printf("--levels == '%s'\n", options.level_directory); } else if (strncmp(option, "-network", option_len) == 0) { @@ -407,6 +446,18 @@ void *checked_malloc(unsigned long size) return ptr; } +void *checked_calloc(unsigned long size) +{ + void *ptr; + + ptr = calloc(1, size); + + if (ptr == NULL) + Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size); + + return ptr; +} + #define TRANSLATE_KEYSYM_TO_KEYNAME 0 #define TRANSLATE_KEYSYM_TO_X11KEYNAME 1 #define TRANSLATE_X11KEYNAME_TO_KEYSYM 2