source: git/Singular/find_exec.c @ c702d2

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