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