util: Move os_misc to util
[mesa.git] / src / util / u_process.c
1 /*
2 * Copyright © 2003 Felix Kuehling
3 * Copyright © 2018 Advanced Micro Devices, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
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.
22 *
23 * The above copyright notice and this permission notice (including the
24 * next paragraph) shall be included in all copies or substantial portions
25 * of the Software.
26 */
27
28 #include "u_process.h"
29 #include <string.h>
30 #include <errno.h>
31 #include <stdlib.h>
32
33 #undef GET_PROGRAM_NAME
34
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;
39 # endif
40 static const char *
41 __getProgramName()
42 {
43 char * arg = strrchr(program_invocation_name, '/');
44 if (arg)
45 return arg+1;
46
47 /* If there was no '/' at all we likely have a windows like path from
48 * a wine application.
49 */
50 arg = strrchr(program_invocation_name, '\\');
51 if (arg)
52 return arg+1;
53
54 return program_invocation_name;
55 }
56 # define GET_PROGRAM_NAME() __getProgramName()
57 #elif defined(__CYGWIN__)
58 # define GET_PROGRAM_NAME() program_invocation_short_name
59 #elif defined(__FreeBSD__) && (__FreeBSD__ >= 2)
60 # include <osreldate.h>
61 # if (__FreeBSD_version >= 440000)
62 # define GET_PROGRAM_NAME() getprogname()
63 # endif
64 #elif defined(__NetBSD__) && defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 106000100)
65 # define GET_PROGRAM_NAME() getprogname()
66 #elif defined(__DragonFly__)
67 # define GET_PROGRAM_NAME() getprogname()
68 #elif defined(__APPLE__)
69 # define GET_PROGRAM_NAME() getprogname()
70 #elif defined(ANDROID)
71 # define GET_PROGRAM_NAME() getprogname()
72 #elif defined(__sun)
73 /* Solaris has getexecname() which returns the full path - return just
74 the basename to match BSD getprogname() */
75 # include <libgen.h>
76
77 static const char *
78 __getProgramName()
79 {
80 static const char *progname;
81
82 if (progname == NULL) {
83 const char *e = getexecname();
84 if (e != NULL) {
85 /* Have to make a copy since getexecname can return a readonly
86 string, but basename expects to be able to modify its arg. */
87 char *n = strdup(e);
88 if (n != NULL) {
89 progname = basename(n);
90 }
91 }
92 }
93 return progname;
94 }
95
96 # define GET_PROGRAM_NAME() __getProgramName()
97 #endif
98
99 #if !defined(GET_PROGRAM_NAME)
100 # if defined(__OpenBSD__) || defined(NetBSD) || defined(__UCLIBC__) || defined(ANDROID)
101 /* This is a hack. It's said to work on OpenBSD, NetBSD and GNU.
102 * Rogelio M.Serrano Jr. reported it's also working with UCLIBC. It's
103 * used as a last resort, if there is no documented facility available. */
104 static const char *
105 __getProgramName()
106 {
107 extern const char *__progname;
108 char * arg = strrchr(__progname, '/');
109 if (arg)
110 return arg+1;
111 else
112 return __progname;
113 }
114 # define GET_PROGRAM_NAME() __getProgramName()
115 # else
116 # define GET_PROGRAM_NAME() ""
117 # pragma message ( "Warning: Per application configuration won't work with your OS version." )
118 # endif
119 #endif
120
121 const char *
122 util_get_process_name(void)
123 {
124 return GET_PROGRAM_NAME();
125 }