make output line buffered if selecting only some of the entries
[unix-history] / usr / src / usr.bin / chpass / util.c
CommitLineData
0c82d36d
KB
1/*
2 * Copyright (c) 1988 The 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 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 */
17
18#ifndef lint
1e1166d4 19static char sccsid[] = "@(#)util.c 5.12 (Berkeley) %G%";
0c82d36d
KB
20#endif /* not lint */
21
22#include <sys/types.h>
23#include <sys/time.h>
24#include <tzfile.h>
cac8e61f 25#include <pwd.h>
0c82d36d 26#include <stdio.h>
0c82d36d
KB
27#include <strings.h>
28#include <ctype.h>
435e8dff 29#include "chpass.h"
cac8e61f 30#include "pathnames.h"
0c82d36d
KB
31
32static int dmsize[] =
33 { -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
34static char *months[] =
ef97d022
KB
35 { "January", "February", "March", "April", "May", "June",
36 "July", "August", "September", "October", "November",
37 "December", NULL };
0c82d36d
KB
38char *
39ttoa(tval)
40 time_t tval;
41{
42 struct tm *tp;
db763e95 43 static char tbuf[50];
0c82d36d 44
ef97d022 45 if (tval) {
0c82d36d 46 tp = localtime(&tval);
d264b2a3
KB
47 (void)sprintf(tbuf, "%s %d, 19%d", months[tp->tm_mon],
48 tp->tm_mday, tp->tm_year);
0c82d36d 49 }
ef97d022
KB
50 else
51 *tbuf = '\0';
0c82d36d
KB
52 return(tbuf);
53}
54
55atot(p, store)
56 char *p;
57 time_t *store;
58{
59 register char *t, **mp;
60 static struct tm *lt;
61 time_t tval, time();
62 int day, month, year;
63
ef97d022 64 if (!*p) {
0c82d36d
KB
65 *store = 0;
66 return(0);
67 }
68 if (!lt) {
69 unsetenv("TZ");
70 (void)time(&tval);
71 lt = localtime(&tval);
72 }
d264b2a3 73 if (!(t = strtok(p, " \t")))
0c82d36d
KB
74 goto bad;
75 for (mp = months;; ++mp) {
76 if (!*mp)
77 goto bad;
78 if (!strncasecmp(*mp, t, 3)) {
79 month = mp - months + 1;
80 break;
81 }
82 }
d264b2a3
KB
83 if (!(t = strtok((char *)NULL, " \t,")) || !isdigit(*t))
84 goto bad;
85 day = atoi(t);
86 if (!(t = strtok((char *)NULL, " \t,")) || !isdigit(*t))
0c82d36d
KB
87 goto bad;
88 year = atoi(t);
89 if (day < 1 || day > 31 || month < 1 || month > 12 || !year)
90 goto bad;
91 if (year < 100)
92 year += TM_YEAR_BASE;
93 if (year <= EPOCH_YEAR)
94bad: return(1);
95 tval = isleap(year) && month > 2;
96 for (--year; year >= EPOCH_YEAR; --year)
97 tval += isleap(year) ?
791ff54b 98 DAYSPERLYEAR : DAYSPERNYEAR;
0c82d36d
KB
99 while (--month)
100 tval += dmsize[month];
db763e95 101 tval += day;
791ff54b 102 tval = tval * HOURSPERDAY * MINSPERHOUR * SECSPERMIN;
0c82d36d
KB
103 tval -= lt->tm_gmtoff;
104 *store = tval;
105 return(0);
106}
107
1e1166d4
KB
108/*
109 * print --
110 * print out the file for the user to edit; strange side-effect:
111 * return if the user is allowed to modify their shell.
112 */
0c82d36d
KB
113print(fp, pw)
114 FILE *fp;
115 struct passwd *pw;
116{
117 register char *p;
1e1166d4 118 int shellval;
34dda72c 119 char *getusershell(), *ok_shell(), *ttoa();
0c82d36d 120
1e1166d4 121 shellval = 1;
34dda72c 122 (void)fprintf(fp, "#Changing user database information for %s.\n",
0c82d36d
KB
123 pw->pw_name);
124 if (!uid) {
34dda72c
KB
125 (void)fprintf(fp, "Login: %s\n", pw->pw_name);
126 (void)fprintf(fp, "Password: %s\n", pw->pw_passwd);
127 (void)fprintf(fp, "Uid [#]: %d\n", pw->pw_uid);
128 (void)fprintf(fp, "Gid [# or name]: %d\n", pw->pw_gid);
129 (void)fprintf(fp, "Change [month day year]: %s\n",
130 ttoa(pw->pw_change));
131 (void)fprintf(fp, "Expire [month day year]: %s\n",
132 ttoa(pw->pw_expire));
133 (void)fprintf(fp, "Class: %s\n", pw->pw_class);
134 (void)fprintf(fp, "Home directory: %s\n", pw->pw_dir);
135 (void)fprintf(fp, "Shell: %s\n",
cac8e61f 136 *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
0c82d36d 137 }
34dda72c
KB
138 /* only admin can change "restricted" shells */
139 else if (ok_shell(pw->pw_shell))
140 (void)fprintf(fp, "Shell: %s\n",
141 *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
1e1166d4
KB
142 else
143 shellval = 0;
0c82d36d 144 p = strsep(pw->pw_gecos, ",");
34dda72c 145 (void)fprintf(fp, "Full Name: %s\n", p ? p : "");
0c82d36d 146 p = strsep((char *)NULL, ",");
34dda72c 147 (void)fprintf(fp, "Location: %s\n", p ? p : "");
0c82d36d 148 p = strsep((char *)NULL, ",");
34dda72c 149 (void)fprintf(fp, "Office Phone: %s\n", p ? p : "");
de29b759 150 p = strsep((char *)NULL, ",");
34dda72c 151 (void)fprintf(fp, "Home Phone: %s\n", p ? p : "");
1e1166d4 152 return(shellval);
34dda72c
KB
153}
154
155char *
156ok_shell(name)
157 register char *name;
158{
159 register char *p, *sh;
160 char *getusershell();
161
162 setusershell();
163 while (sh = getusershell()) {
164 if (!strcmp(name, sh))
165 return(name);
166 /* allow just shell name, but use "real" path */
167 if ((p = rindex(sh, '/')) && !strcmp(name, p + 1))
168 return(sh);
169 }
170 return(NULL);
0c82d36d 171}