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