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