source: git/Singular/find_exec.c @ cc6452

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