utils.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. #ident "ldclt @(#)utils.c 1.4 01/01/11"
  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 : utils.c
  44. AUTHOR : Jean-Luc SCHWING
  45. VERSION : 1.0
  46. DATE : 14 November 2000
  47. DESCRIPTION :
  48. This file contains the utilities functions that will be
  49. used as well by ldclt and by the genldif command, e.g.
  50. the random generator functions, etc...
  51. The main target/reason for creating this file is to be
  52. able to easely separate these functions from ldclt's
  53. framework and data structures, and thus to provide a
  54. kind of library suitable for any command.
  55. LOCAL : None.
  56. HISTORY :
  57. ---------+--------------+------------------------------------------------------
  58. dd/mm/yy | Author | Comments
  59. ---------+--------------+------------------------------------------------------
  60. 14/11/00 | JL Schwing | Creation
  61. ---------+--------------+------------------------------------------------------
  62. 14/11/00 | JL Schwing | 1.2 : Port on AIX.
  63. ---------+--------------+------------------------------------------------------
  64. 16/11/00 | JL Schwing | 1.3 : lint cleanup.
  65. ---------+--------------+------------------------------------------------------
  66. 11/01/01 | JL Schwing | 1.4 : Add new function rndlim().
  67. ---------+--------------+------------------------------------------------------
  68. */
  69. #include "utils.h" /* Basic definitions for this file */
  70. #include <stdio.h> /* sprintf(), etc... */
  71. #include <stdlib.h> /* lrand48(), etc... */
  72. #include <ctype.h> /* isascii(), etc... */ /*JLS 16-11-00*/
  73. #include <string.h> /* strerror(), etc... */ /*JLS 14-11-00*/
  74. /*
  75. * Some global variables...
  76. */
  77. #ifdef AIX
  78. pthread_mutex_t ldcltrand48_mutex; /* ldcltrand48() */ /*JLS 14-11-00*/
  79. #endif
  80. #ifdef AIX /* New */ /*JLS 14-11-00*/
  81. /* ****************************************************************************
  82. FUNCTION : ldcltrand48
  83. PURPOSE : Implement a thread-save version of lrand48()
  84. INPUT : None.
  85. OUTPUT : None.
  86. RETURN : A random value.
  87. DESCRIPTION :
  88. *****************************************************************************/
  89. long int
  90. ldcltrand48 (void)
  91. {
  92. long int val;
  93. int ret;
  94. if ((ret = pthread_mutex_lock (&ldcltrand48_mutex)) != 0)
  95. {
  96. fprintf (stderr,
  97. "Cannot pthread_mutex_lock(ldcltrand48_mutex), error=%d (%s)\n",
  98. ret, strerror (ret));
  99. fflush (stderr);
  100. ldcltExit (99);
  101. }
  102. val = lrand48();
  103. if ((ret = pthread_mutex_unlock (&ldcltrand48_mutex)) != 0)
  104. {
  105. fprintf (stderr,
  106. "Cannot pthread_mutex_unlock(ldcltrand48_mutex), error=%d (%s)\n",
  107. ret, strerror (ret));
  108. fflush (stderr);
  109. ldcltExit (99);
  110. }
  111. return (val);
  112. }
  113. #else /* AIX */
  114. #define ldcltrand48() lrand48()
  115. #endif /* AIX */
  116. /* ****************************************************************************
  117. FUNCTION : utilsInit
  118. PURPOSE : Initiates the utilities functions.
  119. INPUT : None.
  120. OUTPUT : None.
  121. RETURN : -1 if error, 0 else.
  122. DESCRIPTION :
  123. *****************************************************************************/
  124. int
  125. utilsInit (void)
  126. {
  127. #ifdef AIX /*JLS 14-11-00*/
  128. int ret; /*JLS 14-11-00*/
  129. /*
  130. * Initiate the mutex that protect ldcltrand48()
  131. */
  132. if ((ret = pthread_mutex_init(&ldcltrand48_mutex, NULL)) != 0)/*JLS 14-11-00*/
  133. { /*JLS 14-11-00*/
  134. fprintf (stderr, "ldclt: %s\n", strerror (ret)); /*JLS 14-11-00*/
  135. fprintf (stderr, /*JLS 14-11-00*/
  136. "Error: cannot initiate ldcltrand48_mutex\n"); /*JLS 14-11-00*/
  137. fflush (stderr); /*JLS 14-11-00*/
  138. return (-1); /*JLS 14-11-00*/
  139. } /*JLS 14-11-00*/
  140. #endif /*JLS 14-11-00*/
  141. /*
  142. * No error
  143. */
  144. return (0);
  145. }
  146. /* ****************************************************************************
  147. FUNCTION : rndlim
  148. PURPOSE : Returns a random number between the given limits.
  149. INPUT : low = low limit
  150. high = high limit
  151. OUTPUT : None.
  152. RETURN : The random value.
  153. DESCRIPTION :
  154. *****************************************************************************/
  155. int
  156. rndlim (
  157. int low,
  158. int high)
  159. {
  160. return (low + ldcltrand48() % (high-low+1));
  161. }
  162. /* ****************************************************************************
  163. FUNCTION : rnd
  164. PURPOSE : Creates a random number string between the given
  165. arguments. The string is returned in the buffer.
  166. INPUT : low = low limit
  167. high = high limit
  168. ndigits = number of digits - 0 means no zero pad
  169. OUTPUT : buf = buffer to write the random string. Note that
  170. it is generated with fixed number of digits,
  171. completed with leading '0', if ndigits > 0
  172. RETURN : None.
  173. DESCRIPTION :
  174. *****************************************************************************/
  175. void
  176. rnd (
  177. char *buf,
  178. int low,
  179. int high,
  180. int ndigits)
  181. {
  182. sprintf (buf, "%0*d", ndigits,
  183. (int)(low + (ldcltrand48() % (high-low+1)))); /*JLS 14-11-00*/
  184. }
  185. /* ****************************************************************************
  186. FUNCTION : rndstr
  187. PURPOSE : Return a random string, of length ndigits. The string
  188. is returned in the buf.
  189. INPUT : ndigits = number of digits required.
  190. OUTPUT : buf = the buf must be long enough to contain the
  191. requested string.
  192. RETURN : None.
  193. DESCRIPTION :
  194. *****************************************************************************/
  195. void
  196. rndstr (
  197. char *buf,
  198. int ndigits)
  199. {
  200. unsigned int rndNum; /* The random value */
  201. int charNum; /* Random char in buf */
  202. int byteNum; /* Byte in rndNum */
  203. char newChar; /* The new byte as a char */
  204. char *rndArray; /* To cast the rndNum into chars */
  205. charNum = 0;
  206. byteNum = 4;
  207. rndArray = (char *)(&rndNum);
  208. while (charNum < ndigits)
  209. {
  210. /*
  211. * Maybe we should generate a new random number ?
  212. */
  213. if (byteNum == 4)
  214. {
  215. rndNum = ldcltrand48(); /*JLS 14-11-00*/
  216. byteNum = 0;
  217. }
  218. /*
  219. * Is it a valid char ?
  220. */
  221. newChar = rndArray[byteNum];
  222. /*
  223. * The last char must not be a '\' nor a space.
  224. */
  225. if (!(((charNum+1) == ndigits) &&
  226. ((newChar == '\\') || (newChar == ' '))))
  227. {
  228. /*
  229. * First, there are some special characters that have a meaning for
  230. * LDAP, and thus that must be quoted. What LDAP's rfc1779 means by
  231. * "to quote" may be translated by "to antislash"
  232. * Note: we do not check the \ because in this way, it leads to randomly
  233. * quote some valid characters.
  234. */
  235. if ((newChar == '=') || (newChar == ';') || (newChar == ',') ||
  236. (newChar == '+') || (newChar == '"') || (newChar == '<') ||
  237. (newChar == '>') || (newChar == '#'))
  238. {
  239. /*
  240. * Ensure that the previous char is *not* a \ otherwise
  241. * it will result in a \\, rather than a \,
  242. * If it is the case, add one more \ to have \\\,
  243. */
  244. if ((charNum > 0) && (buf[charNum-1] == '\\'))
  245. {
  246. if ((charNum+3) < ndigits)
  247. {
  248. buf[charNum++] = '\\';
  249. buf[charNum++] = '\\';
  250. buf[charNum++] = newChar;
  251. }
  252. }
  253. else
  254. {
  255. if ((charNum+2) < ndigits)
  256. {
  257. buf[charNum++] = '\\';
  258. buf[charNum++] = newChar;
  259. }
  260. }
  261. }
  262. else
  263. {
  264. /*
  265. * strict ascii required
  266. */
  267. if (isascii (newChar) && !iscntrl(newChar))
  268. buf[charNum++] = newChar;
  269. }
  270. }
  271. /*
  272. * Next byte of the random value.
  273. */
  274. byteNum++;
  275. }
  276. /*
  277. * End of function
  278. */
  279. buf[charNum] = '\0';
  280. }
  281. /* End of file */