switch from fd_set to bitmap macros, by popular demand.

This commit is contained in:
christos 2012-12-01 15:30:16 +00:00
parent 586fa6386c
commit f9868bf95f

View File

@ -1,4 +1,4 @@
/* $NetBSD: sysctl.c,v 1.145 2012/11/29 02:24:14 christos Exp $ */ /* $NetBSD: sysctl.c,v 1.146 2012/12/01 15:30:16 christos Exp $ */
/*- /*-
* Copyright (c) 2003 The NetBSD Foundation, Inc. * Copyright (c) 2003 The NetBSD Foundation, Inc.
@ -68,12 +68,10 @@ __COPYRIGHT("@(#) Copyright (c) 1993\
#if 0 #if 0
static char sccsid[] = "@(#)sysctl.c 8.1 (Berkeley) 6/6/93"; static char sccsid[] = "@(#)sysctl.c 8.1 (Berkeley) 6/6/93";
#else #else
__RCSID("$NetBSD: sysctl.c,v 1.145 2012/11/29 02:24:14 christos Exp $"); __RCSID("$NetBSD: sysctl.c,v 1.146 2012/12/01 15:30:16 christos Exp $");
#endif #endif
#endif /* not lint */ #endif /* not lint */
#define FD_SETSIZE 0x10000
#include <sys/fd_set.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/param.h> #include <sys/param.h>
#include <sys/sysctl.h> #include <sys/sysctl.h>
@ -82,6 +80,7 @@ __RCSID("$NetBSD: sysctl.c,v 1.145 2012/11/29 02:24:14 christos Exp $");
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/sched.h> #include <sys/sched.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/bitops.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <netinet/ip_var.h> #include <netinet/ip_var.h>
#include <netinet/tcp.h> #include <netinet/tcp.h>
@ -254,6 +253,8 @@ char *fn;
int req, stale, errs; int req, stale, errs;
FILE *warnfp = stderr; FILE *warnfp = stderr;
#define MAXPORTS 0x10000
/* /*
* vah-riables n stuff * vah-riables n stuff
*/ */
@ -2692,13 +2693,13 @@ mode_bits(HANDLER_ARGS)
} }
static char * static char *
bitmask_print(const fd_set *o) bitmask_print(const uint32_t *o)
{ {
char *s, *os; char *s, *os;
s = os = NULL; s = os = NULL;
for (size_t i = 0; i < FD_SETSIZE; i++) for (size_t i = 0; i < MAXPORTS; i++)
if (FD_ISSET(i, o)) { if (__BITMAP_ISSET(i, o)) {
int rv; int rv;
if (os) if (os)
@ -2716,21 +2717,21 @@ bitmask_print(const fd_set *o)
} }
static void static void
bitmask_scan(const void *v, fd_set *o) bitmask_scan(const void *v, uint32_t *o)
{ {
char *s = strdup(v); char *s = strdup(v);
if (s == NULL) if (s == NULL)
err(1, ""); err(1, "");
FD_ZERO(o); __BITMAP_ZERO(o);
for (s = strtok(s, ","); s; s = strtok(NULL, ",")) { for (s = strtok(s, ","); s; s = strtok(NULL, ",")) {
char *e; char *e;
errno = 0; errno = 0;
unsigned long l = strtoul(s, &e, 0); unsigned long l = strtoul(s, &e, 0);
if ((l == ULONG_MAX && errno == ERANGE) || s == e || *e) if ((l == ULONG_MAX && errno == ERANGE) || s == e || *e)
errx(1, "Invalid port: %s", s); errx(1, "Invalid port: %s", s);
if (l >= FD_SETSIZE) if (l >= MAXPORTS)
errx(1, "Port out of range: %s", s); errx(1, "Port out of range: %s", s);
FD_SET(l, o); __BITMAP_SET(l, o);
} }
} }
@ -2740,15 +2741,16 @@ reserve(HANDLER_ARGS)
{ {
int rc; int rc;
size_t osz, nsz; size_t osz, nsz;
fd_set o, n; uint32_t o[__BITMAP_SIZE(uint32_t, MAXPORTS)];
uint32_t n[__BITMAP_SIZE(uint32_t, MAXPORTS)];
if (fn) if (fn)
trim_whitespace(value, 3); trim_whitespace(value, 3);
osz = sizeof(o); osz = sizeof(o);
if (value) { if (value) {
bitmask_scan(value, &n); bitmask_scan(value, n);
value = (char *)&n; value = (char *)n;
nsz = sizeof(n); nsz = sizeof(n);
} else } else
nsz = 0; nsz = 0;
@ -2763,10 +2765,10 @@ reserve(HANDLER_ARGS)
return; return;
if (rflag || xflag) if (rflag || xflag)
display_struct(pnode, sname, &o, sizeof(o), display_struct(pnode, sname, o, sizeof(o),
value ? DISPLAY_OLD : DISPLAY_VALUE); value ? DISPLAY_OLD : DISPLAY_VALUE);
else { else {
char *s = bitmask_print(&o); char *s = bitmask_print(o);
display_string(pnode, sname, s, strlen(s), display_string(pnode, sname, s, strlen(s),
value ? DISPLAY_OLD : DISPLAY_VALUE); value ? DISPLAY_OLD : DISPLAY_VALUE);
free(s); free(s);
@ -2774,10 +2776,10 @@ reserve(HANDLER_ARGS)
if (value) { if (value) {
if (rflag || xflag) if (rflag || xflag)
display_struct(pnode, sname, &n, sizeof(n), display_struct(pnode, sname, n, sizeof(n),
DISPLAY_NEW); DISPLAY_NEW);
else { else {
char *s = bitmask_print(&n); char *s = bitmask_print(n);
display_string(pnode, sname, s, strlen(s), DISPLAY_NEW); display_string(pnode, sname, s, strlen(s), DISPLAY_NEW);
free(s); free(s);
} }