My Project
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
34char * omFindExec (const char *name, char* executable)
35#else
36static 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
60#ifdef HAVE_GETCWD
61 getcwd (tbuf, MAXPATHLEN);
62#else
63# ifdef HAVE_GETWD
64 getwd (tbuf);
65# endif
66#endif
67 strcat (tbuf, "/");
68 strcat (tbuf, name);
69 if (! access(tbuf, F_OK))
70 {
71 strcpy(executable, tbuf);
72 return executable;
73 }
74 }
75
76
77 search = getenv("PATH");
78/* for winnt under msdos, cwd is implictly in the path */
79 p = search;
80
81 if (p != NULL)
82 {
83 while (1)
84 {
85 char *next;
86 next = tbuf;
87
88 /* Copy directory name into [tbuf]. */
89 /* This is somewhat tricky: empty names mean cwd, w.r.t. some
90 shell spec */
91 while (*p && *p != ':')
92 *next ++ = *p ++;
93 *next = '\0';
94
95 if ((tbuf[0] == '.' && tbuf[1] == '\0') || tbuf[0] == '\0') {
96#ifdef HAVE_GETCWD
97 getcwd (tbuf, MAXPATHLEN);
98#else
99# ifdef HAVE_GETWD
100 getwd (tbuf);
101# endif
102#endif
103 }
104
105 if (tbuf[strlen(tbuf)-1] != '/') strcat(tbuf, "/");
106 strcat (tbuf, name);
107
108 /* If the named file exists, then return it. */
109 if (! access (tbuf, F_OK))
110 {
111 strcpy(executable, tbuf);
112 return executable;
113 }
114
115 if (*p != '\0')
116 {
117 p ++;
118 }
119 else
120 {
121 break;
122 }
123 }
124 }
125 /* try again with LD_LIBRARY_PATH */
126 search = getenv("LD_LIBRARY_PATH");
127 p = search;
128
129 if ((p != NULL)&&(strlen(p)>1))
130 {
131 while (1)
132 {
133 char *next;
134 next = tbuf;
135
136 /* Copy directory name into [tbuf]. */
137 /* This is somewhat tricky: empty names mean cwd, w.r.t. some
138 shell spec */
139 while (*p && *p != ':')
140 *next ++ = *p ++;
141 *next = '\0';
142
143 if (tbuf[strlen(tbuf)-1] != '/') strcat(tbuf, "/");
144 strcat (tbuf, name);
145
146 /* If the named file exists, then return it. */
147 if (! access (tbuf, F_OK))
148 {
149 strcpy(executable, tbuf);
150 return executable;
151 }
152
153 if (*p != '\0')
154 {
155 p ++;
156 }
157 else
158 {
159 break;
160 }
161 }
162 }
163 }
164 /* everything failed, so try the compiled path: */
165 strcpy(tbuf,BIN_DIR);
166 strcat(tbuf,"/");
167 strcat(tbuf,name);
168 /* If the named file exists, then return it. */
169 if (! access (tbuf, F_OK))
170 {
171 strcpy(executable, tbuf);
172 return executable;
173 }
174 strcpy(tbuf,LIB_DIR);
175 strcat(tbuf,"/");
176 strcat(tbuf,name);
177 /* If the named file exists, then return it. */
178 if (! access (tbuf, F_OK))
179 {
180 strcpy(executable, tbuf);
181 /* LIB_DIR is not reliable (may be set out of the Singular tree),
182 * so check also path for standard.lib*/
183 strcpy(tbuf,LIB_DIR);
184 strcat(tbuf,"/../share/singular/LIB/standard.lib");
185 if (! access (tbuf, R_OK))
186 return executable;
187 }
188 return NULL;
189}
190
191#ifdef HAVE_READLINK
192/* similar to readlink, but dont' mess up absolute pathnames */
193static int my_readlink(const char* name, char* buf, size_t bufsize)
194{
195 char buf2[MAXPATHLEN];
196 int ret;
197
198 if ((ret = readlink(name, buf2, bufsize)) > 0)
199 {
200 buf2[ret] = 0;
201 if (*name == '/' && *buf2 != '/')
202 {
203 char* last = strrchr(name, '/');
204 int i = 0;
205 while (&(name[i]) != last)
206 {
207 buf[i] = name[i];
208 i++;
209 }
210 buf[i] = '/';
211 i++;
212 strcpy(&(buf[i]), buf2);
213 return i + ret;
214 }
215 else
216 {
217 strcpy(buf, buf2);
218 }
219 }
220 return ret;
221}
222
223#define MAX_LINK_LEVEL 10
224/* similar to readlink (cf. man readlink), except that symbolic links are
225 followed up to MAX_LINK_LEVEL
226*/
227static int full_readlink(const char* name, char* buf, size_t bufsize)
228{
229 int ret;
230
231 if ((ret=my_readlink(name, buf, bufsize)) > 0)
232 {
233 char buf2[MAXPATHLEN];
234 int ret2, i = 0;
235
236 do
237 {
238 buf[ret] = '\0';
239 if ((ret2 = my_readlink(buf, buf2, MAXPATHLEN)) > 0)
240 {
241 i++;
242 buf2[ret2] = '\0';
243 strcpy(buf, buf2);
244 ret = ret2;
245 }
246 else
247 {
248 return ret;
249 }
250 }
251 while (i<MAX_LINK_LEVEL);
252 }
253 return -1;
254}
255
256#ifdef __CYGWIN__
257/* for windows, serch first for .exe */
258char * _omFindExec (const char *name, char* exec);
259char* omFindExec(const char *name, char* exec)
260{
261
262 if (strstr(name, ".exe") == NULL)
263 {
264 char buf[MAXPATHLEN];
265 char* ret;
266 strcpy(buf, name);
267 strcat(buf, ".exe");
268 ret = _omFindExec(buf, exec);
269 if (ret != NULL) return ret;
270 }
271 return _omFindExec(name, exec);
272}
273#else
274#define _omFindExec omFindExec
275#endif
276
277char * _omFindExec (const char *name, char* exec)
278{
279 char * link = omFindExec_link(name, exec);
280 char buf[MAXPATHLEN];
281 int ret;
282
283 if (link == NULL && (ret=full_readlink(name, buf, MAXPATHLEN)) > 0)
284 {
285 buf[ret] ='\0';
286 link = omFindExec_link(buf, exec);
287 }
288 if (link != NULL && (ret=full_readlink(link, buf, MAXPATHLEN)) > 0)
289 {
290 char *p = strrchr(link, '/');
291
292
293 if(p!=NULL) *(p+1)='\0';
294 buf[ret]='\0';
295
296 if (buf[0] != '/')
297 {
298 strcpy(exec, link);
299 strcat(exec, buf);
300 }
301 else
302 {
303 strcpy(exec, buf);
304 }
305
306 return exec;
307 }
308 return link;
309}
310#endif /* HAVE_READLINK */
311
312#else
313
314char* omFindExec (const char *name, char* exec)
315{
316 return name;
317}
318
319#endif /* defined(HAVE_UNISTD_H) && defined(STDC_HEADERS) */
int i
Definition: cfEzgcd.cc:132
int p
Definition: cfModGcd.cc:4078
CanonicalForm buf2
Definition: facFqBivar.cc:73
int search(const CFArray &A, const CanonicalForm &F, int i, int j)
search for F in A between index i and j
char * getenv()
STATIC_VAR poly last
Definition: hdegree.cc:1151
ListNode * next
Definition: janet.h:31
char * omFindExec(const char *name, char *exec)
Definition: omFindExec.c:314
#define NULL
Definition: omList.c:12
#define MAXPATHLEN
Definition: omRet2Info.c:22
int status int void * buf
Definition: si_signals.h:59
char name(const Variable &v)
Definition: variable.h:95