ptserver: Tidy malloc handling in readpwd
[openafs.git] / src / ptserver / readpwd.c
index bd60991..e0c5c28 100644 (file)
@@ -1,42 +1,48 @@
 /*
  * 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 <stdio.h>
-#ifndef AFS_NT40_ENV
-#include <strings.h>
-#else
+
+#include <roken.h>
+
+#ifdef AFS_NT40_ENV
 #include <WINNT/afsevent.h>
 #endif
+
 #include <rx/rx.h>
 #include <rx/xdr.h>
 #include <afs/cellconfig.h>
 #include <afs/afsutil.h>
+#include <afs/com_err.h>
+
 #include "ptclient.h"
+#include "ptuser.h"
+#include "ptprototypes.h"
 
-int osi_audit()
+int
+osi_audit(void)
 {
 /* OK, this REALLY sucks bigtime, but I can't tell who is calling
  * afsconf_CheckAuth easily, and only *SERVERS* should be calling osi_audit
  * anyway.  It's gonna give somebody fits to debug, I know, I know.
  */
-return 0;
+    return 0;
 }
 
 #include "AFS_component_version_number.c"
 
-main(argc,argv)
-afs_int32 argc;
-char **argv;
+int
+main(afs_int32 argc, char **argv)
 {
 
-    register afs_int32 code;
+    afs_int32 code;
     char name[PR_MAXNAMELEN];
     afs_int32 id;
     char buf[150];
@@ -47,51 +53,53 @@ char **argv;
     char uid[8];
     afs_int32 i;
     afs_int32 verbose = 0;
-    char *cellname;
+    char *cellname = NULL;
 
     if (argc < 2) {
-       fprintf(stderr,"Usage: readpwd [-v] [-c cellname] passwdfile.\n");
+       fprintf(stderr, "Usage: readpwd [-v] [-c cellname] passwdfile.\n");
        exit(1);
     }
-    cellname = 0;
-    for (i = 1;i<argc;i++) {
-       if (!strcmp(argv[i],"-v"))
+    for (i = 1; i < argc; i++) {
+       if (!strcmp(argv[i], "-v"))
            verbose = 1;
        else {
-           if (!strcmp(argv[i],"-c")) {
-               cellname = (char *)malloc(100);
-               strncpy(cellname,argv[++i],100);
-           }
-           else
-               strncpy(buf,argv[i],150);
+           if (!strcmp(argv[i], "-c")) {
+               if (!cellname)
+                   cellname = malloc(100);
+               strncpy(cellname, argv[++i], 100);
+           } else
+               strncpy(buf, argv[i], 150);
        }
     }
     code = pr_Initialize(2, AFSDIR_CLIENT_ETC_DIRPATH, cellname);
+    if (cellname)
+       free(cellname);
     if (code) {
-       fprintf(stderr,"pr_Initialize failed, code %d.\n",code);
+       fprintf(stderr, "pr_Initialize failed, code %d.\n", code);
        exit(1);
     }
 
 
-    if ((fp= fopen(buf,"r")) == NULL) {
-       fprintf(stderr,"Couldn't open %s.\n",argv[1]);
+    if ((fp = fopen(buf, "r")) == NULL) {
+       fprintf(stderr, "Couldn't open %s.\n", argv[1]);
        exit(2);
     }
-    while ((tmp = fgets(buf,150,fp)) != NULL) {
-       bzero(name,PR_MAXNAMELEN);
-       bzero(uid,8);
-       ptr = index(buf,':');
-       strncpy(name,buf,ptr-buf);
-       aptr = index(++ptr,':');
-       ptr = index(++aptr,':');
-       strncpy(uid,aptr,ptr-aptr);
+    while ((tmp = fgets(buf, 150, fp)) != NULL) {
+       memset(name, 0, PR_MAXNAMELEN);
+       memset(uid, 0, 8);
+       ptr = strchr(buf, ':');
+       strncpy(name, buf, ptr - buf);
+       aptr = strchr(++ptr, ':');
+       ptr = strchr(++aptr, ':');
+       strncpy(uid, aptr, ptr - aptr);
        id = atoi(uid);
        if (verbose)
-           printf("Adding %s with id %d.\n",name,id);
-       code = pr_CreateUser(name,&id);
+           printf("Adding %s with id %d.\n", name, id);
+       code = pr_CreateUser(name, &id);
        if (code) {
-           fprintf(stderr,"Failed to add user %s with id %d!\n",name,id);
-           fprintf(stderr,"%s (%d).\n",pr_ErrorMsg(code),code);
+           fprintf(stderr, "Failed to add user %s with id %d!\n", name, id);
+           fprintf(stderr, "%s (%d).\n", pr_ErrorMsg(code), code);
        }
     }
+    return 0;
 }