From: Guillem Jover Date: Mon, 12 May 2008 04:02:29 +0000 (+0300) Subject: Use sizeof_array() instead of ad-hoc calculations X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bd40cd8d18f3751420e402f3e3c4fb5ebf20c72c;p=dpkg Use sizeof_array() instead of ad-hoc calculations --- diff --git a/TODO b/TODO index 938ad346..96e5ba16 100644 --- a/TODO +++ b/TODO @@ -37,7 +37,6 @@ lenny - Refactor src/processarc.c. - Make sure all vsnprintf callers are checking the return value, as the system version will not ohsite on us. - - Refactor ARRAY_SIZE (need to push the patch). - Fix ncursesw headers. - Consider ferror_fclose function. diff --git a/lib/dpkg-priv.h b/lib/dpkg-priv.h index ff42869a..f5fb1445 100644 --- a/lib/dpkg-priv.h +++ b/lib/dpkg-priv.h @@ -26,6 +26,12 @@ extern "C" { #endif +/* Language definitions. */ + +#ifndef sizeof_array +#define sizeof_array(a) (sizeof(a) / sizeof((a)[0])) +#endif + /* Path handling. */ void rtrim_slash_slashdot(char *path); diff --git a/lib/parse.c b/lib/parse.c index d562566b..a1937357 100644 --- a/lib/parse.c +++ b/lib/parse.c @@ -34,6 +34,7 @@ #include #include +#include #include "parsedump.h" #ifdef HAVE_MMAP @@ -76,8 +77,8 @@ const struct fieldinfo fieldinfos[]= { /* Note that aliases are added to the nicknames table in parsehelp.c. */ { NULL /* sentinel - tells code that list is ended */ } }; -#define NFIELDS (sizeof(fieldinfos)/sizeof(struct fieldinfo)) -const int nfields= NFIELDS; + +const int nfields = sizeof_array(fieldinfos); int parsedb(const char *filename, enum parsedbflags flags, struct pkginfo **donep, FILE *warnto, int *warncount) { @@ -92,7 +93,7 @@ int parsedb(const char *filename, enum parsedbflags flags, struct trigaw *ta; int lno; int pdone; - int fieldencountered[NFIELDS]; + int fieldencountered[sizeof_array(fieldinfos)]; const struct fieldinfo *fip; const struct nickname *nick; char *data, *dataptr, *endptr; diff --git a/lib/subproc.c b/lib/subproc.c index cfcfe0dc..85a9162b 100644 --- a/lib/subproc.c +++ b/lib/subproc.c @@ -29,9 +29,8 @@ #include #include -#define NCATCHSIGNALS (int)(sizeof(catch_signals) / sizeof(int) - 1) static int catch_signals[] = { SIGQUIT, SIGINT, 0 }; -static struct sigaction uncatch_signals[NCATCHSIGNALS]; +static struct sigaction uncatch_signals[sizeof_array(catch_signals)]; void setup_subproc_signals(const char *name) @@ -44,7 +43,7 @@ setup_subproc_signals(const char *name) catchsig.sa_handler = SIG_IGN; sigemptyset(&catchsig.sa_mask); catchsig.sa_flags = 0; - for (i = 0; i < NCATCHSIGNALS; i++) + for (i = 0; i < sizeof_array(catch_signals); i++) if (sigaction(catch_signals[i], &catchsig, &uncatch_signals[i])) ohshite(_("unable to ignore signal %s before running %.250s"), strsignal(catch_signals[i]), name); @@ -57,7 +56,7 @@ cu_subproc_signals(int argc, void **argv) { int i; - for (i = 0; i < NCATCHSIGNALS; i++) { + for (i = 0; i < sizeof_array(catch_signals); i++) { if (sigaction(catch_signals[i], &uncatch_signals[i], NULL)) { fprintf(stderr, _("error un-catching signal %s: %s\n"), strsignal(catch_signals[i]), strerror(errno));