1 /* Compatibility functions for strsep and strtoq missing on Solaris */
6 char* strsep(char** str, const char* delims)
17 if (strchr(delims,**str)!=NULL) {
24 /* There is no other token */
30 #define LONG_MIN (-9223372036854775807L-1L)
31 /* min value of a "long int" */
32 #define LONG_MAX 9223372036854775807L
33 /* max value of a "long int" */
36 * Convert a string to a quad integer.
38 * Ignores `locale' stuff. Assumes that the upper and lower case
39 * alphabets and digits are each contiguous.
42 strtoq(const char *nptr, char **endptr, int base)
47 uint64_t qbase, cutoff;
51 * Skip white space and pick up leading +/- sign if any.
52 * If base is 0, allow 0x for hex and 0 for octal, else
53 * assume decimal; if base is already 16, allow 0x.
67 if ((base == 0 || base == 16) &&
68 c == '\0' && (*s == 'x' || *s == 'X')) {
74 base = c == '\0' ? 8 : 10;
77 * Compute the cutoff value between legal numbers and illegal
78 * numbers. That is the largest legal value, divided by the
79 * base. An input number that is greater than this value, if
80 * followed by a legal input character, is too big. One that
81 * is equal to this value may be valid or not; the limit
82 * between valid and invalid numbers is then based on the last
83 * digit. For instance, if the range for quads is
84 * [-9223372036854775808..9223372036854775807] and the input base
85 * is 10, cutoff will be set to 922337203685477580 and cutlim to
86 * either 7 (neg==0) or 8 (neg==1), meaning that if we have
87 * accumulated a value > 922337203685477580, or equal but the
88 * next digit is > 7 (or 8), the number is too big, and we will
89 * return a range error.
91 * Set any if any `digits' consumed; make it negative to indicate
94 qbase = (unsigned)base;
95 cutoff = neg ? (uint64_t)-(LONG_MIN + LONG_MAX) + LONG_MAX : LONG_MAX;
96 cutlim = cutoff % qbase;
98 for (acc = 0, any = 0;; c = *s++) {
104 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
109 if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
118 acc = neg ? LONG_MIN : LONG_MAX;
122 *((const char **)endptr) = any ? s - 1 : nptr;
126 int setenv(const char *name, const char *value, int overwrite)
131 buflen = strlen(name) + strlen(value) + 2;
132 if ((buf = malloc(buflen)) == NULL)
135 if (!overwrite && getenv(name))
138 snprintf(buf, buflen, "%s=%s", name, value);