util.c revision 93086
11590Srgrimes/*- 21590Srgrimes * Copyright (c) 1988, 1993, 1994 31590Srgrimes * The Regents of the University of California. All rights reserved. 41590Srgrimes * 51590Srgrimes * Redistribution and use in source and binary forms, with or without 61590Srgrimes * modification, are permitted provided that the following conditions 71590Srgrimes * are met: 81590Srgrimes * 1. Redistributions of source code must retain the above copyright 91590Srgrimes * notice, this list of conditions and the following disclaimer. 101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111590Srgrimes * notice, this list of conditions and the following disclaimer in the 121590Srgrimes * documentation and/or other materials provided with the distribution. 131590Srgrimes * 3. All advertising materials mentioning features or use of this software 141590Srgrimes * must display the following acknowledgement: 151590Srgrimes * This product includes software developed by the University of 161590Srgrimes * California, Berkeley and its contributors. 171590Srgrimes * 4. Neither the name of the University nor the names of its contributors 181590Srgrimes * may be used to endorse or promote products derived from this software 191590Srgrimes * without specific prior written permission. 201590Srgrimes * 211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311590Srgrimes * SUCH DAMAGE. 321590Srgrimes */ 331590Srgrimes 341590Srgrimes#ifndef lint 3593086Smarkmstatic const char sccsid[] = "@(#)util.c 8.4 (Berkeley) 4/2/94"; 361590Srgrimes#endif /* not lint */ 371590Srgrimes 3893086Smarkm#include <sys/cdefs.h> 3993086Smarkm__FBSDID("$FreeBSD: head/usr.bin/chpass/util.c 93086 2002-03-24 10:21:22Z markm $"); 4093086Smarkm 411590Srgrimes#include <sys/types.h> 421590Srgrimes 431590Srgrimes#include <ctype.h> 441590Srgrimes#include <pwd.h> 451590Srgrimes#include <stdio.h> 461590Srgrimes#include <stdlib.h> 471590Srgrimes#include <string.h> 481590Srgrimes#include <time.h> 491590Srgrimes#include <unistd.h> 501590Srgrimes 511590Srgrimes#include "chpass.h" 521590Srgrimes#include "pathnames.h" 531590Srgrimes 5493086Smarkmstatic const char *months[] = 551590Srgrimes { "January", "February", "March", "April", "May", "June", 561590Srgrimes "July", "August", "September", "October", "November", 571590Srgrimes "December", NULL }; 581590Srgrimes 591590Srgrimeschar * 6093086Smarkmttoa(time_t tval) 611590Srgrimes{ 621590Srgrimes struct tm *tp; 631590Srgrimes static char tbuf[50]; 641590Srgrimes 651590Srgrimes if (tval) { 661590Srgrimes tp = localtime(&tval); 671590Srgrimes (void)sprintf(tbuf, "%s %d, %d", months[tp->tm_mon], 689987Swollman tp->tm_mday, tp->tm_year + 1900); 691590Srgrimes } 701590Srgrimes else 711590Srgrimes *tbuf = '\0'; 721590Srgrimes return (tbuf); 738874Srgrimes} 741590Srgrimes 751590Srgrimesint 7693086Smarkmatot(char *p, time_t *store) 771590Srgrimes{ 781590Srgrimes static struct tm *lt; 7993086Smarkm char *t; 8093086Smarkm const char **mp; 811590Srgrimes time_t tval; 821590Srgrimes int day, month, year; 831590Srgrimes 841590Srgrimes if (!*p) { 851590Srgrimes *store = 0; 861590Srgrimes return (0); 871590Srgrimes } 881590Srgrimes if (!lt) { 891590Srgrimes unsetenv("TZ"); 901590Srgrimes (void)time(&tval); 911590Srgrimes lt = localtime(&tval); 921590Srgrimes } 931590Srgrimes if (!(t = strtok(p, " \t"))) 941590Srgrimes goto bad; 9517544Speter if (isdigit(*t)) { 9617544Speter month = atoi(t); 9717544Speter } else { 9817544Speter for (mp = months;; ++mp) { 9917544Speter if (!*mp) 10017544Speter goto bad; 10117544Speter if (!strncasecmp(*mp, t, 3)) { 10217544Speter month = mp - months + 1; 10317544Speter break; 10417544Speter } 1051590Srgrimes } 1061590Srgrimes } 1071590Srgrimes if (!(t = strtok((char *)NULL, " \t,")) || !isdigit(*t)) 1081590Srgrimes goto bad; 1091590Srgrimes day = atoi(t); 1101590Srgrimes if (!(t = strtok((char *)NULL, " \t,")) || !isdigit(*t)) 1111590Srgrimes goto bad; 1121590Srgrimes year = atoi(t); 11392554Scjc if (day < 1 || day > 31 || month < 1 || month > 12) 1141590Srgrimes goto bad; 11542815Sdanny /* Allow two digit years 1969-2068 */ 11642815Sdanny if (year < 69) 11742815Sdanny year += 2000; 11842815Sdanny else if (year < 100) 1199987Swollman year += 1900; 12042815Sdanny if (year < 1969) 1211590Srgrimesbad: return (1); 1229987Swollman lt->tm_year = year - 1900; 1239554Smpp lt->tm_mon = month - 1; 1249554Smpp lt->tm_mday = day; 1259554Smpp lt->tm_hour = 0; 1269554Smpp lt->tm_min = 0; 1279554Smpp lt->tm_sec = 0; 1289554Smpp lt->tm_isdst = -1; 1299554Smpp if ((tval = mktime(lt)) < 0) 1309554Smpp return (1); 1311590Srgrimes *store = tval; 1321590Srgrimes return (0); 1331590Srgrimes} 1341590Srgrimes 1351590Srgrimeschar * 13693086Smarkmok_shell(char *name) 1371590Srgrimes{ 1381590Srgrimes char *p, *sh; 1391590Srgrimes 1401590Srgrimes setusershell(); 14134797Scharnier while ((sh = getusershell())) { 1421590Srgrimes if (!strcmp(name, sh)) 1431590Srgrimes return (name); 1441590Srgrimes /* allow just shell name, but use "real" path */ 1451590Srgrimes if ((p = strrchr(sh, '/')) && strcmp(name, p + 1) == 0) 1461590Srgrimes return (sh); 1471590Srgrimes } 1481590Srgrimes return (NULL); 1491590Srgrimes} 150