check_utility_compat.c revision 114443
1106065Swollman/*
2106065Swollman * Copyright 2002 Massachusetts Institute of Technology
3106065Swollman *
4106065Swollman * Permission to use, copy, modify, and distribute this software and
5106065Swollman * its documentation for any purpose and without fee is hereby
6106065Swollman * granted, provided that both the above copyright notice and this
7106065Swollman * permission notice appear in all copies, that both the above
8106065Swollman * copyright notice and this permission notice appear in all
9106065Swollman * supporting documentation, and that the name of M.I.T. not be used
10106065Swollman * in advertising or publicity pertaining to distribution of the
11106065Swollman * software without specific, written prior permission.  M.I.T. makes
12106065Swollman * no representations about the suitability of this software for any
13106065Swollman * purpose.  It is provided "as is" without express or implied
14106065Swollman * warranty.
15106065Swollman *
16106065Swollman * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
17106065Swollman * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18106065Swollman * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19106065Swollman * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20106065Swollman * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21106065Swollman * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22106065Swollman * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23106065Swollman * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24106065Swollman * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25106065Swollman * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26106065Swollman * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27106065Swollman * SUCH DAMAGE.
28106065Swollman */
29106065Swollman
30106065Swollman#include <sys/cdefs.h>
31106065Swollman__FBSDID("$FreeBSD: head/lib/libc/gen/check_utility_compat.c 114443 2003-05-01 19:03:14Z nectar $");
32106065Swollman
33114443Snectar/*
34114443Snectar * I din't use "namespace.h" here because none of the relevant utilities
35114443Snectar * are threaded, so I'm not concerned about cancellation points or other
36114443Snectar * niceties.
37114443Snectar */
38106065Swollman#include <limits.h>
39106065Swollman#include <stdlib.h>
40106065Swollman#include <string.h>
41106065Swollman#include <unistd.h>
42106065Swollman
43106065Swollman#ifndef LINE_MAX
44106065Swollman#define	LINE_MAX _POSIX2_LINE_MAX
45106065Swollman#endif
46106065Swollman
47106065Swollman#define	_PATH_UTIL_COMPAT	"/etc/compat-FreeBSD-4-util"
48106065Swollman#define	_ENV_UTIL_COMPAT	"_COMPAT_FreeBSD_4"
49106065Swollman
50106065Swollmanint
51106065Swollmancheck_utility_compat(const char *utility)
52106065Swollman{
53106065Swollman	char buf[LINE_MAX];
54106065Swollman	char *p, *bp;
55106065Swollman	int len;
56106065Swollman
57106065Swollman	if ((p = getenv(_ENV_UTIL_COMPAT)) != NULL) {
58114443Snectar		strlcpy(buf, p, sizeof buf);
59106065Swollman	} else {
60106065Swollman		if ((len = readlink(_PATH_UTIL_COMPAT, buf, sizeof buf)) < 0)
61106065Swollman			return 0;
62106065Swollman		if (len > sizeof buf)
63106065Swollman			len = sizeof buf;
64106065Swollman		buf[len] = '\0';
65106065Swollman	}
66106065Swollman	if (buf[0] == '\0')
67106065Swollman		return 1;
68106065Swollman
69106065Swollman	bp = buf;
70106065Swollman	while ((p = strsep(&bp, ",")) != NULL) {
71106065Swollman		if (strcmp(p, utility) == 0)
72106065Swollman			return 1;
73106065Swollman	}
74106065Swollman	return 0;
75106065Swollman}
76