time.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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-2014 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. envptr = AGALOC(sizeof(fmt) + strlen(opts->pzPkgDataDir), fmt);
  84. sprintf(envptr, fmt, opts->pzPkgDataDir);
  85. putenv(envptr);
  86. }
  87. if (access(envptr+8, R_OK) != 0)
  88. goto default_action;
  89. }
  90. /*
  91. * Convert the date to a time since the epoch and stash it in a long int.
  92. */
  93. {
  94. struct tm stm;
  95. time_t tm;
  96. if (getdate_r(od->optArg.argString, &stm) != 0) {
  97. fprintf(stderr, zNotDate, opts->pzProgName,
  98. od->optArg.argString);
  99. if ((opts->fOptSet & OPTPROC_ERRSTOP) != 0)
  100. (*(opts->pUsageProc))(opts, EXIT_FAILURE);
  101. return;
  102. }
  103. tm = mktime(&stm);
  104. if (od->fOptState & OPTST_ALLOC_ARG) {
  105. AGFREE(od->optArg.argString);
  106. od->fOptState &= ~OPTST_ALLOC_ARG;
  107. }
  108. od->optArg.argInt = tm;
  109. }
  110. return;
  111. default_action:
  112. #endif
  113. optionTimeVal(opts, od);
  114. if (od->optArg.argInt != BAD_TIME)
  115. od->optArg.argInt += (long)time(NULL);
  116. }
  117. /** @}
  118. *
  119. * Local Variables:
  120. * mode: C
  121. * c-file-style: "stroustrup"
  122. * indent-tabs-mode: nil
  123. * End:
  124. * end of autoopts/time.c */