source: git/Singular/find_exec.c @ 9235af

fieker-DuValspielwiese
Last change on this file since 9235af was c06a32, checked in by Olaf Bachmann <obachman@…>, 25 years ago
* New Handling of Command-line options git-svn-id: file:///usr/local/Singular/svn/trunk@3623 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
17#include <unistd.h> /* always defiend */
18#include <stdlib.h>
19#include <strings.h>
20
21#ifndef ESINGULAR
22#include "mmemory.h"
23#endif
24
25
26#ifndef MAXPATHLEN
27#define MAXPATHLEN 1024
28#endif
29
30
31/* Do not return copies of sth, but simply the strings
32   -- we make copies later */
33#define copy_of(string) mstrdup(string)
34
35/* ABSOLUTE_FILENAME_P (fname): True if fname is an absolute filename */
36#ifdef atarist
37#define ABSOLUTE_FILENAME_P(fname)        ((fname[0] == '/') || \
38        (fname[0] && (fname[1] == ':')))
39#else
40#define ABSOLUTE_FILENAME_P(fname)        (fname[0] == '/')
41#endif /* atarist */
42
43/* Return the absolute name of the program named NAME.  This function
44   searches the directories in the PATH environment variable if PROG
45   has no directory components. */
46#ifndef HAVE_READLINK
47char * find_executable (const char *name)
48#else
49char * find_executable_link (const char *name)
50#endif
51{
52  char *search;
53  char *p;
54  char *extra = NULL;
55  char tbuf[MAXPATHLEN];
56
57  if (ABSOLUTE_FILENAME_P(name))
58  {
59      /* If we can execute the named file then return it. */
60      if (! access (name, X_OK))
61        return copy_of (name);
62  }
63  else
64  {
65    if (((name[0] == '.') && (name[1] == '/')) ||
66        ((name[0] == '.') && (name[1] == '.') && (name[2] == '/')) ||
67        strchr(name, '/') != NULL)
68    {
69
70#ifdef HAVE_GETCWD
71      getcwd (tbuf, MAXPATHLEN);
72#else
73# ifdef HAVE_GETWD
74      getwd (tbuf);
75# endif
76#endif
77      strcat (tbuf, "/");
78      strcat (tbuf, name);
79      if (! access(name, X_OK))
80        return copy_of (tbuf);
81    }
82
83    search = getenv("PATH");
84/* for winnt under msdos, cwd is implictly in the path */
85#ifdef WINNT
86    p = getenv("SHELL");
87    if (p == NULL || strlen(p) < 2)
88    {
89      /* we are under msdos display */
90      extra = (char*) AllocL((search != NULL ? strlen(search) : 0) + 3);
91      strcpy(extra, ".:");
92      if (search != NULL) strcat(extra, search);
93      search = extra;
94    }
95#endif
96    p = search;
97
98    if (p != NULL)
99    {
100      while (1)
101      {
102        char *next;
103        next = tbuf;
104
105        /* Copy directory name into [tbuf]. */
106        /* This is somewhat tricky: empty names mean cwd, w.r.t. some
107           shell spec */
108        while (*p && *p != ':')
109          *next ++ = *p ++;
110        *next = '\0';
111
112        if ((tbuf[0] == '.' && tbuf[1] == '\0') || tbuf[0] == '\0') {
113#ifdef HAVE_GETCWD
114          getcwd (tbuf, MAXPATHLEN);
115#else
116# ifdef HAVE_GETWD
117          getwd (tbuf);
118# endif
119#endif
120        }
121
122        if (tbuf[strlen(tbuf)-1] != '/') strcat(tbuf, "/");
123        strcat (tbuf, name);
124       
125        /* If we can execute the named file, then return it. */
126        if (! access (tbuf, X_OK))
127        {
128#ifdef WINNT
129          if (extra != NULL)
130            FreeL(extra);
131#endif
132          return copy_of (tbuf);
133        }
134
135        if (*p != '\0')
136        {
137          p ++;
138        }
139        else
140        {
141          break;
142        }
143
144      }
145    }
146  }
147
148  return NULL;
149}
150
151#ifdef HAVE_READLINK
152char * find_executable (const char *name)
153{
154  char * link = find_executable_link(name);
155  char buf[MAXPATHLEN];
156  int ret;
157
158  if (link == NULL && (ret=readlink(name, buf, MAXPATHLEN)) > 0)
159  {
160    buf[ret] ='\0';
161    link = find_executable_link(buf);
162  }
163  if (link != NULL && (ret=readlink(link, buf, MAXPATHLEN)) > 0)
164  {
165    char *p = strrchr(link, '/');
166    char *executable;
167
168
169    if(p!=NULL) *(p+1)='\0';
170    buf[ret]='\0';
171
172    if (buf[0] != '/')
173    {
174      executable = (char*) AllocL(strlen(link) + ret + 1);
175      strcpy(executable, link);
176      strcat(executable, buf);
177    }
178    else
179    {
180      executable = copy_of(buf);
181    }
182
183    FreeL(link);
184    return executable;
185  }
186
187  return link;
188}
189#endif /* HAVE_READLINK */
190
191#else
192
193char* find_executable (const char *name)
194{
195  return NULL;
196}
197
198#endif /* #if ! defined(MSDOS) && ! defined(__MWERKS__) && defined(HAVE_SYS_TYPES_H) && defined(HAVE_PWD_H)&&defined(HAVE_SYS_PARAM_H) */
199
200
Note: See TracBrowser for help on using the repository browser.