From abd71050605e03aad364838213f800ae14089821 Mon Sep 17 00:00:00 2001 From: Enji Cooper Date: Sun, 27 Sep 2015 07:51:18 +0000 Subject: [PATCH] Replace N #defines with nitems to simplify ifconfig code slightly MFC after: 1 week --- sbin/ifconfig/af_inet6.c | 4 +--- sbin/ifconfig/carp.c | 4 +--- sbin/ifconfig/ifbridge.c | 4 +--- sbin/ifconfig/ifclone.c | 8 +++----- sbin/ifconfig/ifconfig.c | 10 +++------- sbin/ifconfig/iffib.c | 4 +--- sbin/ifconfig/ifgre.c | 4 +--- sbin/ifconfig/ifgroup.c | 6 ++---- sbin/ifconfig/ifieee80211.c | 12 +++--------- sbin/ifconfig/iflagg.c | 4 +--- sbin/ifconfig/ifmac.c | 4 +--- sbin/ifconfig/ifmedia.c | 4 +--- sbin/ifconfig/ifpfsync.c | 6 ++---- sbin/ifconfig/ifvlan.c | 4 +--- sbin/ifconfig/ifvxlan.c | 4 +--- 15 files changed, 23 insertions(+), 59 deletions(-) diff --git a/sbin/ifconfig/af_inet6.c b/sbin/ifconfig/af_inet6.c index 6dd86222cae5..95f2b2dea3dc 100644 --- a/sbin/ifconfig/af_inet6.c +++ b/sbin/ifconfig/af_inet6.c @@ -521,7 +521,6 @@ static struct option in6_Lopt = { static __constructor void inet6_ctor(void) { -#define N(a) (sizeof(a) / sizeof(a[0])) size_t i; #ifndef RESCUE @@ -529,9 +528,8 @@ inet6_ctor(void) return; #endif - for (i = 0; i < N(inet6_cmds); i++) + for (i = 0; i < nitems(inet6_cmds); i++) cmd_register(&inet6_cmds[i]); af_register(&af_inet6); opt_register(&in6_Lopt); -#undef N } diff --git a/sbin/ifconfig/carp.c b/sbin/ifconfig/carp.c index adff153a42ae..2a2ee47d4f86 100644 --- a/sbin/ifconfig/carp.c +++ b/sbin/ifconfig/carp.c @@ -217,11 +217,9 @@ static struct afswtch af_carp = { static __constructor void carp_ctor(void) { -#define N(a) (sizeof(a) / sizeof(a[0])) int i; - for (i = 0; i < N(carp_cmds); i++) + for (i = 0; i < nitems(carp_cmds); i++) cmd_register(&carp_cmds[i]); af_register(&af_carp); -#undef N } diff --git a/sbin/ifconfig/ifbridge.c b/sbin/ifconfig/ifbridge.c index 65c33173cc73..7758e21c0ff0 100644 --- a/sbin/ifconfig/ifbridge.c +++ b/sbin/ifconfig/ifbridge.c @@ -749,11 +749,9 @@ static struct afswtch af_bridge = { static __constructor void bridge_ctor(void) { -#define N(a) (sizeof(a) / sizeof(a[0])) int i; - for (i = 0; i < N(bridge_cmds); i++) + for (i = 0; i < nitems(bridge_cmds); i++) cmd_register(&bridge_cmds[i]); af_register(&af_bridge); -#undef N } diff --git a/sbin/ifconfig/ifclone.c b/sbin/ifconfig/ifclone.c index 1ce92c840b54..6a60d419f0fb 100644 --- a/sbin/ifconfig/ifclone.c +++ b/sbin/ifconfig/ifclone.c @@ -32,9 +32,9 @@ static const char rcsid[] = "$FreeBSD$"; #endif /* not lint */ -#include -#include +#include #include +#include #include #include @@ -184,11 +184,9 @@ static struct option clone_Copt = { .opt = "C", .opt_usage = "[-C]", .cb = clone static __constructor void clone_ctor(void) { -#define N(a) (sizeof(a) / sizeof(a[0])) size_t i; - for (i = 0; i < N(clone_cmds); i++) + for (i = 0; i < nitems(clone_cmds); i++) cmd_register(&clone_cmds[i]); opt_register(&clone_Copt); -#undef N } diff --git a/sbin/ifconfig/ifconfig.c b/sbin/ifconfig/ifconfig.c index 52de660d4fed..0018b5cfcb02 100644 --- a/sbin/ifconfig/ifconfig.c +++ b/sbin/ifconfig/ifconfig.c @@ -43,11 +43,11 @@ static const char rcsid[] = #include #include -#include -#include #include #include #include +#include +#include #include #include @@ -604,7 +604,6 @@ cmd_register(struct cmd *p) static const struct cmd * cmd_lookup(const char *name, int iscreate) { -#define N(a) (sizeof(a)/sizeof(a[0])) const struct cmd *p; for (p = cmds; p != NULL; p = p->c_next) @@ -618,7 +617,6 @@ cmd_lookup(const char *name, int iscreate) } } return NULL; -#undef N } struct callback { @@ -1387,10 +1385,8 @@ static struct cmd basic_cmds[] = { static __constructor void ifconfig_ctor(void) { -#define N(a) (sizeof(a) / sizeof(a[0])) size_t i; - for (i = 0; i < N(basic_cmds); i++) + for (i = 0; i < nitems(basic_cmds); i++) cmd_register(&basic_cmds[i]); -#undef N } diff --git a/sbin/ifconfig/iffib.c b/sbin/ifconfig/iffib.c index 07ded3cbb24f..f54eab720914 100644 --- a/sbin/ifconfig/iffib.c +++ b/sbin/ifconfig/iffib.c @@ -113,11 +113,9 @@ static struct afswtch af_fib = { static __constructor void fib_ctor(void) { -#define N(a) (sizeof(a) / sizeof(a[0])) size_t i; - for (i = 0; i < N(fib_cmds); i++) + for (i = 0; i < nitems(fib_cmds); i++) cmd_register(&fib_cmds[i]); af_register(&af_fib); -#undef N } diff --git a/sbin/ifconfig/ifgre.c b/sbin/ifconfig/ifgre.c index 3ac74545f70b..98d1bf608a6a 100644 --- a/sbin/ifconfig/ifgre.c +++ b/sbin/ifconfig/ifgre.c @@ -113,11 +113,9 @@ static struct afswtch af_gre = { static __constructor void gre_ctor(void) { -#define N(a) (sizeof(a) / sizeof(a[0])) size_t i; - for (i = 0; i < N(gre_cmds); i++) + for (i = 0; i < nitems(gre_cmds); i++) cmd_register(&gre_cmds[i]); af_register(&af_gre); -#undef N } diff --git a/sbin/ifconfig/ifgroup.c b/sbin/ifconfig/ifgroup.c index e3f271d4429f..d12c5fb8e8a6 100644 --- a/sbin/ifconfig/ifgroup.c +++ b/sbin/ifconfig/ifgroup.c @@ -28,7 +28,7 @@ static const char rcsid[] = "$FreeBSD$"; #endif /* not lint */ -#include +#include #include #include #include @@ -174,12 +174,10 @@ static struct option group_gopt = { "g:", "[-g groupname]", printgroup }; static __constructor void group_ctor(void) { -#define N(a) (sizeof(a) / sizeof(a[0])) int i; - for (i = 0; i < N(group_cmds); i++) + for (i = 0; i < nitems(group_cmds); i++) cmd_register(&group_cmds[i]); af_register(&af_group); opt_register(&group_gopt); -#undef N } diff --git a/sbin/ifconfig/ifieee80211.c b/sbin/ifconfig/ifieee80211.c index d0b4917d3b04..accb1754117a 100644 --- a/sbin/ifconfig/ifieee80211.c +++ b/sbin/ifconfig/ifieee80211.c @@ -2856,7 +2856,6 @@ printrsnie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen) static void printwpsie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen) { -#define N(a) (sizeof(a) / sizeof(a[0])) u_int8_t len = ie[1]; printf("%s", tag); @@ -2897,7 +2896,7 @@ printwpsie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen) break; case IEEE80211_WPS_DEV_PASS_ID: n = LE_READ_2(ie); - if (n < N(dev_pass_id)) + if (n < nitems(dev_pass_id)) printf(" dpi:%s", dev_pass_id[n]); break; case IEEE80211_WPS_UUID_E: @@ -2911,7 +2910,6 @@ printwpsie(const char *tag, const u_int8_t *ie, size_t ielen, int maxlen) } printf(">"); } -#undef N } static void @@ -3418,7 +3416,6 @@ list_stations(int s) static const char * mesh_linkstate_string(uint8_t state) { -#define N(a) (sizeof(a) / sizeof(a[0])) static const char *state_names[] = { [0] = "IDLE", [1] = "OPEN-TX", @@ -3428,13 +3425,12 @@ mesh_linkstate_string(uint8_t state) [5] = "HOLDING", }; - if (state >= N(state_names)) { + if (state >= nitems(state_names)) { static char buf[10]; snprintf(buf, sizeof(buf), "#%u", state); return buf; } else return state_names[state]; -#undef N } static const char * @@ -5320,12 +5316,10 @@ static struct afswtch af_ieee80211 = { static __constructor void ieee80211_ctor(void) { -#define N(a) (sizeof(a) / sizeof(a[0])) int i; - for (i = 0; i < N(ieee80211_cmds); i++) + for (i = 0; i < nitems(ieee80211_cmds); i++) cmd_register(&ieee80211_cmds[i]); af_register(&af_ieee80211); clone_setdefcallback("wlan", wlan_create); -#undef N } diff --git a/sbin/ifconfig/iflagg.c b/sbin/ifconfig/iflagg.c index c595dc9806e8..97b42e31e431 100644 --- a/sbin/ifconfig/iflagg.c +++ b/sbin/ifconfig/iflagg.c @@ -308,11 +308,9 @@ static struct afswtch af_lagg = { static __constructor void lagg_ctor(void) { -#define N(a) (sizeof(a) / sizeof(a[0])) int i; - for (i = 0; i < N(lagg_cmds); i++) + for (i = 0; i < nitems(lagg_cmds); i++) cmd_register(&lagg_cmds[i]); af_register(&af_lagg); -#undef N } diff --git a/sbin/ifconfig/ifmac.c b/sbin/ifconfig/ifmac.c index d7e1e5b2aaef..a8bef817f8f0 100644 --- a/sbin/ifconfig/ifmac.c +++ b/sbin/ifconfig/ifmac.c @@ -111,11 +111,9 @@ static struct afswtch af_mac = { static __constructor void mac_ctor(void) { -#define N(a) (sizeof(a) / sizeof(a[0])) size_t i; - for (i = 0; i < N(mac_cmds); i++) + for (i = 0; i < nitems(mac_cmds); i++) cmd_register(&mac_cmds[i]); af_register(&af_mac); -#undef N } diff --git a/sbin/ifconfig/ifmedia.c b/sbin/ifconfig/ifmedia.c index eee33918f778..f1c7752cdeaf 100644 --- a/sbin/ifconfig/ifmedia.c +++ b/sbin/ifconfig/ifmedia.c @@ -845,11 +845,9 @@ static struct afswtch af_media = { static __constructor void ifmedia_ctor(void) { -#define N(a) (sizeof(a) / sizeof(a[0])) size_t i; - for (i = 0; i < N(media_cmds); i++) + for (i = 0; i < nitems(media_cmds); i++) cmd_register(&media_cmds[i]); af_register(&af_media); -#undef N } diff --git a/sbin/ifconfig/ifpfsync.c b/sbin/ifconfig/ifpfsync.c index 0af89e233c13..9dbe1d67a56b 100644 --- a/sbin/ifconfig/ifpfsync.c +++ b/sbin/ifconfig/ifpfsync.c @@ -26,7 +26,7 @@ * $FreeBSD$ */ -#include +#include #include #include @@ -227,11 +227,9 @@ static struct afswtch af_pfsync = { static __constructor void pfsync_ctor(void) { -#define N(a) (sizeof(a) / sizeof(a[0])) int i; - for (i = 0; i < N(pfsync_cmds); i++) + for (i = 0; i < nitems(pfsync_cmds); i++) cmd_register(&pfsync_cmds[i]); af_register(&af_pfsync); -#undef N } diff --git a/sbin/ifconfig/ifvlan.c b/sbin/ifconfig/ifvlan.c index 1a3fbaa5de47..107b87bfeac3 100644 --- a/sbin/ifconfig/ifvlan.c +++ b/sbin/ifconfig/ifvlan.c @@ -194,13 +194,11 @@ static struct afswtch af_vlan = { static __constructor void vlan_ctor(void) { -#define N(a) (sizeof(a) / sizeof(a[0])) size_t i; - for (i = 0; i < N(vlan_cmds); i++) + for (i = 0; i < nitems(vlan_cmds); i++) cmd_register(&vlan_cmds[i]); af_register(&af_vlan); callback_register(vlan_cb, NULL); clone_setdefcallback("vlan", vlan_create); -#undef N } diff --git a/sbin/ifconfig/ifvxlan.c b/sbin/ifconfig/ifvxlan.c index 48f5331bf686..91c25386ae2e 100644 --- a/sbin/ifconfig/ifvxlan.c +++ b/sbin/ifconfig/ifvxlan.c @@ -635,13 +635,11 @@ static struct afswtch af_vxlan = { static __constructor void vxlan_ctor(void) { -#define N(a) (sizeof(a) / sizeof(a[0])) size_t i; - for (i = 0; i < N(vxlan_cmds); i++) + for (i = 0; i < nitems(vxlan_cmds); i++) cmd_register(&vxlan_cmds[i]); af_register(&af_vxlan); callback_register(vxlan_cb, NULL); clone_setdefcallback("vxlan", vxlan_create); -#undef N }