From 29da4b6cf4e4a32cdb0cade0582d989279f35f62 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Wed, 9 Mar 2005 18:44:50 +0000 Subject: [PATCH] (empfile, teldir, motdfil, downfil, disablefil, telfil, annfil) (banfil, authfil, timestampfil): Make file names relative to DATADIR. (main): emp_server and files change to the data directory. fairland has to write newcap_script to the initial current directory, so it keeps it and makes file names relative to DATADIR absolute. (fixup_files): Nothing to do, remove. This fixes the following bug: fixup_files() formed file names by appending ep->name instead of the base name of ep->file to datadir. Thus, fixup_files() changed file names even when datadir had the compiled-in value. When emp_config() couldn't open econfig, it failed to call fixup_files(), and different file names got used. Note that with this revision the file names revert to the names used before fixup_files() was introduced. Closes #1146080. (print_config): Printing file names as comments is not really useful, remove. (otherfiles, ofiles): Unused, remove. (emp_config, main): Previously, pconfig had to call emp_config() even when it didn't want to read econfig, just to run fixup_files(). This is no longer necessary. Simplify. --- include/file.h | 4 +-- src/lib/gen/emp_config.c | 55 +--------------------------------------- src/lib/global/file.c | 30 +++++++++++----------- src/lib/global/path.c | 19 +++++++------- src/server/main.c | 5 ++++ src/util/fairland.c | 12 +++++---- src/util/files.c | 10 +++++--- src/util/pconfig.c | 6 ++--- 8 files changed, 49 insertions(+), 92 deletions(-) diff --git a/include/file.h b/include/file.h index a74bb6e5..dec56c7a 100644 --- a/include/file.h +++ b/include/file.h @@ -37,8 +37,8 @@ #include struct empfile { - char *name; /* file name (e.g., "treaty") */ - char *file; /* file path */ + char *name; /* Empire name (e.g., "treaty") */ + char *file; /* file name (relative to data directory) */ int flags; /* misc stuff */ int mode; /* O_flags */ int size; /* size of object */ diff --git a/src/lib/gen/emp_config.c b/src/lib/gen/emp_config.c index da1be7b2..b7c8c2e1 100644 --- a/src/lib/gen/emp_config.c +++ b/src/lib/gen/emp_config.c @@ -88,14 +88,8 @@ emp_config(char *file) int errors = 0; int i; - if (!file) { - if (!*dflt_econfig) { - /* No default econfig, use compiled in configuration */ - fixup_files(); - return 0; - } + if (!file) file = dflt_econfig; - } if ((fp = fopen(file, "r")) == NULL) { fprintf(stderr, "Can't open %s for reading (%s)\n", file, strerror(errno)); @@ -163,51 +157,11 @@ emp_config(char *file) } } fclose(fp); - fixup_files(); WORLD_X &= ~1; /* make even */ return -errors; } -struct otherfiles { - char **files; - char *name; -}; - -/* list of other well known files... -maybe tailor these oneday - * anyway - meantime they are all relative to datadir */ -static struct otherfiles ofiles[] = { - {&motdfil, "motd"}, - {&downfil, "down"}, - {&disablefil, "disable"}, - {&banfil, "ban"}, - {&authfil, "auth"}, - {&annfil, "ann"}, - {×tampfil, "timestamp"}, - {&teldir, "tel"}, - {&telfil, "tel/tel"}, - {NULL, NULL} -}; - -/* fix up the empfile struct to reference full path names */ -static void -fixup_files(void) -{ - struct empfile *ep; - struct otherfiles *op; - s_char buf[1024]; - - for (ep = empfile; ep < &empfile[EF_MAX]; ep++) { - sprintf(buf, "%s/%s", datadir, ep->name); - ep->file = strdup(buf); - } - - for (op = ofiles; op->files; op++) { - sprintf(buf, "%s/%s", datadir, op->name); - *op->files = strdup(buf); - } -} - /* find the key in the table */ static struct keymatch * keylookup(register s_char *command, struct keymatch *tbl) @@ -226,9 +180,7 @@ keylookup(register s_char *command, struct keymatch *tbl) void print_config(FILE *fp) { - struct empfile *ep; struct option_list *op; - struct otherfiles *ofp; struct keymatch *kp; fprintf(fp, "# Empire Configuration File:\n"); @@ -267,11 +219,6 @@ print_config(FILE *fp) } fprintf(fp, "\n"); - for (ep = empfile; ep < &empfile[EF_MAX]; ep++) - fprintf(fp, "# File %s -> %s\n", ep->name, ep->file); - for (ofp = ofiles; ofp->files; ofp++) - fprintf(fp, "# File %s -> %s\n", ofp->name, *(ofp->files)); - } diff --git a/src/lib/global/file.c b/src/lib/global/file.c index 30a9a2df..051de27c 100644 --- a/src/lib/global/file.c +++ b/src/lib/global/file.c @@ -53,49 +53,49 @@ #include "commodity.h" struct empfile empfile[] = { - {"sect", EMPDIR "/data/sector", EFF_XY | EFF_OWNER, + {"sect", "sector", EFF_XY | EFF_OWNER, 0, sizeof(struct sctstr), 0, 0, 0, offsetof(struct sctstr, sct_item), -1, -1, 0, 0, 0, 0, 0}, - {"ship", EMPDIR "/data/ship", EFF_XY | EFF_OWNER | EFF_GROUP, + {"ship", "ship", EFF_XY | EFF_OWNER | EFF_GROUP, 0, sizeof(struct shpstr), 0, 0, 0, offsetof(struct shpstr, shp_item), -1, -1, 0, 0, 0, 0, 0}, - {"plane", EMPDIR "/data/plane", EFF_XY | EFF_OWNER | EFF_GROUP, + {"plane", "plane", EFF_XY | EFF_OWNER | EFF_GROUP, 0, sizeof(struct plnstr), 0, 0, 0, 0, -1, -1, 0, 0, 0, 0, 0}, - {"land", EMPDIR "/data/land", EFF_XY | EFF_OWNER | EFF_GROUP, + {"land", "land", EFF_XY | EFF_OWNER | EFF_GROUP, 0, sizeof(struct lndstr), 0, 0, 0, offsetof(struct lndstr, lnd_item), -1, -1, 0, 0, 0, 0, 0}, - {"nuke", EMPDIR "/data/nuke", EFF_XY | EFF_OWNER, + {"nuke", "nuke", EFF_XY | EFF_OWNER, 0, sizeof(struct nukstr), 0, 0, 0, 0, -1, -1, 0, 0, 0, 0, 0}, - {"news", EMPDIR "/data/news", 0, + {"news", "news", 0, 0, sizeof(struct nwsstr), 0, 0, 0, 0, -1, -1, 0, 0, 0, 0, 0}, - {"treaty", EMPDIR "/data/treaty", 0, + {"treaty", "treaty", 0, 0, sizeof(struct trtstr), 0, 0, 0, 0, -1, -1, 0, 0, 0, 0, 0}, - {"trade", EMPDIR "/data/trade", 0, + {"trade", "trade", 0, 0, sizeof(struct trdstr), 0, 0, 0, 0, -1, -1, 0, 0, 0, 0, 0}, - {"pow", EMPDIR "/data/power", 0, + {"pow", "power", 0, 0, sizeof(struct powstr), 0, 0, 0, 0, -1, -1, 0, 0, 0, 0, 0}, - {"nat", EMPDIR "/data/nation", 0, + {"nat", "nation", 0, 0, sizeof(struct natstr), 0, 0, 0, 0, -1, -1, 0, 0, 0, 0, 0}, - {"loan", EMPDIR "/data/loan", 0, + {"loan", "loan", 0, 0, sizeof(struct lonstr), 0, 0, 0, 0, -1, -1, 0, 0, 0, 0, 0}, - {"map", EMPDIR "/data/map", 0, + {"map", "map", 0, 0, DEF_WORLD_X * DEF_WORLD_Y / 2, 0, 0, 0, 0, -1, -1, 0, 0, 0, 0, 0}, - {"bmap", EMPDIR "/data/bmap", 0, + {"bmap", "bmap", 0, 0, DEF_WORLD_X * DEF_WORLD_Y / 2, 0, 0, 0, 0, -1, -1, 0, 0, 0, 0, 0}, - {"commodity", EMPDIR "/data/commodity", 0, + {"commodity", "commodity", 0, 0, sizeof(struct comstr), 0, 0, 0, 0, -1, -1, 0, 0, 0, 0, 0}, - {"lost", EMPDIR "/data/lostitems", 0, 0, + {"lost", "lostitems", 0, 0, sizeof(struct loststr), 0, 0, 0, 0, -1, -1, 0, 0, 0, 0, 0} }; diff --git a/src/lib/global/path.c b/src/lib/global/path.c index a52d21d3..ed0e52b6 100644 --- a/src/lib/global/path.c +++ b/src/lib/global/path.c @@ -38,15 +38,16 @@ char dflt_econfig[] = EMPDIR "/data/econfig"; char *infodir = EMPDIR "/info.nr"; char *datadir = EMPDIR "/data"; -char *teldir = EMPDIR "/data/tel"; -char *motdfil = EMPDIR "/data/motd"; -char *downfil = EMPDIR "/data/down"; -char *disablefil = EMPDIR "/data/disable"; -char *telfil = EMPDIR "/data/tel/tel"; -char *annfil = EMPDIR "/data/ann"; -char *banfil = EMPDIR "/data/ban"; -char *authfil = EMPDIR "/data/auth"; -char *timestampfil = EMPDIR "/data/timestamp"; +/* relative to DATADIR */ +char *teldir = "tel"; +char *motdfil = "motd"; +char *downfil = "down"; +char *disablefil = "disable"; +char *telfil = "tel/tel"; +char *annfil = "ann"; +char *banfil = "ban"; +char *authfil = "auth"; +char *timestampfil = "timestamp"; char *loginport = EMP_PORT; diff --git a/src/server/main.c b/src/server/main.c index 837707c7..69db0c58 100644 --- a/src/server/main.c +++ b/src/server/main.c @@ -37,6 +37,7 @@ #if !defined(_WIN32) #include #endif +#include #include #include #include @@ -187,6 +188,10 @@ main(int argc, char **argv) if (emp_config(config_file) < 0) exit(EXIT_FAILURE); + if (chdir(datadir)) { + fprintf(stderr, "Can't chdir to %s (%s)\n", datadir, strerror(errno)); + exit(EXIT_FAILURE); + } #if defined(_WIN32) if (install_service_set) diff --git a/src/util/fairland.c b/src/util/fairland.c index e03da7fb..94e3d191 100644 --- a/src/util/fairland.c +++ b/src/util/fairland.c @@ -64,13 +64,11 @@ static int quiet = 0; /* lower URAN_MIN for more uranium */ #define URAN_MIN 56 -#if defined(aix) || defined(linux) || defined(solaris) -#include -#endif /* aix or linux */ #if defined(_WIN32) #include "../lib/gen/getopt.h" #endif +#include #include #include #include @@ -402,12 +400,16 @@ static int allocate_memory(void) { int i; + char *fname; - sect_fptr = fopen(empfile[EF_SECTOR].file, "wb"); + fname = malloc(strlen(datadir) + 1 + strlen(empfile[EF_SECTOR].file) + 1); + sprintf(fname, "%s/%s", datadir, empfile[EF_SECTOR].file); + sect_fptr = fopen(fname, "wb"); if (sect_fptr == NULL) { - perror(empfile[EF_SECTOR].file); + perror(fname); return -1; } + free(fname); sectsbuf = (struct sctstr *)calloc((YSIZE * XSIZE), sizeof(struct sctstr)); sects = (struct sctstr **)calloc(YSIZE, sizeof(struct sctstr *)); diff --git a/src/util/files.c b/src/util/files.c index 82e42d8b..ceeac41d 100644 --- a/src/util/files.c +++ b/src/util/files.c @@ -33,10 +33,6 @@ * Steve McClure, 1998 */ -#if defined(aix) || defined(linux) || defined(solaris) -#include -#endif /* aix or linux */ - #include #include #if !defined(_WIN32) @@ -46,6 +42,7 @@ #include #include "../lib/gen/getopt.h" #endif +#include #include #include @@ -112,6 +109,11 @@ main(int argc, char *argv[]) printf("Can't make game directory\n"); exit(1); } + if (chdir(datadir)) { + fprintf(stderr, "Can't chdir to %s (%s)\n", datadir, strerror(errno)); + exit(EXIT_FAILURE); + } + if (!force) { printf("WARNING: this blasts the existing game in %s (if any)\n", datadir); diff --git a/src/util/pconfig.c b/src/util/pconfig.c index e9fcb0e2..d4db5fdd 100644 --- a/src/util/pconfig.c +++ b/src/util/pconfig.c @@ -38,9 +38,9 @@ int main(int argc, char **argv) { - *dflt_econfig = 0; /* don't read default econfig */ - if (emp_config(argc > 1 ? argv[1] : NULL) < 0) - exit(1); + if (argc > 1) + if (emp_config(argv[1]) < 0) + exit(1); print_config(stdout); exit(0); }