xref: /netbsd-src/usr.bin/chpass/util.c (revision ce63d6c20fc4ec8ddc95c84bb229e3c4ecf82b69)
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, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 static char sccsid[] = "@(#)util.c	5.14 (Berkeley) 2/12/91";
36 #endif /* not lint */
37 
38 #include <sys/types.h>
39 #include <sys/time.h>
40 #include <tzfile.h>
41 #include <pwd.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <ctype.h>
45 #include "chpass.h"
46 #include "pathnames.h"
47 
48 static int dmsize[] =
49 	{ -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
50 static char *months[] =
51 	{ "January", "February", "March", "April", "May", "June",
52 	  "July", "August", "September", "October", "November",
53 	  "December", NULL };
54 char *
55 ttoa(tval)
56 	time_t tval;
57 {
58 	struct tm *tp;
59 	static char tbuf[50];
60 
61 	if (tval) {
62 		tp = localtime(&tval);
63 		(void)sprintf(tbuf, "%s %d, 19%d", months[tp->tm_mon],
64 		    tp->tm_mday, tp->tm_year);
65 	}
66 	else
67 		*tbuf = '\0';
68 	return(tbuf);
69 }
70 
71 atot(p, store)
72 	char *p;
73 	time_t *store;
74 {
75 	register char *t, **mp;
76 	static struct tm *lt;
77 	time_t tval, time();
78 	int day, month, year;
79 
80 	if (!*p) {
81 		*store = 0;
82 		return(0);
83 	}
84 	if (!lt) {
85 		unsetenv("TZ");
86 		(void)time(&tval);
87 		lt = localtime(&tval);
88 	}
89 	if (!(t = strtok(p, " \t")))
90 		goto bad;
91 	for (mp = months;; ++mp) {
92 		if (!*mp)
93 			goto bad;
94 		if (!strncasecmp(*mp, t, 3)) {
95 			month = mp - months + 1;
96 			break;
97 		}
98 	}
99 	if (!(t = strtok((char *)NULL, " \t,")) || !isdigit(*t))
100 		goto bad;
101 	day = atoi(t);
102 	if (!(t = strtok((char *)NULL, " \t,")) || !isdigit(*t))
103 		goto bad;
104 	year = atoi(t);
105 	if (day < 1 || day > 31 || month < 1 || month > 12 || !year)
106 		goto bad;
107 	if (year < 100)
108 		year += TM_YEAR_BASE;
109 	if (year <= EPOCH_YEAR)
110 bad:		return(1);
111 	tval = isleap(year) && month > 2;
112 	for (--year; year >= EPOCH_YEAR; --year)
113 		tval += isleap(year) ?
114 		    DAYSPERLYEAR : DAYSPERNYEAR;
115 	while (--month)
116 		tval += dmsize[month];
117 	tval += day;
118 	tval = tval * HOURSPERDAY * MINSPERHOUR * SECSPERMIN;
119 	tval -= lt->tm_gmtoff;
120 	*store = tval;
121 	return(0);
122 }
123 
124 char *
125 ok_shell(name)
126 	register char *name;
127 {
128 	register char *p, *sh;
129 	char *getusershell();
130 
131 	setusershell();
132 	while (sh = getusershell()) {
133 		if (!strcmp(name, sh))
134 			return(name);
135 		/* allow just shell name, but use "real" path */
136 		if ((p = rindex(sh, '/')) && !strcmp(name, p + 1))
137 			return(sh);
138 	}
139 	return(NULL);
140 }
141