sd_xplatform.c 2.5 KB

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