os_support.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Various utilities for ffmpeg system
  3. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
  4. * copyright (c) 2002 Francois Revol
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "config.h"
  23. #include "avformat.h"
  24. #if defined(CONFIG_WINCE)
  25. /* Skip includes on WinCE. */
  26. #elif defined(__MINGW32__)
  27. #include <sys/types.h>
  28. #include <sys/timeb.h>
  29. #elif defined(CONFIG_OS2)
  30. #include <string.h>
  31. #include <sys/time.h>
  32. #else
  33. #include <unistd.h>
  34. #include <fcntl.h>
  35. #include <sys/time.h>
  36. #endif
  37. #include <time.h>
  38. #ifndef HAVE_SYS_POLL_H
  39. #if defined(__MINGW32__)
  40. #include <winsock2.h>
  41. #else
  42. #include <sys/select.h>
  43. #endif
  44. #endif
  45. /**
  46. * gets the current time in micro seconds.
  47. */
  48. int64_t av_gettime(void)
  49. {
  50. #if defined(CONFIG_WINCE)
  51. return timeGetTime() * INT64_C(1000);
  52. #elif defined(__MINGW32__)
  53. struct timeb tb;
  54. _ftime(&tb);
  55. return ((int64_t)tb.time * INT64_C(1000) + (int64_t)tb.millitm) * INT64_C(1000);
  56. #else
  57. struct timeval tv;
  58. gettimeofday(&tv,NULL);
  59. return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
  60. #endif
  61. }
  62. #if !defined(CONFIG_WINCE) && !defined(HAVE_LOCALTIME_R)
  63. struct tm *localtime_r(const time_t *t, struct tm *tp)
  64. {
  65. struct tm *l;
  66. l = localtime(t);
  67. if (!l)
  68. return 0;
  69. *tp = *l;
  70. return tp;
  71. }
  72. #endif /* !defined(CONFIG_WINCE) && !defined(HAVE_LOCALTIME_R) */
  73. #if !defined(HAVE_INET_ATON) && defined(CONFIG_NETWORK)
  74. #include <stdlib.h>
  75. #include <strings.h>
  76. #include "network.h"
  77. int inet_aton (const char * str, struct in_addr * add)
  78. {
  79. const char * pch = str;
  80. unsigned int add1 = 0, add2 = 0, add3 = 0, add4 = 0;
  81. add1 = atoi(pch);
  82. pch = strpbrk(pch,".");
  83. if (pch == 0 || ++pch == 0) goto done;
  84. add2 = atoi(pch);
  85. pch = strpbrk(pch,".");
  86. if (pch == 0 || ++pch == 0) goto done;
  87. add3 = atoi(pch);
  88. pch = strpbrk(pch,".");
  89. if (pch == 0 || ++pch == 0) goto done;
  90. add4 = atoi(pch);
  91. done:
  92. add->s_addr=(add4<<24)+(add3<<16)+(add2<<8)+add1;
  93. return 1;
  94. }
  95. #endif /* !defined(HAVE_INET_ATON) && defined(CONFIG_NETWORK) */
  96. #ifdef CONFIG_FFSERVER
  97. #ifndef HAVE_SYS_POLL_H
  98. int poll(struct pollfd *fds, nfds_t numfds, int timeout)
  99. {
  100. fd_set read_set;
  101. fd_set write_set;
  102. fd_set exception_set;
  103. nfds_t i;
  104. int n;
  105. int rc;
  106. FD_ZERO(&read_set);
  107. FD_ZERO(&write_set);
  108. FD_ZERO(&exception_set);
  109. n = -1;
  110. for(i = 0; i < numfds; i++) {
  111. if (fds[i].fd < 0)
  112. continue;
  113. if (fds[i].fd >= FD_SETSIZE) {
  114. errno = EINVAL;
  115. return -1;
  116. }
  117. if (fds[i].events & POLLIN) FD_SET(fds[i].fd, &read_set);
  118. if (fds[i].events & POLLOUT) FD_SET(fds[i].fd, &write_set);
  119. if (fds[i].events & POLLERR) FD_SET(fds[i].fd, &exception_set);
  120. if (fds[i].fd > n)
  121. n = fds[i].fd;
  122. };
  123. if (n == -1)
  124. /* Hey!? Nothing to poll, in fact!!! */
  125. return 0;
  126. if (timeout < 0)
  127. rc = select(n+1, &read_set, &write_set, &exception_set, NULL);
  128. else {
  129. struct timeval tv;
  130. tv.tv_sec = timeout / 1000;
  131. tv.tv_usec = 1000 * (timeout % 1000);
  132. rc = select(n+1, &read_set, &write_set, &exception_set, &tv);
  133. };
  134. if (rc < 0)
  135. return rc;
  136. for(i = 0; i < (nfds_t) n; i++) {
  137. fds[i].revents = 0;
  138. if (FD_ISSET(fds[i].fd, &read_set)) fds[i].revents |= POLLIN;
  139. if (FD_ISSET(fds[i].fd, &write_set)) fds[i].revents |= POLLOUT;
  140. if (FD_ISSET(fds[i].fd, &exception_set)) fds[i].revents |= POLLERR;
  141. };
  142. return rc;
  143. }
  144. #endif /* HAVE_SYS_POLL_H */
  145. #endif /* CONFIG_FFSERVER */