Sync some manuals from bin & sbin with NetBSD-8
[minix.git] / sbin / ifconfig / env.c
blob6d5c690a43272ec11fe074ec74f95b37420c1ec5
1 /* $NetBSD: env.c,v 1.9 2013/02/07 13:20:51 apb Exp $ */
3 /*-
4 * Copyright (c) 2008 David Young. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
28 #include <sys/cdefs.h>
29 #ifndef lint
30 __RCSID("$NetBSD: env.c,v 1.9 2013/02/07 13:20:51 apb Exp $");
31 #endif /* not lint */
33 #include <errno.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include <util.h>
38 #include <net/if.h>
39 #include <sys/socket.h>
40 #include <sys/ioctl.h>
42 #include "env.h"
43 #include "util.h"
44 #include "prog_ops.h"
46 prop_dictionary_t
47 prop_dictionary_augment(prop_dictionary_t bottom, prop_dictionary_t top)
49 prop_object_iterator_t i;
50 prop_dictionary_t d;
51 prop_object_t ko, o;
52 prop_dictionary_keysym_t k;
53 const char *key;
55 d = prop_dictionary_copy_mutable(bottom);
56 if (d == NULL)
57 return NULL;
59 i = prop_dictionary_iterator(top);
61 while (i != NULL && (ko = prop_object_iterator_next(i)) != NULL) {
62 k = (prop_dictionary_keysym_t)ko;
63 key = prop_dictionary_keysym_cstring_nocopy(k);
64 o = prop_dictionary_get_keysym(top, k);
65 if (o == NULL || !prop_dictionary_set(d, key, o)) {
66 prop_object_release((prop_object_t)d);
67 d = NULL;
68 break;
71 if (i != NULL)
72 prop_object_iterator_release(i);
73 if (d != NULL)
74 prop_dictionary_make_immutable(d);
75 return d;
78 int
79 getifflags(prop_dictionary_t env, prop_dictionary_t oenv,
80 unsigned short *flagsp)
82 struct ifreq ifr;
83 const char *ifname;
84 uint64_t ifflags;
85 int s;
87 if (prop_dictionary_get_uint64(env, "ifflags", &ifflags)) {
88 *flagsp = (unsigned short)ifflags;
89 return 0;
92 if ((s = getsock(AF_UNSPEC)) == -1)
93 return -1;
95 if ((ifname = getifname(env)) == NULL)
96 return -1;
98 memset(&ifr, 0, sizeof(ifr));
99 estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
100 if (prog_ioctl(s, SIOCGIFFLAGS, &ifr) == -1)
101 return -1;
103 *flagsp = (unsigned short)ifr.ifr_flags;
105 prop_dictionary_set_uint64(oenv, "ifflags",
106 (unsigned short)ifr.ifr_flags);
108 return 0;
111 const char *
112 getifinfo(prop_dictionary_t env, prop_dictionary_t oenv, unsigned short *flagsp)
114 if (getifflags(env, oenv, flagsp) == -1)
115 return NULL;
117 return getifname(env);
120 const char *
121 getifname(prop_dictionary_t env)
123 const char *s;
125 return prop_dictionary_get_cstring_nocopy(env, "if", &s) ? s : NULL;
128 ssize_t
129 getargdata(prop_dictionary_t env, const char *key, uint8_t *buf, size_t buflen)
131 prop_data_t data;
132 size_t datalen;
134 data = (prop_data_t)prop_dictionary_get(env, key);
135 if (data == NULL) {
136 errno = ENOENT;
137 return -1;
139 datalen = prop_data_size(data);
140 if (datalen > buflen) {
141 errno = ENAMETOOLONG;
142 return -1;
144 memset(buf, 0, buflen);
145 memcpy(buf, prop_data_data_nocopy(data), datalen);
146 return datalen;
149 ssize_t
150 getargstr(prop_dictionary_t env, const char *key, char *buf, size_t buflen)
152 prop_data_t data;
153 size_t datalen;
155 data = (prop_data_t)prop_dictionary_get(env, key);
156 if (data == NULL) {
157 errno = ENOENT;
158 return -1;
160 datalen = prop_data_size(data);
161 if (datalen >= buflen) {
162 errno = ENAMETOOLONG;
163 return -1;
165 memset(buf, 0, buflen);
166 memcpy(buf, prop_data_data_nocopy(data), datalen);
167 return datalen;
171 getaf(prop_dictionary_t env)
173 int64_t af;
175 if (!prop_dictionary_get_int64(env, "af", &af)) {
176 errno = ENOENT;
177 return -1;
179 return (int)af;