Back to index

php5  5.3.10
uniqid.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1997-2012 The PHP Group                                |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 3.01 of the PHP license,      |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.php.net/license/3_01.txt                                  |
00011    | If you did not receive a copy of the PHP license and are unable to   |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@php.net so we can mail you a copy immediately.               |
00014    +----------------------------------------------------------------------+
00015    | Author: Stig Sæther Bakken <ssb@php.net>                             |
00016    +----------------------------------------------------------------------+
00017  */
00018 
00019 /* $Id: uniqid.c 321634 2012-01-01 13:15:04Z felipe $ */
00020 
00021 #include "php.h"
00022 
00023 #include <stdlib.h>
00024 #if HAVE_UNISTD_H
00025 #include <unistd.h>
00026 #endif
00027 
00028 #include <string.h>
00029 #include <errno.h>
00030 
00031 #include <stdio.h>
00032 #ifdef PHP_WIN32
00033 #include "win32/time.h"
00034 #else
00035 #include <sys/time.h>
00036 #endif
00037 
00038 #include "php_lcg.h"
00039 #include "uniqid.h"
00040 
00041 /* {{{ proto string uniqid([string prefix [, bool more_entropy]])
00042    Generates a unique ID */
00043 #ifdef HAVE_GETTIMEOFDAY
00044 PHP_FUNCTION(uniqid)
00045 {
00046        char *prefix = "";
00047 #if defined(__CYGWIN__)
00048        zend_bool more_entropy = 1;
00049 #else
00050        zend_bool more_entropy = 0;
00051 #endif
00052        char *uniqid;
00053        int sec, usec, prefix_len = 0;
00054        struct timeval tv;
00055 
00056        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sb", &prefix, &prefix_len,
00057                                                    &more_entropy)) {
00058               return;
00059        }
00060 
00061 #if HAVE_USLEEP && !defined(PHP_WIN32)
00062        if (!more_entropy) {
00063 #if defined(__CYGWIN__)
00064               php_error_docref(NULL TSRMLS_CC, E_WARNING, "You must use 'more entropy' under CYGWIN");
00065               RETURN_FALSE;
00066 #else
00067               usleep(1);
00068 #endif
00069        }
00070 #endif
00071        gettimeofday((struct timeval *) &tv, (struct timezone *) NULL);
00072        sec = (int) tv.tv_sec;
00073        usec = (int) (tv.tv_usec % 0x100000);
00074 
00075        /* The max value usec can have is 0xF423F, so we use only five hex
00076         * digits for usecs.
00077         */
00078        if (more_entropy) {
00079               spprintf(&uniqid, 0, "%s%08x%05x%.8F", prefix, sec, usec, php_combined_lcg(TSRMLS_C) * 10);
00080        } else {
00081               spprintf(&uniqid, 0, "%s%08x%05x", prefix, sec, usec);
00082        }
00083 
00084        RETURN_STRING(uniqid, 0);
00085 }
00086 #endif
00087 /* }}} */
00088 
00089 /*
00090  * Local variables:
00091  * tab-width: 4
00092  * c-basic-offset: 4
00093  * End:
00094  * vim600: sw=4 ts=4 fdm=marker
00095  * vim<600: sw=4 ts=4
00096  */