source: git/Singular/find_exec.c @ 64684ff

spielwiese
Last change on this file since 64684ff was 64684ff, checked in by Olaf Bachmann <obachman@…>, 24 years ago
* small bug fixes git-svn-id: file:///usr/local/Singular/svn/trunk@4296 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 5.5 KB
Line 
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
17#include <unistd.h> /* always defiend */
18#include <stdlib.h>
19#include <string.h>
20
21#if !defined(ESINGULAR) && !defined(TSINGULAR)
22#include "mmemory.h"
23#endif
24
25
26#ifndef MAXPATHLEN
27#define MAXPATHLEN 1024
28#endif
29
30/* Do not return copies of sth, but simply the strings
31   -- we make copies later */
32#define copy_of(string) mstrdup(string)
33
34/* ABSOLUTE_FILENAME_P (fname): True if fname is an absolute filename */
35#ifdef atarist
36#define ABSOLUTE_FILENAME_P(fname)        ((fname[0] == '/') || \
37        (fname[0] && (fname[1] == ':')))
38#else
39#define ABSOLUTE_FILENAME_P(fname)        (fname[0] == '/')
40#endif /* atarist */
41
42/* Return the absolute name of the program named NAME.  This function
43   searches the directories in the PATH environment variable if PROG
44   has no directory components. */
45#ifndef HAVE_READLINK
46char * find_executable (const char *name)
47#else
48char * find_executable_link (const char *name)
49#endif
50{
51  char *search;
52  char *p;
53#ifdef WINNT
54  char *extra = NULL;
55#endif
56  char tbuf[MAXPATHLEN];
57
58  if (ABSOLUTE_FILENAME_P(name))
59  {
60      /* If we can execute the named file then return it. */
61      if (! access (name, X_OK))
62        return copy_of (name);
63  }
64  else
65  {
66    if (((name[0] == '.') && (name[1] == '/')) ||
67        ((name[0] == '.') && (name[1] == '.') && (name[2] == '/')) ||
68        strchr(name, '/') != NULL)
69    {
70
71#ifdef HAVE_GETCWD
72      getcwd (tbuf, MAXPATHLEN);
73#else
74# ifdef HAVE_GETWD
75      getwd (tbuf);
76# endif
77#endif
78      strcat (tbuf, "/");
79      strcat (tbuf, name);
80      if (! access(name, X_OK))
81        return copy_of (tbuf);
82    }
83
84    search = getenv("PATH");
85/* for winnt under msdos, cwd is implictly in the path */
86#ifdef WINNT
87    p = getenv("SHELL");
88    if (p == NULL || strlen(p) < 2)
89    {
90      /* we are under msdos display */
91      extra = (char*) AllocL((search != NULL ? strlen(search) : 0) + 3);
92      strcpy(extra, ".:");
93      if (search != NULL) strcat(extra, search);
94      search = extra;
95    }
96#endif
97    p = search;
98
99    if (p != NULL)
100    {
101      while (1)
102      {
103        char *next;
104        next = tbuf;
105
106        /* Copy directory name into [tbuf]. */
107        /* This is somewhat tricky: empty names mean cwd, w.r.t. some
108           shell spec */
109        while (*p && *p != ':')
110          *next ++ = *p ++;
111        *next = '\0';
112
113        if ((tbuf[0] == '.' && tbuf[1] == '\0') || tbuf[0] == '\0') {
114#ifdef HAVE_GETCWD
115          getcwd (tbuf, MAXPATHLEN);
116#else
117# ifdef HAVE_GETWD
118          getwd (tbuf);
119# endif
120#endif
121        }
122
123        if (tbuf[strlen(tbuf)-1] != '/') strcat(tbuf, "/");
124        strcat (tbuf, name);
125       
126        /* If we can execute the named file, then return it. */
127        if (! access (tbuf, X_OK))
128        {
129#ifdef WINNT
130          if (extra != NULL)
131            FreeL(extra);
132#endif
133          return copy_of (tbuf);
134        }
135
136        if (*p != '\0')
137        {
138          p ++;
139        }
140        else
141        {
142          break;
143        }
144
145      }
146    }
147  }
148
149  return NULL;
150}
151
152#ifdef HAVE_READLINK
153
154/* similar to readlink, but dont' mess up absolute pathnames */
155int my_readlink(const char* name, char* buf, size_t bufsize)
156{
157  char buf2[MAXPATHLEN];
158  int ret;
159 
160  if ((ret = readlink(name, buf2, bufsize)) > 0)
161  {
162    buf2[ret] = 0;
163    if (*name == '/' && *buf2 != '/')
164    {
165      char* last = strrchr(name, '/');
166      int i = 0;
167      while (&(name[i]) != last)
168      {
169        buf[i] = name[i];
170        i++;
171      }
172      buf[i] = '/';
173      i++;
174      strcpy(&(buf[i]), buf2);
175      return i + ret;
176    }
177    else
178    {
179      strcpy(buf, buf2);
180    }
181  }
182  return ret;
183}
184
185#define MAX_LINK_LEVEL 10
186/* similar to readlink (cf. man readlink), except that symbolic links are
187   followed up to MAX_LINK_LEVEL
188*/
189
190int full_readlink(const char* name, char* buf, size_t bufsize)
191{
192  int ret;
193 
194  if ((ret=my_readlink(name, buf, bufsize)) > 0)
195  {
196    char buf2[MAXPATHLEN];
197    int ret2, i = 0;
198   
199    do
200    {
201      buf[ret] = '\0';
202      if ((ret2 = my_readlink(buf, buf2, MAXPATHLEN)) > 0)
203      {
204        i++;
205        buf2[ret2] = '\0';
206        strcpy(buf, buf2);
207        ret = ret2;
208      }
209      else
210      {
211        return ret;
212      }
213    }
214    while (i<MAX_LINK_LEVEL);
215  }
216  return -1;
217}
218 
219 
220char * find_executable (const char *name)
221{
222  char * link = find_executable_link(name);
223  char buf[MAXPATHLEN];
224  int ret;
225
226  if (link == NULL && (ret=full_readlink(name, buf, MAXPATHLEN)) > 0)
227  {
228    buf[ret] ='\0';
229    link = find_executable_link(buf);
230  }
231  if (link != NULL && (ret=full_readlink(link, buf, MAXPATHLEN)) > 0)
232  {
233    char *p = strrchr(link, '/');
234    char *executable;
235
236
237    if(p!=NULL) *(p+1)='\0';
238    buf[ret]='\0';
239
240    if (buf[0] != '/')
241    {
242      executable = (char*) AllocL(strlen(link) + ret + 1);
243      strcpy(executable, link);
244      strcat(executable, buf);
245    }
246    else
247    {
248      executable = copy_of(buf);
249    }
250
251    FreeL(link);
252    return executable;
253  }
254
255  return link;
256}
257#endif /* HAVE_READLINK */
258
259#else
260
261char* find_executable (const char *name)
262{
263  return NULL;
264}
265
266#endif /* #if ! defined(MSDOS) && ! defined(__MWERKS__) && defined(HAVE_SYS_TYPES_H) && defined(HAVE_PWD_H)&&defined(HAVE_SYS_PARAM_H) */
267
268
Note: See TracBrowser for help on using the repository browser.