diff --git a/applets/applets.c b/applets/applets.c index cff792fb7..57c972f79 100644 --- a/applets/applets.c +++ b/applets/applets.c @@ -47,7 +47,6 @@ static const char usage_messages[] = "" /* Define struct bb_applet applets[] */ #include "applets.h" /* The -1 arises because of the {0,NULL,0,-1} entry. */ -const unsigned short NUM_APPLETS = sizeof(applets) / sizeof(applets[0]) - 1; const struct bb_applet *current_applet; const char *applet_name ATTRIBUTE_EXTERNALLY_VISIBLE; @@ -488,7 +487,7 @@ static int applet_name_compare(const void *name, const void *vapplet) const struct bb_applet *find_applet_by_name(const char *name) { /* Do a binary search to find the applet entry given the name. */ - return bsearch(name, applets, NUM_APPLETS, sizeof(applets[0]), + return bsearch(name, applets, ARRAY_SIZE(applets)-1, sizeof(applets[0]), applet_name_compare); } diff --git a/coreutils/od_bloaty.c b/coreutils/od_bloaty.c index 325085626..af5eba9ce 100644 --- a/coreutils/od_bloaty.c +++ b/coreutils/od_bloaty.c @@ -158,7 +158,7 @@ static const signed char width_bytes[] = { initializer in the width_bytes array. */ struct dummy { int assert_width_bytes_matches_size_spec_decl - [sizeof width_bytes / sizeof width_bytes[0] == N_SIZE_SPECS ? 1 : -1]; + [ARRAY_SIZE(width_bytes) == N_SIZE_SPECS ? 1 : -1]; }; static size_t string_min; diff --git a/coreutils/stty.c b/coreutils/stty.c index e6d054ff2..0983532cf 100644 --- a/coreutils/stty.c +++ b/coreutils/stty.c @@ -319,7 +319,7 @@ static const struct mode_info mode_info[] = { }; enum { - NUM_mode_info = (sizeof(mode_info) / sizeof(mode_info[0])) + NUM_mode_info = ARRAY_SIZE(mode_info) }; /* Control character settings */ @@ -371,7 +371,7 @@ static const struct control_info control_info[] = { }; enum { - NUM_control_info = (sizeof(control_info) / sizeof(control_info[0])) + NUM_control_info = ARRAY_SIZE(control_info) }; /* The width of the screen, for output wrapping */ diff --git a/coreutils/test.c b/coreutils/test.c index 36fb38a64..7b87a42f9 100644 --- a/coreutils/test.c +++ b/coreutils/test.c @@ -146,7 +146,6 @@ static const struct t_op { { ")" , RPAREN , PAREN }, }; -enum { NUM_OPS = sizeof(ops) / sizeof(ops[0]) }; #if ENABLE_FEATURE_TEST_64 typedef int64_t arith_t; @@ -471,7 +470,7 @@ static enum token t_lex(char *s) return op->op_num; } op++; - } while (op < ops + NUM_OPS); + } while (op < ops + ARRAY_SIZE(ops)); return OPERAND; } diff --git a/editors/awk.c b/editors/awk.c index 90ba64348..940a7948e 100644 --- a/editors/awk.c +++ b/editors/awk.c @@ -390,7 +390,7 @@ static const char vValues[] = /* hash size may grow to these values */ #define FIRST_PRIME 61; static const unsigned PRIMES[] = { 251, 1021, 4093, 16381, 65521 }; -enum { NPRIMES = sizeof(PRIMES) / sizeof(PRIMES[0]) }; + /* Globals. Split in two parts so that first one is addressed @@ -570,7 +570,7 @@ static void hash_rebuild(xhash *hash) unsigned newsize, i, idx; hash_item **newitems, *hi, *thi; - if (hash->nprime == NPRIMES) + if (hash->nprime == ARRAY_SIZE(PRIMES)) return; newsize = PRIMES[hash->nprime++]; diff --git a/editors/vi.c b/editors/vi.c index b961ca5b8..01d45e50e 100644 --- a/editors/vi.c +++ b/editors/vi.c @@ -2230,8 +2230,7 @@ static char readit(void) // read (maybe cursor) key from stdin {"[13~", VI_K_FUN3}, // Function Key F3 {"[14~", VI_K_FUN4}, // Function Key F4 }; - -#define ESCCMDS_COUNT (sizeof(esccmds)/sizeof(struct esc_cmds)) + enum { ESCCMDS_COUNT = ARRAY_SIZE(esccmds) }; alarm(0); // turn alarm OFF while we wait for input fflush(stdout); diff --git a/include/libbb.h b/include/libbb.h index b7b0657b6..3cdf28fab 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -1071,4 +1071,7 @@ extern const char bb_default_login_shell[]; #include #endif + +#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) + #endif /* __LIBBUSYBOX_H__ */ diff --git a/libbb/speed_table.c b/libbb/speed_table.c index 6137b7731..0f0b33d3b 100644 --- a/libbb/speed_table.c +++ b/libbb/speed_table.c @@ -54,7 +54,7 @@ static const struct speed_map speeds[] = { #endif }; -enum { NUM_SPEEDS = (sizeof(speeds) / sizeof(struct speed_map)) }; +enum { NUM_SPEEDS = ARRAY_SIZE(speeds) }; unsigned int tty_baud_to_value(speed_t speed) { diff --git a/libbb/u_signal_names.c b/libbb/u_signal_names.c index dc4c0b237..f8eaea76d 100644 --- a/libbb/u_signal_names.c +++ b/libbb/u_signal_names.c @@ -127,7 +127,7 @@ int get_signum(const char *name) return i; if (strncasecmp(name, "SIG", 3) == 0) name += 3; - for (i = 0; i < sizeof(signals) / sizeof(signals[0]); i++) + for (i = 0; i < ARRAY_SIZE(signals); i++) if (strcasecmp(name, signals[i]) == 0) return i; @@ -152,7 +152,7 @@ int get_signum(const char *name) const char *get_signame(int number) { - if ((unsigned)number < sizeof(signals) / sizeof(signals[0])) { + if ((unsigned)number < ARRAY_SIZE(signals)) { if (signals[number][0]) /* if it's not an empty str */ return signals[number]; } diff --git a/miscutils/crond.c b/miscutils/crond.c index a490d417f..5653f6804 100644 --- a/miscutils/crond.c +++ b/miscutils/crond.c @@ -15,7 +15,6 @@ #include "libbb.h" #include -#define arysize(ary) (sizeof(ary)/sizeof((ary)[0])) #ifndef CRONTABS #define CRONTABS "/var/spool/cron/crontabs" @@ -468,13 +467,13 @@ static void FixDayDow(CronLine * line) int weekUsed = 0; int daysUsed = 0; - for (i = 0; i < (int)(arysize(line->cl_Dow)); ++i) { + for (i = 0; i < (int)(ARRAY_SIZE(line->cl_Dow)); ++i) { if (line->cl_Dow[i] == 0) { weekUsed = 1; break; } } - for (i = 0; i < (int)(arysize(line->cl_Days)); ++i) { + for (i = 0; i < (int)(ARRAY_SIZE(line->cl_Days)); ++i) { if (line->cl_Days[i] == 0) { daysUsed = 1; break; diff --git a/modutils/insmod.c b/modutils/insmod.c index cba8dc4a0..8a6cc05dd 100644 --- a/modutils/insmod.c +++ b/modutils/insmod.c @@ -3632,7 +3632,7 @@ static int obj_gpl_license(struct obj_file *f, const char **license) int i; if (license) *license = value+1; - for (i = 0; i < sizeof(gpl_licenses)/sizeof(gpl_licenses[0]); ++i) { + for (i = 0; i < ARRAY_SIZE(gpl_licenses); ++i) { if (strcmp(value+1, gpl_licenses[i]) == 0) return 0; } @@ -3833,7 +3833,7 @@ add_ksymoops_symbols(struct obj_file *f, const char *filename, #endif /* _NOT_SUPPORTED_ */ /* tag the desired sections if size is non-zero */ - for (i = 0; i < sizeof(section_names)/sizeof(section_names[0]); ++i) { + for (i = 0; i < ARRAY_SIZE(section_names); ++i) { sec = obj_find_section(f, section_names[i]); if (sec && sec->header.sh_size) { l = sizeof(symprefix)+ /* "__insmod_" */ diff --git a/networking/httpd.c b/networking/httpd.c index 8c0a83e52..383a00635 100644 --- a/networking/httpd.c +++ b/networking/httpd.c @@ -878,11 +878,8 @@ static int sendHeaders(HttpResponseNum responseNum) time_t timer = time(0); char timeStr[80]; int len; - enum { - numNames = sizeof(httpResponseNames) / sizeof(httpResponseNames[0]) - }; - for (i = 0; i < numNames; i++) { + for (i = 0; i < ARRAY_SIZE(httpResponseNames); i++) { if (httpResponseNames[i].type == responseNum) { responseString = httpResponseNames[i].name; infoString = httpResponseNames[i].info; diff --git a/networking/ifconfig.c b/networking/ifconfig.c index 5742399c5..5e11b2b7c 100644 --- a/networking/ifconfig.c +++ b/networking/ifconfig.c @@ -337,7 +337,7 @@ int ifconfig_main(int argc, char **argv) } /* We fell through, so treat as possible hostname. */ - a1op = Arg1Opt + (sizeof(Arg1Opt) / sizeof(Arg1Opt[0])) - 1; + a1op = Arg1Opt + ARRAY_SIZE(Arg1Opt) - 1; mask = op->arg_flags; goto HOSTNAME; diff --git a/networking/ifupdown.c b/networking/ifupdown.c index 2b748f11d..8843184bf 100644 --- a/networking/ifupdown.c +++ b/networking/ifupdown.c @@ -375,7 +375,7 @@ static const struct method_t methods6[] = { static const struct address_family_t addr_inet6 = { "inet6", - sizeof(methods6) / sizeof(struct method_t), + ARRAY_SIZE(methods6), methods6 }; #endif /* FEATURE_IFUPDOWN_IPV6 */ @@ -471,8 +471,8 @@ static const struct dhcp_client_t ext_dhcp_clients[] = { static int dhcp_up(struct interface_defn_t *ifd, execfn *exec) { #if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP - int i, nclients = sizeof(ext_dhcp_clients) / sizeof(ext_dhcp_clients[0]); - for (i = 0; i < nclients; i++) { + int i ; + for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) { if (exists_execable(ext_dhcp_clients[i].name)) return execute(ext_dhcp_clients[i].startcmd, ifd, exec); } @@ -490,8 +490,8 @@ static int dhcp_up(struct interface_defn_t *ifd, execfn *exec) static int dhcp_down(struct interface_defn_t *ifd, execfn *exec) { #if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP - int i, nclients = sizeof(ext_dhcp_clients) / sizeof(ext_dhcp_clients[0]); - for (i = 0; i < nclients; i++) { + int i ; + for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) { if (exists_execable(ext_dhcp_clients[i].name)) return execute(ext_dhcp_clients[i].stopcmd, ifd, exec); } @@ -551,7 +551,7 @@ static const struct method_t methods[] = { static const struct address_family_t addr_inet = { "inet", - sizeof(methods) / sizeof(methods[0]), + ARRAY_SIZE(methods), methods }; diff --git a/networking/libiproute/ll_proto.c b/networking/libiproute/ll_proto.c index 10d749881..4e62e876e 100644 --- a/networking/libiproute/ll_proto.c +++ b/networking/libiproute/ll_proto.c @@ -96,7 +96,7 @@ const char * ll_proto_n2a(unsigned short id, char *buf, int len) id = ntohs(id); - for (i=0; itext); vp->next = *vpp; @@ -6876,8 +6876,8 @@ static const char *const * findkwd(const char *s) { return bsearch(s, tokname_array + KWDOFFSET, - (sizeof(tokname_array) / sizeof(char *)) - KWDOFFSET, - sizeof(char *), pstrcmp); + ARRAY_SIZE(tokname_array) - KWDOFFSET, + sizeof(tokname_array[0]), pstrcmp); } /* @@ -8094,7 +8094,6 @@ static const struct builtincmd builtintab[] = { { BUILTIN_REGULAR "wait", waitcmd }, }; -#define NUMBUILTINS (sizeof(builtintab) / sizeof(builtintab[0])) #define COMMANDCMD (builtintab + 5 + \ 2 * ENABLE_ASH_BUILTIN_TEST + \ @@ -8116,7 +8115,7 @@ find_builtin(const char *name) struct builtincmd *bp; bp = bsearch( - name, builtintab, NUMBUILTINS, sizeof(builtintab[0]), + name, builtintab, ARRAY_SIZE(builtintab), sizeof(builtintab[0]), pstrcmp ); return bp; @@ -11255,7 +11254,7 @@ helpcmd(int argc, char **argv) int col, i; out1fmt("\nBuilt-in commands:\n-------------------\n"); - for (col = 0, i = 0; i < NUMBUILTINS; i++) { + for (col = 0, i = 0; i < ARRAY_SIZE(builtintab) ; i++) { col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), builtintab[i].name + 1); if (col > 60) { diff --git a/shell/hush.c b/shell/hush.c index a446bbeb2..275b69ef3 100644 --- a/shell/hush.c +++ b/shell/hush.c @@ -2889,10 +2889,10 @@ static int reserved_word(o_string *dest, struct p_context *ctx) { "done", RES_DONE, FLAG_END } #endif }; - enum { NRES = sizeof(reserved_list)/sizeof(reserved_list[0]) }; + const struct reserved_combo *r; - for (r = reserved_list; r < reserved_list + NRES; r++) { + for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) { if (strcmp(dest->data, r->literal) != 0) continue; debug_printf("found reserved word %s, code %d\n", r->literal, r->code); diff --git a/shell/lash.c b/shell/lash.c index 4e8b23776..e5c7ef670 100644 --- a/shell/lash.c +++ b/shell/lash.c @@ -146,8 +146,8 @@ static const struct built_in_command bltins[] = { /* to do: add ulimit */ }; -#define VEC_SIZE(v) (sizeof(v)/sizeof(v[0])) -#define VEC_LAST(v) v[VEC_SIZE(v)-1] + +#define VEC_LAST(v) v[ARRAY_SIZE(v)-1] static int shell_context; /* Type prompt trigger (PS1 or PS2) */ diff --git a/shell/msh.c b/shell/msh.c index 2328e0734..effdc0107 100644 --- a/shell/msh.c +++ b/shell/msh.c @@ -596,7 +596,7 @@ static const char * const signame[] = { "Terminated", }; -#define NSIGNAL (sizeof(signame)/sizeof(signame[0])) + struct res { const char *r_name; @@ -2997,7 +2997,7 @@ static int waitfor(int lastpid, int canintr) } else { rv = WAITSIG(s); if (rv != 0) { - if (rv < NSIGNAL) { + if (rv < ARRAY_SIZE(signame)) { if (signame[rv] != NULL) { if (pid != lastpid) { prn(pid); @@ -3016,7 +3016,7 @@ static int waitfor(int lastpid, int canintr) } if (WAITCORE(s)) prs(" - core dumped"); - if (rv >= NSIGNAL || signame[rv]) + if (rv >= ARRAY_SIZE(signame) || signame[rv]) prs("\n"); rv = -1; } else diff --git a/util-linux/fdisk.c b/util-linux/fdisk.c index 4ecbadf78..870789112 100644 --- a/util-linux/fdisk.c +++ b/util-linux/fdisk.c @@ -20,8 +20,6 @@ # define USE_FEATURE_FDISK_BLKSIZE(a) #endif -#define SIZE(a) (sizeof(a)/sizeof((a)[0])) - #define DEFAULT_SECTOR_SIZE 512 #define MAX_SECTOR_SIZE 2048 #define SECTOR_SIZE 512 /* still used in osf/sgi/sun code */ diff --git a/util-linux/fdisk_osf.c b/util-linux/fdisk_osf.c index afb8459a2..9f0dc7fd3 100644 --- a/util-linux/fdisk_osf.c +++ b/util-linux/fdisk_osf.c @@ -163,7 +163,7 @@ static const char * const xbsd_dktypenames[] = { "floppy", 0 }; -#define BSD_DKMAXTYPES (sizeof(xbsd_dktypenames) / sizeof(xbsd_dktypenames[0]) - 1) + /* * Filesystem type and version. @@ -219,7 +219,6 @@ static const char *const xbsd_fstypes[] = { "\x10" "AdvFS", /* BSD_FS_ADVFS */ NULL }; -#define BSD_FSMAXTYPES (SIZE(xbsd_fstypes)-1) /* @@ -509,7 +508,7 @@ xbsd_print_disklabel(int show_all) #else printf("# %s:\n", partname(disk_device, xbsd_part_index+1, 0)); #endif - if ((unsigned) lp->d_type < BSD_DKMAXTYPES) + if ((unsigned) lp->d_type < ARRAY_SIZE(xbsd_dktypenames)-1) printf("type: %s\n", xbsd_dktypenames[lp->d_type]); else printf("type: %d\n", lp->d_type); @@ -571,7 +570,7 @@ xbsd_print_disklabel(int show_all) ); } - if ((unsigned) pp->p_fstype < BSD_FSMAXTYPES) + if ((unsigned) pp->p_fstype < ARRAY_SIZE(xbsd_fstypes)-1) printf("%8.8s", xbsd_fstypes[pp->p_fstype]); else printf("%8x", pp->p_fstype); diff --git a/util-linux/fdisk_sun.c b/util-linux/fdisk_sun.c index 2fa7b2b86..d4614fd4f 100644 --- a/util-linux/fdisk_sun.c +++ b/util-linux/fdisk_sun.c @@ -207,7 +207,7 @@ sun_autoconfigure_scsi(void) if (!q) break; *q = '\0'; - for (i = 0; i < SIZE(sun_drives); i++) { + for (i = 0; i < ARRAY_SIZE(sun_drives); i++) { if (*sun_drives[i].vendor && strcasecmp(sun_drives[i].vendor, vendor)) continue; if (!strstr(model, sun_drives[i].model)) @@ -244,7 +244,7 @@ create_sunlabel(void) puts("Drive type\n" " ? auto configure\n" " 0 custom (with hardware detected defaults)"); - for (i = 0; i < SIZE(sun_drives); i++) { + for (i = 0; i < ARRAY_SIZE(sun_drives); i++) { printf(" %c %s%s%s\n", i + 'a', sun_drives[i].vendor, (*sun_drives[i].vendor) ? " " : "", @@ -255,11 +255,11 @@ create_sunlabel(void) if (c == '0') { break; } - if (c >= 'a' && c < 'a' + SIZE(sun_drives)) { + if (c >= 'a' && c < 'a' + ARRAY_SIZE(sun_drives)) { p = sun_drives + c - 'a'; break; } - if (c >= 'A' && c < 'A' + SIZE(sun_drives)) { + if (c >= 'A' && c < 'A' + ARRAY_SIZE(sun_drives)) { p = sun_drives + c - 'A'; break; } @@ -446,7 +446,7 @@ verify_sun(void) else array[i] = -1; } - qsort(array,SIZE(array),sizeof(array[0]), + qsort(array, ARRAY_SIZE(array), sizeof(array[0]), (int (*)(const void *,const void *)) verify_sun_cmp); if (array[0] == -1) { printf("No partitions defined\n"); diff --git a/util-linux/mount.c b/util-linux/mount.c index fe2f1d969..4dd1a85a2 100644 --- a/util-linux/mount.c +++ b/util-linux/mount.c @@ -120,7 +120,6 @@ struct { {"remount", MS_REMOUNT}, // action flag }; -#define VECTOR_SIZE(v) (sizeof(v) / sizeof((v)[0])) /* Append mount options to string */ static void append_mount_options(char **oldopts, const char *newopts) @@ -168,7 +167,7 @@ static int parse_mount_options(char *options, char **unrecognized) if (comma) *comma = 0; // Find this option in mount_options - for (i = 0; i < VECTOR_SIZE(mount_options); i++) { + for (i = 0; i < ARRAY_SIZE(mount_options); i++) { if (!strcasecmp(mount_options[i].name, options)) { long fl = mount_options[i].flags; if (fl < 0) flags &= fl; @@ -177,7 +176,7 @@ static int parse_mount_options(char *options, char **unrecognized) } } // If unrecognized not NULL, append unrecognized mount options */ - if (unrecognized && i == VECTOR_SIZE(mount_options)) { + if (unrecognized && i == ARRAY_SIZE(mount_options)) { // Add it to strflags, to pass on to kernel i = *unrecognized ? strlen(*unrecognized) : 0; *unrecognized = xrealloc(*unrecognized, i+strlen(options)+2);