xref: /original-bsd/usr.bin/chpass/util.c (revision 22b902e0)
174aa0de0Smarc /*-
2*22b902e0Sbostic  * Copyright (c) 1988, 1993
3*22b902e0Sbostic  *	The Regents of the University of California.  All rights reserved.
42cabbf52Sbostic  *
574aa0de0Smarc  * %sccs.include.redist.c%
62cabbf52Sbostic  */
72cabbf52Sbostic 
82cabbf52Sbostic #ifndef lint
9*22b902e0Sbostic static char sccsid[] = "@(#)util.c	8.1 (Berkeley) 06/06/93";
102cabbf52Sbostic #endif /* not lint */
112cabbf52Sbostic 
122cabbf52Sbostic #include <sys/types.h>
132cabbf52Sbostic #include <sys/time.h>
142cabbf52Sbostic #include <tzfile.h>
154438b94bSbostic #include <pwd.h>
162cabbf52Sbostic #include <stdio.h>
1774aa0de0Smarc #include <string.h>
182cabbf52Sbostic #include <ctype.h>
19f59f470dSbostic #include "chpass.h"
204438b94bSbostic #include "pathnames.h"
212cabbf52Sbostic 
222cabbf52Sbostic static int dmsize[] =
232cabbf52Sbostic 	{ -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
242cabbf52Sbostic static char *months[] =
25f7fca458Sbostic 	{ "January", "February", "March", "April", "May", "June",
26f7fca458Sbostic 	  "July", "August", "September", "October", "November",
27f7fca458Sbostic 	  "December", NULL };
282cabbf52Sbostic char *
292cabbf52Sbostic ttoa(tval)
302cabbf52Sbostic 	time_t tval;
312cabbf52Sbostic {
322cabbf52Sbostic 	struct tm *tp;
33b46a4992Sbostic 	static char tbuf[50];
342cabbf52Sbostic 
35f7fca458Sbostic 	if (tval) {
362cabbf52Sbostic 		tp = localtime(&tval);
373e4a3008Sbostic 		(void)sprintf(tbuf, "%s %d, 19%d", months[tp->tm_mon],
383e4a3008Sbostic 		    tp->tm_mday, tp->tm_year);
392cabbf52Sbostic 	}
40f7fca458Sbostic 	else
41f7fca458Sbostic 		*tbuf = '\0';
422cabbf52Sbostic 	return(tbuf);
432cabbf52Sbostic }
442cabbf52Sbostic 
452cabbf52Sbostic atot(p, store)
462cabbf52Sbostic 	char *p;
472cabbf52Sbostic 	time_t *store;
482cabbf52Sbostic {
492cabbf52Sbostic 	register char *t, **mp;
502cabbf52Sbostic 	static struct tm *lt;
512cabbf52Sbostic 	time_t tval, time();
522cabbf52Sbostic 	int day, month, year;
532cabbf52Sbostic 
54f7fca458Sbostic 	if (!*p) {
552cabbf52Sbostic 		*store = 0;
562cabbf52Sbostic 		return(0);
572cabbf52Sbostic 	}
582cabbf52Sbostic 	if (!lt) {
592cabbf52Sbostic 		unsetenv("TZ");
602cabbf52Sbostic 		(void)time(&tval);
612cabbf52Sbostic 		lt = localtime(&tval);
622cabbf52Sbostic 	}
633e4a3008Sbostic 	if (!(t = strtok(p, " \t")))
642cabbf52Sbostic 		goto bad;
652cabbf52Sbostic 	for (mp = months;; ++mp) {
662cabbf52Sbostic 		if (!*mp)
672cabbf52Sbostic 			goto bad;
682cabbf52Sbostic 		if (!strncasecmp(*mp, t, 3)) {
692cabbf52Sbostic 			month = mp - months + 1;
702cabbf52Sbostic 			break;
712cabbf52Sbostic 		}
722cabbf52Sbostic 	}
733e4a3008Sbostic 	if (!(t = strtok((char *)NULL, " \t,")) || !isdigit(*t))
743e4a3008Sbostic 		goto bad;
753e4a3008Sbostic 	day = atoi(t);
763e4a3008Sbostic 	if (!(t = strtok((char *)NULL, " \t,")) || !isdigit(*t))
772cabbf52Sbostic 		goto bad;
782cabbf52Sbostic 	year = atoi(t);
792cabbf52Sbostic 	if (day < 1 || day > 31 || month < 1 || month > 12 || !year)
802cabbf52Sbostic 		goto bad;
812cabbf52Sbostic 	if (year < 100)
822cabbf52Sbostic 		year += TM_YEAR_BASE;
832cabbf52Sbostic 	if (year <= EPOCH_YEAR)
842cabbf52Sbostic bad:		return(1);
852cabbf52Sbostic 	tval = isleap(year) && month > 2;
862cabbf52Sbostic 	for (--year; year >= EPOCH_YEAR; --year)
872cabbf52Sbostic 		tval += isleap(year) ?
885b656804Sbostic 		    DAYSPERLYEAR : DAYSPERNYEAR;
892cabbf52Sbostic 	while (--month)
902cabbf52Sbostic 		tval += dmsize[month];
91b46a4992Sbostic 	tval += day;
925b656804Sbostic 	tval = tval * HOURSPERDAY * MINSPERHOUR * SECSPERMIN;
932cabbf52Sbostic 	tval -= lt->tm_gmtoff;
942cabbf52Sbostic 	*store = tval;
952cabbf52Sbostic 	return(0);
962cabbf52Sbostic }
972cabbf52Sbostic 
987d51cb3fSbostic char *
997d51cb3fSbostic ok_shell(name)
1007d51cb3fSbostic 	register char *name;
1017d51cb3fSbostic {
1027d51cb3fSbostic 	register char *p, *sh;
1037d51cb3fSbostic 	char *getusershell();
1047d51cb3fSbostic 
1057d51cb3fSbostic 	setusershell();
1067d51cb3fSbostic 	while (sh = getusershell()) {
1077d51cb3fSbostic 		if (!strcmp(name, sh))
1087d51cb3fSbostic 			return(name);
1097d51cb3fSbostic 		/* allow just shell name, but use "real" path */
1107d51cb3fSbostic 		if ((p = rindex(sh, '/')) && !strcmp(name, p + 1))
1117d51cb3fSbostic 			return(sh);
1127d51cb3fSbostic 	}
1137d51cb3fSbostic 	return(NULL);
1142cabbf52Sbostic }
115