1
0
mirror of https://git.zap.org.au/git/trader.git synced 2024-09-01 17:14:15 -04:00

Change program_name() to be a global variable

We're using global variables all over the place, in any case...
This commit is contained in:
John Zaitseff 2011-08-25 14:26:25 +10:00
parent 485a867b99
commit 4733e98e7f
3 changed files with 34 additions and 59 deletions

View File

@ -147,7 +147,7 @@ static void end_program (void);
int main (int argc, char *argv[])
{
// Strip off leading pathname components from program name
init_program_name(argv);
init_program_name(argv[0]);
// Initialise the locale
if (setlocale(LC_ALL, "") == NULL) {
@ -233,7 +233,7 @@ void process_cmdline (int argc, char *argv[])
if (option_max_turn < MIN_MAX_TURN || p == NULL || *p != '\0') {
fprintf(stderr, _("%s: invalid value for --max-turn: `%s'\n"),
program_name(), optarg);
program_name, optarg);
show_usage(EXIT_FAILURE);
}
}
@ -248,7 +248,7 @@ void process_cmdline (int argc, char *argv[])
if (optind < argc && argv[optind] != NULL) {
if (*argv[optind] == '-') {
fprintf(stderr, _("%s: invalid operand `%s'\n"), program_name(),
fprintf(stderr, _("%s: invalid operand `%s'\n"), program_name,
argv[optind]);
show_usage(EXIT_FAILURE);
}
@ -258,7 +258,7 @@ void process_cmdline (int argc, char *argv[])
game_num = *argv[optind] - '0';
} else {
fprintf(stderr, _("%s: invalid game number `%s'\n"),
program_name(), argv[optind]);
program_name, argv[optind]);
show_usage(EXIT_FAILURE);
}
@ -266,8 +266,8 @@ void process_cmdline (int argc, char *argv[])
}
if (optind < argc && argv[optind] != NULL) {
fprintf(stderr, _("%s: extra operand `%s'\n"), program_name(),
argv[optind]);
fprintf(stderr, _("%s: extra operand `%s'\n"),
program_name, argv[optind]);
show_usage(EXIT_FAILURE);
}
}
@ -293,7 +293,7 @@ This program is free software that is distributed under the terms of the\n\
GNU General Public License, version 3 or later. You are welcome to\n\
modify and/or distribute it under certain conditions. This program has\n\
NO WARRANTY, to the extent permitted by law; see the License for details.\n\
"), program_name(), PACKAGE_VERSION, "1990-2011");
"), program_name, PACKAGE_VERSION, "1990-2011");
exit(EXIT_SUCCESS);
}
@ -304,14 +304,11 @@ NO WARRANTY, to the extent permitted by law; see the License for details.\n\
void show_usage (int status)
{
const char *pn = program_name();
if (status != EXIT_SUCCESS) {
fprintf(stderr, _("%s: Try `%s --help' for more information.\n"),
pn, pn);
program_name, program_name);
} else {
printf(_("Usage: %s [OPTION ...] [GAME]\n"), pn);
printf(_("Usage: %s [OPTION ...] [GAME]\n"), program_name);
printf(_("\
Play Star Traders, a simple game of interstellar trading.\n\n\
"));

View File

@ -35,6 +35,9 @@
* Global variable definitions *
************************************************************************/
const char *program_name = NULL; // Canonical program name
// Global copy, suitably modified, of localeconv() information
struct lconv lconvinfo;
@ -64,7 +67,6 @@ wchar_t *mon_thousands_sep; // Local monetary thousands separator
* Module-specific 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
@ -79,40 +81,27 @@ static bool add_currency_symbol = false; // Do we need to add "$"?
/***********************************************************************/
// init_program_name: Make the program name "canonical"
// init_program_name: Make the program name canonical
void init_program_name (char *argv[])
void init_program_name (char *argv0)
{
/* This implementation assumes a POSIX environment with an ASCII-safe
character encoding (such as ASCII or UTF-8). */
if (argv == NULL || argv[0] == NULL || *argv[0] == '\0') {
program_name_str = PACKAGE;
if (argv0 == NULL || *argv0 == '\0') {
program_name = PACKAGE;
} else {
char *p = strrchr(argv[0], '/');
char *p = strrchr(argv0, '/');
if (p != NULL && *++p != '\0') {
argv[0] = p;
program_name = xstrdup(p);
} else {
program_name = xstrdup(argv0);
}
program_name_str = argv[0];
}
}
/***********************************************************************/
// program_name: Return the canonical program name
const char *program_name (void)
{
if (program_name_str == NULL) {
init_program_name(NULL);
}
return program_name_str;
}
/***********************************************************************/
// home_directory: Return home directory pathname
@ -142,15 +131,14 @@ const char *data_directory (void)
character encoding is ASCII-safe. */
if (data_directory_str == NULL) {
const char *name = program_name();
const char *home = home_directory();
if (name != NULL && home != NULL) {
char *p = xmalloc(strlen(home) + strlen(name) + 3);
if (program_name != NULL && home != NULL) {
char *p = xmalloc(strlen(home) + strlen(program_name) + 3);
strcpy(p, home);
strcat(p, "/.");
strcat(p, name);
strcat(p, program_name);
data_directory_str = p;
}
}
@ -208,7 +196,7 @@ void err_exit (const char *restrict format, ...)
end_screen();
fprintf(stderr, _("%s: "), program_name());
fprintf(stderr, _("%s: "), program_name);
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
@ -229,7 +217,7 @@ void errno_exit (const char *restrict format, ...)
end_screen();
fprintf(stderr, _("%s: "), program_name());
fprintf(stderr, _("%s: "), program_name);
if (format != NULL) {
va_start(args, format);
vfprintf(stderr, format, args);

View File

@ -50,6 +50,9 @@
* Global variable declarations *
************************************************************************/
extern const char *program_name; // Canonical program name
// Global copy, suitably modified, of localeconv() information
extern struct lconv lconvinfo;
@ -66,29 +69,16 @@ extern wchar_t *mon_thousands_sep; // Local monetary thousands separator
************************************************************************/
/*
Function: init_program_name - Make the program name "canonical"
Parameters: argv - Same as passed to main()
Function: init_program_name - Make the program name canonical
Parameters: argv0 - Same as passed to main()
Returns: (nothing)
This function modifies the argv[0] pointer to eliminate any leading
This function modifies the argv0 pointer to eliminate any leading
pathname (directory) components from the program name, leaving just the
basename of the program. It also saves a copy that can be accessed via
the program_name() function.
the program_name global variable.
*/
extern void init_program_name (char *argv[]);
/*
Function: program_name - Return the canonical program name
Parameters: (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 components).
NULL should never be returned; however, init_program_name() SHOULD be
called before using this function.
*/
extern const char *program_name (void);
extern void init_program_name (char *argv0);
/*
@ -111,7 +101,7 @@ extern const char *home_directory (void);
This function returns the full pathname to a potentially-writable
subdirectory within the user's home directory. Essentially, this
function returns home_directory() + "/." + program_name(). Note that
function returns home_directory() + "/." + program_name. 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.
*/