pts: Don't malloc(0) when there's nothing to do
[openafs.git] / src / ptserver / pts.c
index 01248f2..b6601d6 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * 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 <roken.h>
 
-#include <stdio.h>
-#include <string.h>
-#ifdef AFS_AIX32_ENV
-#include <signal.h>
-#endif
 #include <ctype.h>
-#include <sys/types.h>
-#include <errno.h>
-#include <afs/cmd.h>
+
 #ifdef AFS_NT40_ENV
-#include <winsock2.h>
 #include <WINNT/afsevent.h>
 #include <WINNT/afsreg.h>
-#else
-#include <netinet/in.h>
 #endif
+
 #include <afs/cellconfig.h>
+#include <afs/afsutil.h>
+#include <afs/com_err.h>
+#include <afs/cmd.h>
 #include <rx/rx.h>
 #include <rx/xdr.h>
+
 #include "ptclient.h"
 #include "ptuser.h"
 #include "pterror.h"
 #include "ptprototypes.h"
-#include <afs/afsutil.h>
-#include <afs/com_err.h>
 
 #undef FOREIGN
 
@@ -223,6 +217,12 @@ GetGlobals(struct cmd_syndesc *as, void *arock)
        else
            confdir = AFSDIR_CLIENT_ETC_DIRPATH;
     }
+
+    if (as->parms[23].items) { /* -config */
+       changed = 1;
+       confdir = as->parms[23].items->data;
+    }
+
     if (changed) {
        CleanUp(as, arock);
        code = pr_Initialize(sec, confdir, cell);
@@ -282,17 +282,16 @@ CreateGroup(struct cmd_syndesc *as, void *arock)
                        idi->data);
                return code;
            }
-           if (id >= 0) {
+           if (id == 0) {
+               printf("0 isn't a valid group id; aborting\n");
+               return EINVAL;
+           }
+           if (id > 0) {
                code = PRBADARG;
                afs_com_err(whoami, code, "because group id %d was not negative",
                        id);
                return code;
            }
-           
-            if (id == 0) {
-               printf("0 isn't a valid user id; aborting\n");
-               return EINVAL;
-           }
 
            idi = idi->next;
        } else
@@ -340,6 +339,12 @@ CreateUser(struct cmd_syndesc *as, void *arock)
                printf("0 isn't a valid user id; aborting\n");
                return EINVAL;
            }
+           if (id < 0) {
+               code = PRBADARG;
+               afs_com_err(whoami, code, "because user id %d was not positive",
+                       id);
+               return code;
+           }
            idi = idi->next;
        } else
            id = 0;
@@ -466,16 +471,26 @@ GetNameOrId(struct cmd_syndesc *as, struct idlist *lids,
     struct idlist ids, tids;   /* local copy, if not ret. ids */
     int goodCount = 0;
 
+    /* Initialise our outputs */
+    memset(lids, 0, sizeof(struct idlist));
+    if (lnames)
+       memset(lnames, 0, sizeof(struct namelist));
+
     for (i = as->parms[0].items; i; i = i->next)
        n++;
-    lids->idlist_val = (afs_int32 *) malloc(n * sizeof(afs_int32));
+
+    /* Nothing to do, so bail */
+    if (n == 0)
+       return 0;
+
+    lids->idlist_val = malloc(n * sizeof(afs_int32));
     lids->idlist_len = n;
-    ids.idlist_val = (afs_int32 *) malloc(n * sizeof(afs_int32));
+    ids.idlist_val = malloc(n * sizeof(afs_int32));
     ids.idlist_len = n;
-    names.namelist_val = (prname *) malloc(n * PR_MAXNAMELEN);
+    names.namelist_val = malloc(n * PR_MAXNAMELEN);
     names.namelist_len = n;
     if (lnames) {
-       lnames->namelist_val = (prname *) malloc(n * PR_MAXNAMELEN);
+       lnames->namelist_val = malloc(n * PR_MAXNAMELEN);
        lnames->namelist_len = 0;
     }
     for (i = as->parms[0].items; i; i = i->next) {
@@ -503,7 +518,7 @@ GetNameOrId(struct cmd_syndesc *as, struct idlist *lids,
        afs_com_err(whoami, code, "so couldn't look up names");
     else {
        for (n = 0; n < tids.idlist_len; n++) {
-           if ((tids.idlist_val[n] == ANONYMOUSID)) {
+           if (tids.idlist_val[n] == ANONYMOUSID) {
                afs_com_err(whoami, PRNOENT, "so couldn't look up id for %s",
                        names.namelist_val[n]);
            } else
@@ -1087,6 +1102,7 @@ add_std_args(struct cmd_syndesc *ts)
                "use user's authentication (default)");
     cmd_AddParm(ts, "-encrypt", CMD_FLAG, CMD_OPTIONAL,
                "encrypt commands");
+    cmd_AddParm(ts, "-config", CMD_SINGLE, CMD_OPTIONAL, "config location");
 }
 
 /*
@@ -1123,8 +1139,8 @@ main(int argc, char **argv)
 
 #ifdef AFS_AIX32_ENV
     /*
-     * The following signal action for AIX is necessary so that in case of a 
-     * crash (i.e. core is generated) we can include the user's data section 
+     * The following signal action for AIX is necessary so that in case of a
+     * crash (i.e. core is generated) we can include the user's data section
      * in the core dump. Unfortunately, by default, only a partial core is
      * generated which, in many cases, isn't too useful.
      */