source: git/Singular/find_exec.c @ 445016

spielwiese
Last change on this file since 445016 was 445016, checked in by Olaf Bachmann <obachman@…>, 25 years ago
* bug fix git-svn-id: file:///usr/local/Singular/svn/trunk@1958 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 5.3 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    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
54char * find_executable (const char *name)
55#else
56char * 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
182char * 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  // follow, if we have a link
194  if (link != NULL && (ret=readlink(link, buf, MAXPATHLEN)) > 0)
195  {
196    char *p = strrchr(link, '/');
197    char *executable;
198
199   
200    if(p!=NULL) *(p+1)='\0';
201    buf[ret]='\0';
202   
203    if (buf[0] != '/')
204    {
205      executable = (char*) AllocL(strlen(link) + ret + 1);
206      strcpy(executable, link);
207      strcat(executable, buf);
208    }
209    else
210    {
211      executable = copy_of(buf);
212    }
213   
214    FreeL(link);
215    return executable;
216  }
217
218  return link;
219}
220#endif /* HAVE_READLINK */
221
222#else
223
224char* find_executable (const char *name)
225{
226  return NULL;
227}
228
229#endif /* #if ! defined(MSDOS) && ! defined(__MWERKS__) && defined(HAVE_SYS_TYPES_H) && defined(HAVE_PWD_H)&&defined(HAVE_SYS_PARAM_H) */
230
231
Note: See TracBrowser for help on using the repository browser.