Annotation of embedaddon/ntp/sntp/libopts/time.c, revision 1.1

1.1     ! misho       1: 
        !             2: /**
        !             3:  * \file time.c
        !             4:  *
        !             5:  *  Time-stamp:      "2011-03-06 11:52:23 bkorb"
        !             6:  *
        !             7:  *  This file is part of AutoOpts, a companion to AutoGen.
        !             8:  *  AutoOpts is free software.
        !             9:  *  AutoOpts is Copyright (c) 1992-2011 by Bruce Korb - all rights reserved
        !            10:  *
        !            11:  *  AutoOpts is available under any one of two licenses.  The license
        !            12:  *  in use must be one of these two and the choice is under the control
        !            13:  *  of the user of the license.
        !            14:  *
        !            15:  *   The GNU Lesser General Public License, version 3 or later
        !            16:  *      See the files "COPYING.lgplv3" and "COPYING.gplv3"
        !            17:  *
        !            18:  *   The Modified Berkeley Software Distribution License
        !            19:  *      See the file "COPYING.mbsd"
        !            20:  *
        !            21:  *  These files have the following md5sums:
        !            22:  *
        !            23:  *  43b91e8ca915626ed3818ffb1b71248b pkg/libopts/COPYING.gplv3
        !            24:  *  06a1a2e4760c90ea5e1dad8dfaac4d39 pkg/libopts/COPYING.lgplv3
        !            25:  *  66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd
        !            26:  */
        !            27: 
        !            28: /*=export_func  optionTimeVal
        !            29:  * private:
        !            30:  *
        !            31:  * what:  process an option with a time duration.
        !            32:  * arg:   + tOptions* + pOpts    + program options descriptor +
        !            33:  * arg:   + tOptDesc* + pOptDesc + the descriptor for this arg +
        !            34:  *
        !            35:  * doc:
        !            36:  *  Decipher a time duration value.
        !            37: =*/
        !            38: void
        !            39: optionTimeVal(tOptions * pOpts, tOptDesc * pOD)
        !            40: {
        !            41:     time_t val;
        !            42: 
        !            43:     if ((pOD->fOptState & OPTST_RESET) != 0)
        !            44:         return;
        !            45: 
        !            46:     val = parse_duration(pOD->optArg.argString);
        !            47:     if (val == BAD_TIME) {
        !            48:         fprintf(stderr, zNotDuration, pOpts->pzProgName, pOD->optArg.argString);
        !            49:         if ((pOpts->fOptSet & OPTPROC_ERRSTOP) != 0)
        !            50:             (*(pOpts->pUsageProc))(pOpts, EXIT_FAILURE);
        !            51:     }
        !            52: 
        !            53:     if (pOD->fOptState & OPTST_ALLOC_ARG) {
        !            54:         AGFREE(pOD->optArg.argString);
        !            55:         pOD->fOptState &= ~OPTST_ALLOC_ARG;
        !            56:     }
        !            57: 
        !            58:     pOD->optArg.argInt = val;
        !            59: }
        !            60: 
        !            61: /*=export_func  optionTimeDate
        !            62:  * private:
        !            63:  *
        !            64:  * what:  process an option with a time and date.
        !            65:  * arg:   + tOptions* + pOpts    + program options descriptor +
        !            66:  * arg:   + tOptDesc* + pOptDesc + the descriptor for this arg +
        !            67:  *
        !            68:  * doc:
        !            69:  *  Decipher a time and date value.
        !            70: =*/
        !            71: void
        !            72: optionTimeDate(tOptions * pOpts, tOptDesc * pOD)
        !            73: {
        !            74: #if defined(HAVE_GETDATE_R) && defined(HAVE_PUTENV)
        !            75:     if ((! HAS_pzPkgDataDir(pOpts)) || (pOpts->pzPkgDataDir == NULL))
        !            76:         goto default_action;
        !            77: 
        !            78:     /*
        !            79:      *  Export the DATEMSK environment variable.  getdate_r() uses it to
        !            80:      *  find the file with the strptime formats.  If we cannot find the file
        !            81:      *  we need ($PKGDATADIR/datemsk), then fall back to just a time duration.
        !            82:      */
        !            83:     {
        !            84:         static char * envptr = NULL;
        !            85: 
        !            86:         if (envptr == NULL) {
        !            87:             static char const fmt[] = "DATEMSK=%s/datemsk";
        !            88:             envptr = AGALOC(sizeof(fmt) + strlen(pOpts->pzPkgDataDir), fmt);
        !            89:             sprintf(envptr, fmt, pOpts->pzPkgDataDir);
        !            90: 
        !            91:             putenv(envptr);
        !            92:         }
        !            93: 
        !            94:         if (access(envptr+8, R_OK) != 0)
        !            95:             goto default_action;
        !            96:     }
        !            97: 
        !            98:     /*
        !            99:      *  Convert the date to a time since the epoch and stash it in a long int.
        !           100:      */
        !           101:     {
        !           102:         struct tm stm;
        !           103:         time_t tm;
        !           104: 
        !           105:         if (getdate_r(pOD->optArg.argString, &stm) != 0) {
        !           106:             fprintf(stderr, zNotDate, pOpts->pzProgName,
        !           107:                     pOD->optArg.argString);
        !           108:             if ((pOpts->fOptSet & OPTPROC_ERRSTOP) != 0)
        !           109:                 (*(pOpts->pUsageProc))(pOpts, EXIT_FAILURE);
        !           110:             return;
        !           111:         }
        !           112: 
        !           113:         tm = mktime(&stm);
        !           114: 
        !           115:         if (pOD->fOptState & OPTST_ALLOC_ARG) {
        !           116:             AGFREE(pOD->optArg.argString);
        !           117:             pOD->fOptState &= ~OPTST_ALLOC_ARG;
        !           118:         }
        !           119: 
        !           120:         pOD->optArg.argInt = tm;
        !           121:     }
        !           122:     return;
        !           123: 
        !           124: default_action:
        !           125: 
        !           126: #endif
        !           127:     optionTimeVal(pOpts, pOD);
        !           128:     if (pOD->optArg.argInt != BAD_TIME)
        !           129:         pOD->optArg.argInt += (unsigned long)time(NULL);
        !           130: }
        !           131: /*
        !           132:  * Local Variables:
        !           133:  * mode: C
        !           134:  * c-file-style: "stroustrup"
        !           135:  * indent-tabs-mode: nil
        !           136:  * End:
        !           137:  * end of autoopts/time.c */

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>