restore keywords
[unix-history] / usr / src / old / mkpasswd / mkpasswd.c
index d0c359b..a3fe0f3 100644 (file)
@@ -1,6 +1,18 @@
+/*
+ * Copyright (c) 1983 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ */
+
+#ifndef lint
+char copyright[] =
+"@(#) Copyright (c) 1980 Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif not lint
+
 #ifndef lint
 #ifndef lint
-static char *sccsid = "@(#)mkpasswd.c  4.3 (Berkeley) 84/01/25";
-#endif
+static char sccsid[] = "@(#)mkpasswd.c 5.1 (Berkeley) %G%";
+#endif not lint
 
 #include <sys/file.h>
 #include <stdio.h>
 
 #include <sys/file.h>
 #include <stdio.h>
@@ -8,16 +20,12 @@ static       char *sccsid = "@(#)mkpasswd.c  4.3 (Berkeley) 84/01/25";
 #include <ndbm.h>
 
 char   buf[BUFSIZ];
 #include <ndbm.h>
 
 char   buf[BUFSIZ];
-char   line[BUFSIZ+1];
-char   EMPTY[] = "";
-struct passwd passwd;
 
 struct passwd *fgetpwent();
 
 main(argc, argv)
        char *argv[];
 {
 
 struct passwd *fgetpwent();
 
 main(argc, argv)
        char *argv[];
 {
-       FILE *pwf;
        DBM *dp;
        datum key, content;
        register char *cp, *tp;
        DBM *dp;
        datum key, content;
        register char *cp, *tp;
@@ -32,27 +40,30 @@ main(argc, argv)
                fprintf(stderr, "usage: mkpasswd [ -v ] file\n");
                exit(1);
        }
                fprintf(stderr, "usage: mkpasswd [ -v ] file\n");
                exit(1);
        }
-       if ((pwf = fopen(argv[1], "r" )) == NULL) {
+       if (access(argv[1], R_OK) < 0) {
                fprintf(stderr, "mkpasswd: ");
                perror(argv[1]);
                exit(1);
        }
        umask(0);
                fprintf(stderr, "mkpasswd: ");
                perror(argv[1]);
                exit(1);
        }
        umask(0);
-       dp = ndbmopen(argv[1], O_WRONLY|O_CREAT|O_EXCL, 0644);
+       dp = dbm_open(argv[1], O_WRONLY|O_CREAT|O_EXCL, 0644);
        if (dp == NULL) {
                fprintf(stderr, "mkpasswd: ");
                perror(argv[1]);
                exit(1);
        }
        if (dp == NULL) {
                fprintf(stderr, "mkpasswd: ");
                perror(argv[1]);
                exit(1);
        }
-       dp->db_maxbno = 0;
-       while (pwd = fgetpwent(pwf)) {
+       setpwfile(argv[1]);
+       while (pwd = getpwent()) {
                cp = buf;
 #define        COMPACT(e)      tp = pwd->pw_/**/e; while (*cp++ = *tp++);
                COMPACT(name);
                COMPACT(passwd);
                cp = buf;
 #define        COMPACT(e)      tp = pwd->pw_/**/e; while (*cp++ = *tp++);
                COMPACT(name);
                COMPACT(passwd);
-               *(int *)cp = pwd->pw_uid; cp += sizeof (int);
-               *(int *)cp = pwd->pw_gid; cp += sizeof (int);
-               *(int *)cp = pwd->pw_quota; cp += sizeof (int);
+               bcopy((char *)&pwd->pw_uid, cp, sizeof (int));
+               cp += sizeof (int);
+               bcopy((char *)&pwd->pw_gid, cp, sizeof (int));
+               cp += sizeof (int);
+               bcopy((char *)&pwd->pw_quota, cp, sizeof (int));
+               cp += sizeof (int);
                COMPACT(comment);
                COMPACT(gecos);
                COMPACT(dir);
                COMPACT(comment);
                COMPACT(gecos);
                COMPACT(dir);
@@ -63,53 +74,23 @@ main(argc, argv)
                        printf("store %s, uid %d\n", pwd->pw_name, pwd->pw_uid);
                key.dptr = pwd->pw_name;
                key.dsize = strlen(pwd->pw_name);
                        printf("store %s, uid %d\n", pwd->pw_name, pwd->pw_uid);
                key.dptr = pwd->pw_name;
                key.dsize = strlen(pwd->pw_name);
-               dbmstore(dp, key, content, DB_INSERT);
+               if (dbm_store(dp, key, content, DBM_INSERT) < 0) {
+                       fprintf(stderr, "mkpasswd: ");
+                       perror("dbm_store failed");
+                       exit(1);
+               }
                key.dptr = (char *)&pwd->pw_uid;
                key.dsize = sizeof (int);
                key.dptr = (char *)&pwd->pw_uid;
                key.dsize = sizeof (int);
-               dbmstore(dp, key, content, DB_INSERT);
+               if (dbm_store(dp, key, content, DBM_INSERT) < 0) {
+                       fprintf(stderr, "mkpasswd: ");
+                       perror("dbm_store failed");
+                       exit(1);
+               }
                entries++;
                if (cp - buf > maxlen)
                        maxlen = cp - buf;
        }
                entries++;
                if (cp - buf > maxlen)
                        maxlen = cp - buf;
        }
+       dbm_close(dp);
        printf("%d password entries, maximum length %d\n", entries, maxlen);
        exit(0);
 }
        printf("%d password entries, maximum length %d\n", entries, maxlen);
        exit(0);
 }
-
-static char *
-pwskip(p)
-register char *p;
-{
-       while( *p && *p != ':' )
-               ++p;
-       if( *p ) *p++ = 0;
-       return(p);
-}
-
-struct passwd *
-fgetpwent(pwf)
-       FILE *pwf;
-{
-       register char *p;
-
-       p = fgets(line, BUFSIZ, pwf);
-       if (p==NULL)
-               return(0);
-       passwd.pw_name = p;
-       p = pwskip(p);
-       passwd.pw_passwd = p;
-       p = pwskip(p);
-       passwd.pw_uid = atoi(p);
-       p = pwskip(p);
-       passwd.pw_gid = atoi(p);
-       passwd.pw_quota = 0;
-       passwd.pw_comment = EMPTY;
-       p = pwskip(p);
-       passwd.pw_gecos = p;
-       p = pwskip(p);
-       passwd.pw_dir = p;
-       p = pwskip(p);
-       passwd.pw_shell = p;
-       while(*p && *p != '\n') p++;
-       *p = '\0';
-       return(&passwd);
-}