Do not use __progname directly (except in [gs]etprogname(3)).

Also, make an internal _getprogname() that is used only inside
libc. For libc, getprogname(3) is a weak symbol in case a
function of the same name is defined in userland.
This commit is contained in:
Mark Murray 2002-03-29 22:43:43 +00:00
parent 37c973e3dd
commit 4cd0119367
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=93399
32 changed files with 116 additions and 82 deletions

View File

@ -6,7 +6,8 @@ SRCS= crt1.c crti.S crtn.S
OBJS= ${SRCS:N*.h:R:S/$/.o/g}
OBJS+= gcrt1.o
CFLAGS+= -Wall -Wno-unused \
-I${.CURDIR}/../common
-I${.CURDIR}/../common \
-I${.CURDIR}/../../libc/include
NOMAN= true
NOPIC= true
NOPROFILE= true

View File

@ -40,6 +40,7 @@
#endif
#include <stdlib.h>
#include "libc_private.h"
#include "crtbrand.c"
struct Struct_Obj_Entry;
@ -60,7 +61,7 @@ extern int etext;
#endif
char **environ;
char *__progname = "";
const char *__progname = "";
/* The entry function. */
void
@ -72,13 +73,13 @@ _start(char **ap,
int argc;
char **argv;
char **env;
const char *s;
argc = * (long *) ap;
argv = ap + 1;
env = ap + 2 + argc;
environ = env;
if(argc > 0 && argv[0] != NULL) {
char *s;
__progname = argv[0];
for (s = __progname; *s != '\0'; s++)
if (*s == '/')

View File

@ -6,7 +6,8 @@ SRCS= crt1.c crti.S crtn.S
OBJS= ${SRCS:N*.h:R:S/$/.o/g}
OBJS+= gcrt1.o
CFLAGS+= -elf -Wall -fkeep-inline-functions \
-I${.CURDIR}/../common
-I${.CURDIR}/../common \
-I${.CURDIR}/../../libc/include
LDFLAGS+= -elf
NOMAN= true
NOPIC= true

View File

@ -29,6 +29,7 @@
#include <stddef.h>
#include <stdlib.h>
#include "libc_private.h"
#include "crtbrand.c"
typedef void (*fptr)(void);
@ -57,7 +58,7 @@ extern int _DYNAMIC;
#endif
char **environ;
char *__progname = "";
const char *__progname = "";
void
_start(char *arguments, ...)
@ -66,6 +67,7 @@ _start(char *arguments, ...)
int argc;
char **argv;
char **env;
const char *s;
rtld_cleanup = get_rtld_cleanup();
argv = &arguments;
@ -73,7 +75,6 @@ _start(char *arguments, ...)
env = argv + argc + 1;
environ = env;
if (argc > 0 && argv[0] != NULL) {
char *s;
__progname = argv[0];
for (s = __progname; *s != '\0'; s++)
if (*s == '/')

View File

@ -6,7 +6,8 @@ SRCS= crt1.c crti.S crtn.S
OBJS= ${SRCS:N*.h:R:S/$/.o/g}
OBJS+= gcrt1.o
CFLAGS+= -elf -Wall -fkeep-inline-functions \
-I${.CURDIR}/../common
-I${.CURDIR}/../common \
-I${.CURDIR}/../../libc/include
LDFLAGS+= -elf
NOMAN= true
NOPIC= true

View File

@ -29,6 +29,7 @@
#include <stddef.h>
#include <stdlib.h>
#include "libc_private.h"
#include "crtbrand.c"
typedef void (*fptr)(void);
@ -57,7 +58,7 @@ extern int _DYNAMIC;
#endif
char **environ;
char *__progname = "";
const char *__progname = "";
void
_start(char *arguments, ...)
@ -66,6 +67,7 @@ _start(char *arguments, ...)
int argc;
char **argv;
char **env;
const char *s;
rtld_cleanup = get_rtld_cleanup();
argv = &arguments;
@ -73,7 +75,6 @@ _start(char *arguments, ...)
env = argv + argc + 1;
environ = env;
if (argc > 0 && argv[0] != NULL) {
char *s;
__progname = argv[0];
for (s = __progname; *s != '\0'; s++)
if (*s == '/')

View File

@ -1,7 +1,8 @@
# from: @(#)Makefile 5.6 (Berkeley) 5/22/91
# $FreeBSD$
CFLAGS+= -DLIBC_SCCS -fno-omit-frame-pointer
CFLAGS+= -DLIBC_SCCS -fno-omit-frame-pointer \
-I${.CURDIR}/../../libc/include
OBJS= crt0.o c++rt0.o gcrt0.o scrt0.o sgcrt0.o
CLEANFILES= a.out crt0.o.tmp c++rt0.o.tmp gcrt0.o.tmp scrt0.o.tmp \
sgcrt0.o.tmp

View File

@ -37,10 +37,13 @@
#ifdef DYNAMIC
#include <sys/types.h>
#include <sys/syscall.h>
#include <a.out.h>
#include <string.h>
#include <sys/mman.h>
#include <a.out.h>
#include <link.h>
#include <string.h>
#include "libc_private.h"
/* !!!
* This is gross, ld.so is a ZMAGIC a.out, but has `sizeof(hdr)' for
@ -88,8 +91,7 @@ static void __do_dynamic_link(char **argv);
int _callmain();
int errno;
static char empty[1];
char *__progname = empty;
const char *__progname = "";
char **environ;
/* Globals used by dlopen() and related functions in libc */
@ -144,6 +146,7 @@ start()
register struct kframe *kfp;
register char **targv;
register char **argv;
register const char *s;
extern void _mcleanup();
#ifdef DYNAMIC
volatile caddr_t x;
@ -162,7 +165,6 @@ start()
environ = targv;
if (argv[0]) {
register char *s;
__progname = argv[0];
for (s=__progname; *s != '\0'; s++)
if (*s == '/')

View File

@ -6,7 +6,8 @@ SRCS= crt1.c crti.S crtn.S
OBJS= ${SRCS:N*.h:R:S/$/.o/g}
OBJS+= gcrt1.o
CFLAGS+= -Wall -Wno-unused \
-I${.CURDIR}/../common
-I${.CURDIR}/../common \
-I${.CURDIR}/../../libc/include
NOMAN= true
NOPIC= true
NOPROFILE= true

View File

@ -36,6 +36,7 @@
#endif
#include <stdlib.h>
#include "libc_private.h"
#include "crtbrand.c"
struct Struct_Obj_Entry;
@ -56,7 +57,7 @@ extern int etext;
#endif
char **environ;
char *__progname = "";
const char *__progname = "";
/* The entry function. */
void
@ -67,6 +68,7 @@ _start(char **ap,
int argc;
char **argv;
char **env;
const char *s;
/* Calculate gp */
__asm __volatile(" \
@ -82,7 +84,6 @@ _start(char **ap,
env = ap + 2 + argc;
environ = env;
if (argc > 0 && argv[0] != NULL) {
char *s;
__progname = argv[0];
for (s = __progname; *s != '\0'; s++)
if (*s == '/')

View File

@ -6,7 +6,8 @@ SRCS= crt1.c crti.S crtn.S
OBJS= ${SRCS:N*.h:R:S/$/.o/g}
OBJS+= gcrt1.o
CFLAGS+= -Wall -Wno-unused \
-I${.CURDIR}/../common
-I${.CURDIR}/../common \
-I${.CURDIR}/../../libc/include
NOMAN= true
NOPIC= true
NOPROFILE= true

View File

@ -43,6 +43,7 @@
#endif
#include <stdlib.h>
#include "libc_private.h"
#include "crtbrand.c"
struct Struct_Obj_Entry;
@ -63,7 +64,7 @@ extern int etext;
#endif
char **environ;
char *__progname = "";
const char *__progname = "";
struct ps_strings *__ps_strings;
/* The entry function.
@ -80,11 +81,11 @@ _start(argc, argv, envp, obj, cleanup, ps_strings)
struct ps_strings *ps_strings; /* BSD extension */
{
char *namep;
const char *s;
environ = envp;
if (argc > 0 && argv[0] != NULL) {
char *s;
__progname = argv[0];
for (s = __progname; *s != '\0'; s++)
if (*s == '/')

View File

@ -5,7 +5,8 @@
SRCS= crt1.c crti.S crtn.S
OBJS= ${SRCS:N*.h:R:S/$/.o/g}
OBJS+= gcrt1.o
CFLAGS+= -I${.CURDIR}/../common
CFLAGS+= -I${.CURDIR}/../common \
-I${.CURDIR}/../../libc/include
NOMAN= true
NOPIC= true
NOPROFILE= true

View File

@ -34,6 +34,7 @@
#endif
#include <stdlib.h>
#include "libc_private.h"
#include "crtbrand.c"
struct Struct_Obj_Entry;
@ -55,7 +56,7 @@ extern int etext;
#endif
char **environ;
char *__progname = "";
const char *__progname = "";
/* The entry function. */
/*
@ -77,6 +78,7 @@ _start(char **ap,
int argc;
char **argv;
char **env;
const char *s;
#if 0
void (*term)(void);
@ -91,7 +93,6 @@ _start(char **ap,
env = ap + 2 + argc;
environ = env;
if (argc > 0 && argv[0] != NULL) {
char *s;
__progname = argv[0];
for (s = __progname; *s != '\0'; s++)
if (*s == '/')

View File

@ -39,15 +39,14 @@ __FBSDID("$FreeBSD$");
#include "namespace.h"
#include <err.h>
#include "un-namespace.h"
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "un-namespace.h"
#include <stdarg.h>
extern char *__progname; /* Program name, from crt0. */
#include "libc_private.h"
static FILE *err_file; /* file to use for error output */
static void (*err_exit)(int);
@ -110,7 +109,7 @@ verrc(eval, code, fmt, ap)
{
if (err_file == 0)
err_set_file((FILE *)0);
fprintf(err_file, "%s: ", __progname);
fprintf(err_file, "%s: ", _getprogname());
if (fmt != NULL) {
vfprintf(err_file, fmt, ap);
fprintf(err_file, ": ");
@ -138,7 +137,7 @@ verrx(eval, fmt, ap)
{
if (err_file == 0)
err_set_file((FILE *)0);
fprintf(err_file, "%s: ", __progname);
fprintf(err_file, "%s: ", _getprogname());
if (fmt != NULL)
vfprintf(err_file, fmt, ap);
fprintf(err_file, "\n");
@ -183,7 +182,7 @@ vwarnc(code, fmt, ap)
{
if (err_file == 0)
err_set_file((FILE *)0);
fprintf(err_file, "%s: ", __progname);
fprintf(err_file, "%s: ", _getprogname());
if (fmt != NULL) {
vfprintf(err_file, fmt, ap);
fprintf(err_file, ": ");
@ -207,7 +206,7 @@ vwarnx(fmt, ap)
{
if (err_file == 0)
err_set_file((FILE *)0);
fprintf(err_file, "%s: ", __progname);
fprintf(err_file, "%s: ", _getprogname());
if (fmt != NULL)
vfprintf(err_file, fmt, ap);
fprintf(err_file, "\n");

View File

@ -48,7 +48,7 @@ __FBSDID("$FreeBSD$");
#include <pthread.h>
#include "un-namespace.h"
#include <libc_private.h>
#include "libc_private.h"
#define THREAD_LOCK() if (__isthreaded) _pthread_mutex_lock(&logname_mutex)
#define THREAD_UNLOCK() if (__isthreaded) _pthread_mutex_unlock(&logname_mutex)

View File

@ -1,12 +1,16 @@
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include "namespace.h"
#include <stdlib.h>
#include "un-namespace.h"
extern const char *__progname;
#include "libc_private.h"
__weak_reference(_getprogname, getprogname);
const char *
getprogname(void)
_getprogname(void)
{
return (__progname);

View File

@ -18,6 +18,7 @@
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include "namespace.h"
#include <sys/types.h>
#include <sys/param.h>
#include <sys/exec.h>
@ -31,6 +32,9 @@ __FBSDID("$FreeBSD$");
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "un-namespace.h"
#include "libc_private.h"
/*
* Older FreeBSD 2.0, 2.1 and 2.2 had different ps_strings structures and
@ -53,7 +57,6 @@ struct old_ps_strings {
#include <stdarg.h>
#define SPT_BUFSIZE 2048 /* from other parts of sendmail */
extern char * __progname; /* is this defined in a .h anywhere? */
void
setproctitle(const char *fmt, ...)
@ -83,7 +86,7 @@ setproctitle(const char *fmt, ...)
len = 0;
} else {
/* print program name heading for grep */
(void) snprintf(buf, sizeof(buf), "%s: ", __progname);
(void)snprintf(buf, sizeof(buf), "%s: ", _getprogname());
len = strlen(buf);
}

View File

@ -4,7 +4,7 @@ __FBSDID("$FreeBSD$");
#include <stdlib.h>
#include <string.h>
extern const char *__progname;
#include "libc_private.h"
void
setprogname(const char *progname)

View File

@ -49,6 +49,7 @@ __FBSDID("$FreeBSD$");
#include <fcntl.h>
#include <paths.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
@ -60,6 +61,8 @@ __FBSDID("$FreeBSD$");
#endif
#include "un-namespace.h"
#include "libc_private.h"
static int LogFile = -1; /* fd for log */
static int connected; /* have done connect */
static int opened; /* have done openlog() */
@ -67,7 +70,6 @@ static int LogStat = 0; /* status bits, set by openlog() */
static const char *LogTag = NULL; /* string to tag the entry with */
static int LogFacility = LOG_USER; /* default facility code */
static int LogMask = 0xff; /* mask of priorities to be logged */
extern char *__progname; /* Program name, from crt0. */
static void disconnectlog(void); /* disconnect from syslogd */
static void connectlog(void); /* (re)connect to syslogd */
@ -181,7 +183,7 @@ vsyslog(pri, fmt, ap)
stdp = tbuf + (sizeof(tbuf) - tbuf_cookie.left);
}
if (LogTag == NULL)
LogTag = __progname;
LogTag = _getprogname();
if (LogTag != NULL)
(void)fprintf(fp, "%s", LogTag);
if (LogStat & LOG_PID)

View File

@ -43,23 +43,22 @@ __FBSDID("$FreeBSD$");
#include <sys/gmon.h>
#include <sys/sysctl.h>
#include "namespace.h"
#include <err.h>
#include "un-namespace.h"
#include <stdio.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "un-namespace.h"
#include "libc_private.h"
#if defined(__ELF__) && (defined(i386) || defined(__sparc64__))
extern char *minbrk asm (".minbrk");
#else
extern char *minbrk asm ("minbrk");
#endif
extern char *__progname;
struct gmonparam _gmonparam = { GMON_PROF_OFF };
static int s_scale;
@ -175,7 +174,7 @@ _mcleanup()
}
moncontrol(0);
snprintf(outname, sizeof(outname), "%s.gmon", __progname);
snprintf(outname, sizeof(outname), "%s.gmon", _getprogname());
fd = _open(outname, O_CREAT|O_TRUNC|O_WRONLY, 0666);
if (fd < 0) {
_warn("_mcleanup: %s", outname);

View File

@ -63,4 +63,10 @@ extern int __isthreaded;
#define FLOCKFILE(fp) if (__isthreaded) _FLOCKFILE(fp)
#define FUNLOCKFILE(fp) if (__isthreaded) _funlockfile(fp)
/*
* This is a pointer in the C run-time startup code. It is used
* by getprogname() and setprogname().
*/
extern const char *__progname;
#endif /* _LIBC_PRIVATE_H_ */

View File

@ -67,6 +67,7 @@
#define getdirentries _getdirentries
#define getlogin _getlogin
#define getpeername _getpeername
#define getprogname _getprogname
#define getsockname _getsockname
#define getsockopt _getsockopt
#define ioctl _ioctl

View File

@ -52,6 +52,7 @@
#undef getdirentries
#undef getlogin
#undef getpeername
#undef getprogname
#undef getsockname
#undef getsockopt
#undef ioctl

View File

@ -41,6 +41,8 @@ __FBSDID("$FreeBSD$");
#include "collate.h"
#include "setlocale.h"
#include "libc_private.h"
int __collate_load_error = 1;
int __collate_substitute_nontrivial;
char __collate_version[STR_LEN];
@ -176,11 +178,10 @@ __collate_strdup(s)
void
__collate_err(int ex, const char *f)
{
extern char *__progname; /* Program name, from crt0. */
const char *s;
int serrno = errno;
s = __progname;
s = _getprogname();
_write(STDERR_FILENO, s, strlen(s));
_write(STDERR_FILENO, ": ", 2);
s = f;

View File

@ -37,9 +37,13 @@ static char sccsid[] = "@(#)getopt.c 8.3 (Berkeley) 4/27/95";
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include "namespace.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "un-namespace.h"
#include "libc_private.h"
int opterr = 1, /* if error message should be printed */
optind = 1, /* index into parent argv vector */
@ -61,7 +65,6 @@ getopt(nargc, nargv, ostr)
char * const *nargv;
const char *ostr;
{
extern char *__progname;
static char *place = EMSG; /* option letter processing */
char *oli; /* option letter list index */
@ -88,8 +91,8 @@ getopt(nargc, nargv, ostr)
if (!*place)
++optind;
if (opterr && *ostr != ':' && optopt != BADCH)
(void)fprintf(stderr,
"%s: illegal option -- %c\n", __progname, optopt);
(void)fprintf(stderr, "%s: illegal option -- %c\n",
_getprogname(), optopt);
return (BADCH);
}
if (*++oli != ':') { /* don't need argument */
@ -107,7 +110,7 @@ getopt(nargc, nargv, ostr)
if (opterr)
(void)fprintf(stderr,
"%s: option requires an argument -- %c\n",
__progname, optopt);
_getprogname(), optopt);
return (BADCH);
}
else /* white space */

View File

@ -297,14 +297,12 @@ wrtmessage(char *p1, char *p2, char *p3, char *p4)
void (*_malloc_message)(char *p1, char *p2, char *p3, char *p4) = wrtmessage;
extern char *__progname;
static void
wrterror(char *p)
{
suicide = 1;
_malloc_message(__progname, malloc_func, " error: ", p);
_malloc_message(_getprogname(), malloc_func, " error: ", p);
abort();
}
@ -314,7 +312,7 @@ wrtwarning(char *p)
if (malloc_abort)
wrterror(p);
_malloc_message(__progname, malloc_func, " warning: ", p);
_malloc_message(_getprogname(), malloc_func, " warning: ", p);
}
/*

View File

@ -33,14 +33,16 @@
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <sched.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include "pthread_private.h"
#include <sched.h>
#include <pthread.h>
#include <unistd.h>
extern char *__progname;
#include <libc_private.h>
#include "pthread_private.h"
/*
* Lock a location for the running thread. Yield to allow other
@ -91,7 +93,7 @@ _spinlock_debug(spinlock_t *lck, char *fname, int lineno)
cnt++;
if (cnt > 100) {
char str[256];
snprintf(str, sizeof(str), "%s - Warning: Thread %p attempted to lock %p from %s (%d) was left locked from %s (%d)\n", __progname, curthread, lck, fname, lineno, lck->fname, lck->lineno);
snprintf(str, sizeof(str), "%s - Warning: Thread %p attempted to lock %p from %s (%d) was left locked from %s (%d)\n", _getprogname(), curthread, lck, fname, lineno, lck->fname, lck->lineno);
__sys_write(2,str,strlen(str));
__sleep(1);
cnt = 0;

View File

@ -80,8 +80,6 @@ __FBSDID("$FreeBSD$");
#include "common.h"
#include "httperr.h"
extern char *__progname; /* XXX not portable */
/* Maximum number of redirects to follow */
#define MAX_REDIRECT 5
@ -840,7 +838,7 @@ _http_request(struct url *URL, const char *op, struct url_stat *us,
if ((p = getenv("HTTP_USER_AGENT")) != NULL && *p != '\0')
_http_cmd(fd, "User-Agent: %s", p);
else
_http_cmd(fd, "User-Agent: %s " _LIBFETCH_VER, __progname);
_http_cmd(fd, "User-Agent: %s " _LIBFETCH_VER, _getprogname());
if (url->offset)
_http_cmd(fd, "Range: bytes=%lld-", (long long)url->offset);
_http_cmd(fd, "Connection: close");

View File

@ -33,14 +33,16 @@
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <sched.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include "pthread_private.h"
#include <sched.h>
#include <pthread.h>
#include <unistd.h>
extern char *__progname;
#include <libc_private.h>
#include "pthread_private.h"
/*
* Lock a location for the running thread. Yield to allow other
@ -91,7 +93,7 @@ _spinlock_debug(spinlock_t *lck, char *fname, int lineno)
cnt++;
if (cnt > 100) {
char str[256];
snprintf(str, sizeof(str), "%s - Warning: Thread %p attempted to lock %p from %s (%d) was left locked from %s (%d)\n", __progname, curthread, lck, fname, lineno, lck->fname, lck->lineno);
snprintf(str, sizeof(str), "%s - Warning: Thread %p attempted to lock %p from %s (%d) was left locked from %s (%d)\n", _getprogname(), curthread, lck, fname, lineno, lck->fname, lck->lineno);
__sys_write(2,str,strlen(str));
__sleep(1);
cnt = 0;

View File

@ -51,8 +51,6 @@ __FBSDID("$FreeBSD$");
/*#include <netncp/ncp_cfg.h>*/
#include "ncp_mod.h"
extern char *__progname;
int sysentoffset;
void
@ -309,7 +307,7 @@ ncp_getopt(nargc, nargv, ostr)
++ncp_optind;
if (ncp_opterr && *ostr != ':')
(void)fprintf(stderr,
"%s: illegal option -- %c\n", __progname, ncp_optopt);
"%s: illegal option -- %c\n", _getprogname(), ncp_optopt);
return (BADCH);
}
if (*++oli != ':') { /* don't need argument */
@ -327,7 +325,7 @@ ncp_getopt(nargc, nargv, ostr)
if (ncp_opterr)
(void)fprintf(stderr,
"%s: option requires an argument -- %c\n",
__progname, ncp_optopt);
_getprogname(), ncp_optopt);
return (BADCH);
}
else /* white space */
@ -439,7 +437,7 @@ void
ncp_error(const char *fmt, int error, ...) {
va_list ap;
fprintf(stderr, "%s: ", __progname);
fprintf(stderr, "%s: ", _getprogname());
va_start(ap, error);
vfprintf(stderr, fmt, ap);
va_end(ap);

View File

@ -33,14 +33,16 @@
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <sched.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include "pthread_private.h"
#include <sched.h>
#include <pthread.h>
#include <unistd.h>
extern char *__progname;
#include <libc_private.h>
#include "pthread_private.h"
/*
* Lock a location for the running thread. Yield to allow other
@ -91,7 +93,7 @@ _spinlock_debug(spinlock_t *lck, char *fname, int lineno)
cnt++;
if (cnt > 100) {
char str[256];
snprintf(str, sizeof(str), "%s - Warning: Thread %p attempted to lock %p from %s (%d) was left locked from %s (%d)\n", __progname, curthread, lck, fname, lineno, lck->fname, lck->lineno);
snprintf(str, sizeof(str), "%s - Warning: Thread %p attempted to lock %p from %s (%d) was left locked from %s (%d)\n", _getprogname(), curthread, lck, fname, lineno, lck->fname, lck->lineno);
__sys_write(2,str,strlen(str));
__sleep(1);
cnt = 0;