123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- #include <config.h>
- #include "getprogname.h"
- #include <errno.h>
- #include <stdlib.h>
- #ifdef _AIX
- # include <unistd.h>
- # include <procinfo.h>
- # include <string.h>
- #endif
- #ifdef __MVS__
- # ifndef _OPEN_SYS
- # define _OPEN_SYS
- # endif
- # include <string.h>
- # include <sys/ps.h>
- #endif
- #ifdef __hpux
- # include <unistd.h>
- # include <sys/param.h>
- # include <sys/pstat.h>
- # include <string.h>
- #endif
- #ifdef __sgi
- # include <string.h>
- # include <unistd.h>
- # include <stdio.h>
- # include <fcntl.h>
- # include <sys/procfs.h>
- #endif
- #include "dirname.h"
- #ifndef HAVE_GETPROGNAME
- char const *
- getprogname (void)
- {
- # if HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME
-
- return program_invocation_short_name;
- # elif HAVE_DECL_PROGRAM_INVOCATION_NAME
-
- return last_component (program_invocation_name);
- # elif HAVE_GETEXECNAME
-
- const char *p = getexecname ();
- if (!p)
- p = "?";
- return last_component (p);
- # elif HAVE_DECL___ARGV
-
- const char *p = __argv && __argv[0] ? __argv[0] : "?";
- return last_component (p);
- # elif HAVE_VAR___PROGNAME
-
-
-
- extern char *__progname;
- const char *p = __progname;
- return p && p[0] ? p : "?";
- # elif _AIX
-
- static char *p;
- static int first = 1;
- if (first)
- {
- first = 0;
- pid_t pid = getpid ();
- struct procentry64 procs;
- p = (0 < getprocs64 (&procs, sizeof procs, NULL, 0, &pid, 1)
- ? strdup (procs.pi_comm)
- : NULL);
- if (!p)
- p = "?";
- }
- return p;
- # elif defined __hpux
- static char *p;
- static int first = 1;
- if (first)
- {
- first = 0;
- pid_t pid = getpid ();
- struct pst_status status;
- p = (0 < pstat_getproc (&status, sizeof status, 0, pid)
- ? strdup (status.pst_ucomm)
- : NULL);
- if (!p)
- p = "?";
- }
- return p;
- # elif __MVS__
-
- static char *p = "?";
- static int first = 1;
- if (first)
- {
- pid_t pid = getpid ();
- int token;
- W_PSPROC buf;
- first = 0;
- memset (&buf, 0, sizeof(buf));
- buf.ps_cmdptr = (char *) malloc (buf.ps_cmdlen = PS_CMDBLEN_LONG);
- buf.ps_conttyptr = (char *) malloc (buf.ps_conttylen = PS_CONTTYBLEN);
- buf.ps_pathptr = (char *) malloc (buf.ps_pathlen = PS_PATHBLEN);
- if (buf.ps_cmdptr && buf.ps_conttyptr && buf.ps_pathptr)
- {
- for (token = 0; token >= 0;
- token = w_getpsent (token, &buf, sizeof(buf)))
- {
- if (token > 0 && buf.ps_pid == pid)
- {
- char *s = strdup (last_component (buf.ps_pathptr));
- if (s)
- p = s;
- break;
- }
- }
- }
- free (buf.ps_cmdptr);
- free (buf.ps_conttyptr);
- free (buf.ps_pathptr);
- }
- return p;
- # elif defined __sgi
- char filename[50];
- int fd;
- sprintf (filename, "/proc/pinfo/%d", (int) getpid ());
- fd = open (filename, O_RDONLY);
- if (0 <= fd)
- {
- prpsinfo_t buf;
- int ioctl_ok = 0 <= ioctl (fd, PIOCPSINFO, &buf);
- close (fd);
- if (ioctl_ok)
- {
- char *name = buf.pr_fname;
- char *namesize = sizeof buf.pr_fname;
- char *namenul = memchr (name, '\0', namesize);
- size_t namelen = namenul ? namenul - name : namesize;
- char *namecopy = malloc (namelen + 1);
- if (namecopy)
- {
- namecopy[namelen] = 0;
- return memcpy (namecopy, name, namelen);
- }
- }
- }
- return NULL;
- # else
- # error "getprogname module not ported to this OS"
- # endif
- }
- #endif
|