time.c 3.9 KB

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