port.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. #ident "ldclt @(#)port.c 1.2 01/03/14"
  2. /** BEGIN COPYRIGHT BLOCK
  3. * This Program is free software; you can redistribute it and/or modify it under
  4. * the terms of the GNU General Public License as published by the Free Software
  5. * Foundation; version 2 of the License.
  6. *
  7. * This Program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  10. *
  11. * You should have received a copy of the GNU General Public License along with
  12. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  13. * Place, Suite 330, Boston, MA 02111-1307 USA.
  14. *
  15. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  16. * right to link the code of this Program with code not covered under the GNU
  17. * General Public License ("Non-GPL Code") and to distribute linked combinations
  18. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  19. * permitted under this exception must only link to the code of this Program
  20. * through those well defined interfaces identified in the file named EXCEPTION
  21. * found in the source code files (the "Approved Interfaces"). The files of
  22. * Non-GPL Code may instantiate templates or use macros or inline functions from
  23. * the Approved Interfaces without causing the resulting work to be covered by
  24. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  25. * additions to the list of Approved Interfaces. You must obey the GNU General
  26. * Public License in all respects for all of the Program code and other code used
  27. * in conjunction with the Program except the Non-GPL Code covered by this
  28. * exception. If you modify this file, you may extend this exception to your
  29. * version of the file, but you are not obligated to do so. If you do not wish to
  30. * provide this exception without modification, you must delete this exception
  31. * statement from your version and license this file solely under the GPL without
  32. * exception.
  33. *
  34. *
  35. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  36. * Copyright (C) 2006 Red Hat, Inc.
  37. * All rights reserved.
  38. * END COPYRIGHT BLOCK **/
  39. #ifdef HAVE_CONFIG_H
  40. # include <config.h>
  41. #endif
  42. /*
  43. FILE : port.c
  44. AUTHOR : Jean-Luc SCHWING
  45. VERSION : 1.0
  46. DATE : 28 November 2000
  47. DESCRIPTION :
  48. This file contains platform-independant version of
  49. common (unix) functions in order to have portable
  50. source code for either unix and windows platforms.
  51. It is greatly inspired from iPlanet DS 5.0 workspace.
  52. LOCAL : None.
  53. HISTORY :
  54. ---------+--------------+------------------------------------------------------
  55. dd/mm/yy | Author | Comments
  56. ---------+--------------+------------------------------------------------------
  57. 28/11/00 | JL Schwing | Creation
  58. ---------+--------------+------------------------------------------------------
  59. 14/03/01 | JL Schwing | 1.2 : Lint cleanup.
  60. ---------+--------------+------------------------------------------------------
  61. */
  62. #include <stdio.h> /* EOF, etc... */
  63. #ifdef _WIN32
  64. #include <windows.h>
  65. #include <winbase.h>
  66. #else
  67. #include <unistd.h> /* sleep(), etc... */ /*JLS 14-03-01*/
  68. #include <pthread.h> /* pthreads(), etc... */
  69. #endif
  70. #include "port.h"
  71. /************************************************************************/
  72. /************************************************************************/
  73. /**************** NT section ***********************/
  74. /************************************************************************/
  75. /************************************************************************/
  76. #ifdef _WIN32
  77. int
  78. ldclt_mutex_init (
  79. ldclt_mutex_t *mutex)
  80. {
  81. InitializeCriticalSection (mutex);
  82. return (0);
  83. }
  84. int
  85. ldclt_mutex_lock (
  86. ldclt_mutex_t *mutex)
  87. {
  88. EnterCriticalSection(mutex);
  89. return (0);
  90. }
  91. int
  92. ldclt_mutex_unlock (
  93. ldclt_mutex_t *mutex)
  94. {
  95. LeaveCriticalSection (mutex);
  96. return (0);
  97. }
  98. void
  99. ldclt_sleep (
  100. int nseconds)
  101. {
  102. Sleep (1000 * nseconds);
  103. }
  104. int
  105. ldclt_thread_create (
  106. ldclt_tid *tid,
  107. void *fct,
  108. void *param)
  109. {
  110. CreateThread (NULL, 0, fct, param, 0, tid);
  111. return (0);
  112. }
  113. long
  114. lrand48 (void)
  115. {
  116. return ((rand()<<8)+rand());
  117. }
  118. /*
  119. * Implements the Unix getopt function for NT systems
  120. */
  121. char *optarg;
  122. int optind;
  123. int
  124. getopt (
  125. int argc,
  126. char **argv,
  127. char *optstring)
  128. {
  129. static char **prevArgv = NULL; /* Memorize argv to parse */
  130. static int inOption; /* In option parsing ? */
  131. static int cNum; /* Current char num */
  132. int c; /* Current char */
  133. int i; /* Loops */
  134. /*
  135. * Initialization - maybe the first time this function is called ?
  136. */
  137. if (prevArgv != argv)
  138. {
  139. prevArgv = argv;
  140. optind = 0;
  141. inOption = 0;
  142. }
  143. /*
  144. * Maybe we processed the last chars of the option in the previous call
  145. */
  146. if (inOption)
  147. {
  148. if (argv[optind][cNum] == '\0')
  149. inOption = 0;
  150. }
  151. /*
  152. * Maybe we should look for '-'
  153. */
  154. if (!inOption)
  155. {
  156. optind++; /* Next option */
  157. if (optind == argc) /* No more option */
  158. return (EOF);
  159. if (argv[optind][0] != '-') /* Not an option */
  160. return (EOF);
  161. if (argv[optind][1] == '\0') /* Only '-' */
  162. return (EOF);
  163. cNum = 1; /* Next char to process */
  164. inOption = 0; /* We are in an option */
  165. }
  166. /*
  167. * See if this is a valid option
  168. */
  169. c = argv[optind][cNum];
  170. for (i=0 ; (i<strlen(optstring)) && (c!=optstring[i]) ; i++);
  171. if (c != optstring[i]) /* Not an option */
  172. return ('?');
  173. cNum++; /* Next char */
  174. /*
  175. * Check if this option requires an argument
  176. * Note that for the last char of optstring, it is a valid '\0' != ':'
  177. */
  178. if (optstring[i+1] != ':') /* No argument */
  179. return (c);
  180. /*
  181. * Need an argument...
  182. * The argument is either the end of argv[optind] or argv[++optind]
  183. */
  184. if (argv[optind][cNum] == '\0') /* Must return next argument */
  185. {
  186. optind++; /* Next argument */
  187. if (optind == argc) /* There is no next argument */
  188. {
  189. printf ("%s: option requires an argument -- %c\n", argv[0], c);
  190. return ('?');
  191. }
  192. optarg = argv[optind]; /* Set optarg to teh argument argv[] */
  193. inOption = 0; /* No more in option... */
  194. return (c);
  195. }
  196. /*
  197. * Return the end of the current argv[optind]
  198. */
  199. optarg = &(argv[optind][cNum]); /* End of argv[optind] */
  200. inOption = 0; /* No more in option */
  201. return (c);
  202. }
  203. /*
  204. * Implement the Unix getsubopt function for NT systems
  205. */
  206. int
  207. getsubopt(
  208. char **optionp,
  209. char **tokens,
  210. char **valuep)
  211. {
  212. int i; /* Loops */
  213. char *curOpt; /* Current optionp */
  214. curOpt = *optionp; /* Begin of current option */
  215. /*
  216. * Find the end of the current option
  217. */
  218. for (i=0 ; (curOpt[i]!='\0') && (curOpt[i]!=',') ; i++);
  219. if (curOpt[i] == '\0')
  220. *optionp = &(curOpt[i]);
  221. else
  222. *optionp = &(curOpt[i+1]);
  223. curOpt[i] = '\0'; /* Do not forget to end this string */
  224. /*
  225. * Find if there is a subvalue for this option
  226. */
  227. for (i=0 ; (curOpt[i]!='\0') && (curOpt[i]!='=') ; i++);
  228. if (curOpt[i] == '\0')
  229. *valuep = &(curOpt[i]);
  230. else
  231. *valuep = &(curOpt[i+1]);
  232. curOpt[i] = '\0'; /* Do not forget to end this string */
  233. /*
  234. * Find if this option is valid...
  235. */
  236. for (i=0 ; tokens[i] != NULL ; i++)
  237. if (!strcmp (curOpt, tokens[i]))
  238. return (i);
  239. /*
  240. * Not found...
  241. */
  242. return (-1);
  243. }
  244. #else /* NT 4 */
  245. /************************************************************************/
  246. /************************************************************************/
  247. /**************** Unix section ***********************/
  248. /************************************************************************/
  249. /************************************************************************/
  250. int
  251. ldclt_mutex_init (
  252. ldclt_mutex_t *mutex)
  253. {
  254. return (pthread_mutex_init (mutex, NULL));
  255. }
  256. int
  257. ldclt_mutex_lock (
  258. ldclt_mutex_t *mutex)
  259. {
  260. return (pthread_mutex_lock (mutex));
  261. }
  262. int
  263. ldclt_mutex_unlock (
  264. ldclt_mutex_t *mutex)
  265. {
  266. return (pthread_mutex_unlock (mutex));
  267. }
  268. void
  269. ldclt_sleep (
  270. int nseconds)
  271. {
  272. sleep (nseconds);
  273. }
  274. int
  275. ldclt_thread_create (
  276. ldclt_tid *tid,
  277. void *(*fct)(void *),
  278. void *param)
  279. {
  280. return (pthread_create (tid, NULL, fct, param));
  281. }
  282. #endif /* _WIN32 */
  283. /* End of file */