BRL-CAD
app.h
Go to the documentation of this file.
1 /* A P P . H
2  * BRL-CAD
3  *
4  * Copyright (c) 2004-2024 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 
21 #ifndef BU_APP_H
22 #define BU_APP_H
23 
24 #include "common.h"
25 
26 #include <stdio.h> /* for FILE */
27 
28 #include "bu/defines.h"
29 
30 
31 __BEGIN_DECLS
32 
33 
34 /** @addtogroup bu_app
35  *
36  * @brief Routines for inspecting and reporting characteristics of the
37  * current running application environment.
38  *
39  */
40 /** @{ */
41 /** @file bu/app.h */
42 
43 /**
44  * DEPRECATED: This routine is replaced by bu_getcwd().
45  * Do not use.
46  *
47  * returns the full path to argv0, regardless of how the application
48  * was invoked.
49  *
50  * this routine will return "(BRL-CAD)" if argv[0] cannot be
51  * identified but should never return NULL. this routine is not
52  * thread-safe.
53  */
54 DEPRECATED BU_EXPORT extern const char *bu_argv0_full_path(void);
55 
56 /**
57  * Routine for obtaining the current working directory.
58  *
59  * Result is written into the provided buf, up to size chars, and
60  * returned. If buf is NULL, dynamically allocated memory will be
61  * returned and must be free'd via bu_free().
62  */
63 BU_EXPORT extern char *bu_getcwd(char *buf, size_t size);
64 
65 /**
66  * Routine for obtaining the initial working directory during
67  * application startup.
68  *
69  * Result is written into the provided buf, up to size chars, and
70  * returned. If buf is NULL, dynamically allocated memory will be
71  * returned and must be free'd via bu_free().
72  */
73 BU_EXPORT extern char *bu_getiwd(char *buf, size_t size);
74 
75 /**
76  * Get the name of the running application. application codes should
77  * call bu_setprogname() first to ensure that the program name is
78  * stored appropriately on platforms that do not have an intrinsic
79  * method for tracking the program name automatically.
80  *
81  * while this routine is thread-safe and reentrant, the static string
82  * returned is shared amongst all threads.
83  */
84 BU_EXPORT extern const char *bu_getprogname(void);
85 
86 /**
87  * Set the name of the running application. This isn't strictly
88  * necessary on platforms that have an intrinsic method for tracking
89  * the program name automatically, but is still recommended for
90  * portability and is necessary on some strict modes of compilation.
91  *
92  * while the implementation relies on a static string shared across
93  * all threads, this routine is thread-safe and reentrant.
94  */
95 BU_EXPORT extern void bu_setprogname(const char *path);
96 
97 /**
98  * returns the first USER path match to a given executable name.
99  *
100  * Routine to provide BSD "which" functionality, locating binaries of
101  * specified programs from the user's PATH. This is useful to locate
102  * binaries and resources at run-time.
103  *
104  * caller should not free the result, though it will not be preserved
105  * between calls either. the caller should strdup the result if they
106  * need to keep it around.
107  *
108  * routine will return NULL if the executable command cannot be found.
109  */
110 BU_EXPORT extern const char *bu_which(const char *cmd);
111 
112 /**
113  * returns the first SYSTEM path match to a given executable cmd name.
114  *
115  * Routine to provide BSD "whereis" functionality, locating binaries
116  * of specified programs from the SYSTEM path. This is useful to
117  * locate binaries and resources at run-time.
118  *
119  * caller should not free the result, though it will not be preserved
120  * between calls either. the caller should strdup the result if they
121  * need to keep it around.
122  *
123  * routine will return NULL if the executable command cannot be found.
124  */
125 BU_EXPORT extern const char *bu_whereis(const char *cmd);
126 
127 
128 /**
129  * @brief
130  * Routine to open a temporary file.
131  *
132  * Create a temporary file. The first readable/writable directory
133  * will be used, searching TMPDIR/TEMP/TMP environment variable
134  * directories followed by default system temp directories and
135  * ultimately trying the current directory.
136  *
137  * The name of the temporary file will be copied into a user-provided
138  * (filepath) buffer if it is a non-NULL pointer and of a sufficient
139  * (len) length to contain the filename.
140  *
141  * This routine is guaranteed to return a new unique file or return
142  * NULL on failure. The file should be closed via fclose() when it is
143  * no longer needed. The temporary file will be automatically
144  * unlinked on application exit. It is the caller's responsibility to
145  * set file access settings, preserve file contents, or destroy file
146  * contents if the default behavior is non-optimal.
147  *
148  * The temporary file may no longer exist after a call to fclose(), so
149  * do not close a handle until you are are done accessing it. Calling
150  * fileno()+dup()+fdopen() can obtain a second handle on an open file.
151  *
152  * This routine is NOT thread-safe.
153  *
154  * Typical Use:
155  *
156  * @code
157  * FILE *fp;
158  * char filename[MAXPATHLEN];
159  * fp = bu_temp_file(&filename, MAXPATHLEN); // get a named temp file
160  * ...
161  * fclose(fp); // close the file when you're done
162  * ...
163  * fp = bu_temp_file(NULL, 0); // get an anonymous temp file
164  * bu_fchmod(fileno(fp), 0777);
165  * ...
166  * rewind(fp);
167  * while (fputc(0, fp) == 0);
168  * fclose(fp);
169  * @endcode
170  */
171 BU_EXPORT extern FILE *bu_temp_file(char *filepath, size_t len);
172 
173 
174 /**
175  * Generate a temporary file name using a prefix (if supplied),
176  * the current process ID, and the current thread's ID
177  *
178  * This function writes into buffer and returns a temporary file name
179  * that is unique to a process / thread pair. The caller can further
180  * distinguish the generated name using a specified filename prefix.
181  * When not supplied, the PACKAGE_NAME (BRL-CAD) is used as the prefix.
182  *
183  * Note that the filename parameter doubles as both a prefix specifier
184  * as well as a return buffer.
185  *
186  * @param filename prefixes the name, doubles as return buffer if len is set
187  * @param len total size of the available filename buffer
188  *
189  * @return
190  * name in the form of prefix_procID_threadID. This will be
191  * 'filename' or, when NULL, a read-only STATIC buffer will be returned
192  * that will be the caller's responsibility to bu_strdup() or
193  * otherwise save before a subsequent call.
194  *
195  * @code
196  *
197  * // e.g., BRL-CAD_123_456
198  * const char *generic = bu_temp_file_name(NULL, 0);
199  *
200  * // e.g., BRL-CAD_123_456 -> saved to supplied buffer
201  * char generic[25] = {0};
202  * bu_temp_file_name(generic, 25);
203  *
204  * // e.g., prefix_123_456
205  * const char *prefix = bu_temp_file_name("prefix", 0);
206  *
207  * // e.g., prefix_123_456 -> saved to supplied buffer
208  * char prefix[25] = "prefix";
209  * bu_temp_file_name(prefix, 25);
210  *
211  * // e.g., /path/to/temp/dir/BRL-CAD_123_456 -> combine with bu_dir for fullpath
212  * char tmpfil[MAXPATHLEN];
213  * const char* tmp_filename = bu_temp_file_name(NULL, 0);
214  * bu_dir(tmpfil, MAXPATHLEN, BU_DIR_TEMP, tmp_filename, NULL);
215  *
216  * @endcode
217  */
218 BU_EXPORT extern const char* bu_temp_file_name(char* filename, size_t len);
219 
220 
221 /**
222  * @brief Wrapper around fchmod.
223  *
224  * Portable wrapper for setting a file descriptor's permissions ala
225  * fchmod().
226  */
227 BU_EXPORT extern int bu_fchmod(int fd, unsigned long pmode);
228 
229 
230 /**@brief BRL-CAD specific path queries */
231 
232 /**
233  * NOTE: this API is replaced by bu_dir()
234  *
235  * For example:
236  * bu_brlcad_dir("doc", 1);
237  * is now:
238  * bu_dir(NULL, 0, BU_DIR_DOC, NULL);
239  *
240  * @brief
241  * Report the relative paths being used to hold BRL-CAD applications,
242  * libraries, and data.
243  *
244  * Recognized keys include:
245  *
246  * Key | Looks Up
247  * ------- | -------------------------------------------
248  * bin | Directory containing applications
249  * lib | Directory containing libraries
250  * include | Directory containing headers
251  * data | Directory containing shared data
252  * share | Directory containing shared data
253  * doc | Directory containing documentation
254  * man | Directory containing Unix man pages
255  *
256  * @return
257  * A STATIC buffer is returned. It is the caller's responsibility to
258  * call bu_strdup() or make other provisions to save the returned
259  * string, before calling again.
260  *
261  */
262 DEPRECATED BU_EXPORT extern const char *bu_brlcad_dir(const char *dirkey, int fail_quietly);
263 
264 /**
265  * NOTE: this API is replaced by bu_dir()
266  *
267  * For example:
268  * bu_brlcad_root("share/tclscripts/isst/isst.tcl", 1);
269  * is now:
270  * bu_dir(NULL, 0, BU_DIR_DATA, "tclscripts", "isst", "isst.tcl", NULL);
271  *
272  * @brief
273  * Locate where the BRL-CAD applications and libraries are installed.
274  *
275  * The BRL-CAD root is searched for in the following order of
276  * precedence by testing for the rhs existence if provided or the
277  * directory existence otherwise:
278  *
279  * BRLCAD_ROOT environment variable if set
280  * run-time path identification
281  * BRLCAD_ROOT compile-time path
282  * current directory
283  *
284  * @return
285  * A STATIC buffer is returned. It is the caller's responsibility to
286  * call bu_strdup() or make other provisions to save the returned
287  * string, before calling again.
288  */
289 DEPRECATED BU_EXPORT extern const char *bu_brlcad_root(const char *rhs, int fail_quietly);
290 
291 
292 typedef enum {
293  BU_DIR_CURR=1, /**< (unknown) current working directory */
294  BU_DIR_INIT, /**< (unknown) initial working directory */
295  BU_DIR_BIN, /**< (read-only) user executables (bin) */
296  BU_DIR_LIB, /**< (read-only) object libraries (lib) */
297  BU_DIR_LIBEXEC, /**< (read-only) object libraries (libexec) */
298  BU_DIR_INCLUDE, /**< (read-only) C/C++ header files (include) */
299  BU_DIR_DATA, /**< (read-only) data files (share) */
300  BU_DIR_DOC, /**< (read-only) documentation, (DATA/doc) */
301  BU_DIR_MAN, /**< (read-only) manual pages, (DATA/man) */
302  BU_DIR_TEMP, /**< (read/write) temporary files (TEMP) */
303  BU_DIR_HOME, /**< (read/write) user home directory (HOME) */
304  BU_DIR_CACHE, /**< (read/write) user cache directory (BU_CACHE_DIR) */
305  BU_DIR_CONFIG, /**< (read/write) user config directory (HOME/.app) */
306  BU_DIR_EXT, /**< (n/a) optional executable extension */
307  BU_DIR_LIBEXT, /**< (n/a) optional library extension */
308  BU_DIR_END /**< always the last entry, for iterators */
310 
311 
312 /**
313  * Find a particular user, system, or runtime directory.
314  *
315  * This function writes into buffer and returns, if found, the path to
316  * a given filesystm resource. The caller may specify paths to
317  * subdirectories and/or filenames as a NULL-terminated vararg list.
318  *
319  * Paths returned will use the native directory separator. Callers
320  * may also manually concatenate subdirectory resources (e.g.,
321  * "share/db/moss.g") using forward slashes and they will be converted
322  * to the native separator.
323  *
324  * @param result if non-NULL, buffer should have >= MAXPATHLEN chars
325  * @param len is the size of the result buffer
326  * @param ... must be one of the above enumerations or a string/path
327  *
328  * @return
329  * Full path to the specified resource will be returned. This will be
330  * 'buffer' or, when NULL, a read-only STATIC buffer will be returned
331  * that will be the caller's responsibility to bu_strdup() or
332  * otherwise save before a subsequent call to bu_dir() returns.
333  *
334  * @code
335  *
336  * // e.g., /home/user
337  * const char *pwd = bu_dir(NULL, 0, BU_DIR_CURR, NULL);
338  *
339  * // e.g., /opt/brlcad/bin/rt
340  * char rt[MAXPATHLEN] = {0};
341  * bu_dir(rt, MAXPATHLEN, BU_DIR_BIN, "rt", BU_DIR_EXT, NULL);
342  * execl(rt, "rt", NULL);
343  *
344  * // e.g., C:\\BRL-CAD 7.123.4\\bin\\librt.dll
345  * const char *lib = bu_dir(NULL, 0, BU_DIR_LIB, "librt", BU_DIR_LIBEXT, NULL);
346  *
347  * // e.g., /opt/app-7.123.4/share/tclscripts/mged/help.tcl
348  * char ts[MAXPATHLEN] = {0};
349  * bu_dir(ts, MAXPATHLEN, BU_DIR_DATA, "tclscripts/mged", NULL);
350  * bu_dir(ts, MAXPATHLEN, ts, "help.tcl", NULL);
351  *
352  * // e.g., C:\\BRL-CAD 7.123.4\\libexec\\mged\\tops.exe
353  * const char *tops = bu_dir(NULL, 0, BU_DIR_LIBEXEC, "mged/tops", BU_DIR_EXT, NULL);
354  *
355  * @endcode
356  */
357 BU_EXPORT extern const char *bu_dir(char *result, size_t len, .../*, NULL */);
358 
359 /**
360  * Create a directory specified by the string "path". Default mode on platforms
361  * using mkdir will be "775"
362  */
363 BU_EXPORT extern void bu_mkdir(const char *path);
364 
365 /**
366  * Recursively delete all files and subfolders in directory d
367  */
368 BU_EXPORT extern void bu_dirclear(const char *d);
369 
370 /** @} */
371 
372 __END_DECLS
373 
374 #endif /* BU_APP_H */
375 
376 /*
377  * Local Variables:
378  * mode: C
379  * tab-width: 8
380  * indent-tabs-mode: t
381  * c-file-style: "stroustrup"
382  * End:
383  * ex: shiftwidth=4 tabstop=8
384  */
Header file for the BRL-CAD common definitions.
char * bu_getcwd(char *buf, size_t size)
DEPRECATED const char * bu_argv0_full_path(void)
void bu_dirclear(const char *d)
int bu_fchmod(int fd, unsigned long pmode)
Wrapper around fchmod.
bu_dir_t
Definition: app.h:292
void bu_mkdir(const char *path)
const char * bu_dir(char *result, size_t len,...)
FILE * bu_temp_file(char *filepath, size_t len)
Routine to open a temporary file.
const char * bu_temp_file_name(char *filename, size_t len)
DEPRECATED const char * bu_brlcad_root(const char *rhs, int fail_quietly)
Locate where the BRL-CAD applications and libraries are installed.
const char * bu_whereis(const char *cmd)
DEPRECATED const char * bu_brlcad_dir(const char *dirkey, int fail_quietly)
BRL-CAD specific path queries.
const char * bu_which(const char *cmd)
const char * bu_getprogname(void)
char * bu_getiwd(char *buf, size_t size)
void bu_setprogname(const char *path)
@ BU_DIR_DATA
Definition: app.h:299
@ BU_DIR_LIB
Definition: app.h:296
@ BU_DIR_END
Definition: app.h:308
@ BU_DIR_BIN
Definition: app.h:295
@ BU_DIR_CONFIG
Definition: app.h:305
@ BU_DIR_DOC
Definition: app.h:300
@ BU_DIR_INCLUDE
Definition: app.h:298
@ BU_DIR_MAN
Definition: app.h:301
@ BU_DIR_LIBEXEC
Definition: app.h:297
@ BU_DIR_TEMP
Definition: app.h:302
@ BU_DIR_CURR
Definition: app.h:293
@ BU_DIR_LIBEXT
Definition: app.h:307
@ BU_DIR_CACHE
Definition: app.h:304
@ BU_DIR_EXT
Definition: app.h:306
@ BU_DIR_INIT
Definition: app.h:294
@ BU_DIR_HOME
Definition: app.h:303
void float float int int int int float * size
Definition: tig.h:132
#define DEPRECATED
Definition: common.h:401