1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* $id:$ */ |
---|
5 | |
---|
6 | /* find_exec.c -- Find a program, given an argv[0] |
---|
7 | Copyright (C) 1996 Free Software Foundation, Inc. |
---|
8 | Copyright (C) 1990 by W. Wilson Ho. |
---|
9 | This file is part of the GNU Dld Library. */ |
---|
10 | |
---|
11 | /* Adapted for use with Singular by obachman@mathematik.uni-kl.de 4/98*/ |
---|
12 | |
---|
13 | #include "mod2.h" |
---|
14 | |
---|
15 | #if ! defined(MSDOS) && ! defined(__MWERKS__) && \ |
---|
16 | defined(HAVE_SYS_TYPES_H) && defined(HAVE_PWD_H)&&defined(HAVE_SYS_PARAM_H) |
---|
17 | |
---|
18 | #include <sys/types.h> |
---|
19 | #include <unistd.h> /* always defiend */ |
---|
20 | #include <pwd.h> |
---|
21 | |
---|
22 | #include <stdlib.h> |
---|
23 | #include <sys/stat.h> |
---|
24 | |
---|
25 | #ifdef HAVE_SYS_FILE_H |
---|
26 | # include <sys/file.h> /* can live without it */ |
---|
27 | #endif |
---|
28 | #include <sys/param.h> |
---|
29 | #include <strings.h> |
---|
30 | |
---|
31 | #include "mmemory.h" |
---|
32 | |
---|
33 | #ifndef MAXPATHLEN |
---|
34 | #define MAXPATHLEN 1024 |
---|
35 | #endif |
---|
36 | |
---|
37 | |
---|
38 | /* Do not return copies of sth, but simply the strings |
---|
39 | -- we make copies later */ |
---|
40 | #define copy_of(string) mstrdup(string) |
---|
41 | |
---|
42 | /* ABSOLUTE_FILENAME_P (fname): True if fname is an absolute filename */ |
---|
43 | #ifdef atarist |
---|
44 | #define ABSOLUTE_FILENAME_P(fname) ((fname[0] == '/') || \ |
---|
45 | (fname[0] && (fname[1] == ':'))) |
---|
46 | #else |
---|
47 | #define ABSOLUTE_FILENAME_P(fname) (fname[0] == '/') |
---|
48 | #endif /* atarist */ |
---|
49 | |
---|
50 | /* Return the absolute name of the program named NAME. This function |
---|
51 | searches the directories in the PATH environment variable if PROG |
---|
52 | has no directory components. */ |
---|
53 | #ifndef HAVE_READLINK |
---|
54 | char * find_executable (const char *name) |
---|
55 | #else |
---|
56 | char * find_executable_link (const char *name) |
---|
57 | #endif |
---|
58 | { |
---|
59 | char *search; |
---|
60 | char *p; |
---|
61 | char *extra = NULL; |
---|
62 | char tbuf[MAXPATHLEN]; |
---|
63 | |
---|
64 | if (ABSOLUTE_FILENAME_P(name)) { |
---|
65 | /* If we can execute the named file then return it. */ |
---|
66 | if (! access (name, X_OK)) |
---|
67 | return copy_of (name); |
---|
68 | } |
---|
69 | else { |
---|
70 | if (((name[0] == '.') && (name[1] == '/')) || |
---|
71 | ((name[0] == '.') && (name[1] == '.') && (name[2] == '/'))) { |
---|
72 | |
---|
73 | strcpy (tbuf, (name[1] == '.' ? ".." : ".")); |
---|
74 | |
---|
75 | #ifdef HAVE_GETCWD |
---|
76 | getcwd (tbuf, MAXPATHLEN); |
---|
77 | #else |
---|
78 | # ifdef HAVE_GETWD |
---|
79 | getwd (tbuf); |
---|
80 | # endif |
---|
81 | #endif |
---|
82 | strcat (tbuf, "/"); |
---|
83 | strcat (tbuf, name); |
---|
84 | return copy_of (tbuf); |
---|
85 | } |
---|
86 | |
---|
87 | |
---|
88 | search = getenv("PATH"); |
---|
89 | /* for winnt under msdos, cwd is implicetyly in the path */ |
---|
90 | #ifdef WINNT |
---|
91 | p = getenv("SHELL"); |
---|
92 | if (p == NULL || strlen(p < 2)) |
---|
93 | { |
---|
94 | /* we are under msdos display */ |
---|
95 | extra = (char*) AllocL((search != NULL ? strlen(search) : 0) + 3); |
---|
96 | sprintf(extra, ".:"); |
---|
97 | if (search != NULL) strcat(extra, search); |
---|
98 | search = extra; |
---|
99 | } |
---|
100 | #endif |
---|
101 | p = search; |
---|
102 | |
---|
103 | while (*p) { |
---|
104 | char *next; |
---|
105 | next = tbuf; |
---|
106 | |
---|
107 | /* Perform tilde-expansion. Stolen from GNU readline/tilde.c. */ |
---|
108 | if (p[0] == '~') { |
---|
109 | if (! p[1] || p[1] == '/') { |
---|
110 | /* Prepend $HOME to the rest of the string. */ |
---|
111 | char *temp_home = (char *) getenv ("HOME"); |
---|
112 | |
---|
113 | /* If there is no HOME variable, look up the directory in the |
---|
114 | password database. */ |
---|
115 | if (! temp_home) { |
---|
116 | struct passwd *entry; |
---|
117 | |
---|
118 | entry = getpwuid (getuid ()); |
---|
119 | if (entry) |
---|
120 | temp_home = entry->pw_dir; |
---|
121 | } |
---|
122 | |
---|
123 | strcpy (tbuf, temp_home); |
---|
124 | next = tbuf + strlen (tbuf); |
---|
125 | p ++; |
---|
126 | } |
---|
127 | else { |
---|
128 | char username[MAXPATHLEN]; |
---|
129 | struct passwd *user_entry; |
---|
130 | int i; |
---|
131 | |
---|
132 | p ++; /* Skip the tilde. */ |
---|
133 | for (i = 0; *p && *p != '/' && *p != ':'; i++) |
---|
134 | username[i] = *p ++; |
---|
135 | username[i] = '\0'; |
---|
136 | |
---|
137 | user_entry = getpwnam (username); |
---|
138 | if (user_entry) { |
---|
139 | strcpy (tbuf, user_entry->pw_dir); |
---|
140 | next = tbuf + strlen (tbuf); |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | endpwent (); |
---|
145 | } |
---|
146 | |
---|
147 | /* Copy directory name into [tbuf]. */ |
---|
148 | while (*p && *p != ':') |
---|
149 | *next ++ = *p ++; |
---|
150 | *next = '\0'; |
---|
151 | if (*p != '\0') |
---|
152 | p ++; |
---|
153 | |
---|
154 | if (tbuf[0] == '.' && tbuf[1] == '\0') { |
---|
155 | #ifdef HAVE_GETCWD |
---|
156 | getcwd (tbuf, MAXPATHLEN); |
---|
157 | #else |
---|
158 | # ifdef HAVE_GETWD |
---|
159 | getwd (tbuf); |
---|
160 | # endif |
---|
161 | #endif |
---|
162 | } |
---|
163 | |
---|
164 | strcat (tbuf, "/"); |
---|
165 | strcat (tbuf, name); |
---|
166 | |
---|
167 | /* If we can execute the named file, then return it. */ |
---|
168 | if (! access (tbuf, X_OK)) { |
---|
169 | #ifdef WINNT |
---|
170 | if (extra != NULL) |
---|
171 | FreeL(extra); |
---|
172 | #endif |
---|
173 | return copy_of (tbuf); |
---|
174 | } |
---|
175 | } |
---|
176 | } |
---|
177 | |
---|
178 | return NULL; |
---|
179 | } |
---|
180 | |
---|
181 | #ifdef HAVE_READLINK |
---|
182 | char * find_executable (const char *name) |
---|
183 | { |
---|
184 | char * link = find_executable_link(name); |
---|
185 | char buf[MAXPATHLEN]; |
---|
186 | int ret; |
---|
187 | |
---|
188 | if (link == NULL && (ret=readlink(name, buf, MAXPATHLEN)) > 0) |
---|
189 | { |
---|
190 | buf[ret] ='\0'; |
---|
191 | link = find_executable_link(buf); |
---|
192 | } |
---|
193 | if (link != NULL && (ret=readlink(link, buf, MAXPATHLEN)) > 0) |
---|
194 | { |
---|
195 | char *p = strrchr(link, '/'); |
---|
196 | char *executable; |
---|
197 | |
---|
198 | |
---|
199 | if(p!=NULL) *(p+1)='\0'; |
---|
200 | buf[ret]='\0'; |
---|
201 | |
---|
202 | if (buf[0] != '/') |
---|
203 | { |
---|
204 | executable = (char*) AllocL(strlen(link) + ret + 1); |
---|
205 | strcpy(executable, link); |
---|
206 | strcat(executable, buf); |
---|
207 | } |
---|
208 | else |
---|
209 | { |
---|
210 | executable = copy_of(buf); |
---|
211 | } |
---|
212 | |
---|
213 | FreeL(link); |
---|
214 | return executable; |
---|
215 | } |
---|
216 | |
---|
217 | return link; |
---|
218 | } |
---|
219 | #endif /* HAVE_READLINK */ |
---|
220 | |
---|
221 | #else |
---|
222 | |
---|
223 | char* find_executable (const char *name) |
---|
224 | { |
---|
225 | return NULL; |
---|
226 | } |
---|
227 | |
---|
228 | #endif /* #if ! defined(MSDOS) && ! defined(__MWERKS__) && defined(HAVE_SYS_TYPES_H) && defined(HAVE_PWD_H)&&defined(HAVE_SYS_PARAM_H) */ |
---|
229 | |
---|
230 | |
---|