check_utility_compat.c revision 244568
1231990Smp/*
259243Sobrien * Copyright 2002 Massachusetts Institute of Technology
359243Sobrien *
459243Sobrien * Permission to use, copy, modify, and distribute this software and
559243Sobrien * its documentation for any purpose and without fee is hereby
659243Sobrien * granted, provided that both the above copyright notice and this
759243Sobrien * permission notice appear in all copies, that both the above
859243Sobrien * copyright notice and this permission notice appear in all
959243Sobrien * supporting documentation, and that the name of M.I.T. not be used
1059243Sobrien * in advertising or publicity pertaining to distribution of the
1159243Sobrien * software without specific, written prior permission.  M.I.T. makes
1259243Sobrien * no representations about the suitability of this software for any
1359243Sobrien * purpose.  It is provided "as is" without express or implied
1459243Sobrien * warranty.
1559243Sobrien *
1659243Sobrien * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
17100616Smp * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
1859243Sobrien * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
1959243Sobrien * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
2059243Sobrien * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2159243Sobrien * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2259243Sobrien * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
2359243Sobrien * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
2459243Sobrien * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2559243Sobrien * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
2659243Sobrien * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2759243Sobrien * SUCH DAMAGE.
2859243Sobrien */
2959243Sobrien
3059243Sobrien#include <sys/cdefs.h>
3159243Sobrien__FBSDID("$FreeBSD: head/lib/libc/gen/check_utility_compat.c 244568 2012-12-21 22:20:59Z delphij $");
3259243Sobrien
3359243Sobrien/*
3459243Sobrien * I din't use "namespace.h" here because none of the relevant utilities
35231990Smp * are threaded, so I'm not concerned about cancellation points or other
3659243Sobrien * niceties.
3759243Sobrien */
3859243Sobrien#include <sys/limits.h>
3959243Sobrien
4059243Sobrien#include <limits.h>
4159243Sobrien#include <stdlib.h>
4259243Sobrien#include <string.h>
4359243Sobrien#include <unistd.h>
4459243Sobrien
4559243Sobrien#define	_PATH_UTIL_COMPAT	"/etc/compat-FreeBSD-4-util"
4659243Sobrien#define	_ENV_UTIL_COMPAT	"_COMPAT_FreeBSD_4"
4759243Sobrien
4859243Sobrienint
4959243Sobriencheck_utility_compat(const char *utility)
5059243Sobrien{
5159243Sobrien	char buf[PATH_MAX];
5259243Sobrien	char *p, *bp;
5359243Sobrien	int len;
5459243Sobrien
5559243Sobrien	if ((p = getenv(_ENV_UTIL_COMPAT)) != NULL) {
56167465Smp		strlcpy(buf, p, sizeof buf);
57145479Smp	} else {
58145479Smp		if ((len = readlink(_PATH_UTIL_COMPAT, buf, sizeof(buf) - 1)) < 0)
5959243Sobrien			return 0;
6059243Sobrien		buf[len] = '\0';
6159243Sobrien	}
6259243Sobrien	if (buf[0] == '\0')
6359243Sobrien		return 1;
6459243Sobrien
6559243Sobrien	bp = buf;
6659243Sobrien	while ((p = strsep(&bp, ",")) != NULL) {
6759243Sobrien		if (strcmp(p, utility) == 0)
6859243Sobrien			return 1;
6959243Sobrien	}
7059243Sobrien	return 0;
7159243Sobrien}
7259243Sobrien