~ubuntu-branches/ubuntu/precise/memcached/precise

« back to all changes in this revision

Viewing changes to util.c

  • Committer: Bazaar Package Importer
  • Author(s): David Martínez Moreno
  • Date: 2009-10-16 15:09:43 UTC
  • mfrom: (1.3.5 upstream)
  • mto: This revision was merged to the branch mainline in revision 8.
  • Revision ID: james.westby@ubuntu.com-20091016150943-l96biwf7siwdt1ci
Tags: upstream-1.4.2
Import upstream version 1.4.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include <assert.h>
 
3
#include <ctype.h>
 
4
#include <errno.h>
 
5
#include <string.h>
 
6
#include <stdlib.h>
 
7
#include <stdarg.h>
 
8
 
 
9
#include "memcached.h"
 
10
 
 
11
bool safe_strtoull(const char *str, uint64_t *out) {
 
12
    assert(out != NULL);
 
13
    errno = 0;
 
14
    *out = 0;
 
15
    char *endptr;
 
16
    unsigned long long ull = strtoull(str, &endptr, 10);
 
17
    if (errno == ERANGE)
 
18
        return false;
 
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
 
23
             * signed number. */
 
24
            if (strchr(str, '-') != NULL) {
 
25
                return false;
 
26
            }
 
27
        }
 
28
        *out = ull;
 
29
        return true;
 
30
    }
 
31
    return false;
 
32
}
 
33
 
 
34
bool safe_strtoll(const char *str, int64_t *out) {
 
35
    assert(out != NULL);
 
36
    errno = 0;
 
37
    *out = 0;
 
38
    char *endptr;
 
39
    long long ll = strtoll(str, &endptr, 10);
 
40
    if (errno == ERANGE)
 
41
        return false;
 
42
    if (isspace(*endptr) || (*endptr == '\0' && endptr != str)) {
 
43
        *out = ll;
 
44
        return true;
 
45
    }
 
46
    return false;
 
47
}
 
48
 
 
49
bool safe_strtoul(const char *str, uint32_t *out) {
 
50
    char *endptr = NULL;
 
51
    unsigned long l = 0;
 
52
    assert(out);
 
53
    assert(str);
 
54
    *out = 0;
 
55
    errno = 0;
 
56
 
 
57
    l = strtoul(str, &endptr, 10);
 
58
    if (errno == ERANGE) {
 
59
        return false;
 
60
    }
 
61
 
 
62
    if (isspace(*endptr) || (*endptr == '\0' && endptr != str)) {
 
63
        if ((long) l < 0) {
 
64
            /* only check for negative signs in the uncommon case when
 
65
             * the unsigned number is so big that it's negative as a
 
66
             * signed number. */
 
67
            if (strchr(str, '-') != NULL) {
 
68
                return false;
 
69
            }
 
70
        }
 
71
        *out = l;
 
72
        return true;
 
73
    }
 
74
 
 
75
    return false;
 
76
}
 
77
 
 
78
bool safe_strtol(const char *str, int32_t *out) {
 
79
    assert(out != NULL);
 
80
    errno = 0;
 
81
    *out = 0;
 
82
    char *endptr;
 
83
    long l = strtol(str, &endptr, 10);
 
84
    if (errno == ERANGE)
 
85
        return false;
 
86
    if (isspace(*endptr) || (*endptr == '\0' && endptr != str)) {
 
87
        *out = l;
 
88
        return true;
 
89
    }
 
90
    return false;
 
91
}
 
92
 
 
93
void vperror(const char *fmt, ...) {
 
94
    int old_errno = errno;
 
95
    char buf[80];
 
96
    va_list ap;
 
97
 
 
98
    va_start(ap, fmt);
 
99
    vsnprintf(buf, sizeof(buf), fmt, ap);
 
100
    va_end(ap);
 
101
 
 
102
    errno = old_errno;
 
103
 
 
104
    perror(buf);
 
105
}
 
106
 
 
107
#ifndef HAVE_HTONLL
 
108
static uint64_t mc_swap64(uint64_t in) {
 
109
#ifdef ENDIAN_LITTLE
 
110
    /* Little endian, flip the bytes around until someone makes a faster/better
 
111
    * way to do this. */
 
112
    int64_t rv = 0;
 
113
    int i = 0;
 
114
     for(i = 0; i<8; i++) {
 
115
        rv = (rv << 8) | (in & 0xff);
 
116
        in >>= 8;
 
117
     }
 
118
    return rv;
 
119
#else
 
120
    /* big-endian machines don't need byte swapping */
 
121
    return in;
 
122
#endif
 
123
}
 
124
 
 
125
uint64_t ntohll(uint64_t val) {
 
126
   return mc_swap64(val);
 
127
}
 
128
 
 
129
uint64_t htonll(uint64_t val) {
 
130
   return mc_swap64(val);
 
131
}
 
132
#endif
 
133