2 * Copyright (c) 2002 Dag-Erling Coïdan Smørgrav
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
34 #include <sys/sysctl.h>
43 #include "pathnames.h"
45 extern char **environ;
48 main(int argc __unused, char *argv[])
50 char path[PATH_MAX], *cp;
51 const char *cmd, *p, *q, *self;
53 struct stat self_stat, perl_stat;
56 if (stat (self, &self_stat) != 0) {
57 self_stat.st_dev = makedev (0, 0);
60 if ((cmd = strrchr(self, '/')) == NULL)
64 /* If null path (e. g. in mailfilter scripts), use default path. */
65 if ((p = getenv("PATH")) == NULL) {
66 if (sysctlbyname("user.cs_path", (void *)NULL, &len,
67 (void *)NULL, 0) == -1)
68 err(1, "sysctlbyname(\"user.cs_path\")");
69 if ((cp = malloc(len + 1)) == NULL)
70 err(1, "malloc() failed");
71 if (sysctlbyname("user.cs_path", cp, &len, (void *)NULL, 0) == -1)
72 err(1, "sysctlbyname(\"user.cs_path\")");
73 setenv("PATH", cp, 1);
75 /* If default package bindir not there, append it. */
77 if (strstr(p, PATH_PKG_BINDIR) == NULL) {
78 snprintf(path, sizeof path, "%s:%s", p, PATH_PKG_BINDIR);
79 setenv("PATH", path, 1);
82 for (p = q = getenv("PATH"); p && *p && *q; p = q + 1) {
83 for (q = p; *q && *q != ':'; ++q)
85 len = snprintf(path, sizeof path, "%.*s/%s", (int)(q - p), p, cmd);
86 if (len >= PATH_MAX || strcmp(path, self) == 0)
88 if (stat (path, &perl_stat) == 0
89 && self_stat.st_dev == perl_stat.st_dev
90 && self_stat.st_ino == perl_stat.st_ino)
92 execve(path, argv, environ);
96 errx(1, "Perl is not installed, try 'pkg_add -r perl'");