1*9e66e6d7Sabs /* $NetBSD: confstr.c,v 1.13 2012/06/25 22:32:43 abs Exp $ */
22c4d3c4cScgd
3fbb595ccSjtc /*-
4fbb595ccSjtc * Copyright (c) 1993
5fbb595ccSjtc * The Regents of the University of California. All rights reserved.
6653d8ef2Sjtc *
7653d8ef2Sjtc * Redistribution and use in source and binary forms, with or without
8653d8ef2Sjtc * modification, are permitted provided that the following conditions
9653d8ef2Sjtc * are met:
10653d8ef2Sjtc * 1. Redistributions of source code must retain the above copyright
11653d8ef2Sjtc * notice, this list of conditions and the following disclaimer.
12653d8ef2Sjtc * 2. Redistributions in binary form must reproduce the above copyright
13653d8ef2Sjtc * notice, this list of conditions and the following disclaimer in the
14653d8ef2Sjtc * documentation and/or other materials provided with the distribution.
15eb7c1594Sagc * 3. Neither the name of the University nor the names of its contributors
16fbb595ccSjtc * may be used to endorse or promote products derived from this software
17fbb595ccSjtc * without specific prior written permission.
18653d8ef2Sjtc *
19fbb595ccSjtc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20fbb595ccSjtc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21fbb595ccSjtc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22fbb595ccSjtc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23fbb595ccSjtc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24fbb595ccSjtc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25fbb595ccSjtc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26fbb595ccSjtc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27fbb595ccSjtc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28fbb595ccSjtc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29fbb595ccSjtc * SUCH DAMAGE.
30653d8ef2Sjtc */
31653d8ef2Sjtc
3226cc2d4fSchristos #include <sys/cdefs.h>
33892c4f75Sjtc #if defined(LIBC_SCCS) && !defined(lint)
342c4d3c4cScgd #if 0
352c4d3c4cScgd static char sccsid[] = "@(#)confstr.c 8.1 (Berkeley) 6/4/93";
362c4d3c4cScgd #else
37*9e66e6d7Sabs __RCSID("$NetBSD: confstr.c,v 1.13 2012/06/25 22:32:43 abs Exp $");
382c4d3c4cScgd #endif
39892c4f75Sjtc #endif /* LIBC_SCCS and not lint */
40892c4f75Sjtc
4143fa6fe3Sjtc #include "namespace.h"
42fbb595ccSjtc #include <sys/param.h>
43fbb595ccSjtc #include <sys/sysctl.h>
44fbb595ccSjtc
45653d8ef2Sjtc #include <errno.h>
46653d8ef2Sjtc #include <paths.h>
47fbb595ccSjtc #include <stdlib.h>
483ba2c075Sjtc #include <string.h>
49fbb595ccSjtc #include <unistd.h>
50653d8ef2Sjtc
5143fa6fe3Sjtc #ifdef __weak_alias
__weak_alias(confstr,_confstr)5260549036Smycroft __weak_alias(confstr,_confstr)
5343fa6fe3Sjtc #endif
5443fa6fe3Sjtc
55653d8ef2Sjtc size_t
56*9e66e6d7Sabs confstr(int name, char *buf, size_t len)
57653d8ef2Sjtc {
58fbb595ccSjtc size_t tlen;
59fbb595ccSjtc int mib[2], sverrno;
60fbb595ccSjtc char *p;
61653d8ef2Sjtc
628b3eb79fSchristos /*
638b3eb79fSchristos * POSIX 1003.2 requires errors to return 0 --
648b3eb79fSchristos * that is *really* useful.
658b3eb79fSchristos */
66653d8ef2Sjtc switch (name) {
67653d8ef2Sjtc case _CS_PATH:
68fbb595ccSjtc mib[0] = CTL_USER;
69fbb595ccSjtc mib[1] = USER_CS_PATH;
70fbb595ccSjtc if (sysctl(mib, 2, NULL, &tlen, NULL, 0) == -1)
718b3eb79fSchristos return 0;
72fbb595ccSjtc if (len != 0 && buf != NULL) {
73fbb595ccSjtc if ((p = malloc(tlen)) == NULL)
748b3eb79fSchristos return 0;
75fbb595ccSjtc if (sysctl(mib, 2, p, &tlen, NULL, 0) == -1) {
76fbb595ccSjtc sverrno = errno;
77fbb595ccSjtc free(p);
78fbb595ccSjtc errno = sverrno;
798b3eb79fSchristos return 0;
80fbb595ccSjtc }
81fbb595ccSjtc /*
82fbb595ccSjtc * POSIX 1003.2 requires partial return of
838b3eb79fSchristos * the string -- that is even more useful.
84fbb595ccSjtc */
85fbb595ccSjtc (void)strncpy(buf, p, len - 1);
86fbb595ccSjtc buf[len - 1] = '\0';
87fbb595ccSjtc free(p);
88fbb595ccSjtc }
898b3eb79fSchristos return tlen + 1;
90653d8ef2Sjtc default:
91653d8ef2Sjtc errno = EINVAL;
928b3eb79fSchristos return 0;
93653d8ef2Sjtc }
94fbb595ccSjtc /* NOTREACHED */
95653d8ef2Sjtc }
96