aboutsummaryrefslogtreecommitdiffstats
path: root/findexec.c
blob: a56207798f575ebb97705c83349355c1cf190ed8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/* "findexec.c" was part of DLD, a dynamic link/unlink editor for C.
 * Copyright (C) 1990 by W. Wilson Ho.
 *
 * GNU Emacs is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * GNU Emacs is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with GNU Emacs.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

/* The author can be reached electronically by how@cs.ucdavis.edu or
   through physical mail at:

   W. Wilson Ho
   Division of Computer Science
   University of California at Davis
   Davis, CA 95616

Fri Sep 14 22:16:14 1990  Edgar Roeder  (edgar at megamaster)

	* added a separate DLDPATH environment variable in
	dld_find_executable so that users may specify a special path
	for object modules.

Thu Feb  3 01:46:16 1994  Aubrey Jaffer

	* find_exec.c (dld_find_executable): added stat check for
	linux so that it doesn't think directories with the same name
	as the program are executable.

Wed Feb 21 23:06:35 1996  Aubrey Jaffer

	* find_exec.c: extracted for general use.  Generalized to
	MS-DOS.  */

/* Given a filename, dld_find_executable searches the directories
   listed in the environment variable PATH for a file with that
   filename.  A new copy of the complete path name of that file is
   returned.  This new string may be disposed by free() later on.  */

#ifndef __MINGW32__
# ifndef PLAN9
#  include <sys/file.h>
#  include <sys/param.h>
# endif
# ifdef linux
#  include <string.h>
#  include <stdlib.h>
#  include <sys/stat.h>
#  include <unistd.h>     /* for X_OK define */
# endif
# ifdef __SVR4
#  include <string.h>
#  include <stdlib.h>
#  include <sys/stat.h>
#  include <unistd.h>     /* for X_OK define */
# else
#  ifdef __sgi__
#   include <string.h>
#   include <stdlib.h>
#   include <sys/stat.h>
#   include <unistd.h>     /* for X_OK define */
#  else
#   ifdef PLAN9
#    include <u.h>
#    include <libc.h>
#    define getcwd getwd
#    define MAXPATHLEN 256	/* arbitrary? */
#    define X_OK AEXEC
#   else
#    include <strings.h>
#   endif
#  endif
# endif
# ifdef __amigaos__
#  include <stdlib.h>
#  include <sys/stat.h>
#  include <unistd.h>
# endif
# ifndef __STDC__
#  define const /**/
# endif
# ifdef __FreeBSD__
/* This might be same for 44bsd derived system. */
#  include <stdlib.h>
#  include <string.h>
#  include <unistd.h>
#  include <sys/stat.h>
# endif
# ifdef __DragonflyBSD__
/* This might be same for 44bsd derived system. */
#  include <stdlib.h>
#  include <string.h>
#  include <unistd.h>
#  include <sys/stat.h>
# endif
# ifdef __NetBSD__
#  include <stdlib.h>
#  include <string.h>
#  include <unistd.h>
#  include <sys/stat.h>
# endif
# ifdef __OpenBSD__
/* This might be same for 44bsd derived system. */
#  include <stdlib.h>
#  include <unistd.h>
#  include <string.h>
/* #  include <sys/types.h> */
#  include <sys/stat.h>
# endif
# ifdef __alpha
#  include <string.h>
#  include <stdlib.h>
#  include <sys/types.h>
#  include <sys/stat.h>
# endif
# ifdef GO32
#  include <sys/stat.h>
# endif

# ifndef DEFAULT_PATH
#  define DEFAULT_PATH ".:~/bin::/usr/local/bin:/usr/new:/usr/ucb:/usr/bin:/bin:/usr/hosts"
# endif

static char *copy_of(s)
     register const char *s;
{
  register char *p = (char *) malloc(strlen(s)+1);
  if (!p) return 0;
  *p = 0;
  strcpy(p, s);
  return p;
}

/* ABSOLUTE_FILENAME_P(fname): True if fname is an absolute filename */
# ifdef atarist
#  define ABSOLUTE_FILENAME_P(fname)	((fname[0] == '/') || \
	(fname[0] && (fname[1] == ':')))
# else
#  define ABSOLUTE_FILENAME_P(fname)	(fname[0] == '/')
# endif /* atarist */

char *dld_find_executable(name)
     const char *name;
{
  char *search;
  register char *p;
  char tbuf[MAXPATHLEN];

  if (ABSOLUTE_FILENAME_P(name))
    return access(name, X_OK) ? 0 : copy_of(name);

  if (strchr(name, '/')) {
    strcpy (tbuf, ".");		/* in case getcwd doesn't work */
    getcwd(tbuf, MAXPATHLEN);
    if ((name[0] == '.') && (name[1] == '/')) {
      strcat(tbuf, name + 1);
    } else {
      if ('/' != tbuf[strlen(tbuf) - 1]) strcat(tbuf, "/");
      strcat(tbuf, name);
    }
    return copy_of(tbuf);
  }

  if (((search = (char *) getenv("DLDPATH")) == 0) &&
      ((search = (char *) getenv("PATH")) == 0))
    search = DEFAULT_PATH;

  p = search;

  while (*p) {
    register char *next = tbuf;

    if (p[0]=='~' && p[1]=='/' && getenv("HOME")) {
      strcpy(tbuf, (char *)getenv("HOME"));
      next = tbuf + strlen(tbuf);
      p++;
    }

    /* Copy directory name into [tbuf] */
    while (*p && *p != ':') *next++ = *p++;
    *next = 0;
    if (*p) p++;

    if (tbuf[0] == '.' && tbuf[1] == 0)
      getcwd(tbuf, MAXPATHLEN);	/* was getwd(tbuf); */
    else if (tbuf[0]=='~' && tbuf[1]==0 && getenv("HOME"))
      strcpy(tbuf, (char *)getenv("HOME"));

    strcat(tbuf, "/");
    strcat(tbuf, name);

    if (access(tbuf, X_OK) == 0) {
# ifndef hpux
#  ifndef ultrix
#   ifndef __MACH__
#    ifndef PLAN9
      struct stat stat_temp;
      if (stat(tbuf, &stat_temp)) continue;
      if (S_IFREG != (S_IFMT & stat_temp.st_mode)) continue;
#    endif /* PLAN9 */
#   endif /* __MACH__ */
#  endif/* ultrix */
# endif /* hpux */
      return copy_of(tbuf);
    }
  }

  return 0;
}
#endif /* ndef MSDOS */