NetBSD/usr.bin/chpass/util.c

124 lines
3.3 KiB
C
Raw Normal View History

1998-07-26 19:13:14 +04:00
/* $NetBSD: util.c,v 1.10 1998/07/26 15:13:14 mycroft Exp $ */
1995-03-26 08:55:22 +04:00
1993-03-21 12:45:37 +03:00
/*-
1995-03-26 08:55:22 +04:00
* Copyright (c) 1988, 1993, 1994
* The Regents of the University of California. All rights reserved.
1993-03-21 12:45:37 +03:00
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
1993-03-21 12:45:37 +03:00
#ifndef lint
1995-03-26 08:55:22 +04:00
#if 0
static char sccsid[] = "@(#)util.c 8.4 (Berkeley) 4/2/94";
#else
1998-07-26 19:13:14 +04:00
__RCSID("$NetBSD: util.c,v 1.10 1998/07/26 15:13:14 mycroft Exp $");
1995-03-26 08:55:22 +04:00
#endif
1993-03-21 12:45:37 +03:00
#endif /* not lint */
#include <sys/types.h>
1995-03-26 08:55:22 +04:00
#include <ctype.h>
1993-03-21 12:45:37 +03:00
#include <pwd.h>
#include <stdio.h>
1995-03-26 08:55:22 +04:00
#include <stdlib.h>
1993-03-21 12:45:37 +03:00
#include <string.h>
1995-03-26 08:55:22 +04:00
#include <time.h>
#include <tzfile.h>
#include <unistd.h>
1993-03-21 12:45:37 +03:00
#include "chpass.h"
#include "pathnames.h"
char *
1998-02-04 18:21:52 +03:00
ttoa(buf, len, tval)
char *buf;
size_t len;
1993-03-21 12:45:37 +03:00
time_t tval;
{
if (tval) {
1998-02-04 18:21:52 +03:00
struct tm *tp = localtime(&tval);
(void) strftime(buf, len, "%B %d, %Y", tp);
buf[len - 1] = '\0';
1993-03-21 12:45:37 +03:00
}
1998-02-04 18:21:52 +03:00
else if (len > 0)
*buf = '\0';
return (buf);
1993-03-21 12:45:37 +03:00
}
1995-03-26 08:55:22 +04:00
int
1993-03-21 12:45:37 +03:00
atot(p, store)
1998-07-26 19:13:14 +04:00
const char *p;
1993-03-21 12:45:37 +03:00
time_t *store;
{
static struct tm *lt;
1998-02-04 18:21:52 +03:00
struct tm tm;
char *t;
1995-03-26 08:55:22 +04:00
time_t tval;
1993-03-21 12:45:37 +03:00
if (!*p) {
*store = 0;
1995-03-26 08:55:22 +04:00
return (0);
1993-03-21 12:45:37 +03:00
}
if (!lt) {
unsetenv("TZ");
(void)time(&tval);
lt = localtime(&tval);
}
1998-02-04 18:21:52 +03:00
(void) memset(&tm, 0, sizeof(tm));
while ((t = strchr(p, ',')) != NULL)
*t = ' ';
t = strptime(p, "%B %d %Y", &tm);
if (t == NULL || (*t != '\0' && *t != '\n'))
return 1;
if ((*store = mktime(&tm)) == (time_t) -1)
return 1;
1995-03-26 08:55:22 +04:00
return (0);
1993-03-21 12:45:37 +03:00
}
1998-07-26 19:13:14 +04:00
const char *
1993-03-21 12:45:37 +03:00
ok_shell(name)
1998-07-26 19:13:14 +04:00
const char *name;
1993-03-21 12:45:37 +03:00
{
1998-07-26 18:57:56 +04:00
char *p;
const char *sh;
1993-03-21 12:45:37 +03:00
setusershell();
while ((sh = getusershell()) != NULL) {
1993-03-21 12:45:37 +03:00
if (!strcmp(name, sh))
1995-03-26 08:55:22 +04:00
return (name);
1993-03-21 12:45:37 +03:00
/* allow just shell name, but use "real" path */
1995-03-26 08:55:22 +04:00
if ((p = strrchr(sh, '/')) && strcmp(name, p + 1) == 0)
return (sh);
1993-03-21 12:45:37 +03:00
}
1995-03-26 08:55:22 +04:00
return (NULL);
1993-03-21 12:45:37 +03:00
}