util.c revision 17544
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 351590Srgrimesstatic char sccsid[] = "@(#)util.c 8.4 (Berkeley) 4/2/94"; 361590Srgrimes#endif /* not lint */ 371590Srgrimes 381590Srgrimes#include <sys/types.h> 391590Srgrimes 401590Srgrimes#include <ctype.h> 411590Srgrimes#include <pwd.h> 421590Srgrimes#include <stdio.h> 431590Srgrimes#include <stdlib.h> 441590Srgrimes#include <string.h> 451590Srgrimes#include <time.h> 461590Srgrimes#include <unistd.h> 471590Srgrimes 481590Srgrimes#include "chpass.h" 491590Srgrimes#include "pathnames.h" 501590Srgrimes 511590Srgrimesstatic char *months[] = 521590Srgrimes { "January", "February", "March", "April", "May", "June", 531590Srgrimes "July", "August", "September", "October", "November", 541590Srgrimes "December", NULL }; 551590Srgrimes 561590Srgrimeschar * 571590Srgrimesttoa(tval) 581590Srgrimes time_t tval; 591590Srgrimes{ 601590Srgrimes struct tm *tp; 611590Srgrimes static char tbuf[50]; 621590Srgrimes 631590Srgrimes if (tval) { 641590Srgrimes tp = localtime(&tval); 651590Srgrimes (void)sprintf(tbuf, "%s %d, %d", months[tp->tm_mon], 669987Swollman tp->tm_mday, tp->tm_year + 1900); 671590Srgrimes } 681590Srgrimes else 691590Srgrimes *tbuf = '\0'; 701590Srgrimes return (tbuf); 718874Srgrimes} 721590Srgrimes 731590Srgrimesint 741590Srgrimesatot(p, store) 751590Srgrimes char *p; 761590Srgrimes time_t *store; 771590Srgrimes{ 781590Srgrimes static struct tm *lt; 791590Srgrimes char *t, **mp; 801590Srgrimes time_t tval; 811590Srgrimes int day, month, year; 821590Srgrimes 831590Srgrimes if (!*p) { 841590Srgrimes *store = 0; 851590Srgrimes return (0); 861590Srgrimes } 871590Srgrimes if (!lt) { 881590Srgrimes unsetenv("TZ"); 891590Srgrimes (void)time(&tval); 901590Srgrimes lt = localtime(&tval); 911590Srgrimes } 921590Srgrimes if (!(t = strtok(p, " \t"))) 931590Srgrimes goto bad; 9417544Speter if (isdigit(*t)) { 9517544Speter month = atoi(t); 9617544Speter } else { 9717544Speter for (mp = months;; ++mp) { 9817544Speter if (!*mp) 9917544Speter goto bad; 10017544Speter if (!strncasecmp(*mp, t, 3)) { 10117544Speter month = mp - months + 1; 10217544Speter break; 10317544Speter } 1041590Srgrimes } 1051590Srgrimes } 1061590Srgrimes if (!(t = strtok((char *)NULL, " \t,")) || !isdigit(*t)) 1071590Srgrimes goto bad; 1081590Srgrimes day = atoi(t); 1091590Srgrimes if (!(t = strtok((char *)NULL, " \t,")) || !isdigit(*t)) 1101590Srgrimes goto bad; 1111590Srgrimes year = atoi(t); 1121590Srgrimes if (day < 1 || day > 31 || month < 1 || month > 12 || !year) 1131590Srgrimes goto bad; 1141590Srgrimes if (year < 100) 1159987Swollman year += 1900; 1169987Swollman if (year <= 1970) 1171590Srgrimesbad: return (1); 1189987Swollman lt->tm_year = year - 1900; 1199554Smpp lt->tm_mon = month - 1; 1209554Smpp lt->tm_mday = day; 1219554Smpp lt->tm_hour = 0; 1229554Smpp lt->tm_min = 0; 1239554Smpp lt->tm_sec = 0; 1249554Smpp lt->tm_isdst = -1; 1259554Smpp if ((tval = mktime(lt)) < 0) 1269554Smpp return (1); 1271590Srgrimes *store = tval; 1281590Srgrimes return (0); 1291590Srgrimes} 1301590Srgrimes 1311590Srgrimeschar * 1321590Srgrimesok_shell(name) 1331590Srgrimes char *name; 1341590Srgrimes{ 1351590Srgrimes char *p, *sh; 1361590Srgrimes 1371590Srgrimes setusershell(); 1381590Srgrimes while (sh = getusershell()) { 1391590Srgrimes if (!strcmp(name, sh)) 1401590Srgrimes return (name); 1411590Srgrimes /* allow just shell name, but use "real" path */ 1421590Srgrimes if ((p = strrchr(sh, '/')) && strcmp(name, p + 1) == 0) 1431590Srgrimes return (sh); 1441590Srgrimes } 1451590Srgrimes return (NULL); 1461590Srgrimes} 147