util.c revision 9554
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 <tzfile.h> 471590Srgrimes#include <unistd.h> 481590Srgrimes 491590Srgrimes#include "chpass.h" 501590Srgrimes#include "pathnames.h" 511590Srgrimes 521590Srgrimesstatic char *months[] = 531590Srgrimes { "January", "February", "March", "April", "May", "June", 541590Srgrimes "July", "August", "September", "October", "November", 551590Srgrimes "December", NULL }; 561590Srgrimes 571590Srgrimeschar * 581590Srgrimesttoa(tval) 591590Srgrimes time_t tval; 601590Srgrimes{ 611590Srgrimes struct tm *tp; 621590Srgrimes static char tbuf[50]; 631590Srgrimes 641590Srgrimes if (tval) { 651590Srgrimes tp = localtime(&tval); 661590Srgrimes (void)sprintf(tbuf, "%s %d, %d", months[tp->tm_mon], 671590Srgrimes tp->tm_mday, tp->tm_year + TM_YEAR_BASE); 681590Srgrimes } 691590Srgrimes else 701590Srgrimes *tbuf = '\0'; 711590Srgrimes return (tbuf); 728874Srgrimes} 731590Srgrimes 741590Srgrimesint 751590Srgrimesatot(p, store) 761590Srgrimes char *p; 771590Srgrimes time_t *store; 781590Srgrimes{ 791590Srgrimes static struct tm *lt; 801590Srgrimes char *t, **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; 951590Srgrimes for (mp = months;; ++mp) { 961590Srgrimes if (!*mp) 971590Srgrimes goto bad; 981590Srgrimes if (!strncasecmp(*mp, t, 3)) { 991590Srgrimes month = mp - months + 1; 1001590Srgrimes break; 1011590Srgrimes } 1021590Srgrimes } 1031590Srgrimes if (!(t = strtok((char *)NULL, " \t,")) || !isdigit(*t)) 1041590Srgrimes goto bad; 1051590Srgrimes day = atoi(t); 1061590Srgrimes if (!(t = strtok((char *)NULL, " \t,")) || !isdigit(*t)) 1071590Srgrimes goto bad; 1081590Srgrimes year = atoi(t); 1091590Srgrimes if (day < 1 || day > 31 || month < 1 || month > 12 || !year) 1101590Srgrimes goto bad; 1111590Srgrimes if (year < 100) 1121590Srgrimes year += TM_YEAR_BASE; 1131590Srgrimes if (year <= EPOCH_YEAR) 1141590Srgrimesbad: return (1); 1159554Smpp lt->tm_year = year - TM_YEAR_BASE; 1169554Smpp lt->tm_mon = month - 1; 1179554Smpp lt->tm_mday = day; 1189554Smpp lt->tm_hour = 0; 1199554Smpp lt->tm_min = 0; 1209554Smpp lt->tm_sec = 0; 1219554Smpp lt->tm_isdst = -1; 1229554Smpp if ((tval = mktime(lt)) < 0) 1239554Smpp return (1); 1241590Srgrimes *store = tval; 1251590Srgrimes return (0); 1261590Srgrimes} 1271590Srgrimes 1281590Srgrimeschar * 1291590Srgrimesok_shell(name) 1301590Srgrimes char *name; 1311590Srgrimes{ 1321590Srgrimes char *p, *sh; 1331590Srgrimes 1341590Srgrimes setusershell(); 1351590Srgrimes while (sh = getusershell()) { 1361590Srgrimes if (!strcmp(name, sh)) 1371590Srgrimes return (name); 1381590Srgrimes /* allow just shell name, but use "real" path */ 1391590Srgrimes if ((p = strrchr(sh, '/')) && strcmp(name, p + 1) == 0) 1401590Srgrimes return (sh); 1411590Srgrimes } 1421590Srgrimes return (NULL); 1431590Srgrimes} 144