Home | History | Annotate | Line # | Download | only in ld.elf_so
expand.c revision 1.2
      1 /*	$NetBSD: expand.c,v 1.2 2007/05/18 21:46:39 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2007 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Christos Zoulas.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 #include <sys/cdefs.h>
     39 #ifndef lint
     40 __RCSID("$NetBSD: expand.c,v 1.2 2007/05/18 21:46:39 christos Exp $");
     41 #endif /* not lint */
     42 
     43 #include <ctype.h>
     44 #include <string.h>
     45 #include <sys/sysctl.h>
     46 
     47 #ifdef DEBUG_EXPAND
     48 #include <stdio.h>
     49 #include <err.h>
     50 #define xwarn warn
     51 size_t _rtld_expand_path(char *, size_t, const char *, const char *,
     52     const char *);
     53 #else
     54 #include <sys/stat.h>
     55 #include "rtld.h"
     56 #endif
     57 
     58 static const struct {
     59 	const char *name;
     60 	size_t namelen;
     61 } bltn[] = {
     62 #define ADD(a)	{ #a, sizeof(#a) - 1 },
     63 	ADD(HWCAP)	/* SSE, MMX, etc */
     64 	ADD(ISALIST)	/* XXX */
     65 	ADD(ORIGIN) 	/* dirname argv[0] */
     66 	ADD(OSNAME)	/* uname -s */
     67 	ADD(OSREL)	/* uname -r */
     68 	ADD(PLATFORM)	/* uname -p */
     69 };
     70 
     71 static int mib[3][2] = {
     72 	{ CTL_KERN, KERN_OSTYPE },
     73 	{ CTL_KERN, KERN_OSRELEASE },
     74 	{ CTL_HW, HW_MACHINE_ARCH },
     75 };
     76 
     77 static size_t
     78 expand(char *buf, const char *av, int what, size_t bl)
     79 {
     80 	const char *p, *ep;
     81 	char *bp = buf;
     82 	size_t len;
     83 	char name[32];
     84 
     85 	switch (what) {
     86 	case 0:	/* HWCAP XXX: Not yet */
     87 	case 1:	/* ISALIST XXX: Not yet */
     88 		return 0;
     89 
     90 	case 2:	/* ORIGIN */
     91 		ep = strrchr(p = av, '/');
     92 		if (ep == NULL) {
     93 			p = ".";
     94 			ep = p + 1;
     95 		}
     96 		break;
     97 
     98 	case 3:	/* OSNAME */
     99 	case 4:	/* OSREL */
    100 	case 5:	/* PLATFORM */
    101 		len = sizeof(name);
    102 		if (sysctl(mib[what - 3], 2, name, &len, NULL, 0) == -1) {
    103 			xwarn("sysctl");
    104 			return 0;
    105 		}
    106 		ep = (p = name) + len - 1;
    107 		break;
    108 	default:
    109 		return 0;
    110 	}
    111 
    112 	while (p != ep && bl)
    113 		*bp++ = *p++, bl--;
    114 
    115 	return bp - buf;
    116 }
    117 
    118 
    119 size_t
    120 _rtld_expand_path(char *buf, size_t bufsize, const char *av, const char *bp,
    121     const char *ep)
    122 {
    123 	size_t i, ds = bufsize;
    124 	char *dp = buf;
    125 	const char *p;
    126 	int br;
    127 
    128 	for (p = bp; p < ep;) {
    129 		if (*p == '$') {
    130 			br = *++p == '{';
    131 
    132 			if (br)
    133 				p++;
    134 
    135 			for (i = 0; i < sizeof(bltn) / sizeof(bltn[0]); i++) {
    136 				size_t s = bltn[i].namelen;
    137 				const char *es = p + s;
    138 
    139 				if ((br && *es != '}') ||
    140 				    (!br && (es != ep &&
    141 					isalpha((unsigned char)*es))))
    142 					continue;
    143 
    144 				if (strncmp(bltn[i].name, p, s) == 0) {
    145 					size_t ls = expand(dp, av, i, ds);
    146 					if (ls >= ds)
    147 						return bufsize;
    148 					ds -= ls;
    149 					dp += ls;
    150 					p = es + br;
    151 					goto done;
    152 				}
    153 			}
    154 			p -= br + 1;
    155 
    156 		}
    157 		*dp++ = *p++;
    158 		ds--;
    159 done:;
    160 	}
    161 	*dp = '\0';
    162 	return dp - buf;
    163 }
    164 
    165 #ifdef DEBUG_EXPAND
    166 int
    167 main(int argc, char *argv[])
    168 {
    169 	char buf[1024];
    170 	size_t i;
    171 
    172 	for (i = 1; i < argc; i++) {
    173 		char *p = argv[i], *ep = argv[i] + strlen(p);
    174 		size_t n = _rtld_expand_path(buf, sizeof(buf), argv[0], p, ep);
    175 		printf("%s\n", buf);
    176 	}
    177 	return 0;
    178 }
    179 #endif
    180