On Thu, Jan 05, 2006 at 03:06:59PM -0800, Paul Eggert wrote: > [EMAIL PROTECTED] (James Youngman) writes: > > > I would prefer an arrangement which results in a compilation or link > > failure if the user (i.e. software maintainer) fails to initialise > > things properly. > > Perhaps we could change progname.h so that 'program_name' is a > function that returns the program name, instead of being a global > variable. That should catch mishaps at link-time, if not before.
The enclosed patch makes this change. 2006-01-06 James Youngman <[EMAIL PROTECTED]> * lib/progname.h, lib/error.h (program_name): Change program_name from a global variable to a global function. * lib/error.c (error): Use it. * lib/progname.c (program_name): define program_name(). * lib/argmatch.c, lib/c-stack.c, lib/chdir-long.c, lib/dirname.c, lib/euidaccess.c, lib/group-member.c, lib/xstrtol.c (main): use program_name() as a global function not a global variable. * doc/standards.texi: Modify examples to show the new usage pattern for program_name. Index: doc/standards.texi =================================================================== RCS file: /sources/gnulib/gnulib/doc/standards.texi,v retrieving revision 1.10 diff -u -r1.10 standards.texi --- doc/standards.texi 25 Dec 2005 23:59:50 -0000 1.10 +++ doc/standards.texi 6 Jan 2006 11:35:14 -0000 @@ -3,7 +3,7 @@ @setfilename standards.info @settitle GNU Coding Standards @c This date is automagically updated when you save this file: [EMAIL PROTECTED] lastupdate December 25, 2005 [EMAIL PROTECTED] lastupdate January 6, 2006 @c %**end of header @dircategory GNU organization @@ -2726,7 +2726,10 @@ #include <errno.h> #include <stdio.h> -char *program_name = "myprogram"; +const char *program_name(void) [EMAIL PROTECTED] + return "myprogram"; [EMAIL PROTECTED] FILE * xfopen (char const *name) Index: lib/argmatch.c =================================================================== RCS file: /sources/gnulib/gnulib/lib/argmatch.c,v retrieving revision 1.39 diff -u -r1.39 argmatch.c --- lib/argmatch.c 19 Sep 2005 17:28:14 -0000 1.39 +++ lib/argmatch.c 6 Jan 2006 11:35:14 -0000 @@ -213,7 +213,12 @@ /* * Based on "getversion.c" by David MacKenzie <[EMAIL PROTECTED]> */ -char *program_name; +static const char *myname; + +const char *program_name(void) +{ + return myname; +} /* When to make backup files. */ enum backup_type @@ -257,11 +262,11 @@ const char *cp; enum backup_type backup_type = no_backups; - program_name = (char *) argv[0]; + myname = (char *) argv[0]; if (argc > 2) { - fprintf (stderr, "Usage: %s [VERSION_CONTROL]\n", program_name); + fprintf (stderr, "Usage: %s [VERSION_CONTROL]\n", program_name()); exit (1); } @@ -270,7 +275,7 @@ backup_args, backup_vals); if (argc == 2) - backup_type = XARGMATCH (program_name, argv[1], + backup_type = XARGMATCH (program_name(), argv[1], backup_args, backup_vals); printf ("The version control is `%s'\n", Index: lib/c-stack.c =================================================================== RCS file: /sources/gnulib/gnulib/lib/c-stack.c,v retrieving revision 1.7 diff -u -r1.7 c-stack.c --- lib/c-stack.c 19 Sep 2005 17:28:14 -0000 1.7 +++ lib/c-stack.c 6 Jan 2006 11:35:14 -0000 @@ -1,6 +1,6 @@ /* Stack overflow handling. - Copyright (C) 2002, 2004 Free Software Foundation, Inc. + Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc. 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 @@ -98,8 +98,6 @@ # define SIGACTION_WORKS 0 #endif -extern char *program_name; - /* The user-specified action to take when a SEGV-related program error or stack overflow occurs. */ static void (* volatile segv_action) (int); @@ -121,7 +119,7 @@ char const *message; segv_action (signo); message = signo ? program_error_message : stack_overflow_message; - write (STDERR_FILENO, program_name, strlen (program_name)); + write (STDERR_FILENO, program_name(), strlen (program_name())); write (STDERR_FILENO, ": ", 2); write (STDERR_FILENO, message, strlen (message)); write (STDERR_FILENO, "\n", 1); @@ -288,12 +286,18 @@ return *p + recurse (array); } -char *program_name; +char *myname; + +const char *progam_name(void) +{ + return myname; +} + int main (int argc __attribute__ ((unused)), char **argv) { - program_name = argv[0]; + myname = argv[0]; fprintf (stderr, "The last output line should contain \"stack overflow\".\n"); if (c_stack_action (0) == 0) Index: lib/chdir-long.c =================================================================== RCS file: /sources/gnulib/gnulib/lib/chdir-long.c,v retrieving revision 1.6 diff -u -r1.6 chdir-long.c --- lib/chdir-long.c 19 Sep 2005 17:28:14 -0000 1.6 +++ lib/chdir-long.c 6 Jan 2006 11:35:14 -0000 @@ -221,7 +221,13 @@ # include "closeout.h" # include "error.h" -char *program_name; +char *myname; + +const char *program_name(void) +{ + return myname; +} + int main (int argc, char *argv[]) @@ -230,7 +236,7 @@ size_t n = 0; int len; - program_name = argv[0]; + myname = argv[0]; atexit (close_stdout); len = getline (&line, &n, stdin); Index: lib/dirname.c =================================================================== RCS file: /sources/gnulib/gnulib/lib/dirname.c,v retrieving revision 1.35 diff -u -r1.35 dirname.c --- lib/dirname.c 19 Sep 2005 17:28:14 -0000 1.35 +++ lib/dirname.c 6 Jan 2006 11:35:14 -0000 @@ -96,14 +96,20 @@ # define MAX_BUFF_LEN 1024 # include <stdio.h> -char *program_name; +char *myname; + +const char *program_name(void) +{ + return myname; +} + int main (int argc, char *argv[]) { char buff[MAX_BUFF_LEN + 1]; - program_name = argv[0]; + myname = argv[0]; buff[MAX_BUFF_LEN] = 0; while (fgets (buff, MAX_BUFF_LEN, stdin) && buff[0]) Index: lib/error.c =================================================================== RCS file: /sources/gnulib/gnulib/lib/error.c,v retrieving revision 1.43 diff -u -r1.43 error.c --- lib/error.c 14 May 2005 06:03:58 -0000 1.43 +++ lib/error.c 6 Jan 2006 11:35:14 -0000 @@ -1,5 +1,5 @@ /* Error handler for noninteractive utilities - Copyright (C) 1990-1998, 2000-2003, 2004 Free Software Foundation, Inc. + Copyright (C) 1990-1998, 2000-2004, 2005 Free Software Foundation, Inc. This file is part of the GNU C Library. This program is free software; you can redistribute it and/or modify @@ -57,7 +57,6 @@ #ifdef _LIBC /* In the GNU C library, there is a predefined variable for this. */ -# define program_name program_invocation_name # include <errno.h> # include <libio/libioP.h> @@ -92,10 +91,6 @@ # define SIZE_MAX ((size_t) -1) # endif -/* The calling program should define program_name and set it to the - name of the executing program. */ -extern char *program_name; - # if HAVE_STRERROR_R || defined strerror_r # define __strerror_r strerror_r # endif @@ -208,10 +203,10 @@ { #if _LIBC if (_IO_fwide (stderr, 0) > 0) - __fwprintf (stderr, L"%s: ", program_name); + __fwprintf (stderr, L"%s: ", program_name()); else #endif - fprintf (stderr, "%s: ", program_name); + fprintf (stderr, "%s: ", program_name()); } va_start (args, message); @@ -268,10 +263,10 @@ { #if _LIBC if (_IO_fwide (stderr, 0) > 0) - __fwprintf (stderr, L"%s: ", program_name); + __fwprintf (stderr, L"%s: ", program_name()); else #endif - fprintf (stderr, "%s:", program_name); + fprintf (stderr, "%s:", program_name()); } if (file_name != NULL) Index: lib/error.h =================================================================== RCS file: /sources/gnulib/gnulib/lib/error.h,v retrieving revision 1.20 diff -u -r1.20 error.h --- lib/error.h 14 May 2005 06:03:58 -0000 1.20 +++ lib/error.h 6 Jan 2006 11:35:14 -0000 @@ -1,5 +1,5 @@ /* Declaration for error-reporting function - Copyright (C) 1995, 1996, 1997, 2003 Free Software Foundation, Inc. + Copyright (C) 1995, 1996, 1997, 2003, 2005 Free Software Foundation, Inc. This file is part of the GNU C Library. This program is free software; you can redistribute it and/or modify @@ -52,6 +52,12 @@ function without parameters instead. */ extern void (*error_print_progname) (void); +/* program_name() should be defined by the application and should + * return the program's name. This is normally the same string as was + * passed to main() as argv[0]. + */ +extern const char *program_name(void); + /* This variable is incremented each time `error' is called. */ extern unsigned int error_message_count; Index: lib/euidaccess.c =================================================================== RCS file: /sources/gnulib/gnulib/lib/euidaccess.c,v retrieving revision 1.21 diff -u -r1.21 euidaccess.c --- lib/euidaccess.c 23 Sep 2005 04:15:13 -0000 1.21 +++ lib/euidaccess.c 6 Jan 2006 11:35:14 -0000 @@ -201,8 +201,13 @@ # include <stdio.h> # include <stdlib.h> -char *program_name; - +char *myname; + +const char *program_name() +{ + return myname; +} + int main (int argc, char **argv) { @@ -210,7 +215,7 @@ int mode; int err; - program_name = argv[0]; + myname = argv[0]; if (argc < 3) abort (); file = argv[1]; Index: lib/group-member.c =================================================================== RCS file: /sources/gnulib/gnulib/lib/group-member.c,v retrieving revision 1.17 diff -u -r1.17 group-member.c --- lib/group-member.c 23 Sep 2005 04:15:13 -0000 1.17 +++ lib/group-member.c 6 Jan 2006 11:35:14 -0000 @@ -111,14 +111,19 @@ #ifdef TEST -char *program_name; - +char *myname; + +const char *program_name(void) +{ + return myname; +} + int main (int argc, char **argv) { int i; - program_name = argv[0]; + myname = argv[0]; for (i=1; i<argc; i++) { Index: lib/progname.c =================================================================== RCS file: /sources/gnulib/gnulib/lib/progname.c,v retrieving revision 1.5 diff -u -r1.5 progname.c --- lib/progname.c 19 Sep 2005 17:28:14 -0000 1.5 +++ lib/progname.c 6 Jan 2006 11:35:14 -0000 @@ -31,7 +31,13 @@ /* String containing name the program is called with. To be initialized by main(). */ -const char *program_name = NULL; +static const char *the_program_name = NULL; + + +const char *program_name(void) +{ + return the_program_name; +} /* Set program_name, based on argv[0]. */ void @@ -49,5 +55,5 @@ argv0 = base; if (strncmp (base, "lt-", 3) == 0) argv0 = base + 3; - program_name = argv0; + the_program_name = argv0; } Index: lib/progname.h =================================================================== RCS file: /sources/gnulib/gnulib/lib/progname.h,v retrieving revision 1.5 diff -u -r1.5 progname.h --- lib/progname.h 14 May 2005 06:03:58 -0000 1.5 +++ lib/progname.h 6 Jan 2006 11:35:14 -0000 @@ -1,5 +1,5 @@ /* Program name management. - Copyright (C) 2001-2004 Free Software Foundation, Inc. + Copyright (C) 2001-2004, 2005 Free Software Foundation, Inc. Written by Bruno Haible <[EMAIL PROTECTED]>, 2001. This program is free software; you can redistribute it and/or modify @@ -29,8 +29,8 @@ #endif -/* String containing name the program is called with. */ -extern const char *program_name; +/* Returns a string containing the name the program was called with. */ +extern const char *program_name(void); /* Set program_name, based on argv[0]. */ extern void set_program_name (const char *argv0); Index: lib/xstrtol.c =================================================================== RCS file: /sources/gnulib/gnulib/lib/xstrtol.c,v retrieving revision 1.38 diff -u -r1.38 xstrtol.c --- lib/xstrtol.c 19 Sep 2005 17:28:15 -0000 1.38 +++ lib/xstrtol.c 6 Jan 2006 11:35:14 -0000 @@ -256,15 +256,20 @@ # include <stdio.h> # include "error.h" -char *program_name; - +char *myname; + +static const char *program_name(void) +{ + return myname; +} + int main (int argc, char **argv) { strtol_error s_err; int i; - program_name = argv[0]; + myname = argv[0]; for (i=1; i<argc; i++) { char *p; _______________________________________________ bug-gnulib mailing list bug-gnulib@gnu.org http://lists.gnu.org/mailman/listinfo/bug-gnulib