My Project
Toggle main menu visibility
Loading...
Searching...
No Matches
resources
omFindExec.c
Go to the documentation of this file.
1
/*******************************************************************
2
* File: omFindExec.c
3
* Purpose: routine which determines absolute pathname of executable
4
* Author: obachman (Olaf Bachmann)
5
* Created: 11/99
6
*******************************************************************/
7
8
9
#include "singular_resourcesconfig.h"
10
11
12
#if defined(HAVE_UNISTD_H) && defined(STDC_HEADERS)
13
14
#ifdef HAVE_UNISTD_H
15
#include <unistd.h>
/* always defiend */
16
#endif
17
18
#include <stdlib.h>
19
#include <string.h>
20
21
#include "
omFindExec.h
"
22
23
#ifndef MAXPATHLEN
24
#define MAXPATHLEN 1024
25
#endif
26
27
/* ABSOLUTE_FILENAME_P (fname): True if fname is an absolute filename */
28
#define ABSOLUTE_FILENAME_P(fname) (fname[0] == '/')
29
30
/* Return the absolute name of the program named NAME. This function
31
searches the directories in the PATH environment variable if PROG
32
has no directory components. */
33
#ifndef HAVE_READLINK
34
char
*
omFindExec
(
const
char
*
name
,
char
* executable)
35
#else
36
static
char
* omFindExec_link (
const
char
*
name
,
char
* executable)
37
#endif
38
{
39
char
*
search
;
40
char
*
p
;
41
char
tbuf[
MAXPATHLEN
];
42
43
if
(ABSOLUTE_FILENAME_P(
name
))
44
{
45
/* If the named file exists then return it. */
46
if
(! access (
name
, F_OK))
//think of libSingular.so as main binary
47
// r or x is required
48
{
49
strcpy(executable,
name
);
50
return
executable;
51
}
52
}
53
else
54
{
55
if
(((
name
[0] ==
'.'
) && (
name
[1] ==
'/'
)) ||
56
((
name
[0] ==
'.'
) && (
name
[1] ==
'.'
) && (
name
[2] ==
'/'
)) ||
57
strchr(
name
,
'/'
) !=
NULL
)
58
{
59
short
ok=1;
60
61
#ifdef HAVE_GETCWD
62
if
(getcwd (tbuf,
MAXPATHLEN
)==
NULL
) ok=0;
63
#else
64
# ifdef HAVE_GETWD
65
if
(getwd (tbuf)==
NULL
) ok=0;
66
# endif
67
#endif
68
strcat (tbuf,
"/"
);
69
strcat (tbuf,
name
);
70
if
(ok && ! access(tbuf, F_OK))
71
{
72
strcpy(executable, tbuf);
73
return
executable;
74
}
75
}
76
77
78
search
=
getenv
(
"PATH"
);
79
/* for winnt under msdos, cwd is implictly in the path */
80
p
=
search
;
81
82
if
(
p
!=
NULL
)
83
{
84
while
(1)
85
{
86
char
*
next
;
87
next
= tbuf;
88
89
/* Copy directory name into [tbuf]. */
90
/* This is somewhat tricky: empty names mean cwd, w.r.t. some
91
shell spec */
92
while
(*
p
&& *
p
!=
':'
)
93
*
next
++ = *
p
++;
94
*
next
=
'\0'
;
95
96
if
((tbuf[0] ==
'.'
&& tbuf[1] ==
'\0'
) || tbuf[0] ==
'\0'
) {
97
#ifdef HAVE_GETCWD
98
if
(getcwd (tbuf,
MAXPATHLEN
) ==
NULL
)
99
goto
next_path_entry;
100
#else
101
# ifdef HAVE_GETWD
102
if
(getwd (tbuf) ==
NULL
)
103
goto
next_path_entry;
104
# endif
105
#endif
106
}
107
108
if
(tbuf[strlen(tbuf)-1] !=
'/'
) strcat(tbuf,
"/"
);
109
strcat (tbuf,
name
);
110
111
/* If the named file exists, then return it. */
112
if
(! access (tbuf, F_OK))
113
{
114
strcpy(executable, tbuf);
115
return
executable;
116
}
117
118
next_path_entry:
119
if
(*
p
!=
'\0'
)
120
{
121
p
++;
122
}
123
else
124
{
125
break
;
126
}
127
}
128
}
129
/* try again with LD_LIBRARY_PATH */
130
search
=
getenv
(
"LD_LIBRARY_PATH"
);
131
p
=
search
;
132
133
if
((
p
!=
NULL
)&&(strlen(
p
)>1))
134
{
135
while
(1)
136
{
137
char
*
next
;
138
next
= tbuf;
139
140
/* Copy directory name into [tbuf]. */
141
/* This is somewhat tricky: empty names mean cwd, w.r.t. some
142
shell spec */
143
while
(*
p
&& *
p
!=
':'
)
144
*
next
++ = *
p
++;
145
*
next
=
'\0'
;
146
147
if
(tbuf[strlen(tbuf)-1] !=
'/'
) strcat(tbuf,
"/"
);
148
strcat (tbuf,
name
);
149
150
/* If the named file exists, then return it. */
151
if
(! access (tbuf, F_OK))
152
{
153
strcpy(executable, tbuf);
154
return
executable;
155
}
156
157
if
(*
p
!=
'\0'
)
158
{
159
p
++;
160
}
161
else
162
{
163
break
;
164
}
165
}
166
}
167
}
168
/* everything failed, so try the compiled path: */
169
strcpy(tbuf,BIN_DIR);
170
strcat(tbuf,
"/"
);
171
strcat(tbuf,
name
);
172
/* If the named file exists, then return it. */
173
if
(! access (tbuf, F_OK))
174
{
175
strcpy(executable, tbuf);
176
return
executable;
177
}
178
strcpy(tbuf,LIB_DIR);
179
strcat(tbuf,
"/"
);
180
strcat(tbuf,
name
);
181
/* If the named file exists, then return it. */
182
if
(! access (tbuf, F_OK))
183
{
184
strcpy(executable, tbuf);
185
/* LIB_DIR is not reliable (may be set out of the Singular tree),
186
* so check also path for standard.lib*/
187
strcpy(tbuf,LIB_DIR);
188
strcat(tbuf,
"/../share/singular/LIB/standard.lib"
);
189
if
(! access (tbuf, R_OK))
190
return
executable;
191
}
192
return
NULL
;
193
}
194
195
#ifdef HAVE_READLINK
196
/* similar to readlink, but dont' mess up absolute pathnames */
197
static
int
my_readlink(
const
char
*
name
,
char
*
buf
,
size_t
bufsize)
198
{
199
char
buf2
[
MAXPATHLEN
];
200
int
ret;
201
202
if
((ret = readlink(
name
,
buf2
, bufsize)) > 0)
203
{
204
buf2
[ret] = 0;
205
if
(*
name
==
'/'
&& *
buf2
!=
'/'
)
206
{
207
char
*
last
= strrchr(
name
,
'/'
);
208
int
i
= 0;
209
while
(&(
name
[
i
]) !=
last
)
210
{
211
buf
[
i
] =
name
[
i
];
212
i
++;
213
}
214
buf
[
i
] =
'/'
;
215
i
++;
216
strcpy(&(
buf
[
i
]),
buf2
);
217
return
i
+ ret;
218
}
219
else
220
{
221
strcpy(
buf
,
buf2
);
222
}
223
}
224
return
ret;
225
}
226
227
#define MAX_LINK_LEVEL 10
228
/* similar to readlink (cf. man readlink), except that symbolic links are
229
followed up to MAX_LINK_LEVEL
230
*/
231
static
int
full_readlink(
const
char
*
name
,
char
*
buf
,
size_t
bufsize)
232
{
233
int
ret;
234
235
if
((ret=my_readlink(
name
,
buf
, bufsize)) > 0)
236
{
237
char
buf2
[
MAXPATHLEN
];
238
int
ret2,
i
= 0;
239
240
do
241
{
242
buf
[ret] =
'\0'
;
243
if
((ret2 = my_readlink(
buf
,
buf2
,
MAXPATHLEN
)) > 0)
244
{
245
i
++;
246
buf2
[ret2] =
'\0'
;
247
strcpy(
buf
,
buf2
);
248
ret = ret2;
249
}
250
else
251
{
252
return
ret;
253
}
254
}
255
while
(
i
<MAX_LINK_LEVEL);
256
}
257
return
-1;
258
}
259
260
#ifdef __CYGWIN__
261
/* for windows, serch first for .exe */
262
char
* _omFindExec (
const
char
*
name
,
char
* exec);
263
char
*
omFindExec
(
const
char
*
name
,
char
* exec)
264
{
265
266
if
(strstr(
name
,
".exe"
) ==
NULL
)
267
{
268
char
buf
[
MAXPATHLEN
];
269
char
* ret;
270
strcpy(
buf
,
name
);
271
strcat(
buf
,
".exe"
);
272
ret = _omFindExec(
buf
, exec);
273
if
(ret !=
NULL
)
return
ret;
274
}
275
return
_omFindExec(
name
, exec);
276
}
277
#else
278
#define _omFindExec omFindExec
279
#endif
280
281
char
* _omFindExec (
const
char
*
name
,
char
* exec)
282
{
283
char
* link = omFindExec_link(
name
, exec);
284
char
buf
[
MAXPATHLEN
];
285
int
ret;
286
287
if
(link ==
NULL
&& (ret=full_readlink(
name
,
buf
,
MAXPATHLEN
)) > 0)
288
{
289
buf
[ret] =
'\0'
;
290
link = omFindExec_link(
buf
, exec);
291
}
292
if
(link !=
NULL
&& (ret=full_readlink(link,
buf
,
MAXPATHLEN
)) > 0)
293
{
294
char
*
p
= strrchr(link,
'/'
);
295
296
297
if
(
p
!=
NULL
) *(
p
+1)=
'\0'
;
298
buf
[ret]=
'\0'
;
299
300
if
(
buf
[0] !=
'/'
)
301
{
302
strcpy(exec, link);
303
strcat(exec,
buf
);
304
}
305
else
306
{
307
strcpy(exec,
buf
);
308
}
309
310
return
exec;
311
}
312
return
link;
313
}
314
#endif
/* HAVE_READLINK */
315
316
#else
317
318
char
*
omFindExec
(
const
char
*
name
,
char
* exec)
319
{
320
return
name
;
321
}
322
323
#endif
/* defined(HAVE_UNISTD_H) && defined(STDC_HEADERS) */
i
int i
Definition
cfEzgcd.cc:132
p
int p
Definition
cfModGcd.cc:4086
buf2
CanonicalForm buf2
Definition
facFqBivar.cc:76
search
int search(const CFArray &A, const CanonicalForm &F, int i, int j)
search for F in A between index i and j
Definition
facSparseHensel.h:566
getenv
char * getenv()
last
STATIC_VAR poly last
Definition
hdegree.cc:1138
next
ListNode * next
Definition
janet.h:31
omFindExec
char * omFindExec(const char *name, char *exec)
Definition
omFindExec.c:318
omFindExec.h
NULL
#define NULL
Definition
omList.c:12
MAXPATHLEN
#define MAXPATHLEN
Definition
omRet2Info.c:22
buf
int status int void * buf
Definition
si_signals.h:69
name
int name
New type name for int.
Definition
templateForC.h:21
Generated on
for My Project by
doxygen 1.17.0
for
Singular