source: git/Singular/find_exec.c @ ca8389

spielwiese
Last change on this file since ca8389 was ca8389, checked in by Jens Schmidt <schmidt@…>, 25 years ago
* find_exec.c (find_executable): bug fix. Recognizes `../Singular' properly. git-svn-id: file:///usr/local/Singular/svn/trunk@1637 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 4.4 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. */
53char *
54find_executable (const char *name)
55{
56  char *search;
57  char *p;
58  char tbuf[MAXPATHLEN];
59
60  if (ABSOLUTE_FILENAME_P(name)) {
61      /* If we can execute the named file, and it is normal, then return it. */
62      if (! access (name, X_OK)) {
63        struct stat stat_temp;
64
65        if (stat (name, &stat_temp))
66          return 0;
67
68#ifndef STAT_MACROS_BROKEN
69        if (! S_ISREG(stat_temp.st_mode))
70          return 0;
71#endif
72        return copy_of (name);
73      }
74    }
75  else {
76    if (((name[0] == '.') && (name[1] == '/')) || 
77        ((name[0] == '.') && (name[1] == '.') && (name[2] == '/'))) {
78
79      strcpy (tbuf, (name[1] == '.' ? ".." : "."));
80     
81#ifdef HAVE_GETCWD
82      getcwd (tbuf, MAXPATHLEN);
83#else
84# ifdef HAVE_GETWD
85      getwd (tbuf);
86# endif
87#endif
88      strcat (tbuf, "/");
89      strcat (tbuf, name);
90      return copy_of (tbuf);
91    }
92
93    search = getenv ("PATH");
94    p = search;
95
96    while (*p) {
97      char *next;
98      next = tbuf;
99
100      /* Perform tilde-expansion. Stolen from GNU readline/tilde.c. */
101      if (p[0] == '~') {
102        if (! p[1] || p[1] == '/') {
103          /* Prepend $HOME to the rest of the string. */
104          char *temp_home = (char *) getenv ("HOME");
105
106          /* If there is no HOME variable, look up the directory in the
107             password database. */
108          if (! temp_home) {
109            struct passwd *entry;
110
111            entry = getpwuid (getuid ());
112            if (entry)
113              temp_home = entry->pw_dir;
114          }
115
116          strcpy (tbuf, temp_home);
117          next = tbuf + strlen (tbuf);
118          p ++;
119        }
120        else {
121          char username[MAXPATHLEN];
122          struct passwd *user_entry;
123          int i;
124
125          p ++;                        /* Skip the tilde. */
126          for (i = 0; *p && *p != '/' && *p != ':'; i++)
127            username[i] = *p ++;
128          username[i] = '\0';
129
130          user_entry = getpwnam (username);
131          if (user_entry) {
132            strcpy (tbuf, user_entry->pw_dir);
133            next = tbuf + strlen (tbuf);
134          }
135        }
136
137        endpwent ();
138      }
139
140      /* Copy directory name into [tbuf]. */
141      while (*p && *p != ':')
142        *next ++ = *p ++;
143      *next = '\0';
144      if (*p != '\0')
145        p ++;
146
147      if (tbuf[0] == '.' && tbuf[1] == '\0') {
148#ifdef HAVE_GETCWD
149        getcwd (tbuf, MAXPATHLEN);
150#else
151# ifdef HAVE_GETWD
152        getwd (tbuf);
153# endif
154#endif
155      }
156
157      strcat (tbuf, "/");
158      strcat (tbuf, name);
159
160      /* If we can execute the named file, and it is normal, then return it. */
161      if (! access (tbuf, X_OK)) {
162        struct stat stat_temp;
163
164        if (stat (tbuf, &stat_temp))
165          continue;
166
167#ifndef STAT_MACROS_BROKEN
168        if (! S_ISREG(stat_temp.st_mode))
169          continue;
170#endif
171        return copy_of (tbuf);
172      }
173    }
174  }
175
176  return NULL;
177}
178
179#else
180
181char* find_executable (const char *name)
182{
183  return NULL;
184}
185
186#endif /* #if ! defined(MSDOS) && ! defined(__MWERKS__) && defined(HAVE_SYS_TYPES_H) && defined(HAVE_PWD_H)&&defined(HAVE_SYS_PARAM_H) */
187
188
Note: See TracBrowser for help on using the repository browser.