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