Home | History | Annotate | Line # | Download | only in chpass
util.c revision 1.6
      1  1.6  mikel /*	$NetBSD: util.c,v 1.6 1997/07/25 06:38:02 mikel 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.1    cgd #ifndef lint
     37  1.4  glass #if 0
     38  1.4  glass static char sccsid[] = "@(#)util.c	8.4 (Berkeley) 4/2/94";
     39  1.4  glass #else
     40  1.6  mikel static char rcsid[] = "$NetBSD: util.c,v 1.6 1997/07/25 06:38:02 mikel Exp $";
     41  1.4  glass #endif
     42  1.1    cgd #endif /* not lint */
     43  1.1    cgd 
     44  1.1    cgd #include <sys/types.h>
     45  1.4  glass 
     46  1.4  glass #include <ctype.h>
     47  1.1    cgd #include <pwd.h>
     48  1.1    cgd #include <stdio.h>
     49  1.4  glass #include <stdlib.h>
     50  1.1    cgd #include <string.h>
     51  1.4  glass #include <time.h>
     52  1.4  glass #include <tzfile.h>
     53  1.4  glass #include <unistd.h>
     54  1.4  glass 
     55  1.1    cgd #include "chpass.h"
     56  1.1    cgd #include "pathnames.h"
     57  1.1    cgd 
     58  1.1    cgd static int dmsize[] =
     59  1.1    cgd 	{ -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
     60  1.1    cgd static char *months[] =
     61  1.1    cgd 	{ "January", "February", "March", "April", "May", "June",
     62  1.1    cgd 	  "July", "August", "September", "October", "November",
     63  1.1    cgd 	  "December", NULL };
     64  1.4  glass 
     65  1.1    cgd char *
     66  1.1    cgd ttoa(tval)
     67  1.1    cgd 	time_t tval;
     68  1.1    cgd {
     69  1.1    cgd 	struct tm *tp;
     70  1.1    cgd 	static char tbuf[50];
     71  1.1    cgd 
     72  1.1    cgd 	if (tval) {
     73  1.1    cgd 		tp = localtime(&tval);
     74  1.5    mrg 		(void)snprintf(tbuf, sizeof tbuf, "%s %d, %d", months[tp->tm_mon],
     75  1.3    cgd 		    tp->tm_mday, tp->tm_year + TM_YEAR_BASE);
     76  1.1    cgd 	}
     77  1.1    cgd 	else
     78  1.1    cgd 		*tbuf = '\0';
     79  1.4  glass 	return (tbuf);
     80  1.1    cgd }
     81  1.1    cgd 
     82  1.4  glass int
     83  1.1    cgd atot(p, store)
     84  1.1    cgd 	char *p;
     85  1.1    cgd 	time_t *store;
     86  1.1    cgd {
     87  1.1    cgd 	static struct tm *lt;
     88  1.4  glass 	char *t, **mp;
     89  1.4  glass 	time_t tval;
     90  1.1    cgd 	int day, month, year;
     91  1.1    cgd 
     92  1.1    cgd 	if (!*p) {
     93  1.1    cgd 		*store = 0;
     94  1.4  glass 		return (0);
     95  1.1    cgd 	}
     96  1.1    cgd 	if (!lt) {
     97  1.1    cgd 		unsetenv("TZ");
     98  1.1    cgd 		(void)time(&tval);
     99  1.1    cgd 		lt = localtime(&tval);
    100  1.1    cgd 	}
    101  1.1    cgd 	if (!(t = strtok(p, " \t")))
    102  1.1    cgd 		goto bad;
    103  1.1    cgd 	for (mp = months;; ++mp) {
    104  1.1    cgd 		if (!*mp)
    105  1.1    cgd 			goto bad;
    106  1.1    cgd 		if (!strncasecmp(*mp, t, 3)) {
    107  1.1    cgd 			month = mp - months + 1;
    108  1.1    cgd 			break;
    109  1.1    cgd 		}
    110  1.1    cgd 	}
    111  1.1    cgd 	if (!(t = strtok((char *)NULL, " \t,")) || !isdigit(*t))
    112  1.1    cgd 		goto bad;
    113  1.1    cgd 	day = atoi(t);
    114  1.1    cgd 	if (!(t = strtok((char *)NULL, " \t,")) || !isdigit(*t))
    115  1.1    cgd 		goto bad;
    116  1.1    cgd 	year = atoi(t);
    117  1.1    cgd 	if (day < 1 || day > 31 || month < 1 || month > 12 || !year)
    118  1.1    cgd 		goto bad;
    119  1.1    cgd 	if (year < 100)
    120  1.1    cgd 		year += TM_YEAR_BASE;
    121  1.1    cgd 	if (year <= EPOCH_YEAR)
    122  1.4  glass bad:		return (1);
    123  1.1    cgd 	tval = isleap(year) && month > 2;
    124  1.1    cgd 	for (--year; year >= EPOCH_YEAR; --year)
    125  1.1    cgd 		tval += isleap(year) ?
    126  1.1    cgd 		    DAYSPERLYEAR : DAYSPERNYEAR;
    127  1.1    cgd 	while (--month)
    128  1.1    cgd 		tval += dmsize[month];
    129  1.1    cgd 	tval += day;
    130  1.1    cgd 	tval = tval * HOURSPERDAY * MINSPERHOUR * SECSPERMIN;
    131  1.1    cgd 	tval -= lt->tm_gmtoff;
    132  1.1    cgd 	*store = tval;
    133  1.4  glass 	return (0);
    134  1.1    cgd }
    135  1.1    cgd 
    136  1.1    cgd char *
    137  1.1    cgd ok_shell(name)
    138  1.4  glass 	char *name;
    139  1.1    cgd {
    140  1.4  glass 	char *p, *sh;
    141  1.1    cgd 
    142  1.1    cgd 	setusershell();
    143  1.6  mikel 	while ((sh = getusershell()) != NULL) {
    144  1.1    cgd 		if (!strcmp(name, sh))
    145  1.4  glass 			return (name);
    146  1.1    cgd 		/* allow just shell name, but use "real" path */
    147  1.4  glass 		if ((p = strrchr(sh, '/')) && strcmp(name, p + 1) == 0)
    148  1.4  glass 			return (sh);
    149  1.1    cgd 	}
    150  1.4  glass 	return (NULL);
    151  1.1    cgd }
    152