Repository navigation
Expand file tree
/
Copy pathfunctions.h
More file actions
502 lines (416 loc) · 13.5 KB
/
Copy pathfunctions.h
File metadata and controls
502 lines (416 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/* /////////////////////////////////////////////////////////////////////////
* File: unixstl/process/functions.h
*
* Purpose: Process functions.
*
* Created: 8th October 2026
* Updated: 2nd November 2026
*
* Home: http://stlsoft.org/
*
* Copyright (c) 2026, Matthew Wilson and Synesis Information Systems
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - Neither the name(s) of Matthew Wilson and Synesis Information Systems
* nor the names of any contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ////////////////////////////////////////////////////////////////////// */
/** \file unixstl/process/functions.h
*
* \brief [C, C++] Process functions
* (\ref group__library__System "System" Library).
*/
#ifndef UNIXSTL_INCL_UNIXSTL_PROCESS_H_FUNCTIONS
#define UNIXSTL_INCL_UNIXSTL_PROCESS_H_FUNCTIONS
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
# define UNIXSTL_VER_UNIXSTL_PROCESS_H_FUNCTIONS_MAJOR 1
# define UNIXSTL_VER_UNIXSTL_PROCESS_H_FUNCTIONS_MINOR 1
# define UNIXSTL_VER_UNIXSTL_PROCESS_H_FUNCTIONS_REVISION 4
# define UNIXSTL_VER_UNIXSTL_PROCESS_H_FUNCTIONS_EDIT 5
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
/* /////////////////////////////////////////////////////////////////////////
* includes
*/
#ifndef UNIXSTL_INCL_UNIXSTL_H_UNIXSTL
# include <unixstl/unixstl.h>
#endif /* !UNIXSTL_INCL_UNIXSTL_H_UNIXSTL */
#ifdef STLSOFT_TRACE_INCLUDE
# pragma message(__FILE__)
#endif /* STLSOFT_TRACE_INCLUDE */
#ifndef STLSOFT_INCL_STLSOFT_API_external_h_memfns
# include <stlsoft/api/external/memfns.h>
#endif /* !STLSOFT_INCL_STLSOFT_API_external_h_memfns */
#ifndef STLSOFT_INCL_STLSOFT_MEMORY_H_AUTO_BUFFER
# include <stlsoft/memory/auto_buffer.h>
#endif /* !STLSOFT_INCL_STLSOFT_MEMORY_H_AUTO_BUFFER */
#ifndef STLSOFT_INCL_H_ERRNO
# define STLSOFT_INCL_H_ERRNO
# include <errno.h>
#endif /* !STLSOFT_INCL_H_ERRNO */
#ifndef STLSOFT_INCL_H_LIMITS
# define STLSOFT_INCL_H_LIMITS
# include <limits.h>
#endif /* !STLSOFT_INCL_H_LIMITS */
#ifndef STLSOFT_INCL_H_STDLIB
# define STLSOFT_INCL_H_STDLIB
# include <stdlib.h>
#endif /* !STLSOFT_INCL_H_STDLIB */
#ifndef STLSOFT_INCL_H_STRING
# define STLSOFT_INCL_H_STRING
# include <string.h>
#endif /* !STLSOFT_INCL_H_STRING */
#if 0
#elif defined(_WIN32)
# ifndef WINSTL_INCL_WINSTL_API_external_h_DynamicLinkLibrary
# include <winstl/api/external/DynamicLinkLibrary.h>
# endif /* !WINSTL_INCL_WINSTL_API_external_h_DynamicLinkLibrary */
# ifndef STLSOFT_INCL_H_PROCESS
# define STLSOFT_INCL_H_PROCESS
# include <process.h>
# endif /* !STLSOFT_INCL_H_PROCESS */
#elif defined(__APPLE__)
# ifndef STLSOFT_INCL_H_LIBPROC
# define STLSOFT_INCL_H_LIBPROC
# include <libproc.h>
# endif /* !STLSOFT_INCL_H_LIBPROC */
# ifndef STLSOFT_INCL_MACH_O_H_DYLD
# define STLSOFT_INCL_MACH_O_H_DYLD
# include <mach-o/dyld.h>
# endif /* !STLSOFT_INCL_MACH_O_H_DYLD */
# ifndef STLSOFT_INCL_H_UNISTD
# define STLSOFT_INCL_H_UNISTD
# include <unistd.h>
# endif /* !STLSOFT_INCL_H_UNISTD */
#elif defined(__linux__)
# ifndef STLSOFT_INCL_H_UNISTD
# define STLSOFT_INCL_H_UNISTD
# include <unistd.h>
# endif /* !STLSOFT_INCL_H_UNISTD */
#elif 0 ||\
defined(__FreeBSD__) ||\
defined(__NetBSD__) ||\
defined(__OpenBSD__) ||\
0
# ifndef STLSOFT_INCL_SYS_H_SYSCTL
# define STLSOFT_INCL_SYS_H_SYSCTL
# include <sys/sysctl.h>
# endif /* !STLSOFT_INCL_SYS_H_SYSCTL */
# ifndef STLSOFT_INCL_SYS_H_TYPES
# define STLSOFT_INCL_SYS_H_TYPES
# include <sys/types.h>
# endif /* !STLSOFT_INCL_SYS_H_TYPES */
#elif defined(__sun)
# ifndef STLSOFT_INCL_H_UNISTD
# define STLSOFT_INCL_H_UNISTD
# include <unistd.h>
# endif /* !STLSOFT_INCL_H_UNISTD */
#else
# ifndef STLSOFT_INCL_SYS_H_TYPES
# define STLSOFT_INCL_SYS_H_TYPES
# include <sys/types.h>
# endif /* !STLSOFT_INCL_SYS_H_TYPES */
# ifndef STLSOFT_INCL_H_UNISTD
# define STLSOFT_INCL_H_UNISTD
# include <unistd.h>
# endif /* !STLSOFT_INCL_H_UNISTD */
#endif
/* /////////////////////////////////////////////////////////////////////////
* constants
*/
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
# if !defined(PATH_MAX)
# define UNIXSTL_C_GET_CURRENT_PROCESS_PATH_INTERNAL_SIZE_ 4096
# else
# define UNIXSTL_C_GET_CURRENT_PROCESS_PATH_INTERNAL_SIZE_ PATH_MAX
# endif
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
/* /////////////////////////////////////////////////////////////////////////
* namespace
*/
#if !defined(UNIXSTL_NO_NAMESPACE) && \
!defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
# if defined(STLSOFT_NO_NAMESPACE)
/* There is no stlsoft namespace, so must define ::unixstl */
namespace unixstl
{
# else
/* Define stlsoft::unixstl_project */
namespace stlsoft
{
namespace unixstl_project
{
# endif /* STLSOFT_NO_NAMESPACE */
#endif /* !UNIXSTL_NO_NAMESPACE */
/* /////////////////////////////////////////////////////////////////////////
* functions
*/
/** \brief Obtains the id of the calling process.
*
* \ingroup group__library__System
*
* \return The process id, as obtained from \c _getpid() on Windows / UNIXem
* builds or \c getpid() otherwise.
*/
STLSOFT_INLINE
int
unixstl_C_get_current_process_id(void)
{
#if 0
#elif defined(_WIN32)
return STLSOFT_NS_GLOBAL(_getpid)();
#else
STLSOFT_STATIC_ASSERT(sizeof(pid_t) <= sizeof(int));
return STLSOFT_STATIC_CAST(int, STLSOFT_NS_GLOBAL(getpid)());
#endif
}
/** \brief Obtains the fully qualified path of the current process image
* (multibyte).
*
* \ingroup group__library__System
*
* \param buff Destination buffer. May be \c NULL if and only if \c cch is
* 0;
* \param cch Number of characters available in \c buff, including space for
* a terminating NUL;
*
* \return If \c buff is sufficient for the full path and a terminating NUL
* character then this returns the length of the path (excluding the NUL
* character). Otherwise, as much of the result as can be written is done
* so and a character count of <em>at least</em> the full length + 1 (for
* the NUL character) is returned. The returned demand may exceed that
* minimum. Returns 0 on failure;
*
* \pre 0 == cch || NULL != buff
*
* \note The normative client pattern is to call with \c cch of 0 to obtain
* a sufficient size, allocate, then call again.
*/
STLSOFT_INLINE
us_size_t
unixstl_C_get_current_process_path_m(
us_char_a_t* buff
, us_size_t cch
)
{
STLSOFT_C_AUTO_BUFFER_DECLARE(us_char_a_t, UNIXSTL_C_GET_CURRENT_PROCESS_PATH_INTERNAL_SIZE_, ab);
us_size_t len = 0;
us_size_t result = 0;
UNIXSTL_ASSERT(0 == cch || NULL != buff);
STLSOFT_C_AUTO_BUFFER_INITIALISE_FROM_INTERNAL(ab);
#if 0
#elif defined(_WIN32)
/* NOTE: this only for UNIXem-ulation, so we don't care about its
* performance
*/
/* Windows / UNIXem: GetModuleFileNameA */
for (;;)
{
DWORD const n = WINSTL_API_EXTERNAL_DynamicLinkLibrary_GetModuleFileNameA(
NULL
, ab.ptr
, STLSOFT_STATIC_CAST(DWORD, ab.size)
);
if (0 == n)
{
goto done;
}
if (n < ab.size)
{
len = STLSOFT_STATIC_CAST(us_size_t, n);
break;
}
if (0 != STLSOFT_C_AUTO_BUFFER_RESIZE(ab, ab.size * 2))
{
errno = ENOMEM;
goto done;
}
}
#elif defined(__APPLE__)
/* macOS: prefer proc_pidpath; fallback to _NSGetExecutablePath (+ realpath). */
{
int const ret = STLSOFT_NS_GLOBAL(proc_pidpath)(
STLSOFT_NS_GLOBAL(getpid)()
, ab.ptr
, STLSOFT_STATIC_CAST(uint32_t, ab.size)
);
if (ret <= 0)
{
uint32_t size = STLSOFT_STATIC_CAST(uint32_t, ab.size);
if (0 != STLSOFT_NS_GLOBAL(_NSGetExecutablePath)(ab.ptr, &size))
{
if (0 != STLSOFT_C_AUTO_BUFFER_RESIZE(ab, size))
{
errno = ENOMEM;
goto done;
}
size = STLSOFT_STATIC_CAST(uint32_t, ab.size);
if (0 != STLSOFT_NS_GLOBAL(_NSGetExecutablePath)(ab.ptr, &size))
{
goto done;
}
}
{
char* const resolved = STLSOFT_NS_GLOBAL(realpath)(ab.ptr, NULL);
if (NULL != resolved)
{
us_size_t const cchResolved = STLSOFT_NS_GLOBAL(strlen)(resolved);
if (cchResolved + 1 > ab.size)
{
if (0 != STLSOFT_C_AUTO_BUFFER_RESIZE(ab, cchResolved + 1))
{
STLSOFT_NS_GLOBAL(free)(resolved);
errno = ENOMEM;
goto done;
}
}
STLSOFT_API_EXTERNAL_memfns_memcpy(ab.ptr, resolved, cchResolved + 1);
STLSOFT_NS_GLOBAL(free)(resolved);
}
}
}
len = STLSOFT_NS_GLOBAL(strlen)(ab.ptr);
}
#elif defined(__linux__)
/* Linux: readlink("/proc/self/exe") */
for (;;)
{
ssize_t const n = STLSOFT_NS_GLOBAL(readlink)("/proc/self/exe", ab.ptr, ab.size);
if (n < 0)
{
goto done;
}
if (STLSOFT_STATIC_CAST(us_size_t, n) < ab.size)
{
ab.ptr[n] = '\0';
len = STLSOFT_STATIC_CAST(us_size_t, n);
break;
}
if (0 != STLSOFT_C_AUTO_BUFFER_RESIZE(ab, ab.size * 2))
{
errno = ENOMEM;
goto done;
}
}
#elif 0 ||\
defined(__FreeBSD__) ||\
defined(__NetBSD__) ||\
defined(__OpenBSD__) ||\
0
/* BSD: sysctl(KERN_PROC_PATHNAME) */
{
int mib[4];
size_t cb = ab.size;
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_PATHNAME;
mib[3] = -1;
if (0 != STLSOFT_NS_GLOBAL(sysctl)(mib, 4, ab.ptr, &cb, NULL, 0))
{
if (ENOMEM == errno)
{
if (0 != STLSOFT_C_AUTO_BUFFER_RESIZE(ab, cb))
{
errno = ENOMEM;
goto done;
}
cb = ab.size;
if (0 != STLSOFT_NS_GLOBAL(sysctl)(mib, 4, ab.ptr, &cb, NULL, 0))
{
goto done;
}
}
else
{
goto done;
}
}
len = STLSOFT_NS_GLOBAL(strlen)(ab.ptr);
}
#elif defined(__sun)
/* Solaris: getexecname() + realpath() */
{
char const* const exe = STLSOFT_NS_GLOBAL(getexecname)();
if (NULL == exe)
{
goto done;
}
if (NULL == STLSOFT_NS_GLOBAL(realpath)(exe, ab.ptr))
{
goto done;
}
len = STLSOFT_NS_GLOBAL(strlen)(ab.ptr);
}
#else
errno = ENOSYS;
goto done;
#endif
if (0 == cch)
{
result = len + 1;
}
else if (cch <= len)
{
us_size_t const nCopy = cch - 1;
if (0 != nCopy)
{
STLSOFT_API_EXTERNAL_memfns_memcpy(buff, ab.ptr, nCopy * sizeof(us_char_a_t));
}
buff[nCopy] = '\0';
result = len + 1;
}
else
{
if (0 != len)
{
STLSOFT_API_EXTERNAL_memfns_memcpy(buff, ab.ptr, len * sizeof(us_char_a_t));
}
buff[len] = '\0';
result = len;
}
done:
STLSOFT_C_AUTO_BUFFER_FREE(ab);
return result;
}
/* /////////////////////////////////////////////////////////////////////////
* namespace
*/
#ifndef UNIXSTL_NO_NAMESPACE
# if defined(STLSOFT_NO_NAMESPACE) || \
defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
} /* namespace unixstl */
# else
} /* namespace unixstl_project */
} /* namespace stlsoft */
# endif /* STLSOFT_NO_NAMESPACE */
#endif /* !UNIXSTL_NO_NAMESPACE */
/* /////////////////////////////////////////////////////////////////////////
* inclusion control
*/
#ifdef STLSOFT_CF_PRAGMA_ONCE_SUPPORT
# pragma once
#endif /* STLSOFT_CF_PRAGMA_ONCE_SUPPORT */
#endif /* !UNIXSTL_INCL_UNIXSTL_PROCESS_H_FUNCTIONS */
/* ///////////////////////////// end of file //////////////////////////// */