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
|
%% options
copyright owner = Dirk Krause
copyright year = 2015-xxxx
SPDX-License-Identifier: BSD-3-Clause
%% header
/** @file
Find name of executed file.
*/
#ifndef DK4CONF_H_INCLUDED
#if DK4_BUILDING_DKTOOLS4
#include "dk4conf.h"
#else
#include <dktools-4/dk4conf.h>
#endif
#endif
#ifndef DK4TYPES_H_INCLUDED
#if DK4_BUILDING_DKTOOLS4
#include "dk4types.h"
#else
#include <dktools-4/dk4types.h>
#endif
#endif
#ifndef DK4ERROR_H_INCLUDED
#if DK4_BUILDING_DKTOOLS4
#include "dk4error.h"
#else
#include <dktools-4/dk4error.h>
#endif
#endif
#ifdef __cplusplus
extern "C" {
#endif
/** Retrieve path to executable file.
CRT on Windows: Not used.
@param buffer Buffer to store the file name.
@param sz Buffer size.
@param argv0 First command line word.
@param erp Error report, may be NULL.
@return 1 on success, 0 on error.
Error codes:
- DK4_E_INVALID_ARGUMENTS<br>
if buffer is NULL or sz is 0,
- DK4_E_NOT_FOUND<br>
if the system function(s) failed to find the name,
- DK4_E_SYSTEM<br>
if the function failed to find the current working directory,
- DK4_E_BUFFER_TOO_SMALL<br>
if the buffer is too small to hold the file name.
*/
int
dk4execpath_c8(char *buffer, size_t sz, const char *argv0, dk4_er_t *erp);
#ifdef __cplusplus
}
#endif
%% module
#include "dk4conf.h"
#ifndef DK4USE_H_INCLUDED
#include "dk4use.h"
#endif
#ifndef DK4EXEP8_H_INCLUDED
#include "dk4exep8.h"
#endif
#ifndef DK4STR8_H_INCLUDED
#include "dk4str8.h"
#endif
#ifndef DK4MEM_H_INCLUDED
#include "dk4mem.h"
#endif
#ifndef DK4PATH8_H_INCLUDED
#include "dk4path8.h"
#endif
#ifndef DK4STAT8_H_INCLUDED
#include "dk4stat8.h"
#endif
#ifndef DK4STAT_H_INCLUDED
#include "dk4stat.h"
#endif
#ifndef DK4MAO8D_H_INCLUDED
#include "dk4mao8d.h"
#endif
#ifndef DK4MPL_H_INCLUDED
#include "dk4mpl.h"
#endif
#ifndef DK4UNUSED_H_INCLUDED
#include "dk4unused.h"
#endif
#if DK4_ON_WINDOWS
#ifndef WINDOWS_H_INCLUDED
#include <windows.h>
#define WINDOWS_H_INCLUDED 1
#endif
#ifndef WINBASE_H_INCLUDED
#include <winbase.h>
#define WINBASE_H_INCLUDED 1
#endif
#endif
#if DK4_HAVE_SYS_TYPES_H
#ifndef SYS_TYPES_H_INCLUDED
#include <sys/types.h>
#define SYS_TYPES_H_INCLUDED 1
#endif
#endif
#if DK4_HAVE_SYS_STAT_H
#ifndef SYS_STAT_H_INCLUDED
#include <sys/stat.h>
#define SYS_STAT_H_INCLUDED 1
#endif
#endif
#ifndef STDLIB_H_INCLUDED
#include <stdlib.h>
#define STDLIB_H_INCLUDED 1
#endif
#if DK4_HAVE_ERRNO_H
#ifndef ERRNO_H_INCLUDED
#include <errno.h>
#define ERRNO_H_INCLUDED 1
#endif
#endif
#if DK4_HAVE_UNISTD_H
#ifndef UNISTD_H_INCLUDED
#include <unistd.h>
#define UNISTD_H_INCLUDED 1
#endif
#endif
#if DK4_HAVE_STRING_H
#ifndef STRING_H_INCLUDED
#include <string.h>
#define STRING_H_INCLUDED 1
#endif
#endif
#if DK4_HAVE_ASSERT_H
#ifndef ASSERT_H_INCLUDED
#include <assert.h>
#define ASSERT_H_INCLUDED 1
#endif
#endif
$!trace-include
static const char * const dk4execpath_c8_kw[] = {
/* 0 */ "PATH",
#if DK4_HAVE_BACKSLASH_AS_SEP
/* 1 */ "\\",
#else
/* 1 */ "/",
#endif
NULL
};
#if !DK4_ON_WINDOWS
/** Retrieve name of executable file by PATH environment variable
inspection.
@param buffer Buffer to store result.
@param sz Size of buffer.
@param argv0 First command line word.
@param erp Error report, may be NULL.
@return 1 on success, 0 on error.
*/
static
int
dk4execpath_c8_inspect_path(
char *buffer, size_t sz, const char * argv0, dk4_er_t *erp
)
{
dk4_stat_t stbuf;
char *envpath;
char *mycopy;
char *p1;
char *p2;
int back = 0;
int cc = 1;
#if DK4_USE_ASSERT
assert(NULL != buffer);
assert(0 < sz);
assert(NULL != argv0);
#endif
envpath = getenv(dk4execpath_c8_kw[0]);
if (NULL != envpath) {
mycopy = dk4str8_dup(envpath, erp);
if (NULL != mycopy) {
p1 = mycopy;
while ((NULL != p1) && (0 != cc) && (0 == back)) {
p2 = dk4str8_chr(p1, ':');
if (NULL != p2) { *(p2++) = '\0'; }
if (0 != dk4str8_cpy_s(buffer, sz, p1, erp)) {
if (0 != dk4str8_cat_s(buffer, sz, dk4execpath_c8_kw[1], erp)) {
if (0 != dk4str8_cat_s(buffer, sz, argv0, erp)) {
if (0 != dk4stat_c8(&stbuf, buffer, NULL)) {
if (0 != dk4stat_is_regular(&stbuf, NULL)) {
back = 1;
}
} else { $? "! no such file"
}
} else {
cc = 0;
}
} else {
cc = 0;
}
} else {
cc = 0;
}
p1 = p2;
}
dk4mem_free(mycopy);
}
} else {
dk4error_set_simple_error_code(erp, DK4_E_NOT_FOUND);
}
return back;
}
#endif
#if !DK4_ON_WINDOWS
#if DK4_HAVE_PROC_PID_EXE
static const char * const dk4exep8_kw[] = {
/* 0 */ "/proc/",
/* 1 */ "/exe"
};
#endif
#endif
#if DK4_USE_PRAGMA_WARNING_DISABLE
#pragma warning( push )
#pragma warning( disable: 4100 )
#endif
int
dk4execpath_c8(
char *buffer,
size_t sz,
#if DK4_ON_WINDOWS
const char * DK4_ARG_UNUSED(argv0),
#else
const char *argv0,
#endif
dk4_er_t *erp
)
{
#if DK4_ON_WINDOWS
DWORD res;
DWORD dwsz;
#else
#if DK4_HAVE_GETEXECNAME
char cwd[DK4_MAX_PATH];
const char *enptr;
#endif
#if DK4_HAVE_PROC_PID_EXE
char numbuf[64];
char fnbuf[sizeof(numbuf)+11];
ssize_t rlres;
int res;
#endif
#endif
int back = 0;
#if DK4_ON_WINDOWS
DK4_UNUSED_ARG(argv0)
#endif
$? "+ dk4execpath_c8"
#if DK4_USE_ASSERT
assert(NULL != buffer);
assert(0 < sz);
#endif
if ((NULL != buffer) && (0 < sz)) {
#if DK4_ON_WINDOWS
if ((dk4_um_t)0xFFFFFFFFUL >= (dk4_um_t)sz) {
dwsz = (DWORD)sz;
} else {
dwsz = 0xFFFFFFFFUL;
}
res = GetModuleFileNameA(GetModuleHandle(NULL), buffer, dwsz);
if (0 < res) {
if ((dk4_um_t)res < (dk4_um_t)sz) {
buffer[res] = '\0';
back = 1;
} else {
dk4error_set_simple_error_code(erp, DK4_E_BUFFER_TOO_SMALL);
}
} else {
/* ERROR: Failed to find executable file name */
dk4error_set_simple_error_code(erp, DK4_E_NOT_FOUND);
}
#else
#if DK4_HAVE_GETEXECNAME
$? ". attempt getexecname"
#if 0
/* Prototype is const char *getexecname(void);
*/
if (0 == getexecname(buffer, sz)) {
back = 1; $? ". ok getexecname"
}
#endif
enptr = getexecname();
if (NULL != enptr) {
if ('/' == *enptr) {
if (strlen(enptr) < sz) {
strcpy(buffer, enptr);
back = 1;
} else {
dk4error_set_simple_error_code(erp, DK4_E_BUFFER_TOO_SMALL);
}
} else {
errno = 0;
if (NULL != getcwd(cwd, sizeof(cwd))) {
if ((strlen(cwd) + strlen(dk4execpath_c8_kw[1]) + strlen(enptr)) < sz)
{
strcpy(buffer, cwd);
strcat(buffer, dk4execpath_c8_kw[1]);
strcat(buffer, enptr);
back = 1;
}
else
{
dk4error_set_simple_error_code(erp, DK4_E_BUFFER_TOO_SMALL);
}
} else {
/* ERROR: Current directory not found */
dk4error_set_idetails(erp, DK4_E_SYSTEM, errno);
}
}
}
#endif
#if DK4_HAVE_PROC_PID_EXE
if (0 == back) { $? ". inspect /proc/pid/exe"
res = dk4ma_write_c8_decimal_unsigned(
numbuf, sizeof(numbuf), (dk4_um_t)getpid(), 0, NULL
);
if (0 != res) { $? ". number"
if (0 != dk4str8_cpy_s(fnbuf, sizeof(fnbuf), dk4exep8_kw[0], NULL)) {
$? ". cpy ok"
if (0 != dk4str8_cat_s(fnbuf, sizeof(fnbuf), numbuf, NULL)) {
$? ". cat ok"
if (0 != dk4str8_cat_s(fnbuf,sizeof(fnbuf),dk4exep8_kw[1],NULL)) {
$? ". cat ok \"%!8s\"", fnbuf
rlres = readlink(fnbuf, buffer, sz);
$? ". readlink -> %d", (int)rlres
if (0 < rlres) { $? ". readlink ok"
if ((size_t)rlres < sz) { $? ". path size ok"
buffer[rlres] = '\0'; $? ". ok /proc/pid/exe"
back = 1; $? ". file \"%!8s\"", buffer
}
}
}
}
}
}
}
#endif
if (0 == back) { $? ". inspect PATH"
if (NULL != argv0) {
back = dk4execpath_c8_inspect_path(buffer, sz, argv0, erp);
if (0 != back) { $? ". ok PATH"
}
} else {
dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
}
}
#endif
} else {
dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
} $? "- dk4execpath_c8 %d", back
return back;
}
#if DK4_USE_PRAGMA_WARNING_DISABLE
#pragma warning( pop )
#endif
|