From 6910370557ba258653bf161e757b81769dd77c98 Mon Sep 17 00:00:00 2001 From: John Zaitseff Date: Wed, 15 Jun 2011 22:34:28 +1000 Subject: [PATCH] Add various header files Add the system.h header file for all system header includes, and utils.c and utils.h for various low-level utility functions. --- src/Makefile.am | 10 +++- src/system.h | 53 +++++++++++++++++ src/trader.c | 8 ++- src/trader.h | 15 ++++- src/utils.c | 150 ++++++++++++++++++++++++++++++++++++++++++++++++ src/utils.h | 46 +++++++++++++++ 6 files changed, 278 insertions(+), 4 deletions(-) create mode 100644 src/system.h create mode 100644 src/utils.c create mode 100644 src/utils.h diff --git a/src/Makefile.am b/src/Makefile.am index 4c346f1..05b93c4 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -27,5 +27,11 @@ # along with this program. If not, see http://www.gnu.org/licenses/. -bin_PROGRAMS = trader -trader_SOURCES = trader.c trader.h globals.c globals.h +bin_PROGRAMS = trader + +trader_SOURCES = \ + trader.c trader.h \ + globals.c globals.h \ + utils.c utils.h \ + system.h +trader_LDADD = @CURSES_LIB@ diff --git a/src/system.h b/src/system.h new file mode 100644 index 0000000..1757c48 --- /dev/null +++ b/src/system.h @@ -0,0 +1,53 @@ +/************************************************************************ +* * +* Star Traders: A Game of Interstellar Trading * +* Copyright (C) 1990-2011, John Zaitseff * +* * +************************************************************************/ + +/* + Author: John Zaitseff + $Id$ + + This file, system.h, contains system #include directives for Star + Traders. + + + This program is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see http://www.gnu.org/licenses/. +*/ + + +#ifndef included_SYSTEM_H +#define included_SYSTEM_H 1 + + +/************************************************************************ +* Portability definitions * +************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + + +/************************************************************************ +* System header files * +************************************************************************/ + +#include +#include +#include + + +#endif /* included_SYSTEM_H */ diff --git a/src/trader.c b/src/trader.c index 27769a8..10f0fb0 100644 --- a/src/trader.c +++ b/src/trader.c @@ -32,7 +32,6 @@ #include "trader.h" -#include /************************************************************************ @@ -41,5 +40,12 @@ int main (int argc, char *argv[]) { + init_program_name(argv); + + // Testing... + printf("program_name: %s\n", program_name()); + printf("home_directory: %s\n", home_directory()); + printf("data_directory: %s\n", data_directory()); + return EXIT_SUCCESS; } diff --git a/src/trader.h b/src/trader.h index 71cbbf8..86d3b89 100644 --- a/src/trader.h +++ b/src/trader.h @@ -9,7 +9,7 @@ Author: John Zaitseff $Id$ - This file, trader.h, contains various definitions for Star Traders. + This file, trader.h, contains overall definitions for Star Traders. This program is free software: you can redistribute it and/or modify it @@ -31,7 +31,20 @@ #define included_TRADER_H 1 +/************************************************************************ +* Global definitions * +************************************************************************/ + +#define GAME_FILE_API_VERSION "7.0" /* For game loads and saves */ + + +/************************************************************************ +* Program-specific header files * +************************************************************************/ + +#include "system.h" #include "globals.h" +#include "utils.h" #endif /* included_TRADER_H */ diff --git a/src/utils.c b/src/utils.c new file mode 100644 index 0000000..84db493 --- /dev/null +++ b/src/utils.c @@ -0,0 +1,150 @@ +/************************************************************************ +* * +* Star Traders: A Game of Interstellar Trading * +* Copyright (C) 1990-2011, John Zaitseff * +* * +************************************************************************/ + +/* + Author: John Zaitseff + $Id$ + + This file, utils.c, contains the implementation of various utility + function for Star Traders. + + + This program is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see http://www.gnu.org/licenses/. +*/ + + +#include "system.h" +#include "utils.h" + + +/************************************************************************ +* Module variables * +************************************************************************/ + +static char *program_name_str = NULL; // Canonical program name +static char *home_directory_str = NULL; // Full pathname to home +static char *data_directory_str = NULL; // Writable data dir pathname + + +/************************************************************************ +* Utility function definitions * +************************************************************************/ + + +/*----------------------------------------------------------------------- + Function: init_program_name - Make the program name "canonical" + Arguments: argv - Same as passed to main() + Returns: (nothing) + + This function modifies the argv[0] pointer to eliminate the leading + path name from the program name. In other words, it strips any leading + directories and leaves just the base name of the program. It also + assigns the module variable "program_name_str". +*/ + +void init_program_name (char *argv[]) +{ + if ((argv == NULL) || (argv[0] == NULL) || (*(argv[0]) == '\0')) { + program_name_str = PACKAGE; + } else { + char *p = strrchr(argv[0], '/'); + + if ((p != NULL) && (*++p != '\0')) { + argv[0] = p; + } + + program_name_str = argv[0]; + } +} + + +/*----------------------------------------------------------------------- + Function: program_name - Return the canonical program name + Arguments: (none) + Returns: const char * - Pointer to program name + + This function returns the canonical program name (the program name as + invoked on the command line, without any leading pathname). NULL + should never be returned. +*/ + +const char *program_name (void) +{ + if (program_name_str == NULL) { + init_program_name(NULL); + } + + return program_name_str; +} + + +/*----------------------------------------------------------------------- + Function: home_directory - Return home directory pathname + Arguments: (none) + Returns: const char * - Pointer to home directory + + This function returns the full pathname to the user's home directory, + using the HOME environment variable. Note that the existance or + writability of this pathname is NOT checked by this function. NULL is + returned if the home directory cannot be determined. +*/ + +const char *home_directory (void) +{ + if (home_directory_str == NULL) { + // Use the HOME environment variable where possible + char *p = getenv("HOME"); + if ((p != NULL) && (*p != '\0')) { + home_directory_str = strdup(p); + } + } + + return home_directory_str; +} + + +/*----------------------------------------------------------------------- + Function: data_directory - Return writable data directory pathname + Arguments: (none) + Returns: const char * - Pointer to data directory + + This function returns the full pathname to a writable subdirectory + within the user's home directory. Essentially, home_directory() + "/." + + program_name() is returned. Note that this path is NOT created by + this function, nor is the writability of this path checked. NULL is + returned if this path cannot be determined. +*/ + +const char *data_directory (void) +{ + if (data_directory_str == NULL) { + const char *name = program_name(); + const char *home = home_directory(); + if ((name != NULL) && (home != NULL)) { + char *p = malloc(strlen(home) + strlen(name) + 3); + if (p != NULL) { + strcpy(p, home); + strcat(p, "/."); + strcat(p, name); + data_directory_str = p; + } + } + } + + return data_directory_str; +} diff --git a/src/utils.h b/src/utils.h new file mode 100644 index 0000000..489c99e --- /dev/null +++ b/src/utils.h @@ -0,0 +1,46 @@ +/************************************************************************ +* * +* Star Traders: A Game of Interstellar Trading * +* Copyright (C) 1990-2011, John Zaitseff * +* * +************************************************************************/ + +/* + Author: John Zaitseff + $Id$ + + This file, utils.h, contains various utility function declarations for + Star Traders. + + + This program is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see http://www.gnu.org/licenses/. +*/ + + +#ifndef included_UTILS_H +#define included_UTILS_H 1 + + +/************************************************************************ +* Utility function declarations * +************************************************************************/ + +extern void init_program_name (char *argv[]); +extern const char *program_name (void); + +extern const char *home_directory (void); +extern const char *data_directory (void); + + +#endif /* included_UTILS_H */