BRL-CAD
app.h
Go to the documentation of this file.
1/* A P P . H
2 * BRL-CAD
3 *
4 * Copyright (c) 2004-2023 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 */
54DEPRECATED 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 */
63BU_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 */
73BU_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 */
84BU_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 */
95BU_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 */
110BU_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 */
125BU_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 */
171BU_EXPORT extern FILE *bu_temp_file(char *filepath, size_t len);
172
173
174/**
175 * @brief Wrapper around fchmod.
176 *
177 * Portable wrapper for setting a file descriptor's permissions ala
178 * fchmod().
179 */
180BU_EXPORT extern int bu_fchmod(int fd, unsigned long pmode);
181
182
183/**@brief BRL-CAD specific path queries */
184
185/**
186 * NOTE: this API is replaced by bu_dir()
187 *
188 * For example:
189 * bu_brlcad_dir("doc", 1);
190 * is now:
191 * bu_dir(NULL, 0, BU_DIR_DOC, NULL);
192 *
193 * @brief
194 * Report the relative paths being used to hold BRL-CAD applications,
195 * libraries, and data.
196 *
197 * Recognized keys include:
198 *
199 * Key | Looks Up
200 * ------- | -------------------------------------------
201 * bin | Directory containing applications
202 * lib | Directory containing libraries
203 * include | Directory containing headers
204 * data | Directory containing shared data
205 * share | Directory containing shared data
206 * doc | Directory containing documentation
207 * man | Directory containing Unix man pages
208 *
209 * @return
210 * A STATIC buffer is returned. It is the caller's responsibility to
211 * call bu_strdup() or make other provisions to save the returned
212 * string, before calling again.
213 *
214 */
215DEPRECATED BU_EXPORT extern const char *bu_brlcad_dir(const char *dirkey, int fail_quietly);
216
217/**
218 * NOTE: this API is replaced by bu_dir()
219 *
220 * For example:
221 * bu_brlcad_root("share/tclscripts/isst/isst.tcl", 1);
222 * is now:
223 * bu_dir(NULL, 0, BU_DIR_DATA, "tclscripts", "isst", "isst.tcl", NULL);
224 *
225 * @brief
226 * Locate where the BRL-CAD applications and libraries are installed.
227 *
228 * The BRL-CAD root is searched for in the following order of
229 * precedence by testing for the rhs existence if provided or the
230 * directory existence otherwise:
231 *
232 * BRLCAD_ROOT environment variable if set
233 * run-time path identification
234 * BRLCAD_ROOT compile-time path
235 * current directory
236 *
237 * @return
238 * A STATIC buffer is returned. It is the caller's responsibility to
239 * call bu_strdup() or make other provisions to save the returned
240 * string, before calling again.
241 */
242DEPRECATED BU_EXPORT extern const char *bu_brlcad_root(const char *rhs, int fail_quietly);
243
244
245typedef enum {
246 BU_DIR_CURR=1, /**< (unknown) current working directory */
247 BU_DIR_INIT, /**< (unknown) initial working directory */
248 BU_DIR_BIN, /**< (read-only) user executables (bin) */
249 BU_DIR_LIB, /**< (read-only) object libraries (lib) */
250 BU_DIR_LIBEXEC, /**< (read-only) object libraries (libexec) */
251 BU_DIR_INCLUDE, /**< (read-only) C/C++ header files (include) */
252 BU_DIR_DATA, /**< (read-only) data files (share) */
253 BU_DIR_DOC, /**< (read-only) documentation, (DATA/doc) */
254 BU_DIR_MAN, /**< (read-only) manual pages, (DATA/man) */
255 BU_DIR_TEMP, /**< (read/write) temporary files (TEMP) */
256 BU_DIR_HOME, /**< (read/write) user home directory (HOME) */
257 BU_DIR_CACHE, /**< (read/write) user cache directory (BU_CACHE_DIR) */
258 BU_DIR_CONFIG, /**< (read/write) user config directory (HOME/.app) */
259 BU_DIR_EXT, /**< (n/a) optional executable extension */
260 BU_DIR_LIBEXT, /**< (n/a) optional library extension */
261 BU_DIR_END /**< always the last entry, for iterators */
263
264
265/**
266 * Find a particular user, system, or runtime directory.
267 *
268 * This function writes into buffer and returns, if found, the path to
269 * a given filesystm resource. The caller may specify paths to
270 * subdirectories and/or filenames as a NULL-terminated vararg list.
271 *
272 * Paths returned will use the native directory separator. Callers
273 * may also manually concatenate subdirectory resources (e.g.,
274 * "share/db/moss.g") using forward slashes and they will be converted
275 * to the native separator.
276 *
277 * @param result if non-NULL, buffer should have >= MAXPATHLEN chars
278 * @param len is the size of the result buffer
279 * @param ... must be one of the above enumerations or a string/path
280 *
281 * @return
282 * Full path to the specified resource will be returned. This will be
283 * 'buffer' or, when NULL, a read-only STATIC buffer will be returned
284 * that will be the caller's responsibility to bu_strdup() or
285 * otherwise save before a subsequent call to bu_dir() returns.
286 *
287 * @code
288 *
289 * // e.g., /home/user
290 * const char *pwd = bu_dir(NULL, 0, BU_DIR_CURR, NULL);
291 *
292 * // e.g., /opt/brlcad/bin/rt
293 * char rt[MAXPATHLEN] = {0};
294 * bu_dir(rt, MAXPATHLEN, BU_DIR_BIN, "rt", BU_DIR_EXT, NULL);
295 * execl(rt, "rt", NULL);
296 *
297 * // e.g., C:\\BRL-CAD 7.123.4\\bin\\librt.dll
298 * const char *lib = bu_dir(NULL, 0, BU_DIR_LIB, "librt", BU_DIR_LIBEXT, NULL);
299 *
300 * // e.g., /opt/app-7.123.4/share/tclscripts/mged/help.tcl
301 * char ts[MAXPATHLEN] = {0};
302 * bu_dir(ts, MAXPATHLEN, BU_DIR_DATA, "tclscripts/mged", NULL);
303 * bu_dir(ts, MAXPATHLEN, ts, "help.tcl", NULL);
304 *
305 * // e.g., C:\\BRL-CAD 7.123.4\\libexec\\mged\\tops.exe
306 * const char *tops = bu_dir(NULL, 0, BU_DIR_LIBEXEC, "mged/tops", BU_DIR_EXT, NULL);
307 *
308 * @endcode
309 */
310BU_EXPORT extern const char *bu_dir(char *result, size_t len, .../*, NULL */);
311
312/** @} */
313
314__END_DECLS
315
316#endif /* BU_APP_H */
317
318/*
319 * Local Variables:
320 * mode: C
321 * tab-width: 8
322 * indent-tabs-mode: t
323 * c-file-style: "stroustrup"
324 * End:
325 * ex: shiftwidth=4 tabstop=8
326 */
Header file for the BRL-CAD common definitions.
const char * bu_which(const char *cmd)
DEPRECATED const char * bu_brlcad_root(const char *rhs, int fail_quietly)
Locate where the BRL-CAD applications and libraries are installed.
int bu_fchmod(int fd, unsigned long pmode)
Wrapper around fchmod.
bu_dir_t
Definition: app.h:245
DEPRECATED const char * bu_argv0_full_path(void)
DEPRECATED const char * bu_brlcad_dir(const char *dirkey, int fail_quietly)
BRL-CAD specific path queries.
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_getprogname(void)
char * bu_getcwd(char *buf, size_t size)
const char * bu_whereis(const char *cmd)
char * bu_getiwd(char *buf, size_t size)
void bu_setprogname(const char *path)
@ BU_DIR_DATA
Definition: app.h:252
@ BU_DIR_LIB
Definition: app.h:249
@ BU_DIR_END
Definition: app.h:261
@ BU_DIR_BIN
Definition: app.h:248
@ BU_DIR_CONFIG
Definition: app.h:258
@ BU_DIR_DOC
Definition: app.h:253
@ BU_DIR_INCLUDE
Definition: app.h:251
@ BU_DIR_MAN
Definition: app.h:254
@ BU_DIR_LIBEXEC
Definition: app.h:250
@ BU_DIR_TEMP
Definition: app.h:255
@ BU_DIR_CURR
Definition: app.h:246
@ BU_DIR_LIBEXT
Definition: app.h:260
@ BU_DIR_CACHE
Definition: app.h:257
@ BU_DIR_EXT
Definition: app.h:259
@ BU_DIR_INIT
Definition: app.h:247
@ BU_DIR_HOME
Definition: app.h:256
void float float int int int int float * size
Definition: tig.h:132
#define DEPRECATED
Definition: common.h:400