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