11 #include "memcached.h"
13 /* Avoid warnings on solaris, where isspace() is an index into an array, and gcc uses signed chars */
14 #define xisspace(c) isspace((unsigned char)c)
16 bool safe_strtoull(const char *str
, uint64_t *out
) {
21 unsigned long long ull
= strtoull(str
, &endptr
, 10);
22 if ((errno
== ERANGE
) || (str
== endptr
)) {
26 if (xisspace(*endptr
) || (*endptr
== '\0' && endptr
!= str
)) {
27 if ((long long) ull
< 0) {
28 /* only check for negative signs in the uncommon case when
29 * the unsigned number is so big that it's negative as a
31 if (strchr(str
, '-') != NULL
) {
41 bool safe_strtoll(const char *str
, int64_t *out
) {
46 long long ll
= strtoll(str
, &endptr
, 10);
47 if ((errno
== ERANGE
) || (str
== endptr
)) {
51 if (xisspace(*endptr
) || (*endptr
== '\0' && endptr
!= str
)) {
58 bool safe_strtoul(const char *str
, uint32_t *out
) {
66 l
= strtoul(str
, &endptr
, 10);
67 if ((errno
== ERANGE
) || (str
== endptr
)) {
71 if (xisspace(*endptr
) || (*endptr
== '\0' && endptr
!= str
)) {
73 /* only check for negative signs in the uncommon case when
74 * the unsigned number is so big that it's negative as a
76 if (strchr(str
, '-') != NULL
) {
87 bool safe_strtol(const char *str
, int32_t *out
) {
92 long l
= strtol(str
, &endptr
, 10);
93 if ((errno
== ERANGE
) || (str
== endptr
)) {
97 if (xisspace(*endptr
) || (*endptr
== '\0' && endptr
!= str
)) {
104 void vperror(const char *fmt
, ...) {
105 int old_errno
= errno
;
110 if (vsnprintf(buf
, sizeof(buf
), fmt
, ap
) == -1) {
111 buf
[sizeof(buf
) - 1] = '\0';
121 static uint64_t mc_swap64(uint64_t in
) {
123 /* Little endian, flip the bytes around until someone makes a faster/better
127 for(i
= 0; i
<8; i
++) {
128 rv
= (rv
<< 8) | (in
& 0xff);
133 /* big-endian machines don't need byte swapping */
138 uint64_t ntohll(uint64_t val
) {
139 return mc_swap64(val
);
142 uint64_t htonll(uint64_t val
) {
143 return mc_swap64(val
);