file reorg, pathnames.h, paths.h
[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
435e8dff 19static char sccsid[] = "@(#)util.c 5.10 (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
108print(fp, pw)
109 FILE *fp;
110 struct passwd *pw;
111{
112 register char *p;
113 char *getusershell(), *ttoa();
114
7df40547 115 fprintf(fp, "#Changing user database information for %s.\n",
0c82d36d
KB
116 pw->pw_name);
117 if (!uid) {
118 fprintf(fp, "Login: %s\n", pw->pw_name);
8557219a 119 fprintf(fp, "Password: %s\n", pw->pw_passwd);
0c82d36d
KB
120 fprintf(fp, "Uid [#]: %d\n", pw->pw_uid);
121 fprintf(fp, "Gid [# or name]: %d\n", pw->pw_gid);
ef97d022
KB
122 fprintf(fp, "Change [month day year]: %s\n", ttoa(pw->pw_change));
123 fprintf(fp, "Expire [month day year]: %s\n", ttoa(pw->pw_expire));
0c82d36d
KB
124 fprintf(fp, "Class: %s\n", pw->pw_class);
125 fprintf(fp, "Home directory: %s\n", pw->pw_dir);
126 fprintf(fp, "Shell: %s\n",
cac8e61f 127 *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
0c82d36d
KB
128 }
129 else {
130 /* only admin can change "restricted" shells */
131 setusershell();
132 for (;;)
133 if (!(p = getusershell()))
134 break;
135 else if (!strcmp(pw->pw_shell, p)) {
cac8e61f
KB
136 fprintf(fp, "Shell: %s\n", *pw->pw_shell ?
137 pw->pw_shell : _PATH_BSHELL);
0c82d36d
KB
138 break;
139 }
140 }
141 p = strsep(pw->pw_gecos, ",");
142 fprintf(fp, "Full Name: %s\n", p ? p : "");
143 p = strsep((char *)NULL, ",");
144 fprintf(fp, "Location: %s\n", p ? p : "");
145 p = strsep((char *)NULL, ",");
0c82d36d 146 fprintf(fp, "Office Phone: %s\n", p ? p : "");
de29b759
KB
147 p = strsep((char *)NULL, ",");
148 fprintf(fp, "Home Phone: %s\n", p ? p : "");
0c82d36d 149}