source: git/Singular/find_exec.c @ 3b82fb

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