util.c revision 42815
11590Srgrimes/*-
21590Srgrimes * Copyright (c) 1988, 1993, 1994
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer in the
121590Srgrimes *    documentation and/or other materials provided with the distribution.
131590Srgrimes * 3. All advertising materials mentioning features or use of this software
141590Srgrimes *    must display the following acknowledgement:
151590Srgrimes *	This product includes software developed by the University of
161590Srgrimes *	California, Berkeley and its contributors.
171590Srgrimes * 4. Neither the name of the University nor the names of its contributors
181590Srgrimes *    may be used to endorse or promote products derived from this software
191590Srgrimes *    without specific prior written permission.
201590Srgrimes *
211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311590Srgrimes * SUCH DAMAGE.
321590Srgrimes */
331590Srgrimes
341590Srgrimes#ifndef lint
3534797Scharnier#if 0
361590Srgrimesstatic char sccsid[] = "@(#)util.c	8.4 (Berkeley) 4/2/94";
3734797Scharnier#endif
3834797Scharnierstatic const char rcsid[] =
3942815Sdanny	"$Id: util.c,v 1.6 1998/03/23 07:41:49 charnier Exp $";
401590Srgrimes#endif /* not lint */
411590Srgrimes
421590Srgrimes#include <sys/types.h>
431590Srgrimes
441590Srgrimes#include <ctype.h>
451590Srgrimes#include <pwd.h>
461590Srgrimes#include <stdio.h>
471590Srgrimes#include <stdlib.h>
481590Srgrimes#include <string.h>
491590Srgrimes#include <time.h>
501590Srgrimes#include <unistd.h>
511590Srgrimes
521590Srgrimes#include "chpass.h"
531590Srgrimes#include "pathnames.h"
541590Srgrimes
551590Srgrimesstatic char *months[] =
561590Srgrimes	{ "January", "February", "March", "April", "May", "June",
571590Srgrimes	  "July", "August", "September", "October", "November",
581590Srgrimes	  "December", NULL };
591590Srgrimes
601590Srgrimeschar *
611590Srgrimesttoa(tval)
621590Srgrimes	time_t tval;
631590Srgrimes{
641590Srgrimes	struct tm *tp;
651590Srgrimes	static char tbuf[50];
661590Srgrimes
671590Srgrimes	if (tval) {
681590Srgrimes		tp = localtime(&tval);
691590Srgrimes		(void)sprintf(tbuf, "%s %d, %d", months[tp->tm_mon],
709987Swollman		    tp->tm_mday, tp->tm_year + 1900);
711590Srgrimes	}
721590Srgrimes	else
731590Srgrimes		*tbuf = '\0';
741590Srgrimes	return (tbuf);
758874Srgrimes}
761590Srgrimes
771590Srgrimesint
781590Srgrimesatot(p, store)
791590Srgrimes	char *p;
801590Srgrimes	time_t *store;
811590Srgrimes{
821590Srgrimes	static struct tm *lt;
831590Srgrimes	char *t, **mp;
841590Srgrimes	time_t tval;
851590Srgrimes	int day, month, year;
861590Srgrimes
871590Srgrimes	if (!*p) {
881590Srgrimes		*store = 0;
891590Srgrimes		return (0);
901590Srgrimes	}
911590Srgrimes	if (!lt) {
921590Srgrimes		unsetenv("TZ");
931590Srgrimes		(void)time(&tval);
941590Srgrimes		lt = localtime(&tval);
951590Srgrimes	}
961590Srgrimes	if (!(t = strtok(p, " \t")))
971590Srgrimes		goto bad;
9817544Speter	if (isdigit(*t)) {
9917544Speter		month = atoi(t);
10017544Speter	} else {
10117544Speter		for (mp = months;; ++mp) {
10217544Speter			if (!*mp)
10317544Speter				goto bad;
10417544Speter			if (!strncasecmp(*mp, t, 3)) {
10517544Speter				month = mp - months + 1;
10617544Speter				break;
10717544Speter			}
1081590Srgrimes		}
1091590Srgrimes	}
1101590Srgrimes	if (!(t = strtok((char *)NULL, " \t,")) || !isdigit(*t))
1111590Srgrimes		goto bad;
1121590Srgrimes	day = atoi(t);
1131590Srgrimes	if (!(t = strtok((char *)NULL, " \t,")) || !isdigit(*t))
1141590Srgrimes		goto bad;
1151590Srgrimes	year = atoi(t);
1161590Srgrimes	if (day < 1 || day > 31 || month < 1 || month > 12 || !year)
1171590Srgrimes		goto bad;
11842815Sdanny	/* Allow two digit years 1969-2068 */
11942815Sdanny	if (year < 69)
12042815Sdanny		year += 2000;
12142815Sdanny	else if (year < 100)
1229987Swollman		year += 1900;
12342815Sdanny	if (year < 1969)
1241590Srgrimesbad:		return (1);
1259987Swollman	lt->tm_year = year - 1900;
1269554Smpp	lt->tm_mon = month - 1;
1279554Smpp	lt->tm_mday = day;
1289554Smpp	lt->tm_hour = 0;
1299554Smpp	lt->tm_min = 0;
1309554Smpp	lt->tm_sec = 0;
1319554Smpp	lt->tm_isdst = -1;
1329554Smpp	if ((tval = mktime(lt)) < 0)
1339554Smpp		return (1);
1341590Srgrimes	*store = tval;
1351590Srgrimes	return (0);
1361590Srgrimes}
1371590Srgrimes
1381590Srgrimeschar *
1391590Srgrimesok_shell(name)
1401590Srgrimes	char *name;
1411590Srgrimes{
1421590Srgrimes	char *p, *sh;
1431590Srgrimes
1441590Srgrimes	setusershell();
14534797Scharnier	while ((sh = getusershell())) {
1461590Srgrimes		if (!strcmp(name, sh))
1471590Srgrimes			return (name);
1481590Srgrimes		/* allow just shell name, but use "real" path */
1491590Srgrimes		if ((p = strrchr(sh, '/')) && strcmp(name, p + 1) == 0)
1501590Srgrimes			return (sh);
1511590Srgrimes	}
1521590Srgrimes	return (NULL);
1531590Srgrimes}
154