diff options
author | Patrick McDermott <patrick.mcdermott@libiquity.com> | 2021-03-16 09:33:07 (EDT) |
---|---|---|
committer | Patrick McDermott <patrick.mcdermott@libiquity.com> | 2021-03-16 09:48:11 (EDT) |
commit | aeb3d25983f4912a00352c82e983da347488fedf (patch) | |
tree | 36b348f210c74bf134dafe9507bb17e9a9b3b14e | |
parent | c1c166ec5498a9f07b0ab6e443469c65849468b3 (diff) | |
download | dodge-balls-aeb3d25983f4912a00352c82e983da347488fedf.zip dodge-balls-aeb3d25983f4912a00352c82e983da347488fedf.tar.gz dodge-balls-aeb3d25983f4912a00352c82e983da347488fedf.tar.bz2 |
Find games either in build dir or installed
-rw-r--r-- | Makefile.am | 1 | ||||
-rw-r--r-- | configure.ac | 4 | ||||
-rw-r--r-- | src/game.c | 4 | ||||
-rw-r--r-- | src/game.h | 2 | ||||
-rw-r--r-- | src/main.c | 21 |
5 files changed, 23 insertions, 9 deletions
diff --git a/Makefile.am b/Makefile.am index 4b77625..3aed5f2 100644 --- a/Makefile.am +++ b/Makefile.am @@ -51,6 +51,7 @@ dodge_balls_CPPFLAGS = \ $(SDL_IMAGE_CFLAGS) \ $(ZLIB_CFLAGS) \ $(EXPAT_CFLAGS) \ + -DABS_BUILDDIR=\"$(abs_builddir)\" \ -DGAMESDIR=\"$(gamesdir)\" dodge_balls_LDADD = \ $(ASAN_CFLAGS) \ diff --git a/configure.ac b/configure.ac index 012a287..ecc933a 100644 --- a/configure.ac +++ b/configure.ac @@ -36,8 +36,8 @@ test -d "${srcdir}/.git" || CFLAGS="${save_CFLAGS}" funcs_missing=false AC_CHECK_FUNCS( [\ - alphasort calloc free malloc printf putchar scandir sscanf \ - strcmp strdup vprintf + alphasort calloc dirname free malloc printf putchar realpath \ + scandir sscanf strcmp strdup vprintf ], [], [funcs_missing=true]) @@ -30,13 +30,13 @@ _db_game_is_dir(const struct dirent *entry) } int -db_games_find(char ***games) +db_games_find(const char *games_dir, char ***games) { struct dirent **entries; int n; int i; - n = scandir("games", &entries, &_db_game_is_dir, alphasort); + n = scandir(games_dir, &entries, &_db_game_is_dir, alphasort); *games = calloc(n, sizeof(**games)); if (*games == NULL) { @@ -20,6 +20,6 @@ #ifndef DB_GAME_H_ #define DB_GAME_H_ -int db_games_find(char ***games); +int db_games_find(const char *games_dir, char ***games); #endif /* DB_GAME_H_ */ @@ -1,15 +1,28 @@ +#include <libgen.h> #include <stdio.h> #include <stdlib.h> +#include <string.h> #include "game.h" int main(int argc, char *argv[]) { - char **games; - int n; - int i; + char *program_dir; + const char *games_dir; + char **games; + int n; + int i; - n = db_games_find(&games); + program_dir = realpath(dirname(argv[0]), NULL); + if (strcmp(program_dir, ABS_BUILDDIR) == 0) { + /* Running in place */ + games_dir = ABS_BUILDDIR "/games"; + } else { + /* Running from installation */ + games_dir = GAMESDIR; + } + free(program_dir); + n = db_games_find(games_dir, &games); printf("%d games:\n", n); for (i = 0; i < n; ++i) { printf("\t%s\n", games[i]); |