/* *---------------------------------------------------------------------------- * Copyright (c) 2002, Daniel B. Hemmerich * All rights reserved. *---------------------------------------------------------------------------- * * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following disclaimer * in the documentation and/or other materials provided with the * distribution. * * * Neither the name of the ipex developers nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * *---------------------------------------------------------------------------- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *---------------------------------------------------------------------------- */ #ifndef lint static const char rcsid[] = \ "@(#) $Header: /usr/cvs/ipex/time.c,v 1.2 2004/08/05 23:00:35 modulus Exp $"; #endif #include "ipex_includes.h" const static char ncharset[] = "1234567890"; const static char scharset[] = "smhd"; void isvalid(char *expr); unsigned long int timer_secs(char *arg) { unsigned long int secs = 0; char unit = 0; if (strchr(scharset, arg[strlen(arg) - 1])) { unit = arg[strlen(arg) - 1]; arg[strlen(arg) - 1] = '\0'; } switch(unit) { case 'm': secs = atol(arg) * 60; break; case 'h': secs = atol(arg) * 3600; break; case 'd': secs = atol(arg) * 86400; break; case 's': default: secs = atol(arg); break; } return (secs); } void set_timer(char *arg) { struct itimerval itv; isvalid(arg); memset(&itv, 0, sizeof(itv)); itv.it_interval.tv_sec = 0; itv.it_interval.tv_usec = 0; itv.it_value.tv_sec = timer_secs(arg); if (setitimer(ITIMER_REAL, &itv, NULL) == -1) errx(EX_OSERR, "fatal: setitimer: %s", strerror(errno)); } void isvalid(char *expr) { int nc = 0; while(*expr) { if (*(expr + 1) == '\0') { if (strchr(ncharset, *expr)) { nc++; break; } if (!nc) errx(EX_USAGE, "fatal: at least one digit " \ "for -t is required"); if (!strchr(scharset, *expr)) errx(EX_USAGE, "fatal: suffix for -t must be " \ "s m h or d"); nc++; break; } if (!strchr(ncharset, *expr)) errx(EX_USAGE, "fatal: -t requires a numeric unit" \ " with optional suffix (%c invalid)", *expr); expr++; nc++; } if (!nc) errx(EX_USAGE, "fatal: -t requires a numeric unit with" \ " optional suffix"); }