xref: /minix3/usr.bin/chpass/util.c (revision 5c00743626989a611dd2966296c00a8cc4abca6f)
1*5c007436SBen Gras /*	$NetBSD: util.c,v 1.12 2005/02/17 17:09:48 xtraeme Exp $	*/
2*5c007436SBen Gras 
3*5c007436SBen Gras /*-
4*5c007436SBen Gras  * Copyright (c) 1988, 1993, 1994
5*5c007436SBen Gras  *	The Regents of the University of California.  All rights reserved.
6*5c007436SBen Gras  *
7*5c007436SBen Gras  * Redistribution and use in source and binary forms, with or without
8*5c007436SBen Gras  * modification, are permitted provided that the following conditions
9*5c007436SBen Gras  * are met:
10*5c007436SBen Gras  * 1. Redistributions of source code must retain the above copyright
11*5c007436SBen Gras  *    notice, this list of conditions and the following disclaimer.
12*5c007436SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
13*5c007436SBen Gras  *    notice, this list of conditions and the following disclaimer in the
14*5c007436SBen Gras  *    documentation and/or other materials provided with the distribution.
15*5c007436SBen Gras  * 3. Neither the name of the University nor the names of its contributors
16*5c007436SBen Gras  *    may be used to endorse or promote products derived from this software
17*5c007436SBen Gras  *    without specific prior written permission.
18*5c007436SBen Gras  *
19*5c007436SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20*5c007436SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*5c007436SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*5c007436SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23*5c007436SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*5c007436SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*5c007436SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*5c007436SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*5c007436SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*5c007436SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*5c007436SBen Gras  * SUCH DAMAGE.
30*5c007436SBen Gras  */
31*5c007436SBen Gras 
32*5c007436SBen Gras #include <sys/cdefs.h>
33*5c007436SBen Gras #ifndef lint
34*5c007436SBen Gras #if 0
35*5c007436SBen Gras static char sccsid[] = "@(#)util.c	8.4 (Berkeley) 4/2/94";
36*5c007436SBen Gras #else
37*5c007436SBen Gras __RCSID("$NetBSD: util.c,v 1.12 2005/02/17 17:09:48 xtraeme Exp $");
38*5c007436SBen Gras #endif
39*5c007436SBen Gras #endif /* not lint */
40*5c007436SBen Gras 
41*5c007436SBen Gras #include <sys/types.h>
42*5c007436SBen Gras 
43*5c007436SBen Gras #include <ctype.h>
44*5c007436SBen Gras #include <pwd.h>
45*5c007436SBen Gras #include <stdio.h>
46*5c007436SBen Gras #include <stdlib.h>
47*5c007436SBen Gras #include <string.h>
48*5c007436SBen Gras #include <time.h>
49*5c007436SBen Gras #include <tzfile.h>
50*5c007436SBen Gras #include <unistd.h>
51*5c007436SBen Gras 
52*5c007436SBen Gras #include "chpass.h"
53*5c007436SBen Gras #include "pathnames.h"
54*5c007436SBen Gras 
55*5c007436SBen Gras char *
ttoa(char * buf,size_t len,time_t tval)56*5c007436SBen Gras ttoa(char *buf, size_t len, time_t tval)
57*5c007436SBen Gras {
58*5c007436SBen Gras 
59*5c007436SBen Gras 	if (tval) {
60*5c007436SBen Gras 		struct tm *tp = localtime(&tval);
61*5c007436SBen Gras 
62*5c007436SBen Gras 		(void) strftime(buf, len, "%B %d, %Y", tp);
63*5c007436SBen Gras 		buf[len - 1] = '\0';
64*5c007436SBen Gras 	}
65*5c007436SBen Gras 	else if (len > 0)
66*5c007436SBen Gras 		*buf = '\0';
67*5c007436SBen Gras 	return (buf);
68*5c007436SBen Gras }
69*5c007436SBen Gras 
70*5c007436SBen Gras int
atot(const char * p,time_t * store)71*5c007436SBen Gras atot(const char *p, time_t *store)
72*5c007436SBen Gras {
73*5c007436SBen Gras 	static struct tm *lt;
74*5c007436SBen Gras 	struct tm tm;
75*5c007436SBen Gras 	char *t;
76*5c007436SBen Gras 	time_t tval;
77*5c007436SBen Gras 
78*5c007436SBen Gras 	if (!*p) {
79*5c007436SBen Gras 		*store = 0;
80*5c007436SBen Gras 		return (0);
81*5c007436SBen Gras 	}
82*5c007436SBen Gras 	if (!lt) {
83*5c007436SBen Gras 		unsetenv("TZ");
84*5c007436SBen Gras 		(void)time(&tval);
85*5c007436SBen Gras 		lt = localtime(&tval);
86*5c007436SBen Gras 	}
87*5c007436SBen Gras 	(void) memset(&tm, 0, sizeof(tm));
88*5c007436SBen Gras 	while ((t = strchr(p, ',')) != NULL)
89*5c007436SBen Gras 		*t = ' ';
90*5c007436SBen Gras 	t = strptime(p, "%B %d %Y", &tm);
91*5c007436SBen Gras 	if (t == NULL || (*t != '\0' && *t != '\n'))
92*5c007436SBen Gras 		return 1;
93*5c007436SBen Gras 	if ((*store = mktime(&tm)) == (time_t) -1)
94*5c007436SBen Gras 		return 1;
95*5c007436SBen Gras 	return (0);
96*5c007436SBen Gras }
97*5c007436SBen Gras 
98*5c007436SBen Gras const char *
ok_shell(const char * name)99*5c007436SBen Gras ok_shell(const char *name)
100*5c007436SBen Gras {
101*5c007436SBen Gras 	char *p;
102*5c007436SBen Gras 	const char *sh;
103*5c007436SBen Gras 
104*5c007436SBen Gras 	setusershell();
105*5c007436SBen Gras 	while ((sh = getusershell()) != NULL) {
106*5c007436SBen Gras 		if (!strcmp(name, sh))
107*5c007436SBen Gras 			return (name);
108*5c007436SBen Gras 		/* allow just shell name, but use "real" path */
109*5c007436SBen Gras 		if ((p = strrchr(sh, '/')) && strcmp(name, p + 1) == 0)
110*5c007436SBen Gras 			return (sh);
111*5c007436SBen Gras 	}
112*5c007436SBen Gras 	return (NULL);
113*5c007436SBen Gras }
114