sd_xplatform.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. static const char version[] = "$Id$";
  2. /*
  3. * sd_xplatform.c
  4. *
  5. * See the COPYING file for the terms of usage and distribution.
  6. */
  7. #include <tchar.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include "log4c/defs.h"
  11. #include "sd_xplatform.h"
  12. /****************** getopt *******************************/
  13. #define EOF (-1)
  14. int sd_opterr = 1;
  15. int sd_optind = 1;
  16. int sd_optopt = 0;
  17. TCHAR *sd_optarg = NULL;
  18. int _sp = 1;
  19. #define warn(a,b,c) _ftprintf(stderr,a,b,c)
  20. void getopt_reset(void)
  21. {
  22. sd_opterr = 1;
  23. sd_optind = 1;
  24. sd_optopt = 0;
  25. sd_optarg = NULL;
  26. _sp = 1;
  27. }
  28. int sd_getopt(int argc, TCHAR *const *argv, const TCHAR *opts)
  29. {
  30. TCHAR c;
  31. const TCHAR *cp;
  32. if (_sp == 1)
  33. {
  34. if (sd_optind >= argc || argv[sd_optind][0] != _T('-') || argv[sd_optind] == NULL || argv[sd_optind][1] == _T('\0'))
  35. return (EOF);
  36. else if (_tcscmp(argv[sd_optind], _T("--")) == 0)
  37. {
  38. sd_optind++;
  39. return (EOF);
  40. }
  41. }
  42. sd_optopt = c = (unsigned char)argv[sd_optind][_sp];
  43. if (c == _T(':') || (cp = _tcschr(opts, c)) == NULL)
  44. {
  45. if (opts[0] != _T(':'))
  46. warn(_T("%s: illegal option -- %c\n"), argv[0], c);
  47. if (argv[sd_optind][++_sp] == _T('\0'))
  48. {
  49. sd_optind++;
  50. _sp = 1;
  51. }
  52. return _T('?');
  53. }
  54. if (*(cp + 1) == _T(':'))
  55. {
  56. if (argv[sd_optind][_sp+1] != _T('\0'))
  57. sd_optarg = &argv[sd_optind++][_sp+1];
  58. else if (++sd_optind >= argc)
  59. {
  60. if (opts[0] != _T(':'))
  61. {
  62. warn(_T("%s: option requires an argument -- %c\n"), argv[0], c);
  63. }
  64. _sp = 1;
  65. sd_optarg = NULL;
  66. return (opts[0] == _T(':') ? _T(':') : _T('?'));
  67. }
  68. else
  69. sd_optarg = argv[sd_optind++];
  70. _sp = 1;
  71. }
  72. else
  73. {
  74. if (argv[sd_optind][++_sp] == _T('\0'))
  75. {
  76. _sp = 1;
  77. sd_optind++;
  78. }
  79. sd_optarg = NULL;
  80. }
  81. return (c);
  82. }
  83. /***************************** gettimeofday *******************/
  84. #ifdef _WIN32
  85. #if 0 /* also in winsock[2].h */
  86. #define _TIMEVAL_DEFINED
  87. struct timeval {
  88. long tv_sec;
  89. long tv_usec;
  90. long tv_usec;
  91. };
  92. #endif /* _TIMEVAL_DEFINED */
  93. int sd_gettimeofday(LPFILETIME lpft, void* tzp) {
  94. if (lpft) {
  95. GetSystemTimeAsFileTime(lpft);
  96. }
  97. /* 0 indicates that the call succeeded. */
  98. return 0;
  99. }
  100. #endif /* _WIN32 */
  101. /*
  102. * Placeholder for WIN32 version to get last changetime of a file
  103. */
  104. #ifdef WIN32
  105. int sd_stat_ctime(const TCHAR* path, time_t* time)
  106. {
  107. return -1;
  108. }
  109. #else
  110. int sd_stat_ctime(const char* path, time_t* time)
  111. {
  112. struct stat astat;
  113. int statret=stat(path,&astat);
  114. if (0 != statret)
  115. {
  116. return statret;
  117. }
  118. #ifdef __USE_MISC
  119. *time=astat.st_ctim.tv_sec;
  120. #else
  121. *time=astat.st_ctime;
  122. #endif
  123. return statret;
  124. }
  125. #endif