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