11
bool safe_strtoull(const char *str, uint64_t *out) {
16
unsigned long long ull = strtoull(str, &endptr, 10);
19
if (isspace(*endptr) || (*endptr == '\0' && endptr != str)) {
20
if ((long long) ull < 0) {
21
/* only check for negative signs in the uncommon case when
22
* the unsigned number is so big that it's negative as a
24
if (strchr(str, '-') != NULL) {
34
bool safe_strtoll(const char *str, int64_t *out) {
39
long long ll = strtoll(str, &endptr, 10);
42
if (isspace(*endptr) || (*endptr == '\0' && endptr != str)) {
49
bool safe_strtoul(const char *str, uint32_t *out) {
57
l = strtoul(str, &endptr, 10);
58
if (errno == ERANGE) {
62
if (isspace(*endptr) || (*endptr == '\0' && endptr != str)) {
64
/* only check for negative signs in the uncommon case when
65
* the unsigned number is so big that it's negative as a
67
if (strchr(str, '-') != NULL) {
78
bool safe_strtol(const char *str, int32_t *out) {
83
long l = strtol(str, &endptr, 10);
86
if (isspace(*endptr) || (*endptr == '\0' && endptr != str)) {
93
void vperror(const char *fmt, ...) {
94
int old_errno = errno;
99
vsnprintf(buf, sizeof(buf), fmt, ap);
108
static uint64_t mc_swap64(uint64_t in) {
110
/* Little endian, flip the bytes around until someone makes a faster/better
114
for(i = 0; i<8; i++) {
115
rv = (rv << 8) | (in & 0xff);
120
/* big-endian machines don't need byte swapping */
125
uint64_t ntohll(uint64_t val) {
126
return mc_swap64(val);
129
uint64_t htonll(uint64_t val) {
130
return mc_swap64(val);