Home | History | Annotate | Line # | Download | only in chpass
util.c revision 1.9
      1  1.9   mycroft /*	$NetBSD: util.c,v 1.9 1998/07/26 15:08:02 mycroft 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.9   mycroft __RCSID("$NetBSD: util.c,v 1.9 1998/07/26 15:08:02 mycroft 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 char *
     60  1.8  christos ttoa(buf, len, tval)
     61  1.8  christos 	char *buf;
     62  1.8  christos 	size_t len;
     63  1.1       cgd 	time_t tval;
     64  1.1       cgd {
     65  1.1       cgd 
     66  1.1       cgd 	if (tval) {
     67  1.8  christos 		struct tm *tp = localtime(&tval);
     68  1.8  christos 
     69  1.8  christos 		(void) strftime(buf, len, "%B %d, %Y", tp);
     70  1.8  christos 		buf[len - 1] = '\0';
     71  1.1       cgd 	}
     72  1.8  christos 	else if (len > 0)
     73  1.8  christos 		*buf = '\0';
     74  1.8  christos 	return (buf);
     75  1.1       cgd }
     76  1.1       cgd 
     77  1.4     glass int
     78  1.1       cgd atot(p, store)
     79  1.1       cgd 	char *p;
     80  1.1       cgd 	time_t *store;
     81  1.1       cgd {
     82  1.1       cgd 	static struct tm *lt;
     83  1.8  christos 	struct tm tm;
     84  1.8  christos 	char *t;
     85  1.4     glass 	time_t tval;
     86  1.1       cgd 
     87  1.1       cgd 	if (!*p) {
     88  1.1       cgd 		*store = 0;
     89  1.4     glass 		return (0);
     90  1.1       cgd 	}
     91  1.1       cgd 	if (!lt) {
     92  1.1       cgd 		unsetenv("TZ");
     93  1.1       cgd 		(void)time(&tval);
     94  1.1       cgd 		lt = localtime(&tval);
     95  1.1       cgd 	}
     96  1.8  christos 	(void) memset(&tm, 0, sizeof(tm));
     97  1.8  christos 	while ((t = strchr(p, ',')) != NULL)
     98  1.8  christos 		*t = ' ';
     99  1.8  christos 	t = strptime(p, "%B %d %Y", &tm);
    100  1.8  christos 	if (t == NULL || (*t != '\0' && *t != '\n'))
    101  1.8  christos 		return 1;
    102  1.8  christos 	if ((*store = mktime(&tm)) == (time_t) -1)
    103  1.8  christos 		return 1;
    104  1.4     glass 	return (0);
    105  1.1       cgd }
    106  1.1       cgd 
    107  1.1       cgd char *
    108  1.1       cgd ok_shell(name)
    109  1.4     glass 	char *name;
    110  1.1       cgd {
    111  1.9   mycroft 	char *p;
    112  1.9   mycroft 	const char *sh;
    113  1.1       cgd 
    114  1.1       cgd 	setusershell();
    115  1.6     mikel 	while ((sh = getusershell()) != NULL) {
    116  1.1       cgd 		if (!strcmp(name, sh))
    117  1.4     glass 			return (name);
    118  1.1       cgd 		/* allow just shell name, but use "real" path */
    119  1.4     glass 		if ((p = strrchr(sh, '/')) && strcmp(name, p + 1) == 0)
    120  1.4     glass 			return (sh);
    121  1.1       cgd 	}
    122  1.4     glass 	return (NULL);
    123  1.1       cgd }
    124