xref: /netbsd-src/usr.bin/chpass/util.c (revision d9158b13b5dfe46201430699a3f7a235ecf28df3)
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[] = "from: @(#)util.c	5.14 (Berkeley) 2/12/91";*/
36 static char rcsid[] = "$Id: util.c,v 1.3 1994/01/03 08:21:14 cgd Exp $";
37 #endif /* not lint */
38 
39 #include <sys/types.h>
40 #include <sys/time.h>
41 #include <tzfile.h>
42 #include <pwd.h>
43 #include <stdio.h>
44 #include <string.h>
45 #include <ctype.h>
46 #include "chpass.h"
47 #include "pathnames.h"
48 
49 static int dmsize[] =
50 	{ -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
51 static char *months[] =
52 	{ "January", "February", "March", "April", "May", "June",
53 	  "July", "August", "September", "October", "November",
54 	  "December", NULL };
55 char *
56 ttoa(tval)
57 	time_t tval;
58 {
59 	struct tm *tp;
60 	static char tbuf[50];
61 
62 	if (tval) {
63 		tp = localtime(&tval);
64 		(void)sprintf(tbuf, "%s %d, %d", months[tp->tm_mon],
65 		    tp->tm_mday, tp->tm_year + TM_YEAR_BASE);
66 	}
67 	else
68 		*tbuf = '\0';
69 	return(tbuf);
70 }
71 
72 atot(p, store)
73 	char *p;
74 	time_t *store;
75 {
76 	register char *t, **mp;
77 	static struct tm *lt;
78 	time_t tval, time();
79 	int day, month, year;
80 
81 	if (!*p) {
82 		*store = 0;
83 		return(0);
84 	}
85 	if (!lt) {
86 		unsetenv("TZ");
87 		(void)time(&tval);
88 		lt = localtime(&tval);
89 	}
90 	if (!(t = strtok(p, " \t")))
91 		goto bad;
92 	for (mp = months;; ++mp) {
93 		if (!*mp)
94 			goto bad;
95 		if (!strncasecmp(*mp, t, 3)) {
96 			month = mp - months + 1;
97 			break;
98 		}
99 	}
100 	if (!(t = strtok((char *)NULL, " \t,")) || !isdigit(*t))
101 		goto bad;
102 	day = atoi(t);
103 	if (!(t = strtok((char *)NULL, " \t,")) || !isdigit(*t))
104 		goto bad;
105 	year = atoi(t);
106 	if (day < 1 || day > 31 || month < 1 || month > 12 || !year)
107 		goto bad;
108 	if (year < 100)
109 		year += TM_YEAR_BASE;
110 	if (year <= EPOCH_YEAR)
111 bad:		return(1);
112 	tval = isleap(year) && month > 2;
113 	for (--year; year >= EPOCH_YEAR; --year)
114 		tval += isleap(year) ?
115 		    DAYSPERLYEAR : DAYSPERNYEAR;
116 	while (--month)
117 		tval += dmsize[month];
118 	tval += day;
119 	tval = tval * HOURSPERDAY * MINSPERHOUR * SECSPERMIN;
120 	tval -= lt->tm_gmtoff;
121 	*store = tval;
122 	return(0);
123 }
124 
125 char *
126 ok_shell(name)
127 	register char *name;
128 {
129 	register char *p, *sh;
130 	char *getusershell();
131 
132 	setusershell();
133 	while (sh = getusershell()) {
134 		if (!strcmp(name, sh))
135 			return(name);
136 		/* allow just shell name, but use "real" path */
137 		if ((p = rindex(sh, '/')) && !strcmp(name, p + 1))
138 			return(sh);
139 	}
140 	return(NULL);
141 }
142