sd_xplatform.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #if !defined(_WIN32) || defined(__MINGW32__) || defined(__MINGW64__)
  8. #include <sys/time.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #else
  12. //#include <windows.h>
  13. #endif
  14. #include <stdarg.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <time.h>
  18. #include "log4c/defs.h"
  19. #include "sd_xplatform.h"
  20. /****************** getopt *******************************/
  21. #define EOF (-1)
  22. int sd_opterr = 1;
  23. int sd_optind = 1;
  24. int sd_optopt = 0;
  25. char *sd_optarg = NULL;
  26. int _sp = 1;
  27. #define warn(a,b,c)fprintf(stderr,a,b,c)
  28. void
  29. getopt_reset(void)
  30. {
  31. sd_opterr = 1;
  32. sd_optind = 1;
  33. sd_optopt = 0;
  34. sd_optarg = NULL;
  35. _sp = 1;
  36. }
  37. int
  38. sd_getopt(int argc, char *const *argv, const char *opts)
  39. {
  40. char c;
  41. const char *cp;
  42. if (_sp == 1) {
  43. if (sd_optind >= argc || argv[sd_optind][0] != '-' ||
  44. argv[sd_optind] == NULL || argv[sd_optind][1] == '\0')
  45. return (EOF);
  46. else if (strcmp(argv[sd_optind], "--") == 0) {
  47. sd_optind++;
  48. return (EOF);
  49. }
  50. }
  51. sd_optopt = c = (unsigned char)argv[sd_optind][_sp];
  52. if (c == ':' || (cp = strchr(opts, c)) == NULL) {
  53. if (opts[0] != ':')
  54. warn("%s: illegal option -- %c\n", argv[0], c);
  55. if (argv[sd_optind][++_sp] == '\0') {
  56. sd_optind++;
  57. _sp = 1;
  58. }
  59. return ('?');
  60. }
  61. if (*(cp + 1) == ':') {
  62. if (argv[sd_optind][_sp+1] != '\0')
  63. sd_optarg = &argv[sd_optind++][_sp+1];
  64. else if (++sd_optind >= argc) {
  65. if (opts[0] != ':') {
  66. warn("%s: option requires an argument"
  67. " -- %c\n", argv[0], c);
  68. }
  69. _sp = 1;
  70. sd_optarg = NULL;
  71. return (opts[0] == ':' ? ':' : '?');
  72. } else
  73. sd_optarg = argv[sd_optind++];
  74. _sp = 1;
  75. } else {
  76. if (argv[sd_optind][++_sp] == '\0') {
  77. _sp = 1;
  78. sd_optind++;
  79. }
  80. sd_optarg = NULL;
  81. }
  82. return (c);
  83. }
  84. #if !defined(_WIN32) || defined(__MINGW32__) || defined(__MINGW64__)
  85. /* POSIX-like environment */
  86. /*
  87. * Get last changetime of a file
  88. */
  89. int sd_stat_ctime(const char* path, time_t* time)
  90. {
  91. struct stat astat;
  92. int statret=stat(path,&astat);
  93. if (0 != statret)
  94. {
  95. return statret;
  96. }
  97. *time=astat.st_ctime;
  98. return statret;
  99. }
  100. #if !defined(HAVE_GMTIME_R) || !HAVE_DECL_GMTIME_R
  101. /* non-thread-safe replacement */
  102. struct tm *sd_gmtime_r(const time_t *timep, struct tm *result) {
  103. struct tm *tmp = NULL;
  104. tmp = gmtime(timep);
  105. if (tmp) *result = *tmp;
  106. return tmp;
  107. }
  108. #endif
  109. #if !defined(HAVE_LOCALTIME_R) || !HAVE_DECL_LOCALTIME_R
  110. /* non-thread-safe replacement */
  111. struct tm *sd_localtime_r(const time_t *timep, struct tm *result) {
  112. struct tm *tmp = NULL;
  113. tmp = localtime(timep);
  114. if (tmp) *result = *tmp;
  115. return tmp;
  116. }
  117. #endif
  118. #else /* _WIN32 && !__MINGW32__ && !__MINGW64__ */
  119. /* native Windows environment */
  120. int sd_gettimeofday(LPFILETIME lpft, void* tzp) {
  121. if (lpft) {
  122. GetSystemTimeAsFileTime(lpft);
  123. }
  124. /* 0 indicates that the call succeeded. */
  125. return 0;
  126. }
  127. /*
  128. * Placeholder for WIN32 version to get last changetime of a file
  129. */
  130. int sd_stat_ctime(const char* path, time_t* time)
  131. { return -1; }
  132. #endif /* _WIN32 && !__MINGW32__ && !__MINGW64__ */
  133. #if defined(__MINGW32__) || defined(__MINGW64__)
  134. /* snprintf()/vsnprintf() on MinGW not adding null character and returns -1 */
  135. int sd_vsnprintf(char *str, size_t size, const char *format, va_list ap) {
  136. int ret;
  137. ret = vsnprintf(str, size, format, ap);
  138. if (ret < 0) ret = size;
  139. if (ret >= size) str[size - 1] = '\0';
  140. return ret;
  141. }
  142. int sd_snprintf(char *str, size_t size, const char *format, ...) {
  143. va_list ap;
  144. int ret;
  145. va_start(ap, format);
  146. ret = sd_vsnprintf(str, size, format, ap);
  147. va_end(ap);
  148. return ret;
  149. }
  150. #endif