xref: /original-bsd/usr.bin/chpass/util.c (revision cd1f3a57)
174aa0de0Smarc /*-
2*cd1f3a57Spendry  * Copyright (c) 1988, 1993, 1994
322b902e0Sbostic  *	The Regents of the University of California.  All rights reserved.
42cabbf52Sbostic  *
574aa0de0Smarc  * %sccs.include.redist.c%
62cabbf52Sbostic  */
72cabbf52Sbostic 
82cabbf52Sbostic #ifndef lint
9*cd1f3a57Spendry static char sccsid[] = "@(#)util.c	8.4 (Berkeley) 04/02/94";
102cabbf52Sbostic #endif /* not lint */
112cabbf52Sbostic 
122cabbf52Sbostic #include <sys/types.h>
13577d5484Spendry 
14577d5484Spendry #include <ctype.h>
154438b94bSbostic #include <pwd.h>
162cabbf52Sbostic #include <stdio.h>
17577d5484Spendry #include <stdlib.h>
1874aa0de0Smarc #include <string.h>
19577d5484Spendry #include <time.h>
20577d5484Spendry #include <tzfile.h>
21577d5484Spendry #include <unistd.h>
22577d5484Spendry 
23f59f470dSbostic #include "chpass.h"
244438b94bSbostic #include "pathnames.h"
252cabbf52Sbostic 
262cabbf52Sbostic static int dmsize[] =
272cabbf52Sbostic 	{ -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
282cabbf52Sbostic static char *months[] =
29f7fca458Sbostic 	{ "January", "February", "March", "April", "May", "June",
30f7fca458Sbostic 	  "July", "August", "September", "October", "November",
31f7fca458Sbostic 	  "December", NULL };
32577d5484Spendry 
332cabbf52Sbostic char *
ttoa(tval)342cabbf52Sbostic ttoa(tval)
352cabbf52Sbostic 	time_t tval;
362cabbf52Sbostic {
372cabbf52Sbostic 	struct tm *tp;
38b46a4992Sbostic 	static char tbuf[50];
392cabbf52Sbostic 
40f7fca458Sbostic 	if (tval) {
412cabbf52Sbostic 		tp = localtime(&tval);
42cb22a785Sbostic 		(void)sprintf(tbuf, "%s %d, %d", months[tp->tm_mon],
43cb22a785Sbostic 		    tp->tm_mday, tp->tm_year + TM_YEAR_BASE);
442cabbf52Sbostic 	}
45f7fca458Sbostic 	else
46f7fca458Sbostic 		*tbuf = '\0';
472cabbf52Sbostic 	return (tbuf);
482cabbf52Sbostic }
492cabbf52Sbostic 
50577d5484Spendry int
atot(p,store)512cabbf52Sbostic atot(p, store)
522cabbf52Sbostic 	char *p;
532cabbf52Sbostic 	time_t *store;
542cabbf52Sbostic {
552cabbf52Sbostic 	static struct tm *lt;
56577d5484Spendry 	char *t, **mp;
57577d5484Spendry 	time_t tval;
582cabbf52Sbostic 	int day, month, year;
592cabbf52Sbostic 
60f7fca458Sbostic 	if (!*p) {
612cabbf52Sbostic 		*store = 0;
622cabbf52Sbostic 		return (0);
632cabbf52Sbostic 	}
642cabbf52Sbostic 	if (!lt) {
652cabbf52Sbostic 		unsetenv("TZ");
662cabbf52Sbostic 		(void)time(&tval);
672cabbf52Sbostic 		lt = localtime(&tval);
682cabbf52Sbostic 	}
693e4a3008Sbostic 	if (!(t = strtok(p, " \t")))
702cabbf52Sbostic 		goto bad;
712cabbf52Sbostic 	for (mp = months;; ++mp) {
722cabbf52Sbostic 		if (!*mp)
732cabbf52Sbostic 			goto bad;
742cabbf52Sbostic 		if (!strncasecmp(*mp, t, 3)) {
752cabbf52Sbostic 			month = mp - months + 1;
762cabbf52Sbostic 			break;
772cabbf52Sbostic 		}
782cabbf52Sbostic 	}
793e4a3008Sbostic 	if (!(t = strtok((char *)NULL, " \t,")) || !isdigit(*t))
803e4a3008Sbostic 		goto bad;
813e4a3008Sbostic 	day = atoi(t);
823e4a3008Sbostic 	if (!(t = strtok((char *)NULL, " \t,")) || !isdigit(*t))
832cabbf52Sbostic 		goto bad;
842cabbf52Sbostic 	year = atoi(t);
852cabbf52Sbostic 	if (day < 1 || day > 31 || month < 1 || month > 12 || !year)
862cabbf52Sbostic 		goto bad;
872cabbf52Sbostic 	if (year < 100)
882cabbf52Sbostic 		year += TM_YEAR_BASE;
892cabbf52Sbostic 	if (year <= EPOCH_YEAR)
902cabbf52Sbostic bad:		return (1);
912cabbf52Sbostic 	tval = isleap(year) && month > 2;
922cabbf52Sbostic 	for (--year; year >= EPOCH_YEAR; --year)
932cabbf52Sbostic 		tval += isleap(year) ?
945b656804Sbostic 		    DAYSPERLYEAR : DAYSPERNYEAR;
952cabbf52Sbostic 	while (--month)
962cabbf52Sbostic 		tval += dmsize[month];
97b46a4992Sbostic 	tval += day;
985b656804Sbostic 	tval = tval * HOURSPERDAY * MINSPERHOUR * SECSPERMIN;
992cabbf52Sbostic 	tval -= lt->tm_gmtoff;
1002cabbf52Sbostic 	*store = tval;
1012cabbf52Sbostic 	return (0);
1022cabbf52Sbostic }
1032cabbf52Sbostic 
1047d51cb3fSbostic char *
ok_shell(name)1057d51cb3fSbostic ok_shell(name)
106577d5484Spendry 	char *name;
1077d51cb3fSbostic {
108577d5484Spendry 	char *p, *sh;
1097d51cb3fSbostic 
1107d51cb3fSbostic 	setusershell();
1117d51cb3fSbostic 	while (sh = getusershell()) {
1127d51cb3fSbostic 		if (!strcmp(name, sh))
1137d51cb3fSbostic 			return (name);
1147d51cb3fSbostic 		/* allow just shell name, but use "real" path */
115577d5484Spendry 		if ((p = strrchr(sh, '/')) && strcmp(name, p + 1) == 0)
1167d51cb3fSbostic 			return (sh);
1177d51cb3fSbostic 	}
1187d51cb3fSbostic 	return (NULL);
1192cabbf52Sbostic }
120