PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/ext/standard/uniqid.c

http://php52-backports.googlecode.com/
C | 96 lines | 52 code | 12 blank | 32 comment | 5 complexity | 8a29bd886543bc4124ea7837db8964c6 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-3-Clause
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2010 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Stig S?ther Bakken <ssb@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* $Id: uniqid.c 293036 2010-01-03 09:23:27Z sebastian $ */
  19. #include "php.h"
  20. #include <stdlib.h>
  21. #if HAVE_UNISTD_H
  22. #include <unistd.h>
  23. #endif
  24. #include <string.h>
  25. #include <errno.h>
  26. #include <stdio.h>
  27. #ifdef PHP_WIN32
  28. #include "win32/time.h"
  29. #else
  30. #include <sys/time.h>
  31. #endif
  32. #include "php_lcg.h"
  33. #include "uniqid.h"
  34. /* {{{ proto string uniqid([string prefix [, bool more_entropy]])
  35. Generates a unique ID */
  36. #ifdef HAVE_GETTIMEOFDAY
  37. PHP_FUNCTION(uniqid)
  38. {
  39. char *prefix = "";
  40. #if defined(__CYGWIN__)
  41. zend_bool more_entropy = 1;
  42. #else
  43. zend_bool more_entropy = 0;
  44. #endif
  45. char *uniqid;
  46. int sec, usec, prefix_len = 0;
  47. struct timeval tv;
  48. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sb", &prefix, &prefix_len,
  49. &more_entropy)) {
  50. return;
  51. }
  52. #if HAVE_USLEEP && !defined(PHP_WIN32)
  53. if (!more_entropy) {
  54. #if defined(__CYGWIN__)
  55. php_error_docref(NULL TSRMLS_CC, E_WARNING, "You must use 'more entropy' under CYGWIN");
  56. RETURN_FALSE;
  57. #else
  58. usleep(1);
  59. #endif
  60. }
  61. #endif
  62. gettimeofday((struct timeval *) &tv, (struct timezone *) NULL);
  63. sec = (int) tv.tv_sec;
  64. usec = (int) (tv.tv_usec % 0x100000);
  65. /* The max value usec can have is 0xF423F, so we use only five hex
  66. * digits for usecs.
  67. */
  68. if (more_entropy) {
  69. spprintf(&uniqid, 0, "%s%08x%05x%.8F", prefix, sec, usec, php_combined_lcg(TSRMLS_C) * 10);
  70. } else {
  71. spprintf(&uniqid, 0, "%s%08x%05x", prefix, sec, usec);
  72. }
  73. RETURN_STRING(uniqid, 0);
  74. }
  75. #endif
  76. /* }}} */
  77. /*
  78. * Local variables:
  79. * tab-width: 4
  80. * c-basic-offset: 4
  81. * End:
  82. * vim600: sw=4 ts=4 fdm=marker
  83. * vim<600: sw=4 ts=4
  84. */