source: git/Singular/find_exec.c @ 63be42

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