xref: /original-bsd/usr.bin/chpass/util.c (revision f45a138d)
1 /*-
2  * Copyright (c) 1988, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)util.c	8.3 (Berkeley) 04/02/94";
10 #endif /* not lint */
11 
12 #include <sys/types.h>
13 
14 #include <ctype.h>
15 #include <pwd.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <time.h>
20 #include <tzfile.h>
21 #include <unistd.h>
22 
23 #include "chpass.h"
24 #include "pathnames.h"
25 
26 static int dmsize[] =
27 	{ -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
28 static char *months[] =
29 	{ "January", "February", "March", "April", "May", "June",
30 	  "July", "August", "September", "October", "November",
31 	  "December", NULL };
32 
33 char *
34 ttoa(tval)
35 	time_t tval;
36 {
37 	struct tm *tp;
38 	static char tbuf[50];
39 
40 	if (tval) {
41 		tp = localtime(&tval);
42 		(void)sprintf(tbuf, "%s %d, %d", months[tp->tm_mon],
43 		    tp->tm_mday, tp->tm_year + TM_YEAR_BASE);
44 	}
45 	else
46 		*tbuf = '\0';
47 	return (tbuf);
48 }
49 
50 int
51 atot(p, store)
52 	char *p;
53 	time_t *store;
54 {
55 	static struct tm *lt;
56 	char *t, **mp;
57 	time_t tval;
58 	int day, month, year;
59 
60 	if (!*p) {
61 		*store = 0;
62 		return (0);
63 	}
64 	if (!lt) {
65 		unsetenv("TZ");
66 		(void)time(&tval);
67 		lt = localtime(&tval);
68 	}
69 	if (!(t = strtok(p, " \t")))
70 		goto bad;
71 	for (mp = months;; ++mp) {
72 		if (!*mp)
73 			goto bad;
74 		if (!strncasecmp(*mp, t, 3)) {
75 			month = mp - months + 1;
76 			break;
77 		}
78 	}
79 	if (!(t = strtok((char *)NULL, " \t,")) || !isdigit(*t))
80 		goto bad;
81 	day = atoi(t);
82 	if (!(t = strtok((char *)NULL, " \t,")) || !isdigit(*t))
83 		goto bad;
84 	year = atoi(t);
85 	if (day < 1 || day > 31 || month < 1 || month > 12 || !year)
86 		goto bad;
87 	if (year < 100)
88 		year += TM_YEAR_BASE;
89 	if (year <= EPOCH_YEAR)
90 bad:		return (1);
91 	tval = isleap(year) && month > 2;
92 	for (--year; year >= EPOCH_YEAR; --year)
93 		tval += isleap(year) ?
94 		    DAYSPERLYEAR : DAYSPERNYEAR;
95 	while (--month)
96 		tval += dmsize[month];
97 	tval += day;
98 	tval = tval * HOURSPERDAY * MINSPERHOUR * SECSPERMIN;
99 	tval -= lt->tm_gmtoff;
100 	*store = tval;
101 	return (0);
102 }
103 
104 char *
105 ok_shell(name)
106 	char *name;
107 {
108 	char *p, *sh;
109 
110 	setusershell();
111 	while (sh = getusershell()) {
112 		if (!strcmp(name, sh))
113 			return (name);
114 		/* allow just shell name, but use "real" path */
115 		if ((p = strrchr(sh, '/')) && strcmp(name, p + 1) == 0)
116 			return (sh);
117 	}
118 	return (NULL);
119 }
120