xref: /freebsd-src/lib/libc/gen/check_utility_compat.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
1c9885518SGarrett Wollman /*
2c9885518SGarrett Wollman  * Copyright 2002 Massachusetts Institute of Technology
3c9885518SGarrett Wollman  *
4c9885518SGarrett Wollman  * Permission to use, copy, modify, and distribute this software and
5c9885518SGarrett Wollman  * its documentation for any purpose and without fee is hereby
6c9885518SGarrett Wollman  * granted, provided that both the above copyright notice and this
7c9885518SGarrett Wollman  * permission notice appear in all copies, that both the above
8c9885518SGarrett Wollman  * copyright notice and this permission notice appear in all
9c9885518SGarrett Wollman  * supporting documentation, and that the name of M.I.T. not be used
10c9885518SGarrett Wollman  * in advertising or publicity pertaining to distribution of the
11c9885518SGarrett Wollman  * software without specific, written prior permission.  M.I.T. makes
12c9885518SGarrett Wollman  * no representations about the suitability of this software for any
13c9885518SGarrett Wollman  * purpose.  It is provided "as is" without express or implied
14c9885518SGarrett Wollman  * warranty.
15c9885518SGarrett Wollman  *
16c9885518SGarrett Wollman  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
17c9885518SGarrett Wollman  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18c9885518SGarrett Wollman  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19c9885518SGarrett Wollman  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20c9885518SGarrett Wollman  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21c9885518SGarrett Wollman  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22c9885518SGarrett Wollman  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23c9885518SGarrett Wollman  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24c9885518SGarrett Wollman  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25c9885518SGarrett Wollman  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26c9885518SGarrett Wollman  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27c9885518SGarrett Wollman  * SUCH DAMAGE.
28c9885518SGarrett Wollman  */
29c9885518SGarrett Wollman 
30d0509082SJacques Vidrine /*
31d0509082SJacques Vidrine  * I din't use "namespace.h" here because none of the relevant utilities
32d0509082SJacques Vidrine  * are threaded, so I'm not concerned about cancellation points or other
33d0509082SJacques Vidrine  * niceties.
34d0509082SJacques Vidrine  */
35*53e075c0SXin LI #include <sys/limits.h>
36*53e075c0SXin LI 
37c9885518SGarrett Wollman #include <limits.h>
38c9885518SGarrett Wollman #include <stdlib.h>
39c9885518SGarrett Wollman #include <string.h>
40c9885518SGarrett Wollman #include <unistd.h>
41c9885518SGarrett Wollman 
42c9885518SGarrett Wollman #define	_PATH_UTIL_COMPAT	"/etc/compat-FreeBSD-4-util"
43c9885518SGarrett Wollman #define	_ENV_UTIL_COMPAT	"_COMPAT_FreeBSD_4"
44c9885518SGarrett Wollman 
45c9885518SGarrett Wollman int
check_utility_compat(const char * utility)46c9885518SGarrett Wollman check_utility_compat(const char *utility)
47c9885518SGarrett Wollman {
48*53e075c0SXin LI 	char buf[PATH_MAX];
49c9885518SGarrett Wollman 	char *p, *bp;
50c9885518SGarrett Wollman 	int len;
51c9885518SGarrett Wollman 
52c9885518SGarrett Wollman 	if ((p = getenv(_ENV_UTIL_COMPAT)) != NULL) {
53d0509082SJacques Vidrine 		strlcpy(buf, p, sizeof buf);
54c9885518SGarrett Wollman 	} else {
55*53e075c0SXin LI 		if ((len = readlink(_PATH_UTIL_COMPAT, buf, sizeof(buf) - 1)) < 0)
56c9885518SGarrett Wollman 			return 0;
57c9885518SGarrett Wollman 		buf[len] = '\0';
58c9885518SGarrett Wollman 	}
59c9885518SGarrett Wollman 	if (buf[0] == '\0')
60c9885518SGarrett Wollman 		return 1;
61c9885518SGarrett Wollman 
62c9885518SGarrett Wollman 	bp = buf;
63c9885518SGarrett Wollman 	while ((p = strsep(&bp, ",")) != NULL) {
64c9885518SGarrett Wollman 		if (strcmp(p, utility) == 0)
65c9885518SGarrett Wollman 			return 1;
66c9885518SGarrett Wollman 	}
67c9885518SGarrett Wollman 	return 0;
68c9885518SGarrett Wollman }
69