-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathplatform.c
More file actions
485 lines (369 loc) · 10.4 KB
/
platform.c
File metadata and controls
485 lines (369 loc) · 10.4 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
// ***************************************************************************
//
// platform.c
// platform-dependent path handling and supplemental string library routines
// platform_startup() is a place for any conditional code required by platform
//
// ***************************************************************************
#include "platform.h"
#if defined(MAC)
#include "macprefs.h"
#include "macstartup.h"
#endif
// ***************************************************************
// platform_startup() for any conditional code required by platform
// ***************************************************************
int platform_startup(int *argcp, char ***argv)
{
#if defined(MAC)
return macStartup(argcp, argv);
#endif
}
// ***************************************************************
// strdup - malloc space and copy the string
// ***************************************************************
#if LACKS_STRDUP
char * strdup(const char *str)
{
char *newstr;
if (str == NULL)
{
return NULL;
}
newstr = malloc(strlen(str) + 1);
strcpy(newstr, str);
return newstr;
}
#endif
// NOTE: we could use strcasecmp() and strncasecmp() in linux.
// ***************************************************************
// stricmp - case insensitive strcmp()
// ***************************************************************
#if LACKS_STRICMP
int stricmp(const char *str1, const char *str2)
{
const unsigned char * ptr1 = (unsigned char *) str1;
const unsigned char * ptr2 = (unsigned char *) str2;
unsigned char c1, c2;
while ((c1 = toupper(*ptr1++)) == (c2 = toupper(*ptr2++)))
{
if (!c1)
{
return(0); // end of both strings reached, so they match
}
}
// first non-matching char was reached, including possibly 0 on one or the other string
return(c1 - c2);
}
#endif
// ***************************************************************
// strnicmp - case insensitive strncmp()
// ***************************************************************
#if LACKS_STRNICMP
int strnicmp(const char *str1, const char *str2, size_t n)
{
const unsigned char * ptr1 = (unsigned char *) str1;
const unsigned char * ptr2 = (unsigned char *) str2;
unsigned char c1, c2;
while (--n)
{
if ((c1 = toupper(*ptr1++)) != (c2 = toupper(*ptr2++)))
{
return (c1-c2);
}
else if (!c1)
{
break;
}
}
return(0); // went n chars with no mismatch
}
#endif
// ***********************************************************************
// dirname - return the part of a pathname that is the directory (or ./)
// ***********************************************************************
#if LACKS_DIRNAME
char *dirname( const char *filepath )
{
char *tmpstr, *ptr;
int i;
if (filepath == NULL)
{
return NULL;
}
#if defined(MAC)
if ( (ptr = strrchr(filepath, ':')) )
#elif defined(WINDOWS)
if ( (ptr = strrchr(filepath, '\\')) || (ptr = strrchr(filepath, '/')) )
#elif defined(MACOS_X)
if ( (ptr = strrchr(filepath, '/')) || (ptr = strrchr(filepath, ':')) )
#elif defined(UNIX)
if ( (ptr = strrchr(filepath, '/')) )
#else
#error ambiguous platform in dirname() definition
#endif
{
i = ptr - filepath;
tmpstr = malloc(i+1);
strncpy(tmpstr, filepath, i); // do not include trailing separator
tmpstr[i] = 0;
}
else
{
#if defined(MAC)
tmpstr = strdup("");
#elif defined(WINDOWS)
tmpstr = strdup(".\\");
#elif defined(UNIX)
tmpstr = strdup("./");
#else
#error unspecified platform in dirname() definition
#endif
}
return tmpstr;
}
#endif
// ********************************************************************
// basename - return the part of a pathname that is NOT the directory
// ********************************************************************
#if LACKS_BASENAME
char *basename( const char *filepath )
{
char *tmpstr, *ptr;
if (filepath == NULL)
{
return NULL;
}
#if defined(MAC)
if ( (ptr = strrchr(filepath, ':')) )
#elif defined(WINDOWS)
if ( (ptr = strrchr(filepath, '\\')) || (ptr = strrchr(filepath, '/')) )
#elif defined(MACOS_X)
if ( (ptr = strrchr(filepath, '/')) || (ptr = strrchr(filepath, ':')) )
#elif defined(UNIX)
if ( (ptr = strrchr(filepath, '/')) )
#else
#error unspecified platform in basename() definition
#endif
{
// If there isn't anything after the last separator, the result is a 0-length string
tmpstr = malloc(strlen(ptr+1)+1);
tmpstr = strdup(ptr+1);
}
else
{
// dup the string, so caller can safely free whatever we return
tmpstr = strdup(filepath);
}
return tmpstr;
}
#endif
// ********************************************************************
// strlastchar - return last char of string (convenience function)
// ********************************************************************
char strlastchar(const char *str1)
{
int i;
i = strlen(str1);
if (i>0) { i--; }
return str1[i];
}
// ***************************************************************************
// localize_path - change embedded directory separators from / or \\
// ***************************************************************************
char *
localize_path(char *inoutPath)
{
#if defined(UNIX)
char separator = '/';
#elif defined(MAC)
char separator = ':';
#elif defined(WINDOWS)
char separator = '\\';
#else
#error unspecified platform in localize_path() definition
#endif
int i;
for(i=0; i<strlen(inoutPath); i++)
{
/* Localize Directory Separators */
if ((inoutPath[i] == '/')
|| (inoutPath[i] == '\\')
#if defined(MACOS_X)
|| (inoutPath[i] == ':')
#endif
)
{
inoutPath[i] = separator;
}
}
return inoutPath;
}
// ******************************************************************************
// concat_path - concatenate path1 and path2 with directory separator if needed
// ******************************************************************************
char *concat_path(const char *path1, const char *path2, char *result)
{
char *ptr;
int i;
if (!result)
{
result = malloc(strlen(path1)+strlen(path2)+2);
}
ptr = result;
ptr[0] = 0;
strcat(ptr, path1); ptr += strlen(ptr);
#if defined(MAC)
if (strlastchar(path1) != ':')
{
strcat(ptr, ":"); ptr += strlen(ptr);
}
#elif defined(MACOS_X)
if ((strlastchar(path1) != '/') && (strlastchar(path1) != ':'))
{
strcat(ptr, "/"); ptr += strlen(ptr);
}
#elif defined(UNIX)
if (strlastchar(path1) != '/')
{
strcat(ptr, "/"); ptr += strlen(ptr);
}
#elif defined(WINDOWS)
if ((strlastchar(path1) != '/') && (strlastchar(path1) != '\\'))
{
strcat(ptr, "\\"); ptr += strlen(ptr);
}
#else
#error unspecified platform in format_path() definition
#endif
strcat(ptr, path2); ptr += strlen(ptr);
localize_path(result); // Just in case of embedded separators.
// Subparts use embedded \s and hires primitives use embedded \48
return result;
}
// ********************************************************************************
// platform_getenv - wrapper for getenv, in case we want to make a nice interface
// ********************************************************************************
char *platform_getenv(const char *var)
{
#if defined(UNIX)
return getenv(var);
#elif defined(MAC)
return macprefs_getenv(var);
#elif defined(WINDOWS)
return getenv(var);
#else
#error unspecified platform in platform_getenv() definition
#endif
}
/***************************************************************/
#include <stdio.h>
void platform_comment(char *message, int level)
{
// Handy place to parse new META commands unhandled by ldlite parser.
extern int ldlite_parse_colour_meta(char *s);
// Intercept the ldconfig.ldr !COLOUR meta command.
if (ldlite_parse_colour_meta(message))
return;
if (level == 0)
printf("comment %s\n", message);
}
/***************************************************************/
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/types.h>
#if defined(WINDOWS)
# include <windows.h>
#endif
#if defined(UNIX)
#include <sys/param.h>
#include <limits.h> // For realpath() on Red Hat 9.
#define PATHSIZE MAXPATHLEN
#else
#ifndef MAXNAMLEN
#define MAXNAMLEN 256
#endif
#define PATHSIZE MAXNAMLEN
#endif
/***********************************************************************/
long get_file_mode(char *path)
{
struct stat stats;
if ( stat( path, &stats) < 0 )
return ( -1 );
return ( stats.st_mode );
}
/***********************************************************************/
/* Strip off first element of path into buf. Returns new path. */
char *getPathElement(char *path, char *buf)
{
while ( (*buf = *path++) != '\0' ) {
if (*buf == ':') {
*buf = '\0';
return(path);
} ++buf;
} return(0);
}
/***********************************************************************/
int GetExecName(char *argv0, char *buf, int buflen)
{
char *path;
char *getenv();
#ifdef WINDOWS
GetModuleFileName(NULL, buf, buflen);
return(0);
#else
// getexecname() on Solaris??
char tmpbuf[PATHSIZE];
// Handle absolute paths. Easy!
if (*argv0 == '/') {
strcpy( buf, argv0 );
return(0);
}
// Handle relative paths. Almost easy. Work off of getcwd()
if (*argv0 == '.' && *(argv0+1) == '/') {
strcpy(buf, argv0 + 2);
goto found;
}
if (*argv0 == '.' && *(argv0+1) == '.' && *(argv0+2) == '/' ) {
strcpy(buf, argv0);
goto found;
}
// Search for the executable in the PATH environment variable.
path = getenv("PATH");
while(path != 0) {
path = getPathElement(path, buf);
if (*buf == '\0')
continue; /* ignore empty components (::) */
strcat(buf, "/");
strcat(buf, argv0);
if (access(buf, X_OK) == 0) {
/* watch out for directories that happen to match */
if((get_file_mode(buf) & S_IFMT) == S_IFDIR) continue;
/* Got it. Now if it is not absolute, just prepend cwd */
if (*buf != '/') {
found:
strncpy(tmpbuf, buf, PATHSIZE/sizeof(char));
#if 0
if ( getcwd(buf, (PATHSIZE/sizeof(char))-1) == 0 ) break;
strncat(buf, "/", PATHSIZE/sizeof(char));
strncat( buf, tmpbuf, PATHSIZE/sizeof(char));
#else
if ( getcwd(buf, buflen-1) == 0 ) break;
strncat(buf, "/", buflen);
strncat( buf, tmpbuf, buflen);
#endif
if ( realpath(buf, tmpbuf) == NULL)
return 1;
//strncpy(buf, tmpbuf, PATHSIZE/sizeof(char));
strncpy(buf, tmpbuf, buflen);
return 2;
}
return(0);
}
}
/* Not found */
*buf = 0;
return(-1);
#endif
}