BSD 4_3_Tahoe release
[unix-history] / usr / src / etc / mkpasswd.c
CommitLineData
380054c3 1/*
ca67e7b4
C
2 * Copyright (c) 1980, 1983 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
380054c3
DF
16 */
17
18#ifndef lint
19char copyright[] =
ca67e7b4 20"@(#) Copyright (c) 1980, 1983 Regents of the University of California.\n\
380054c3 21 All rights reserved.\n";
ca67e7b4 22#endif /* not lint */
380054c3 23
2b55f05a 24#ifndef lint
ca67e7b4
C
25static char sccsid[] = "@(#)mkpasswd.c 5.3 (Berkeley) 6/18/88";
26#endif /* not lint */
2b55f05a
RC
27
28#include <sys/file.h>
29#include <stdio.h>
30#include <pwd.h>
31#include <ndbm.h>
32
2b55f05a 33main(argc, argv)
ca67e7b4 34 int argc;
2b55f05a
RC
35 char *argv[];
36{
37 DBM *dp;
38 datum key, content;
39 register char *cp, *tp;
40 register struct passwd *pwd;
41 int verbose = 0, entries = 0, maxlen = 0;
ca67e7b4 42 char buf[BUFSIZ];
2b55f05a
RC
43
44 if (argc > 1 && strcmp(argv[1], "-v") == 0) {
45 verbose++;
46 argv++, argc--;
47 }
48 if (argc != 2) {
49 fprintf(stderr, "usage: mkpasswd [ -v ] file\n");
50 exit(1);
51 }
0cc97e50 52 if (access(argv[1], R_OK) < 0) {
63e9078a
RC
53 fprintf(stderr, "mkpasswd: ");
54 perror(argv[1]);
55 exit(1);
56 }
ca67e7b4 57 (void)umask(0);
403de8f5 58 dp = dbm_open(argv[1], O_WRONLY|O_CREAT|O_EXCL, 0644);
2b55f05a 59 if (dp == NULL) {
63e9078a 60 fprintf(stderr, "mkpasswd: ");
2b55f05a
RC
61 perror(argv[1]);
62 exit(1);
63 }
0cc97e50
RC
64 setpwfile(argv[1]);
65 while (pwd = getpwent()) {
2b55f05a 66 cp = buf;
ca67e7b4
C
67#define COMPACT(e) tp = pwd->e; while (*cp++ = *tp++);
68 COMPACT(pw_name);
69 COMPACT(pw_passwd);
1ed2084e
RC
70 bcopy((char *)&pwd->pw_uid, cp, sizeof (int));
71 cp += sizeof (int);
72 bcopy((char *)&pwd->pw_gid, cp, sizeof (int));
73 cp += sizeof (int);
74 bcopy((char *)&pwd->pw_quota, cp, sizeof (int));
75 cp += sizeof (int);
ca67e7b4
C
76 COMPACT(pw_comment);
77 COMPACT(pw_gecos);
78 COMPACT(pw_dir);
79 COMPACT(pw_shell);
2b55f05a
RC
80 content.dptr = buf;
81 content.dsize = cp - buf;
82 if (verbose)
83 printf("store %s, uid %d\n", pwd->pw_name, pwd->pw_uid);
84 key.dptr = pwd->pw_name;
85 key.dsize = strlen(pwd->pw_name);
403de8f5
RC
86 if (dbm_store(dp, key, content, DBM_INSERT) < 0) {
87 fprintf(stderr, "mkpasswd: ");
88 perror("dbm_store failed");
89 exit(1);
90 }
2b55f05a
RC
91 key.dptr = (char *)&pwd->pw_uid;
92 key.dsize = sizeof (int);
403de8f5
RC
93 if (dbm_store(dp, key, content, DBM_INSERT) < 0) {
94 fprintf(stderr, "mkpasswd: ");
95 perror("dbm_store failed");
96 exit(1);
97 }
2b55f05a
RC
98 entries++;
99 if (cp - buf > maxlen)
100 maxlen = cp - buf;
101 }
403de8f5 102 dbm_close(dp);
2b55f05a
RC
103 printf("%d password entries, maximum length %d\n", entries, maxlen);
104 exit(0);
105}