Path: blob/main/lib/libc/gen/check_utility_compat.c
39530 views
/*1* Copyright 2002 Massachusetts Institute of Technology2*3* Permission to use, copy, modify, and distribute this software and4* its documentation for any purpose and without fee is hereby5* granted, provided that both the above copyright notice and this6* permission notice appear in all copies, that both the above7* copyright notice and this permission notice appear in all8* supporting documentation, and that the name of M.I.T. not be used9* in advertising or publicity pertaining to distribution of the10* software without specific, written prior permission. M.I.T. makes11* no representations about the suitability of this software for any12* purpose. It is provided "as is" without express or implied13* warranty.14*15* THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS16* ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,17* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF18* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT19* SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,20* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT21* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF22* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND23* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,24* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT25* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/2829/*30* I din't use "namespace.h" here because none of the relevant utilities31* are threaded, so I'm not concerned about cancellation points or other32* niceties.33*/34#include <sys/limits.h>3536#include <limits.h>37#include <stdlib.h>38#include <string.h>39#include <unistd.h>4041#define _PATH_UTIL_COMPAT "/etc/compat-FreeBSD-4-util"42#define _ENV_UTIL_COMPAT "_COMPAT_FreeBSD_4"4344int45check_utility_compat(const char *utility)46{47char buf[PATH_MAX];48char *p, *bp;49int len;5051if ((p = getenv(_ENV_UTIL_COMPAT)) != NULL) {52strlcpy(buf, p, sizeof buf);53} else {54if ((len = readlink(_PATH_UTIL_COMPAT, buf, sizeof(buf) - 1)) < 0)55return 0;56buf[len] = '\0';57}58if (buf[0] == '\0')59return 1;6061bp = buf;62while ((p = strsep(&bp, ",")) != NULL) {63if (strcmp(p, utility) == 0)64return 1;65}66return 0;67}686970