Przeglądaj źródła

Use sizeof_array() instead of ad-hoc calculations

Guillem Jover 18 lat temu
rodzic
commit
bd40cd8d18
4 zmienionych plików z 13 dodań i 8 usunięć
  1. 0 1
      TODO
  2. 6 0
      lib/dpkg-priv.h
  3. 4 3
      lib/parse.c
  4. 3 4
      lib/subproc.c

+ 0 - 1
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.
 

+ 6 - 0
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);

+ 4 - 3
lib/parse.c

@@ -34,6 +34,7 @@
 
 #include <dpkg.h>
 #include <dpkg-db.h>
+#include <dpkg-priv.h>
 #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;

+ 3 - 4
lib/subproc.c

@@ -29,9 +29,8 @@
 #include <dpkg.h>
 #include <dpkg-priv.h>
 
-#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));