util.c revision 42815
1219820Sjeff/*- 2219820Sjeff * Copyright (c) 1988, 1993, 1994 3219820Sjeff * The Regents of the University of California. All rights reserved. 4219820Sjeff * 5219820Sjeff * Redistribution and use in source and binary forms, with or without 6219820Sjeff * modification, are permitted provided that the following conditions 7219820Sjeff * are met: 8219820Sjeff * 1. Redistributions of source code must retain the above copyright 9219820Sjeff * notice, this list of conditions and the following disclaimer. 10219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright 11219820Sjeff * notice, this list of conditions and the following disclaimer in the 12219820Sjeff * documentation and/or other materials provided with the distribution. 13219820Sjeff * 3. All advertising materials mentioning features or use of this software 14219820Sjeff * must display the following acknowledgement: 15219820Sjeff * This product includes software developed by the University of 16219820Sjeff * California, Berkeley and its contributors. 17219820Sjeff * 4. Neither the name of the University nor the names of its contributors 18219820Sjeff * may be used to endorse or promote products derived from this software 19219820Sjeff * without specific prior written permission. 20219820Sjeff * 21219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22219820Sjeff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23219820Sjeff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24219820Sjeff * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25219820Sjeff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26219820Sjeff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27219820Sjeff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28219820Sjeff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29219820Sjeff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30219820Sjeff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31219820Sjeff * SUCH DAMAGE. 32219820Sjeff */ 33219820Sjeff 34219820Sjeff#ifndef lint 35219820Sjeff#if 0 36219820Sjeffstatic char sccsid[] = "@(#)util.c 8.4 (Berkeley) 4/2/94"; 37219820Sjeff#endif 38219820Sjeffstatic const char rcsid[] = 39219820Sjeff "$Id: util.c,v 1.6 1998/03/23 07:41:49 charnier Exp $"; 40219820Sjeff#endif /* not lint */ 41219820Sjeff 42219820Sjeff#include <sys/types.h> 43219820Sjeff 44219820Sjeff#include <ctype.h> 45219820Sjeff#include <pwd.h> 46219820Sjeff#include <stdio.h> 47219820Sjeff#include <stdlib.h> 48219820Sjeff#include <string.h> 49219820Sjeff#include <time.h> 50219820Sjeff#include <unistd.h> 51219820Sjeff 52219820Sjeff#include "chpass.h" 53219820Sjeff#include "pathnames.h" 54219820Sjeff 55219820Sjeffstatic char *months[] = 56219820Sjeff { "January", "February", "March", "April", "May", "June", 57219820Sjeff "July", "August", "September", "October", "November", 58219820Sjeff "December", NULL }; 59219820Sjeff 60219820Sjeffchar * 61219820Sjeffttoa(tval) 62219820Sjeff time_t tval; 63219820Sjeff{ 64219820Sjeff struct tm *tp; 65219820Sjeff static char tbuf[50]; 66219820Sjeff 67219820Sjeff if (tval) { 68219820Sjeff tp = localtime(&tval); 69219820Sjeff (void)sprintf(tbuf, "%s %d, %d", months[tp->tm_mon], 70219820Sjeff tp->tm_mday, tp->tm_year + 1900); 71219820Sjeff } 72219820Sjeff else 73219820Sjeff *tbuf = '\0'; 74219820Sjeff return (tbuf); 75219820Sjeff} 76219820Sjeff 77219820Sjeffint 78219820Sjeffatot(p, store) 79219820Sjeff char *p; 80219820Sjeff time_t *store; 81219820Sjeff{ 82219820Sjeff static struct tm *lt; 83219820Sjeff char *t, **mp; 84219820Sjeff time_t tval; 85219820Sjeff int day, month, year; 86219820Sjeff 87219820Sjeff if (!*p) { 88219820Sjeff *store = 0; 89219820Sjeff return (0); 90219820Sjeff } 91219820Sjeff if (!lt) { 92219820Sjeff unsetenv("TZ"); 93219820Sjeff (void)time(&tval); 94219820Sjeff lt = localtime(&tval); 95219820Sjeff } 96219820Sjeff if (!(t = strtok(p, " \t"))) 97219820Sjeff goto bad; 98219820Sjeff if (isdigit(*t)) { 99219820Sjeff month = atoi(t); 100219820Sjeff } else { 101219820Sjeff for (mp = months;; ++mp) { 102219820Sjeff if (!*mp) 103219820Sjeff goto bad; 104219820Sjeff if (!strncasecmp(*mp, t, 3)) { 105219820Sjeff month = mp - months + 1; 106219820Sjeff break; 107219820Sjeff } 108219820Sjeff } 109219820Sjeff } 110219820Sjeff if (!(t = strtok((char *)NULL, " \t,")) || !isdigit(*t)) 111219820Sjeff goto bad; 112219820Sjeff day = atoi(t); 113219820Sjeff if (!(t = strtok((char *)NULL, " \t,")) || !isdigit(*t)) 114219820Sjeff goto bad; 115219820Sjeff year = atoi(t); 116219820Sjeff if (day < 1 || day > 31 || month < 1 || month > 12 || !year) 117219820Sjeff goto bad; 118219820Sjeff /* Allow two digit years 1969-2068 */ 119219820Sjeff if (year < 69) 120219820Sjeff year += 2000; 121219820Sjeff else if (year < 100) 122219820Sjeff year += 1900; 123219820Sjeff if (year < 1969) 124219820Sjeffbad: return (1); 125219820Sjeff lt->tm_year = year - 1900; 126219820Sjeff lt->tm_mon = month - 1; 127219820Sjeff lt->tm_mday = day; 128219820Sjeff lt->tm_hour = 0; 129219820Sjeff lt->tm_min = 0; 130219820Sjeff lt->tm_sec = 0; 131219820Sjeff lt->tm_isdst = -1; 132219820Sjeff if ((tval = mktime(lt)) < 0) 133219820Sjeff return (1); 134219820Sjeff *store = tval; 135219820Sjeff return (0); 136219820Sjeff} 137219820Sjeff 138219820Sjeffchar * 139219820Sjeffok_shell(name) 140219820Sjeff char *name; 141219820Sjeff{ 142219820Sjeff char *p, *sh; 143219820Sjeff 144219820Sjeff setusershell(); 145219820Sjeff while ((sh = getusershell())) { 146219820Sjeff if (!strcmp(name, sh)) 147219820Sjeff return (name); 148219820Sjeff /* allow just shell name, but use "real" path */ 149219820Sjeff if ((p = strrchr(sh, '/')) && strcmp(name, p + 1) == 0) 150219820Sjeff return (sh); 151219820Sjeff } 152219820Sjeff return (NULL); 153219820Sjeff} 154219820Sjeff