1*971b39dfSxtraeme /* $NetBSD: util.c,v 1.12 2005/02/17 17:09:48 xtraeme Exp $ */
27659edadSglass
361f28255Scgd /*-
47659edadSglass * Copyright (c) 1988, 1993, 1994
57659edadSglass * The Regents of the University of California. All rights reserved.
661f28255Scgd *
761f28255Scgd * Redistribution and use in source and binary forms, with or without
861f28255Scgd * modification, are permitted provided that the following conditions
961f28255Scgd * are met:
1061f28255Scgd * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd * notice, this list of conditions and the following disclaimer.
1261f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd * notice, this list of conditions and the following disclaimer in the
1461f28255Scgd * documentation and/or other materials provided with the distribution.
1589aaa1bbSagc * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd * may be used to endorse or promote products derived from this software
1761f28255Scgd * without specific prior written permission.
1861f28255Scgd *
1961f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd * SUCH DAMAGE.
3061f28255Scgd */
3161f28255Scgd
32ad23fe25Slukem #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
347659edadSglass #if 0
357659edadSglass static char sccsid[] = "@(#)util.c 8.4 (Berkeley) 4/2/94";
367659edadSglass #else
37*971b39dfSxtraeme __RCSID("$NetBSD: util.c,v 1.12 2005/02/17 17:09:48 xtraeme Exp $");
387659edadSglass #endif
3961f28255Scgd #endif /* not lint */
4061f28255Scgd
4161f28255Scgd #include <sys/types.h>
427659edadSglass
437659edadSglass #include <ctype.h>
4461f28255Scgd #include <pwd.h>
4561f28255Scgd #include <stdio.h>
467659edadSglass #include <stdlib.h>
4761f28255Scgd #include <string.h>
487659edadSglass #include <time.h>
497659edadSglass #include <tzfile.h>
507659edadSglass #include <unistd.h>
517659edadSglass
5261f28255Scgd #include "chpass.h"
5361f28255Scgd #include "pathnames.h"
5461f28255Scgd
5561f28255Scgd char *
ttoa(char * buf,size_t len,time_t tval)56*971b39dfSxtraeme ttoa(char *buf, size_t len, time_t tval)
5761f28255Scgd {
5861f28255Scgd
5961f28255Scgd if (tval) {
6091fe4341Schristos struct tm *tp = localtime(&tval);
6191fe4341Schristos
6291fe4341Schristos (void) strftime(buf, len, "%B %d, %Y", tp);
6391fe4341Schristos buf[len - 1] = '\0';
6461f28255Scgd }
6591fe4341Schristos else if (len > 0)
6691fe4341Schristos *buf = '\0';
6791fe4341Schristos return (buf);
6861f28255Scgd }
6961f28255Scgd
707659edadSglass int
atot(const char * p,time_t * store)71*971b39dfSxtraeme atot(const char *p, time_t *store)
7261f28255Scgd {
7361f28255Scgd static struct tm *lt;
7491fe4341Schristos struct tm tm;
7591fe4341Schristos char *t;
767659edadSglass time_t tval;
7761f28255Scgd
7861f28255Scgd if (!*p) {
7961f28255Scgd *store = 0;
8061f28255Scgd return (0);
8161f28255Scgd }
8261f28255Scgd if (!lt) {
8361f28255Scgd unsetenv("TZ");
8461f28255Scgd (void)time(&tval);
8561f28255Scgd lt = localtime(&tval);
8661f28255Scgd }
8791fe4341Schristos (void) memset(&tm, 0, sizeof(tm));
8891fe4341Schristos while ((t = strchr(p, ',')) != NULL)
8991fe4341Schristos *t = ' ';
9091fe4341Schristos t = strptime(p, "%B %d %Y", &tm);
9191fe4341Schristos if (t == NULL || (*t != '\0' && *t != '\n'))
9291fe4341Schristos return 1;
9391fe4341Schristos if ((*store = mktime(&tm)) == (time_t) -1)
9491fe4341Schristos return 1;
9561f28255Scgd return (0);
9661f28255Scgd }
9761f28255Scgd
98decd62c2Smycroft const char *
ok_shell(const char * name)99*971b39dfSxtraeme ok_shell(const char *name)
10061f28255Scgd {
1010db548a9Smycroft char *p;
1020db548a9Smycroft const char *sh;
10361f28255Scgd
10461f28255Scgd setusershell();
1052aa6b3b7Smikel while ((sh = getusershell()) != NULL) {
10661f28255Scgd if (!strcmp(name, sh))
10761f28255Scgd return (name);
10861f28255Scgd /* allow just shell name, but use "real" path */
1097659edadSglass if ((p = strrchr(sh, '/')) && strcmp(name, p + 1) == 0)
11061f28255Scgd return (sh);
11161f28255Scgd }
11261f28255Scgd return (NULL);
11361f28255Scgd }
114