1 /*- 2 * Copyright (c) 1990 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 static char sccsid[] = "@(#)pw_copy.c 5.1 (Berkeley) 02/12/91"; 10 #endif /* not lint */ 11 12 /* 13 * This module is used to copy the master password file, replacing a single 14 * record, by chpass(1) and passwd(1). 15 */ 16 17 #include <pwd.h> 18 #include <stdio.h> 19 #include <string.h> 20 21 extern char *tempname; 22 23 pw_copy(ffd, pw) 24 int ffd; 25 struct passwd *pw; 26 { 27 register FILE *from, *to; 28 register int done; 29 register char *p; 30 char buf[8192]; 31 32 if (!(from = fdopen(ffd, "r"))) 33 pw_error(_PATH_MASTERPASSWD, 1, 1); 34 if (!(to = fopen(tempname, "w"))) 35 pw_error(tempname, 1, 1); 36 37 for (done = 0; fgets(buf, sizeof(buf), from);) { 38 if (!index(buf, '\n')) { 39 (void)fprintf(stderr, "chpass: %s: line too long\n", 40 _PATH_MASTERPASSWD); 41 pw_error((char *)NULL, 0, 1); 42 } 43 if (done) { 44 (void)fprintf(to, "%s", buf); 45 continue; 46 } 47 if (!(p = index(buf, ':'))) { 48 (void)fprintf(stderr, "chpass: %s: corrupted entry\n", 49 _PATH_MASTERPASSWD); 50 pw_error((char *)NULL, 0, 1); 51 } 52 *p = '\0'; 53 if (strcmp(buf, pw->pw_name)) { 54 *p = ':'; 55 (void)fprintf(to, "%s", buf); 56 continue; 57 } 58 (void)fprintf(to, "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n", 59 pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid, 60 pw->pw_class, pw->pw_change, pw->pw_expire, pw->pw_gecos, 61 pw->pw_dir, pw->pw_shell); 62 done = 1; 63 } 64 if (!done) 65 (void)fprintf(to, "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n", 66 pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid, 67 pw->pw_class, pw->pw_change, pw->pw_expire, pw->pw_gecos, 68 pw->pw_dir, pw->pw_shell); 69 (void)fclose(to); 70 } 71