summaryrefslogtreecommitdiff
path: root/support/dktools/dk4exep8.c
blob: e4368a621d505716927b0cab4f515e12d9763d20 (plain)
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
/*
	WARNING: This file was generated by dkct.
	Changes you make here will be lost if dkct is run again!
	You should modify the original source and run dkct on it.
	Original source: dk4exep8.ctr
*/

/*
Copyright (C) 2015-2017, Dirk Krause

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 opyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.
* Neither the name of the author nor the names of 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 dk4exep8.c The dk4exep8 module.
*/


#line 57 "dk4exep8.ctr"

#include "dk4exep8.h"

#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

#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





#line 142 "dk4exep8.ctr"



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;

  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 {	

#line 197 "dk4exep8.ctr"
	      }
	    } 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_ON_WINDOWS
#pragma warning (disable:4100)
#endif

int
dk4execpath_c8(char *buffer, size_t sz, const char *argv0, 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;
  

#line 255 "dk4exep8.ctr"
  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
    

#line 277 "dk4exep8.ctr"

#if 0
    /*	Prototype is const char *getexecname(void);
    */
    if (0 == getexecname(buffer, sz)) {
      back = 1;				

#line 283 "dk4exep8.ctr"
    }
#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) {	

#line 318 "dk4exep8.ctr"
      res = dk4ma_write_c8_decimal_unsigned(
        numbuf, sizeof(numbuf), (dk4_um_t)getpid(), 0, NULL
      );
      if (0 != res) {		

#line 322 "dk4exep8.ctr"
        if (0 != dk4str8_cpy_s(fnbuf, sizeof(fnbuf), dk4exep8_kw[0], NULL)) {
	  

#line 324 "dk4exep8.ctr"
	  if (0 != dk4str8_cat_s(fnbuf, sizeof(fnbuf), numbuf, NULL)) {
	    

#line 326 "dk4exep8.ctr"
	    if (0 != dk4str8_cat_s(fnbuf,sizeof(fnbuf),dk4exep8_kw[1],NULL)) {
	      

#line 328 "dk4exep8.ctr"
	      rlres = readlink(fnbuf, buffer, sz);
	      

#line 330 "dk4exep8.ctr"
	      if (0 < rlres) {		

#line 331 "dk4exep8.ctr"
	        if (rlres < sz) {	

#line 332 "dk4exep8.ctr"
		  buffer[rlres] = '\0';	

#line 333 "dk4exep8.ctr"
		  back = 1;		

#line 334 "dk4exep8.ctr"
		}
	      }
	    }
	  }
	}
      }
    }
#endif
    if (0 == back) {	

#line 343 "dk4exep8.ctr"
      if (NULL != argv0) {
        back = dk4execpath_c8_inspect_path(buffer, sz, argv0, erp);
        if (0 != back) {			

#line 346 "dk4exep8.ctr"
        }
      } else {
        dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
      }
    }
#endif
  } else {
    dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
  } 

#line 355 "dk4exep8.ctr"
  return back;
}

#if DK4_ON_WINDOWS
#pragma warning (default:4100)
#endif