Use asprintf for string construction
[openafs.git] / src / ptserver / db_verify.c
index ce36378..6a04ab1 100644 (file)
@@ -1,16 +1,17 @@
 /*
  * Copyright 2000, International Business Machines Corporation and others.
  * All Rights Reserved.
- * 
+ *
  * This software has been released under the terms of the IBM Public
  * License.  For details, see the LICENSE file in the top-level source
  * directory or online at http://www.openafs.org/dl/license10.html
  */
 
-
 #include <afsconfig.h>
 #include <afs/param.h>
+#include <afs/stds.h>
 
+#include <roken.h>
 
 /*
  *                      (3) Define a structure, idused, instead of an
  *                          conditions.
  */
 
-#include <afs/stds.h>
-#include <sys/types.h>
+
 #ifdef AFS_NT40_ENV
-#include <winsock2.h>
 #include <WINNT/afsevent.h>
-#include <io.h>
 #else
-#include <netdb.h>
-#include <netinet/in.h>
 #include <sys/file.h>
 #endif
-#include <stdio.h>
-#include <string.h>
-#include <errno.h>
-#include <fcntl.h>
+
 #include <afs/cellconfig.h>
 #include <afs/afsutil.h>
 #include <ubik.h>
@@ -138,11 +131,11 @@ IDHash(afs_int32 x)
 }
 
 static afs_int32
-NameHash(register char *aname)
+NameHash(char *aname)
 {
     /* returns hash bucket for aname */
-    register unsigned int hash = 0;
-    register int i;
+    unsigned int hash = 0;
+    int i;
 /* stolen directly from the HashString function in the vol package */
     for (i = strlen(aname), aname += i - 1; i--; aname--)
        hash = (hash * 31) + (*(unsigned char *)aname - 31);
@@ -211,13 +204,13 @@ readUbikHeader(struct misc_data *misc)
     /* now read the info */
     r = read(fd, &uheader, sizeof(uheader));
     if (r != sizeof(uheader)) {
-       printf("error: read of %lu bytes failed: %d %d\n", sizeof(uheader), r,
-              errno);
+       printf("error: read of %" AFS_SIZET_FMT " bytes failed: %d %d\n",
+              sizeof(uheader), r, errno);
        return (-1);
     }
 
     uheader.magic = ntohl(uheader.magic);
-    uheader.size = ntohl(uheader.size);
+    uheader.size = ntohs(uheader.size);
     uheader.version.epoch = ntohl(uheader.version.epoch);
     uheader.version.counter = ntohl(uheader.version.counter);
 
@@ -402,7 +395,7 @@ WalkNextChain(char map[],           /* one byte per db entry */
     int noErrors = 1;
     int length;                        /* length of chain */
 #if defined(SUPERGROUPS)
-    int sgcount;               /* number of sgentrys */
+    int sgcount = 0;           /* number of sgentrys */
     afs_int32 sghead;
 #define g (((struct prentryg *)e))
 #endif
@@ -412,7 +405,6 @@ WalkNextChain(char map[],           /* one byte per db entry */
        eid = ntohl(e->id);
        bit = MAP_CONT;
 #if defined(SUPERGROUPS)
-       sgcount = 0;
        sghead = ntohl(g->next);
 #endif
        for (i = 0; i < PRSIZE; i++) {
@@ -932,12 +924,12 @@ GC(char map[], struct misc_data *misc)
            id = ntohl(e.id);
 #if defined(SUPERGROUPS)
            if ((id != ANONYMOUSID)
-               && ((refCount = idcount(&misc->idmap, id)) != ntohl(e.count))) 
+               && ((refCount = idcount(&misc->idmap, id)) != ntohl(e.count)))
 #else
            if ((id >= misc->minId) && (id <= misc->maxId)
                && (id != ANONYMOUSID)
                && ((refCount = misc->idmap[id - misc->minId]) !=
-                   ntohl(e.count))) 
+                   ntohl(e.count)))
 #endif /* SUPERGROUPS */
              {
                afs_int32 na;
@@ -970,10 +962,7 @@ QuoteName(char *s)
 {
     char *qs;
     if (strpbrk(s, " \t")) {
-       qs = (char *)malloc(strlen(s) + 3);
-       strcpy(qs, "\"");
-       strcat(qs, s);
-       strcat(qs, "\"");
+       asprintf(&qs, "\"%s\"", s);
     } else
        qs = s;
     return qs;
@@ -1041,9 +1030,9 @@ DumpRecreate(char map[], struct misc_data *misc)
                /* check for duplicate id.  This may still lead to duplicate
                 * names. */
 #if defined(SUPERGROUPS)
-               if (idcount(&idmap, id)) 
+               if (idcount(&idmap, id))
 #else
-               if (idmap[id - misc->minId]) 
+               if (idmap[id - misc->minId])
 #endif
                  {
                    fprintf(stderr, "Skipping entry with duplicate id %di\n",
@@ -1152,9 +1141,9 @@ DumpRecreate(char map[], struct misc_data *misc)
 
            owner = ntohl(e.owner);
 #if defined(SUPERGROUPS)
-           if (!idcount(&idmap, owner)) 
+           if (!idcount(&idmap, owner))
 #else
-           if (idmap[owner - misc->minId] == 0) 
+           if (idmap[owner - misc->minId] == 0)
 #endif
              {
                fprintf(stderr,
@@ -1181,9 +1170,6 @@ DumpRecreate(char map[], struct misc_data *misc)
            if ((id < 0) && (flags & PRGRP)) {
                int count = 0;
                afs_int32 na;
-#if defined(SUPERGROUPS)
-               afs_int32 ng;
-#endif
                int i;
                for (i = 0; i < PRSIZE; i++) {
                    afs_int32 uid = ntohl(e.entries[i]);
@@ -1202,43 +1188,6 @@ DumpRecreate(char map[], struct misc_data *misc)
                                id);
 #endif
                }
-#if defined(SUPERGROUPS)
-#define g      (*((struct prentryg *)&e))
-               ng = ntohl(g.nextsg);
-               for (i = 0; i < SGSIZE; i++) {
-                   afs_int32 uid = ntohl(g.supergroup[i]);
-                   if (uid == 0)
-                       break;
-                   if (uid == PRBADID)
-                       continue;
-                   fprintf(rc, "au %d %d\n", uid, id);
-                   count++;
-               }
-               while (ng) {
-                   struct prentry c;
-                   code = pr_Read(ng, (char *)&c, sizeof(c));
-                   if (code)
-                       return code;
-
-                   if ((id == ntohl(c.id)) && (c.flags & htonl(PRCONT))) {
-                       for (i = 0; i < COSIZE; i++) {
-                           afs_int32 uid = ntohl(c.entries[i]);
-                           if (uid == 0)
-                               break;
-                           if (uid == PRBADID)
-                               continue;
-                           fprintf(rc, "au %d %d\n", uid, id);
-                           count++;
-                       }
-                   } else {
-                       fprintf(stderr, "Skipping continuation block at %d\n",
-                               ng);
-                       break;
-                   }
-                   ng = ntohl(c.next);
-               }
-#undef g
-#endif /* SUPERGROUPS */
                na = ntohl(e.next);
                while (na) {
                    struct prentry c;
@@ -1296,7 +1245,8 @@ CheckPrDatabase(struct misc_data *misc)   /* info & statistics */
     n = eof / sizeof(struct prentry);
     if ((eof < 0) || (n * sizeof(struct prentry) != eof)) {
        code = PRDBBAD;
-       afs_com_err(whoami, code, "eof ptr no good: eof=%d, sizeof(prentry)=%d",
+       afs_com_err(whoami, code,
+                   "eof ptr no good: eof=%d, sizeof(prentry)=%" AFS_SIZET_FMT,
                eof, sizeof(struct prentry));
       abort:
        return code;
@@ -1423,7 +1373,7 @@ CheckPrDatabase(struct misc_data *misc)   /* info & statistics */
     free(map);
     return code;
 }
-    
+
 #include "AFS_component_version_number.c"
 
 int
@@ -1510,7 +1460,7 @@ void
 zeromap(struct idused *idmap)
 {
     while (idmap) {
-       memset((char *)idmap->idcount, 0, sizeof idmap->idcount);
+       memset(idmap->idcount, 0, sizeof idmap->idcount);
        idmap = idmap->idnext;
     }
 }
@@ -1524,7 +1474,7 @@ inccount(struct idused **idmapp, int id)
        fprintf(stderr, "IDCOUNT must be power of 2!\n");
        exit(1);
     }
-    while (idmap = *idmapp) {
+    while ((idmap = *idmapp) != NULL) {
        if (idmap->idstart == (id & ~(IDCOUNT - 1)))
            break;
        idmapp = &idmap->idnext;
@@ -1535,7 +1485,7 @@ inccount(struct idused **idmapp, int id)
            perror("idmap");
            exit(1);
        }
-       memset((char *)idmap, 0, sizeof idmap);
+       memset(idmap, 0, sizeof idmap);
        idmap->idstart = id & ~(IDCOUNT - 1);
        idmap->idnext = *idmapp;
        *idmapp = idmap;
@@ -1552,7 +1502,7 @@ idcount(struct idused **idmapp, int id)
        fprintf(stderr, "IDCOUNT must be power of 2!\n");
        exit(1);
     }
-    while (idmap = *idmapp) {
+    while ((idmap = *idmapp) != NULL) {
        if (idmap->idstart == (id & ~(IDCOUNT - 1))) {
            return idmap->idcount[id & (IDCOUNT - 1)];
        }