No empty .Rs/.Re
[netbsd-mini2440.git] / sbin / ifconfig / env.c
blob298053cb7f41b6bc1ee216e42ee7351b075c119d
1 /* $NetBSD: env.c,v 1.5 2008/05/12 21:53:49 dyoung 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$");
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"
45 prop_dictionary_t
46 prop_dictionary_augment(prop_dictionary_t bottom, prop_dictionary_t top)
48 prop_object_iterator_t i;
49 prop_dictionary_t d;
50 prop_object_t ko, o;
51 prop_dictionary_keysym_t k;
52 const char *key;
54 d = prop_dictionary_copy_mutable(bottom);
56 i = prop_dictionary_iterator(top);
58 while ((ko = prop_object_iterator_next(i)) != NULL) {
59 k = (prop_dictionary_keysym_t)ko;
60 key = prop_dictionary_keysym_cstring_nocopy(k);
61 o = prop_dictionary_get_keysym(top, k);
62 if (o == NULL || !prop_dictionary_set(d, key, o)) {
63 prop_object_release((prop_object_t)d);
64 d = NULL;
65 break;
68 prop_object_iterator_release(i);
69 prop_dictionary_make_immutable(d);
70 return d;
73 int
74 getifflags(prop_dictionary_t env, prop_dictionary_t oenv,
75 unsigned short *flagsp)
77 struct ifreq ifr;
78 const char *ifname;
79 uint64_t ifflags;
80 int s;
82 if (prop_dictionary_get_uint64(env, "ifflags", &ifflags)) {
83 *flagsp = (unsigned short)ifflags;
84 return 0;
87 if ((s = getsock(AF_UNSPEC)) == -1)
88 return -1;
90 if ((ifname = getifname(env)) == NULL)
91 return -1;
93 memset(&ifr, 0, sizeof(ifr));
94 estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
95 if (ioctl(s, SIOCGIFFLAGS, &ifr) == -1)
96 return -1;
98 *flagsp = (unsigned short)ifr.ifr_flags;
100 prop_dictionary_set_uint64(oenv, "ifflags",
101 (unsigned short)ifr.ifr_flags);
103 return 0;
106 const char *
107 getifinfo(prop_dictionary_t env, prop_dictionary_t oenv, unsigned short *flagsp)
109 if (getifflags(env, oenv, flagsp) == -1)
110 return NULL;
112 return getifname(env);
115 const char *
116 getifname(prop_dictionary_t env)
118 const char *s;
120 return prop_dictionary_get_cstring_nocopy(env, "if", &s) ? s : NULL;
123 ssize_t
124 getargdata(prop_dictionary_t env, const char *key, uint8_t *buf, size_t buflen)
126 prop_data_t data;
127 size_t datalen;
129 data = (prop_data_t)prop_dictionary_get(env, key);
130 if (data == NULL) {
131 errno = ENOENT;
132 return -1;
134 datalen = prop_data_size(data);
135 if (datalen > buflen) {
136 errno = ENAMETOOLONG;
137 return -1;
139 memset(buf, 0, buflen);
140 memcpy(buf, prop_data_data_nocopy(data), datalen);
141 return datalen;
144 ssize_t
145 getargstr(prop_dictionary_t env, const char *key, char *buf, size_t buflen)
147 prop_data_t data;
148 size_t datalen;
150 data = (prop_data_t)prop_dictionary_get(env, key);
151 if (data == NULL) {
152 errno = ENOENT;
153 return -1;
155 datalen = prop_data_size(data);
156 if (datalen >= buflen) {
157 errno = ENAMETOOLONG;
158 return -1;
160 memset(buf, 0, buflen);
161 memcpy(buf, prop_data_data_nocopy(data), datalen);
162 return datalen;
166 getaf(prop_dictionary_t env)
168 int64_t af;
170 if (!prop_dictionary_get_int64(env, "af", &af)) {
171 errno = ENOENT;
172 return -1;
174 return (int)af;