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

fieker-DuValspielwiese
Last change on this file since 3a20c1 was 1caa72, checked in by Olaf Bachmann <obachman@…>, 26 years ago
1998-04-06 Olaf Bachmann <obachman@mathematik.uni-kl.de> * spSpolyLoop.h: neww calling interface for spGetSpolyLoop * kstd1.cc (kNF): moved strat->ak field initailization out of initBuchMora into single routines * febase.cc (feGetSearchPath): added feGetSearchPath; changed algorithm for searching files: $SINGULARPATH -> relative to executable -> burnt-in locations * added find_exec.c to get absolute pathname of executable git-svn-id: file:///usr/local/Singular/svn/trunk@1341 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 4.2 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 */
12
13#include "mod2.h"
14
15#if ! defined(MSDOS) && ! defined(macintosh) && \
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
34
35/* Do not return copies of sth, but simply the strings
36   -- we make copies later */
37#define copy_of(string) mstrdup(string)
38
39/* ABSOLUTE_FILENAME_P (fname): True if fname is an absolute filename */
40#ifdef atarist
41#define ABSOLUTE_FILENAME_P(fname)      ((fname[0] == '/') || \
42        (fname[0] && (fname[1] == ':')))
43#else
44#define ABSOLUTE_FILENAME_P(fname)      (fname[0] == '/')
45#endif /* atarist */
46
47/* Return the absolute name of the program named NAME.  This function
48   searches the directories in the PATH environment variable if PROG
49   has no directory components. */
50char *
51find_executable (const char *name)
52{
53  char *search;
54  char *p;
55  char tbuf[MAXPATHLEN];
56
57  if (ABSOLUTE_FILENAME_P(name)) {
58      /* If we can execute the named file, and it is normal, then return it. */
59      if (! access (name, X_OK)) {
60        struct stat stat_temp;
61
62        if (stat (name, &stat_temp))
63          return 0;
64
65#ifndef STAT_MACROS_BROKEN
66        if (! S_ISREG(stat_temp.st_mode))
67          return 0;
68#endif
69        return copy_of (name);
70      }
71    }
72  else {
73    if ((name[0] == '.') && (name[1] == '/')) {
74      strcpy (tbuf, ".");
75
76#ifdef HAVE_GETCWD
77      getcwd (tbuf, MAXPATHLEN);
78#else
79# ifdef HAVE_GETWD
80      getwd (tbuf);
81# endif
82#endif
83      strcat (tbuf, name + 1);
84      return copy_of (tbuf);
85    }
86
87    search = getenv ("PATH");
88    p = search;
89
90    while (*p) {
91      char *next;
92      next = tbuf;
93
94      /* Perform tilde-expansion. Stolen from GNU readline/tilde.c. */
95      if (p[0] == '~') {
96        if (! p[1] || p[1] == '/') {
97          /* Prepend $HOME to the rest of the string. */
98          char *temp_home = (char *) getenv ("HOME");
99
100          /* If there is no HOME variable, look up the directory in the
101             password database. */
102          if (! temp_home) {
103            struct passwd *entry;
104
105            entry = getpwuid (getuid ());
106            if (entry)
107              temp_home = entry->pw_dir;
108          }
109
110          strcpy (tbuf, temp_home);
111          next = tbuf + strlen (tbuf);
112          p ++;
113        }
114        else {
115          char username[MAXPATHLEN];
116          struct passwd *user_entry;
117          int i;
118
119          p ++;                 /* Skip the tilde. */
120          for (i = 0; *p && *p != '/' && *p != ':'; i++)
121            username[i] = *p ++;
122          username[i] = '\0';
123
124          user_entry = getpwnam (username);
125          if (user_entry) {
126            strcpy (tbuf, user_entry->pw_dir);
127            next = tbuf + strlen (tbuf);
128          }
129        }
130
131        endpwent ();
132      }
133
134      /* Copy directory name into [tbuf]. */
135      while (*p && *p != ':')
136        *next ++ = *p ++;
137      *next = '\0';
138      if (*p != '\0')
139        p ++;
140
141      if (tbuf[0] == '.' && tbuf[1] == '\0') {
142#ifdef HAVE_GETCWD
143        getcwd (tbuf, MAXPATHLEN);
144#else
145# ifdef HAVE_GETWD
146        getwd (tbuf);
147# endif
148#endif
149      }
150
151      strcat (tbuf, "/");
152      strcat (tbuf, name);
153
154      /* If we can execute the named file, and it is normal, then return it. */
155      if (! access (tbuf, X_OK)) {
156        struct stat stat_temp;
157
158        if (stat (tbuf, &stat_temp))
159          continue;
160
161#ifndef STAT_MACROS_BROKEN
162        if (! S_ISREG(stat_temp.st_mode))
163          continue;
164#endif
165        return copy_of (tbuf);
166      }
167    }
168  }
169
170  return NULL;
171}
172
173char* find_executable_path(const char *name)
174{
175  char *ef = find_executable(name);
176  char *temp, *temp2;
177 
178  temp2 = ef;
179  if (ef != NULL)
180  {
181    temp = NULL;
182    while (*ef != '\0')
183    {
184      if (*ef == '/') temp = ef;
185      ef++;
186    }
187    if (temp != NULL)
188    {
189      *temp = '\0';
190    }
191  }
192  return temp2;
193}
194
195 
196#else
197
198char* find_executable (const char *name)
199{
200  return NULL;
201}
202
203char* find_executable_path(const char *name)
204{
205  return NULL;
206}
207
208#endif /* #if ! defined(MSDOS) && ! defined(macintosh) && defined(HAVE_SYS_TYPES_H) && defined(HAVE_PWD_H)&&defined(HAVE_SYS_PARAM_H) */
209
210
Note: See TracBrowser for help on using the repository browser.