restore keywords
[unix-history] / usr / src / old / mkpasswd / mkpasswd.c
CommitLineData
380054c3
DF
1/*
2 * Copyright (c) 1983 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 */
6
7#ifndef lint
8char copyright[] =
9"@(#) Copyright (c) 1980 Regents of the University of California.\n\
10 All rights reserved.\n";
11#endif not lint
12
2b55f05a 13#ifndef lint
380054c3
DF
14static char sccsid[] = "@(#)mkpasswd.c 5.1 (Berkeley) %G%";
15#endif not lint
2b55f05a
RC
16
17#include <sys/file.h>
18#include <stdio.h>
19#include <pwd.h>
20#include <ndbm.h>
21
22char buf[BUFSIZ];
63e9078a
RC
23
24struct passwd *fgetpwent();
2b55f05a
RC
25
26main(argc, argv)
27 char *argv[];
28{
29 DBM *dp;
30 datum key, content;
31 register char *cp, *tp;
32 register struct passwd *pwd;
33 int verbose = 0, entries = 0, maxlen = 0;
34
35 if (argc > 1 && strcmp(argv[1], "-v") == 0) {
36 verbose++;
37 argv++, argc--;
38 }
39 if (argc != 2) {
40 fprintf(stderr, "usage: mkpasswd [ -v ] file\n");
41 exit(1);
42 }
0cc97e50 43 if (access(argv[1], R_OK) < 0) {
63e9078a
RC
44 fprintf(stderr, "mkpasswd: ");
45 perror(argv[1]);
46 exit(1);
47 }
2b55f05a 48 umask(0);
403de8f5 49 dp = dbm_open(argv[1], O_WRONLY|O_CREAT|O_EXCL, 0644);
2b55f05a 50 if (dp == NULL) {
63e9078a 51 fprintf(stderr, "mkpasswd: ");
2b55f05a
RC
52 perror(argv[1]);
53 exit(1);
54 }
0cc97e50
RC
55 setpwfile(argv[1]);
56 while (pwd = getpwent()) {
2b55f05a
RC
57 cp = buf;
58#define COMPACT(e) tp = pwd->pw_/**/e; while (*cp++ = *tp++);
59 COMPACT(name);
60 COMPACT(passwd);
1ed2084e
RC
61 bcopy((char *)&pwd->pw_uid, cp, sizeof (int));
62 cp += sizeof (int);
63 bcopy((char *)&pwd->pw_gid, cp, sizeof (int));
64 cp += sizeof (int);
65 bcopy((char *)&pwd->pw_quota, cp, sizeof (int));
66 cp += sizeof (int);
2b55f05a
RC
67 COMPACT(comment);
68 COMPACT(gecos);
69 COMPACT(dir);
70 COMPACT(shell);
71 content.dptr = buf;
72 content.dsize = cp - buf;
73 if (verbose)
74 printf("store %s, uid %d\n", pwd->pw_name, pwd->pw_uid);
75 key.dptr = pwd->pw_name;
76 key.dsize = strlen(pwd->pw_name);
403de8f5
RC
77 if (dbm_store(dp, key, content, DBM_INSERT) < 0) {
78 fprintf(stderr, "mkpasswd: ");
79 perror("dbm_store failed");
80 exit(1);
81 }
2b55f05a
RC
82 key.dptr = (char *)&pwd->pw_uid;
83 key.dsize = sizeof (int);
403de8f5
RC
84 if (dbm_store(dp, key, content, DBM_INSERT) < 0) {
85 fprintf(stderr, "mkpasswd: ");
86 perror("dbm_store failed");
87 exit(1);
88 }
2b55f05a
RC
89 entries++;
90 if (cp - buf > maxlen)
91 maxlen = cp - buf;
92 }
403de8f5 93 dbm_close(dp);
2b55f05a
RC
94 printf("%d password entries, maximum length %d\n", entries, maxlen);
95 exit(0);
96}