util.c revision 9987
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; 941590Srgrimes for (mp = months;; ++mp) { 951590Srgrimes if (!*mp) 961590Srgrimes goto bad; 971590Srgrimes if (!strncasecmp(*mp, t, 3)) { 981590Srgrimes month = mp - months + 1; 991590Srgrimes break; 1001590Srgrimes } 1011590Srgrimes } 1021590Srgrimes if (!(t = strtok((char *)NULL, " \t,")) || !isdigit(*t)) 1031590Srgrimes goto bad; 1041590Srgrimes day = atoi(t); 1051590Srgrimes if (!(t = strtok((char *)NULL, " \t,")) || !isdigit(*t)) 1061590Srgrimes goto bad; 1071590Srgrimes year = atoi(t); 1081590Srgrimes if (day < 1 || day > 31 || month < 1 || month > 12 || !year) 1091590Srgrimes goto bad; 1101590Srgrimes if (year < 100) 1119987Swollman year += 1900; 1129987Swollman if (year <= 1970) 1131590Srgrimesbad: return (1); 1149987Swollman lt->tm_year = year - 1900; 1159554Smpp lt->tm_mon = month - 1; 1169554Smpp lt->tm_mday = day; 1179554Smpp lt->tm_hour = 0; 1189554Smpp lt->tm_min = 0; 1199554Smpp lt->tm_sec = 0; 1209554Smpp lt->tm_isdst = -1; 1219554Smpp if ((tval = mktime(lt)) < 0) 1229554Smpp return (1); 1231590Srgrimes *store = tval; 1241590Srgrimes return (0); 1251590Srgrimes} 1261590Srgrimes 1271590Srgrimeschar * 1281590Srgrimesok_shell(name) 1291590Srgrimes char *name; 1301590Srgrimes{ 1311590Srgrimes char *p, *sh; 1321590Srgrimes 1331590Srgrimes setusershell(); 1341590Srgrimes while (sh = getusershell()) { 1351590Srgrimes if (!strcmp(name, sh)) 1361590Srgrimes return (name); 1371590Srgrimes /* allow just shell name, but use "real" path */ 1381590Srgrimes if ((p = strrchr(sh, '/')) && strcmp(name, p + 1) == 0) 1391590Srgrimes return (sh); 1401590Srgrimes } 1411590Srgrimes return (NULL); 1421590Srgrimes} 143