xref: /csrg-svn/usr.bin/chpass/util.c (revision 66651)
144573Smarc /*-
2*66651Spendry  * Copyright (c) 1988, 1993, 1994
361940Sbostic  *	The Regents of the University of California.  All rights reserved.
436865Sbostic  *
544573Smarc  * %sccs.include.redist.c%
636865Sbostic  */
736865Sbostic 
836865Sbostic #ifndef lint
9*66651Spendry static char sccsid[] = "@(#)util.c	8.4 (Berkeley) 04/02/94";
1036865Sbostic #endif /* not lint */
1136865Sbostic 
1236865Sbostic #include <sys/types.h>
1366621Spendry 
1466621Spendry #include <ctype.h>
1537023Sbostic #include <pwd.h>
1636865Sbostic #include <stdio.h>
1766621Spendry #include <stdlib.h>
1844573Smarc #include <string.h>
1966621Spendry #include <time.h>
2066621Spendry #include <tzfile.h>
2166621Spendry #include <unistd.h>
2266621Spendry 
2337867Sbostic #include "chpass.h"
2437023Sbostic #include "pathnames.h"
2536865Sbostic 
2636865Sbostic static int dmsize[] =
2736865Sbostic 	{ -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
2836865Sbostic static char *months[] =
2937120Sbostic 	{ "January", "February", "March", "April", "May", "June",
3037120Sbostic 	  "July", "August", "September", "October", "November",
3137120Sbostic 	  "December", NULL };
3266621Spendry 
3336865Sbostic char *
ttoa(tval)3436865Sbostic ttoa(tval)
3536865Sbostic 	time_t tval;
3636865Sbostic {
3736865Sbostic 	struct tm *tp;
3837237Sbostic 	static char tbuf[50];
3936865Sbostic 
4037120Sbostic 	if (tval) {
4136865Sbostic 		tp = localtime(&tval);
4265348Sbostic 		(void)sprintf(tbuf, "%s %d, %d", months[tp->tm_mon],
4365348Sbostic 		    tp->tm_mday, tp->tm_year + TM_YEAR_BASE);
4436865Sbostic 	}
4537120Sbostic 	else
4637120Sbostic 		*tbuf = '\0';
4766621Spendry 	return (tbuf);
4836865Sbostic }
4936865Sbostic 
5066621Spendry int
atot(p,store)5136865Sbostic atot(p, store)
5236865Sbostic 	char *p;
5336865Sbostic 	time_t *store;
5436865Sbostic {
5536865Sbostic 	static struct tm *lt;
5666621Spendry 	char *t, **mp;
5766621Spendry 	time_t tval;
5836865Sbostic 	int day, month, year;
5936865Sbostic 
6037120Sbostic 	if (!*p) {
6136865Sbostic 		*store = 0;
6266621Spendry 		return (0);
6336865Sbostic 	}
6436865Sbostic 	if (!lt) {
6536865Sbostic 		unsetenv("TZ");
6636865Sbostic 		(void)time(&tval);
6736865Sbostic 		lt = localtime(&tval);
6836865Sbostic 	}
6936882Sbostic 	if (!(t = strtok(p, " \t")))
7036865Sbostic 		goto bad;
7136865Sbostic 	for (mp = months;; ++mp) {
7236865Sbostic 		if (!*mp)
7336865Sbostic 			goto bad;
7436865Sbostic 		if (!strncasecmp(*mp, t, 3)) {
7536865Sbostic 			month = mp - months + 1;
7636865Sbostic 			break;
7736865Sbostic 		}
7836865Sbostic 	}
7936882Sbostic 	if (!(t = strtok((char *)NULL, " \t,")) || !isdigit(*t))
8036865Sbostic 		goto bad;
8136882Sbostic 	day = atoi(t);
8236882Sbostic 	if (!(t = strtok((char *)NULL, " \t,")) || !isdigit(*t))
8336882Sbostic 		goto bad;
8436865Sbostic 	year = atoi(t);
8536865Sbostic 	if (day < 1 || day > 31 || month < 1 || month > 12 || !year)
8636865Sbostic 		goto bad;
8736865Sbostic 	if (year < 100)
8836865Sbostic 		year += TM_YEAR_BASE;
8936865Sbostic 	if (year <= EPOCH_YEAR)
9066621Spendry bad:		return (1);
9136865Sbostic 	tval = isleap(year) && month > 2;
9236865Sbostic 	for (--year; year >= EPOCH_YEAR; --year)
9336865Sbostic 		tval += isleap(year) ?
9437185Sbostic 		    DAYSPERLYEAR : DAYSPERNYEAR;
9536865Sbostic 	while (--month)
9636865Sbostic 		tval += dmsize[month];
9737237Sbostic 	tval += day;
9837185Sbostic 	tval = tval * HOURSPERDAY * MINSPERHOUR * SECSPERMIN;
9936865Sbostic 	tval -= lt->tm_gmtoff;
10036865Sbostic 	*store = tval;
10166621Spendry 	return (0);
10236865Sbostic }
10336865Sbostic 
10438212Sbostic char *
ok_shell(name)10538212Sbostic ok_shell(name)
10666621Spendry 	char *name;
10738212Sbostic {
10866621Spendry 	char *p, *sh;
10938212Sbostic 
11038212Sbostic 	setusershell();
11138212Sbostic 	while (sh = getusershell()) {
11238212Sbostic 		if (!strcmp(name, sh))
11366621Spendry 			return (name);
11438212Sbostic 		/* allow just shell name, but use "real" path */
11566621Spendry 		if ((p = strrchr(sh, '/')) && strcmp(name, p + 1) == 0)
11666621Spendry 			return (sh);
11738212Sbostic 	}
11866621Spendry 	return (NULL);
11938212Sbostic }
120