Make conftab.c independent of the current directory

read_builtin_tables() wanted to run in builtindir, and
read_custom_tables() wanted to run in configdir.  Bothersome.  Use new
fopenat() to relax those requirements.

The chdir() satisfying them are now superflous, remove them.
This commit is contained in:
Markus Armbruster 2008-02-03 21:43:49 +01:00
parent 4bb23dd1a6
commit b76e5a5eed
4 changed files with 30 additions and 17 deletions

View file

@ -323,6 +323,7 @@ extern int xundump(FILE *, char *, int *, int);
*/ */
/* fnameat.c */ /* fnameat.c */
extern char *fnameat(const char *, const char *); extern char *fnameat(const char *, const char *);
extern FILE *fopenat(const char *, const char *, const char *);
/* fsize.c */ /* fsize.c */
extern int fsize(int); extern int fsize(int);
extern int blksize(int); extern int blksize(int);

View file

@ -42,6 +42,10 @@
static int read_custom_table_file(char *); static int read_custom_table_file(char *);
/*
* Read builtin configuration tables.
* Return 0 on success, -1 on failure.
*/
int int
read_builtin_tables(void) read_builtin_tables(void)
{ {
@ -56,7 +60,7 @@ read_builtin_tables(void)
*/ */
for (ep = empfile; ep->name; ep++) { for (ep = empfile; ep->name; ep++) {
if (!EF_IS_GAME_STATE(ep->uid) && ep->file) { if (!EF_IS_GAME_STATE(ep->uid) && ep->file) {
if ((fp = fopen(ep->file, "r")) == NULL) { if ((fp = fopenat(ep->file, "r", builtindir)) == NULL) {
fprintf(stderr, "Can't open %s for reading (%s)\n", fprintf(stderr, "Can't open %s for reading (%s)\n",
ep->file, strerror(errno)); ep->file, strerror(errno));
return -1; return -1;
@ -107,7 +111,7 @@ read_custom_table_file(char *fname)
int lineno, res, n; int lineno, res, n;
FILE *fp; FILE *fp;
if (!(fp = fopen(fname, "r"))) { if (!(fp = fopenat(fname, "r", configdir))) {
fprintf(stderr, "Can't open config table %s for reading (%s)\n", fprintf(stderr, "Can't open config table %s for reading (%s)\n",
fname, strerror(errno)); fname, strerror(errno));
return -1; return -1;

View file

@ -71,3 +71,26 @@ fname_is_abs(const char *fname)
return fname[0] == '/'; return fname[0] == '/';
#endif #endif
} }
/*
* Open a stream like fopen(), optionally relative to a directory.
* This is exactly like fopen(), except FNAME is interpreted relative
* to DIR if that is neither null nor empty.
*/
FILE *
fopenat(const char *fname, const char *mode, const char *dir)
{
char *fnat;
FILE *fp;
fnat = fnameat(fname, dir);
if (!fnat)
return NULL;
fp = fopen(fnat, mode);
if (fnat != fname)
free(fnat);
return fp;
}

View file

@ -214,23 +214,8 @@ main(int argc, char **argv)
if (emp_config(config_file) < 0) if (emp_config(config_file) < 0)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
ef_init(); ef_init();
if (chdir(configdir)) {
fprintf(stderr, "Can't chdir to %s (%s)\n",
configdir, strerror(errno));
exit(EXIT_FAILURE);
}
if (chdir(builtindir)) {
fprintf(stderr, "Can't chdir to %s (%s)\n",
builtindir, strerror(errno));
exit(EXIT_FAILURE);
}
if (read_builtin_tables() < 0) if (read_builtin_tables() < 0)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
if (chdir(configdir)) {
fprintf(stderr, "Can't chdir to %s (%s)\n",
configdir, strerror(errno));
exit(EXIT_FAILURE);
}
if (read_custom_tables() < 0) if (read_custom_tables() < 0)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
if (chdir(gamedir)) { if (chdir(gamedir)) {