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