2 * Copyright © 2003 Felix Kuehling
3 * Copyright © 2018 Advanced Micro Devices, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
18 * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 * The above copyright notice and this permission notice (including the
24 * next paragraph) shall be included in all copies or substantial portions
28 #include "u_process.h"
33 #undef GET_PROGRAM_NAME
35 #if (defined(__GNU_LIBRARY__) || defined(__GLIBC__)) && !defined(__UCLIBC__)
36 # if !defined(__GLIBC__) || (__GLIBC__ < 2)
37 /* These aren't declared in any libc5 header */
38 extern char *program_invocation_name
, *program_invocation_short_name
;
43 char * arg
= strrchr(program_invocation_name
, '/');
47 return program_invocation_name
;
49 # define GET_PROGRAM_NAME() __getProgramName()
50 #elif defined(__CYGWIN__)
51 # define GET_PROGRAM_NAME() program_invocation_short_name
52 #elif defined(__FreeBSD__) && (__FreeBSD__ >= 2)
53 # include <osreldate.h>
54 # if (__FreeBSD_version >= 440000)
55 # define GET_PROGRAM_NAME() getprogname()
57 #elif defined(__NetBSD__) && defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 106000100)
58 # define GET_PROGRAM_NAME() getprogname()
59 #elif defined(__DragonFly__)
60 # define GET_PROGRAM_NAME() getprogname()
61 #elif defined(__APPLE__)
62 # define GET_PROGRAM_NAME() getprogname()
63 #elif defined(ANDROID)
64 # define GET_PROGRAM_NAME() getprogname()
66 /* Solaris has getexecname() which returns the full path - return just
67 the basename to match BSD getprogname() */
73 static const char *progname
;
75 if (progname
== NULL
) {
76 const char *e
= getexecname();
78 /* Have to make a copy since getexecname can return a readonly
79 string, but basename expects to be able to modify its arg. */
82 progname
= basename(n
);
89 # define GET_PROGRAM_NAME() __getProgramName()
92 #if !defined(GET_PROGRAM_NAME)
93 # if defined(__OpenBSD__) || defined(NetBSD) || defined(__UCLIBC__) || defined(ANDROID)
94 /* This is a hack. It's said to work on OpenBSD, NetBSD and GNU.
95 * Rogelio M.Serrano Jr. reported it's also working with UCLIBC. It's
96 * used as a last resort, if there is no documented facility available. */
100 extern const char *__progname
;
101 char * arg
= strrchr(__progname
, '/');
107 # define GET_PROGRAM_NAME() __getProgramName()
109 # define GET_PROGRAM_NAME() ""
110 # pragma message ( "Warning: Per application configuration won't work with your OS version." )
115 util_get_process_name(void)
117 return GET_PROGRAM_NAME();