11 /* Avoid warnings on solaris, where isspace() is an index into an array, and gcc uses signed chars */
12 #define xisspace(c) isspace((unsigned char)c)
14 bool safe_strtoull(const char *str
, uint64_t *out
) {
19 unsigned long long ull
= strtoull(str
, &endptr
, 10);
20 if ((errno
== ERANGE
) || (str
== endptr
)) {
24 if (xisspace(*endptr
) || (*endptr
== '\0' && endptr
!= str
)) {
25 if ((long long) ull
< 0) {
26 /* only check for negative signs in the uncommon case when
27 * the unsigned number is so big that it's negative as a
29 if (strchr(str
, '-') != NULL
) {
39 bool safe_strtoll(const char *str
, int64_t *out
) {
44 long long ll
= strtoll(str
, &endptr
, 10);
45 if ((errno
== ERANGE
) || (str
== endptr
)) {
49 if (xisspace(*endptr
) || (*endptr
== '\0' && endptr
!= str
)) {
56 bool safe_strtoul(const char *str
, uint32_t *out
) {
64 l
= strtoul(str
, &endptr
, 10);
65 if ((errno
== ERANGE
) || (str
== endptr
)) {
69 if (xisspace(*endptr
) || (*endptr
== '\0' && endptr
!= str
)) {
71 /* only check for negative signs in the uncommon case when
72 * the unsigned number is so big that it's negative as a
74 if (strchr(str
, '-') != NULL
) {
85 bool safe_strtol(const char *str
, int32_t *out
) {
90 long l
= strtol(str
, &endptr
, 10);
91 if ((errno
== ERANGE
) || (str
== endptr
)) {
95 if (xisspace(*endptr
) || (*endptr
== '\0' && endptr
!= str
)) {
102 void vperror(const char *fmt
, ...) {
103 int old_errno
= errno
;
108 if (vsnprintf(buf
, sizeof(buf
), fmt
, ap
) == -1) {
109 buf
[sizeof(buf
) - 1] = '\0';
119 static uint64_t mc_swap64(uint64_t in
) {
121 /* Little endian, flip the bytes around until someone makes a faster/better
125 for(i
= 0; i
<8; i
++) {
126 rv
= (rv
<< 8) | (in
& 0xff);
131 /* big-endian machines don't need byte swapping */
136 uint64_t ntohll(uint64_t val
) {
137 return mc_swap64(val
);
140 uint64_t htonll(uint64_t val
) {
141 return mc_swap64(val
);