Home | History | Annotate | Line # | Download | only in chpass
util.c revision 1.7
      1  1.7  lukem /*	$NetBSD: util.c,v 1.7 1997/10/18 12:49:17 lukem Exp $	*/
      2  1.4  glass 
      3  1.1    cgd /*-
      4  1.4  glass  * Copyright (c) 1988, 1993, 1994
      5  1.4  glass  *	The Regents of the University of California.  All rights reserved.
      6  1.1    cgd  *
      7  1.1    cgd  * Redistribution and use in source and binary forms, with or without
      8  1.1    cgd  * modification, are permitted provided that the following conditions
      9  1.1    cgd  * are met:
     10  1.1    cgd  * 1. Redistributions of source code must retain the above copyright
     11  1.1    cgd  *    notice, this list of conditions and the following disclaimer.
     12  1.1    cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1    cgd  *    notice, this list of conditions and the following disclaimer in the
     14  1.1    cgd  *    documentation and/or other materials provided with the distribution.
     15  1.1    cgd  * 3. All advertising materials mentioning features or use of this software
     16  1.1    cgd  *    must display the following acknowledgement:
     17  1.1    cgd  *	This product includes software developed by the University of
     18  1.1    cgd  *	California, Berkeley and its contributors.
     19  1.1    cgd  * 4. Neither the name of the University nor the names of its contributors
     20  1.1    cgd  *    may be used to endorse or promote products derived from this software
     21  1.1    cgd  *    without specific prior written permission.
     22  1.1    cgd  *
     23  1.1    cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.1    cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1    cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1    cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.1    cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1    cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1    cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1    cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1    cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1    cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1    cgd  * SUCH DAMAGE.
     34  1.1    cgd  */
     35  1.1    cgd 
     36  1.7  lukem #include <sys/cdefs.h>
     37  1.1    cgd #ifndef lint
     38  1.4  glass #if 0
     39  1.4  glass static char sccsid[] = "@(#)util.c	8.4 (Berkeley) 4/2/94";
     40  1.4  glass #else
     41  1.7  lukem __RCSID("$NetBSD: util.c,v 1.7 1997/10/18 12:49:17 lukem Exp $");
     42  1.4  glass #endif
     43  1.1    cgd #endif /* not lint */
     44  1.1    cgd 
     45  1.1    cgd #include <sys/types.h>
     46  1.4  glass 
     47  1.4  glass #include <ctype.h>
     48  1.1    cgd #include <pwd.h>
     49  1.1    cgd #include <stdio.h>
     50  1.4  glass #include <stdlib.h>
     51  1.1    cgd #include <string.h>
     52  1.4  glass #include <time.h>
     53  1.4  glass #include <tzfile.h>
     54  1.4  glass #include <unistd.h>
     55  1.4  glass 
     56  1.1    cgd #include "chpass.h"
     57  1.1    cgd #include "pathnames.h"
     58  1.1    cgd 
     59  1.1    cgd static int dmsize[] =
     60  1.1    cgd 	{ -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
     61  1.1    cgd static char *months[] =
     62  1.1    cgd 	{ "January", "February", "March", "April", "May", "June",
     63  1.1    cgd 	  "July", "August", "September", "October", "November",
     64  1.1    cgd 	  "December", NULL };
     65  1.4  glass 
     66  1.1    cgd char *
     67  1.1    cgd ttoa(tval)
     68  1.1    cgd 	time_t tval;
     69  1.1    cgd {
     70  1.1    cgd 	struct tm *tp;
     71  1.1    cgd 	static char tbuf[50];
     72  1.1    cgd 
     73  1.1    cgd 	if (tval) {
     74  1.1    cgd 		tp = localtime(&tval);
     75  1.5    mrg 		(void)snprintf(tbuf, sizeof tbuf, "%s %d, %d", months[tp->tm_mon],
     76  1.3    cgd 		    tp->tm_mday, tp->tm_year + TM_YEAR_BASE);
     77  1.1    cgd 	}
     78  1.1    cgd 	else
     79  1.1    cgd 		*tbuf = '\0';
     80  1.4  glass 	return (tbuf);
     81  1.1    cgd }
     82  1.1    cgd 
     83  1.4  glass int
     84  1.1    cgd atot(p, store)
     85  1.1    cgd 	char *p;
     86  1.1    cgd 	time_t *store;
     87  1.1    cgd {
     88  1.1    cgd 	static struct tm *lt;
     89  1.4  glass 	char *t, **mp;
     90  1.4  glass 	time_t tval;
     91  1.1    cgd 	int day, month, year;
     92  1.1    cgd 
     93  1.1    cgd 	if (!*p) {
     94  1.1    cgd 		*store = 0;
     95  1.4  glass 		return (0);
     96  1.1    cgd 	}
     97  1.1    cgd 	if (!lt) {
     98  1.1    cgd 		unsetenv("TZ");
     99  1.1    cgd 		(void)time(&tval);
    100  1.1    cgd 		lt = localtime(&tval);
    101  1.1    cgd 	}
    102  1.1    cgd 	if (!(t = strtok(p, " \t")))
    103  1.1    cgd 		goto bad;
    104  1.1    cgd 	for (mp = months;; ++mp) {
    105  1.1    cgd 		if (!*mp)
    106  1.1    cgd 			goto bad;
    107  1.1    cgd 		if (!strncasecmp(*mp, t, 3)) {
    108  1.1    cgd 			month = mp - months + 1;
    109  1.1    cgd 			break;
    110  1.1    cgd 		}
    111  1.1    cgd 	}
    112  1.1    cgd 	if (!(t = strtok((char *)NULL, " \t,")) || !isdigit(*t))
    113  1.1    cgd 		goto bad;
    114  1.1    cgd 	day = atoi(t);
    115  1.1    cgd 	if (!(t = strtok((char *)NULL, " \t,")) || !isdigit(*t))
    116  1.1    cgd 		goto bad;
    117  1.1    cgd 	year = atoi(t);
    118  1.1    cgd 	if (day < 1 || day > 31 || month < 1 || month > 12 || !year)
    119  1.1    cgd 		goto bad;
    120  1.1    cgd 	if (year < 100)
    121  1.1    cgd 		year += TM_YEAR_BASE;
    122  1.1    cgd 	if (year <= EPOCH_YEAR)
    123  1.4  glass bad:		return (1);
    124  1.1    cgd 	tval = isleap(year) && month > 2;
    125  1.1    cgd 	for (--year; year >= EPOCH_YEAR; --year)
    126  1.1    cgd 		tval += isleap(year) ?
    127  1.1    cgd 		    DAYSPERLYEAR : DAYSPERNYEAR;
    128  1.1    cgd 	while (--month)
    129  1.1    cgd 		tval += dmsize[month];
    130  1.1    cgd 	tval += day;
    131  1.1    cgd 	tval = tval * HOURSPERDAY * MINSPERHOUR * SECSPERMIN;
    132  1.1    cgd 	tval -= lt->tm_gmtoff;
    133  1.1    cgd 	*store = tval;
    134  1.4  glass 	return (0);
    135  1.1    cgd }
    136  1.1    cgd 
    137  1.1    cgd char *
    138  1.1    cgd ok_shell(name)
    139  1.4  glass 	char *name;
    140  1.1    cgd {
    141  1.4  glass 	char *p, *sh;
    142  1.1    cgd 
    143  1.1    cgd 	setusershell();
    144  1.6  mikel 	while ((sh = getusershell()) != NULL) {
    145  1.1    cgd 		if (!strcmp(name, sh))
    146  1.4  glass 			return (name);
    147  1.1    cgd 		/* allow just shell name, but use "real" path */
    148  1.4  glass 		if ((p = strrchr(sh, '/')) && strcmp(name, p + 1) == 0)
    149  1.4  glass 			return (sh);
    150  1.1    cgd 	}
    151  1.4  glass 	return (NULL);
    152  1.1    cgd }
    153