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 librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
25 * $XConsortium: util.h /main/4 1995/11/06 18:52:47 rswiston $
27 * @(#)util.h 1.39 14 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, 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.
44 * util.h - general utilities
50 #define _POSIX_SOURCE 1 /* we want to be POSIX-compliant */
53 #include <sys/types.h>
57 #include <ab_private/AB.h>
58 #include <ab/util_types.h>
59 #include <ab_private/util_err.h>
62 * <string.h> seems to have a problem cc -Xc is continually complaining
69 extern char *strdup(const char *);
87 AB_OS_TYPE_NUM_VALUES /* must be last */
91 typedef int UTIL_OUTPUT_HANDLER(STRING error_message);
92 typedef UTIL_OUTPUT_HANDLER *UtilOutputHandler;
94 extern const STRING Util_null_string; /* "(nil)" */
95 extern const STRING Util_empty_string; /* "" */
96 extern int utilP_verbosityPriv3602759317; /* private! */
101 int util_init(int *argc_in_out, STRING **argv_in_out);
106 #define util_free(ptr) {if ((ptr) != NULL) {free(ptr); (ptr)= NULL;}}
107 #define util_malloc(size) (malloc(size))
113 int util_putenv(STRING string);
114 #define util_min(a,b) (((a) < (b))? (a):(b))
115 #define util_max(a,b) (((a) > (b))? (a):(b))
116 #define util_xor(a,b) ( (((a)?1:0) + ((b)?1:0)) == 1 ) /* logical xor */
117 int util_set_program_name(STRING this_prog_name);
118 STRING util_get_program_name(void);
119 int util_set_program_name_from_argv0(STRING argv0);
120 AB_OS_TYPE util_get_os_type(void);
121 STRING util_os_type_to_string(AB_OS_TYPE);
122 STRING util_os_type_to_ident(AB_OS_TYPE);
128 * If verbosity is >= 3, debugging is turned on.
129 * verbosity < 3 => debugging level 0
130 * verbosity 3 => debugging level 1
131 * verbosity 4 => debugging level 2
134 int util_set_verbosity(int verbosity);
135 int util_get_verbosity(void);
136 BOOL util_be_silent(void);
137 BOOL util_be_verbose(void);
138 int util_get_debug_level(void);
139 int debug_level(void); /* these are special-case shortcuts that */
140 BOOL debugging(); /* don't conform to the naming convention */
146 * util_puts_err() and util_printf_err() should be used for messages
147 * that absolutely must be seen by the user (this should cause a popup
148 * to appear, when ab is running).
150 * util_puts() and util_printf() should be used for informative messages,
151 * and may or may not actually be presented to the user.
153 * util_dputs() and util_dprintf() should be used for debugging messages.
154 * Their first parameter is the debugging level at which the message should
155 * be printed. (verbosity 3 = debug level 1). If debugging is disabled
156 * (e.g., verbosity < 3), these functions never generate output.
158 * To redirect the error output, use util_set_err_output_handler. ALL error
159 * output will then be sent to that error handler. A value of NULL sends
160 * error messages to stderr (which is the startup default). This is normally
161 * used to add an error handler that will pop up an error dialog when
162 * running in a windowed application.
164 * util_set_output_handler() works similarly to the error handler.
166 int util_set_err_output_handler(UtilOutputHandler);
167 int util_set_output_handler(UtilOutputHandler);
168 int util_puts(STRING msg);
169 int util_puts_err(STRING msg);
170 int util_printf(STRING fmt, ...);
171 int util_printf_err(STRING fmt, ...);
172 void util_set_help_data(STRING help, STRING vol, STRING locID);
173 int util_get_help_data(STRING *help, STRING *vol, STRING *locID);
175 /* print output if debugging level >= specified */
176 int util_dputs(int debug_level, STRING msg);
177 int util_dprintf(int debug_level, STRING fmt, ...);
182 #define util_fopen fopen /* for consistency */
183 int util_unbuffer_file(FILE *fp); /* for debugging - removes all */
184 /* buffer from the stream */
185 BOOL util_file_is_regular_file(STRING filename);
186 BOOL util_file_is_directory(STRING filename);
187 BOOL util_directory_exists(STRING dirName);
188 BOOL util_file_exists(STRING fileName);
189 long util_file_size(STRING fileName); /* error if file don't exist */
190 BOOL util_paths_are_same_file(STRING path1, STRING path2);
191 int util_fdtruncate(int fildes, off_t length); /* truncate open file*/
192 int util_ftruncate(FILE *file, off_t length, const char *accessType);
205 FILE *util_fopen_locked(const char *filename, const char *type);
207 BOOL util_file_name_has_extension(STRING file_name, STRING ext);
208 BOOL util_file_name_has_ab_extension(STRING file_name);
209 BOOL util_file_name_is_bil_encapsulated(STRING file_name);
210 BOOL util_file_name_is_bil_module(STRING file_name);
211 BOOL util_file_name_is_bil_proj(STRING file_name);
212 FILE* util_create_tmp_file(char *data);
214 int util_cvt_path_to_relative( /* NULL from = cwd */
223 * THE UTIL STRING FUNCTIONS *ALWAYS* NULL-TERMINATE ANY RETURNED STRINGS!
225 * Note: len = length of string withOUT terminating 0
226 * size = size of string including terminating 0 ( = len+1 )
228 BOOL util_streq(STRING s1, STRING s2); /* True if strings = */
229 BOOL util_strcmp(STRING s1, STRING s2); /* allows NULL strings */
230 STRING util_strsafe(STRING s); /*returns "(nil)" for NULL strs*/
231 BOOL util_strempty(STRING s); /* looks for NULL AND "" */
232 int util_strncpy(STRING to, STRING from, int to_size);
233 int util_strcvt_to_lower(STRING to, STRING from, int to_size);
234 int util_strcasestr(STRING str, STRING substr);
235 int util_strncasecmp(STRING s1, STRING s2, int max_chars);
236 STRING util_strip_white_space(STRING string);
239 STRING util_get_file_name_from_path(
245 STRING util_get_dir_name_from_path(
255 BOOL util_path_is_absolute(
259 int util_derive_name_from_path(
272 pid_t util_vfork(void); /* lightweight fork() */
278 STRING util_cvt_bool_to_string(BOOL bool_value, STRING buf, int buf_size);
282 /*************************************************************************
284 ** Inline implementation **
286 *************************************************************************/
288 /* check 1st chars before calling strcmp - avoids a lot of calls */
289 #define util_streq(s1,s2) \
291 || (((s1) != NULL) && ((s2) != NULL) && (((s1)[0] == (s2)[0]) && (strcmp(s1,s2) == 0))) \
295 * allows compare of null strings (NULL < "")
297 #define util_strcmp(s1,s2) \
312 #define util_strlen(s) ((s) == NULL? 0:strlen(s))
314 #define util_strsafe(s) ((s) == NULL? Util_null_string:(s))
316 #define util_strempty(s) (s == NULL || (strcmp(s, "") == 0))
321 #define util_get_verbosity() (utilP_verbosityPriv3602759317)
322 #define util_be_silent() (util_get_verbosity() < 1)
323 #define util_be_verbose() (util_get_verbosity() > 1)
324 #define util_get_debug_level() (util_max(0, util_get_verbosity() - 2))
325 #define debug_level() ((int)(util_get_debug_level()))
326 #define debugging() ((BOOL)(debug_level() > 0))
329 * We key on DEBUG here, so that if DEBUG is turned off, these macros
330 * evaluate to constants. That should allow the compiler to remove
331 * sections of code that are executed conditionally based on these
335 #undef util_get_debug_level
336 #define util_get_debug_level() (0)
338 #define debug_level() (0)
340 #define debugging() (FALSE)
343 #define util_fclose(fp) ((fp) == NULL? 0:(util_funlock(fp, 0, 0), fclose(fp), (fp)= NULL, 0))
346 * These functions are obsolete. Here for backwards compatibility
348 #define util_error(s) util_puts_err(s)
350 #endif /* _AB_ABUTIL_H_ */