]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/perl/perl.c
This commit was generated by cvs2svn to compensate for changes in r107207,
[FreeBSD/FreeBSD.git] / usr.bin / perl / perl.c
1 /*-
2  * Copyright (c) 2002 Dag-Erling Coïdan Smørgrav
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  *
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.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/stat.h>
34 #include <sys/sysctl.h>
35
36 #include <err.h>
37 #include <errno.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42
43 #include "pathnames.h"
44
45 extern char **environ;
46
47 int
48 main(int argc __unused, char *argv[])
49 {
50         char path[PATH_MAX], *cp;
51         const char *cmd, *p, *q, *self;
52         size_t len;
53         struct stat self_stat, perl_stat;
54
55         self = argv[0];
56         if (stat (self, &self_stat) != 0) {
57                 self_stat.st_dev = makedev (0, 0);
58                 self_stat.st_ino = 0;
59         }
60         if ((cmd = strrchr(self, '/')) == NULL)
61                 cmd = self;
62         else
63                 cmd++;
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);
74         }
75         /* If default package bindir not there, append it. */
76         p = getenv("PATH");
77         if (strstr(p, PATH_PKG_BINDIR) == NULL) {
78                 snprintf(path, sizeof path, "%s:%s", p, PATH_PKG_BINDIR);
79                 setenv("PATH", path, 1);
80         }
81         argv[0] = path;
82         for (p = q = getenv("PATH"); p && *p && *q; p = q + 1) {
83                 for (q = p; *q && *q != ':'; ++q)
84                         /* nothing */ ;
85                 len = snprintf(path, sizeof path, "%.*s/%s", (int)(q - p), p, cmd);
86                 if (len >= PATH_MAX || strcmp(path, self) == 0)
87                         continue;
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)
91                         continue;
92                 execve(path, argv, environ);
93                 if (errno != ENOENT)
94                         err(1, "%s", path);
95         }
96         errx(1, "Perl is not installed, try 'pkg_add -r perl'");
97 }