2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
25 * $TOG: util.c /main/6 1998/04/06 13:13:09 mgreess $
27 * @(#)util.c 1.21 16 Feb 1994 cde_app_builder/src/libAButil
29 * RESTRICTED CONFIDENTIAL INFORMATION:
31 * The information in this document is subject to special
32 * restrictions in a confidential disclosure agreement between
33 * HP, IBM, Sun, USL, SCO and Univel. Do not distribute this
34 * document outside HP, IBM, Sun, USL, SCO, Fujitsu, or Univel without
35 * Sun's specific written approval. This document and all copies
36 * and derivative works thereof must be returned or destroyed at
39 * Copyright 1993 Sun Microsystems, Inc. All rights reserved.
45 * File: util.c - general utilities
52 #include <sys/param.h>
53 #include <sys/utsname.h>
54 #include <ab_private/AB.h> /* include first! */
55 #include <ab_private/util.h>
56 #include <ab_private/istr.h>
59 static char *const util_verbosity_env_var_name= "AB_DEBUG_VERBOSITY";
60 static char *const default_program_name= "AppBuilder";
61 static ISTRING program_name= NULL;
63 const STRING Util_null_string= "(nil)";
64 const STRING Util_empty_string= "";
66 const int Util_major_version = 1;
67 const int Util_minor_version = 0;
69 int utilP_verbosity= 1;
76 util_init(int *argc_in_out, STRING **argv_in_out)
78 #define argc (*argc_in_out)
79 #define argv (*argv_in_out)
80 STRING argv0 = argv[0];
81 char *verboseVar= NULL;
82 int verboseVarValue= -1;
85 verbosity = util_get_verbosity();
88 * If this is a debugging build, look for the debugging level env. var
91 verboseVar= getenv(util_verbosity_env_var_name);
92 if (verboseVar != NULL)
94 verboseVarValue= atoi(verboseVar);
95 verbosity= verboseVarValue;
100 * When debugging, keeps the output order consistent. A little
105 /* there's really no good reason to unbuffer stdin */
106 util_unbuffer_file(stdout);
107 util_unbuffer_file(stderr);
112 util_set_program_name_from_argv0(argv0);
117 util_puts("Debugging output enabled, because verbosity > 2.\n");
119 util_set_verbosity(verbosity);
120 verbosity= util_get_verbosity();
129 util_set_verbosity(int newVerbosity)
131 int oldVerbosity = utilP_verbosity;
133 utilP_verbosity= newVerbosity;
136 * If this is not a debugging build, do not allow verbosity to
137 * go outside the range that is normal for an end-user.
140 utilP_verbosity = util_max(0, utilP_verbosity);
141 utilP_verbosity = util_min(2, utilP_verbosity);
145 * Nifty message if we're debugging.
148 if ( ((oldVerbosity >= 3) || (newVerbosity >= 3))
149 && (oldVerbosity != newVerbosity) )
151 int newDebugLevel = debug_level();
152 utilP_verbosity = 3; /* make sure dprintf() prints something */
153 util_dprintf(0, "Verbosity = %d", newVerbosity);
154 if (newDebugLevel > 0)
156 util_dprintf(0," [ Debugging level %d ]", newDebugLevel);
158 util_dprintf(0,"\n");
159 utilP_verbosity = newVerbosity;
168 util_set_program_name(STRING new_program_name)
170 istr_destroy(program_name);
171 if (new_program_name == NULL)
173 program_name= istr_const(default_program_name);
177 program_name= istr_create(new_program_name);
184 util_set_program_name_from_argv0(STRING argv0)
186 char progName[MAXPATHLEN];
187 util_get_file_name_from_path(argv0, progName, MAXPATHLEN);
188 if (strlen(progName) > (size_t)0)
190 util_set_program_name(progName);
197 util_get_program_name(void)
199 return (program_name == NULL?
201 :istr_string(program_name));
206 * Performs a lighter-weight fork that does not immediately copy
207 * all the pages of the application. This is ideal for doing a
208 * a fork and immediately execing another application.
210 * Not all UNIX implementations support a lightweight fork. For
211 * the ones that don't we'll do a normal fork.
221 * putenv() is non-POSIX, so the parameter types can vary a bit...
226 #if defined(__hpux) || (defined(sun) && OSMAJORVERSION >= 5 && OSMINORVERSION >= 4 && OSMINORVERSION <=10 )
227 extern int putenv(const char *string);
228 #elif !(defined(__osf__) || defined(__aix) || defined(__NetBSD__))
229 extern int putenv(char *string);
235 util_putenv(STRING string)
237 return putenv(string);
244 util_get_os_type(void)
246 static AB_OS_TYPE osType = AB_OS_UNDEF;
247 static BOOL osTypeDefined = FALSE;
248 int rc = 0; /* return code */
249 struct utsname sysInfo;
253 int unixwareIndex = -1;
264 if ((rc = uname(&sysInfo)) < 0)
269 aixIndex = util_strcasestr(sysInfo.sysname, "aix");
270 hpIndex = util_strcasestr(sysInfo.sysname, "hp");
271 sunIndex = util_strcasestr(sysInfo.sysname, "sun");
272 unixwareIndex = util_strcasestr(sysInfo.sysname, "UNIX_SV");
273 osf1Index = util_strcasestr(sysInfo.sysname, "osf1");
274 lnxIndex = util_strcasestr(sysInfo.sysname, "linux");
275 fbsdIndex = util_strcasestr(sysInfo.sysname, "freebsd");
276 nbsdIndex = util_strcasestr(sysInfo.sysname, "netbsd");
277 obsdIndex = util_strcasestr(sysInfo.sysname, "openbsd");
279 if (aixIndex < 0) aixIndex = INT_MAX;
280 if (hpIndex < 0) hpIndex = INT_MAX;
281 if (sunIndex < 0) sunIndex = INT_MAX;
282 if (unixwareIndex < 0) unixwareIndex = INT_MAX;
283 if (osf1Index < 0) osf1Index = INT_MAX;
284 if (lnxIndex < 0) lnxIndex = INT_MAX;
285 if (fbsdIndex < 0) fbsdIndex = INT_MAX;
286 if (nbsdIndex < 0) nbsdIndex = INT_MAX;
287 if (obsdIndex < 0) obsdIndex = INT_MAX;
289 #define IS_MATCH(a,b,c,d,e,f,g,h,i) \
290 (((a) < (b)) && ((a) < (c)) && ((a) < (d)) && ((a) < (e)) && ((a) < (f)) && \
291 ((a) < (g)) && ((a) < (h)) && ((a) < (i)))
293 if (IS_MATCH(aixIndex, hpIndex, sunIndex, osf1Index, unixwareIndex,
294 lnxIndex, fbsdIndex, nbsdIndex, obsdIndex))
298 if (IS_MATCH(hpIndex, aixIndex, sunIndex, osf1Index, unixwareIndex,
299 lnxIndex, fbsdIndex, nbsdIndex, obsdIndex))
303 if (IS_MATCH(sunIndex, aixIndex, hpIndex, osf1Index, unixwareIndex,
304 lnxIndex, fbsdIndex, nbsdIndex, obsdIndex))
308 if (IS_MATCH(osf1Index, aixIndex, hpIndex, sunIndex, unixwareIndex,
309 lnxIndex, fbsdIndex, nbsdIndex, obsdIndex))
313 if (IS_MATCH(unixwareIndex, aixIndex, hpIndex, sunIndex, osf1Index,
314 lnxIndex, fbsdIndex, nbsdIndex, obsdIndex))
316 return AB_OS_UNIXWARE;
318 if (IS_MATCH(lnxIndex, aixIndex, hpIndex, sunIndex, osf1Index,
319 unixwareIndex, fbsdIndex, nbsdIndex, obsdIndex))
323 if (IS_MATCH(fbsdIndex, aixIndex, hpIndex, sunIndex, osf1Index,
324 unixwareIndex, lnxIndex, nbsdIndex, obsdIndex))
328 if (IS_MATCH(nbsdIndex, aixIndex, hpIndex, sunIndex, osf1Index,
329 unixwareIndex, lnxIndex, fbsdIndex, obsdIndex))
333 if (IS_MATCH(obsdIndex, aixIndex, hpIndex, sunIndex, osf1Index,
334 unixwareIndex, lnxIndex, fbsdIndex, nbsdIndex))
339 osTypeDefined = TRUE;