summaryrefslogtreecommitdiffstats
path: root/findexec.c
blob: 18285c7f6a56222207cd979fd59dfb3518a3fb0a (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
/* "findexec.c" was part of DLD, a dynamic link/unlink editor for C.

   Copyright (C) 1990 by W. Wilson Ho.

   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  (jaffer@jacal)

	* 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  <jaffer@jacal.bertronics>

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

/* This program 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 1, or (at your option) any
   later version. */

/* 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.  */

#include <sys/file.h>
#include <sys/param.h>
#ifdef linux
# 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
#  include <strings.h>
# endif
#endif
#ifdef __amigados__
# include <stdlib.h>
# include <sys/stat.h>
#endif
#ifndef __STDC__
# define const /**/
#endif
#ifdef freebsd
/* This might be same for 44bsd derived system. */
# 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
      struct stat stat_temp;
      if (stat(tbuf,&stat_temp)) continue;
      if (S_IFREG != (S_IFMT & stat_temp.st_mode)) continue;
# endif/* ultrix */
#endif /* hpux */
      return copy_of(tbuf);
    }
  }

  return 0;
}