Annotation of src/usr.bin/chpass/util.c, Revision 1.5
1.5 ! deraadt 1: /* $OpenBSD: util.c,v 1.4 1998/03/30 06:59:34 deraadt Exp $ */
1.1 deraadt 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
1.5 ! deraadt 41: static char rcsid[] = "$OpenBSD: util.c,v 1.4 1998/03/30 06:59:34 deraadt Exp $";
1.1 deraadt 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 *
1.5 ! deraadt 60: ttoa(char *buf, size_t len, time_t tval)
1.1 deraadt 61: {
1.4 deraadt 62: if (tval) {
63: struct tm *tp = localtime(&tval);
1.1 deraadt 64:
1.4 deraadt 65: (void) strftime(buf, len, "%B %d, %Y", tp);
66: buf[len - 1] = '\0';
1.5 ! deraadt 67: } else if (len > 0)
1.4 deraadt 68: *buf = '\0';
69: return (buf);
1.5 ! deraadt 70: }
1.1 deraadt 71:
72: int
1.5 ! deraadt 73: atot(char *p, time_t *store)
1.1 deraadt 74: {
75: static struct tm *lt;
1.4 deraadt 76: struct tm tm;
1.5 ! deraadt 77: time_t tval;
1.4 deraadt 78: char *t;
1.1 deraadt 79:
80: if (!*p) {
81: *store = 0;
82: return (0);
83: }
84: if (!lt) {
85: unsetenv("TZ");
86: (void)time(&tval);
87: lt = localtime(&tval);
88: }
1.4 deraadt 89: (void) memset(&tm, 0, sizeof(tm));
90: for (t = p; (t = strchr(t, ',')) != NULL; t++)
91: *t = ' ';
92: t = strptime(p, "%B %d %Y", &tm);
93: if (t == NULL || (*t != '\0' && *t != '\n'))
94: return 1;
95: *store = mktime(&tm);
96: if (*store == (time_t) -1)
97: return 1;
1.1 deraadt 98: return (0);
99: }
100:
101: char *
1.5 ! deraadt 102: ok_shell(char *name)
1.1 deraadt 103: {
104: char *p, *sh;
105:
106: setusershell();
1.4 deraadt 107: while ((sh = getusershell()) != NULL) {
1.1 deraadt 108: if (!strcmp(name, sh))
109: return (name);
110: /* allow just shell name, but use "real" path */
111: if ((p = strrchr(sh, '/')) && strcmp(name, p + 1) == 0)
112: return (sh);
113: }
114: return (NULL);
115: }