+
+/**
+ * Separate strings.
+ *
+ * @param stringp reference of the string to separate.
+ * @param delim string containing all delimiters.
+ * @return Token string.
+ */
+char *strsep(char **stringp, const char *delim)
+{
+ char *walk, *token;
+
+ if (!stringp || !*stringp || !**stringp)
+ return NULL;
+
+ token = walk = *stringp;
+
+ /* Walk, search for delimiters */
+ while(*walk && !strchr(delim, *walk))
+ walk++;
+
+ if (*walk) {
+ /* NUL terminate */
+ *walk = '\0';
+ walk++;
+ }
+
+ *stringp = walk;
+
+ return token;
+}
+
+/* Check that a character is in the valid range for the
+ given base
+*/
+
+static int _valid(char ch, int base)
+{
+ char end = (base > 9) ? '9' : '0' + (base - 1);
+
+ /* all bases will be some subset of the 0-9 range */
+
+ if (ch >= '0' && ch <= end)
+ return 1;
+
+ /* Bases > 11 will also have to match in the a-z range */
+
+ if (base > 11) {
+ if (tolower(ch) >= 'a' &&
+ tolower(ch) <= 'a' + (base - 11))
+ return 1;
+ }
+
+ return 0;
+}
+
+/* Return the "value" of the character in the given base */
+
+static int _offset(char ch, int base)
+{
+ if (ch >= '0' && ch <= '9')
+ return ch - '0';
+ else
+ return 10 + tolower(ch) - 'a';
+}
+
+/**
+ * Convert the initial portion of a string into a signed int
+ * @param ptr A pointer to the string to convert
+ * @param endptr A pointer to the unconverted part of the string
+ * @param base The base of the number to convert, or 0 for auto
+ * @return A signed integer representation of the string
+ */
+
+long int strtol(const char *ptr, char **endptr, int base)
+{
+ int ret = 0;
+ int negative = 1;
+
+ if (endptr != NULL)
+ *endptr = (char *) ptr;
+
+ /* Purge whitespace */
+
+ for( ; *ptr && isspace(*ptr); ptr++);
+
+ if (ptr[0] == '-') {
+ negative = -1;
+ ptr++;
+ }
+
+ if (!*ptr)
+ return 0;
+
+ /* Determine the base */
+
+ if (base == 0) {
+ if (ptr[0] == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
+ base = 16;
+ else if (ptr[0] == '0') {
+ base = 8;
+ ptr++;
+ }
+ else
+ base = 10;
+ }
+
+ /* Base 16 allows the 0x on front - so skip over it */
+
+ if (base == 16) {
+ if (ptr[0] == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
+ ptr += 2;
+ }
+
+ /* If the first character isn't valid, then don't
+ * bother */
+
+ if (!*ptr || !_valid(*ptr, base))
+ return 0;
+
+ for( ; *ptr && _valid(*ptr, base); ptr++)
+ ret = (ret * base) + _offset(*ptr, base);
+
+ if (endptr != NULL)
+ *endptr = (char *) ptr;
+
+ return ret * negative;
+}
+
+/**
+ * Convert the initial portion of a string into an unsigned int
+ * @param ptr A pointer to the string to convert
+ * @param endptr A pointer to the unconverted part of the string
+ * @param base The base of the number to convert, or 0 for auto
+ * @return An unsigned integer representation of the string
+ */
+
+unsigned long int strtoul(const char *ptr, char **endptr, int base)
+{
+ int ret = 0;
+
+ if (endptr != NULL)
+ *endptr = (char *) ptr;
+
+ /* Purge whitespace */
+
+ for( ; *ptr && isspace(*ptr); ptr++);
+
+ if (!*ptr)
+ return 0;
+
+ /* Determine the base */
+
+ if (base == 0) {
+ if (ptr[0] == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
+ base = 16;
+ else if (ptr[0] == '0') {
+ base = 8;
+ ptr++;
+ }
+ else
+ base = 10;
+ }
+
+ /* Base 16 allows the 0x on front - so skip over it */
+
+ if (base == 16) {
+ if (ptr[0] == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
+ ptr += 2;
+ }
+
+ /* If the first character isn't valid, then don't
+ * bother */
+
+ if (!*ptr || !_valid(*ptr, base))
+ return 0;
+
+ for( ; *ptr && _valid(*ptr, base); ptr++)
+ ret = (ret * base) + _offset(*ptr, base);
+
+ if (endptr != NULL)
+ *endptr = (char *) ptr;
+
+ return ret;
+}
+
+/**
+ * Determine the number of leading characters in s that match characters in a
+ * @param s A pointer to the string to analyse
+ * @param a A pointer to an array of characters that match the prefix
+ * @return The number of matching characters
+ */
+
+size_t strspn(const char *s, const char *a)
+{
+ int i, j;
+ int al = strlen(a);
+ for (i = 0; s[i] != 0; i++) {
+ int found = 0;
+ for (j = 0; j < al; j++) {
+ if (s[i] == a[j]) {
+ found = 1;
+ break;
+ }
+ }
+ if (!found)
+ break;
+ }
+ return i;
+}
+
+/**
+ * Determine the number of leading characters in s that do not match characters in a
+ * @param s A pointer to the string to analyse
+ * @param a A pointer to an array of characters that do not match the prefix
+ * @return The number of not matching characters
+ */
+
+size_t strcspn(const char *s, const char *a)
+{
+ int i, j;
+ int al = strlen(a);
+ for (i = 0; s[i] != 0; i++) {
+ int found = 0;
+ for (j = 0; j < al; j++) {
+ if (s[i] == a[j]) {
+ found = 1;
+ break;
+ }
+ }
+ if (found)
+ break;
+ }
+ return i;
+}
+
+/**
+ * Extract first token in string str that is delimited by a character in tokens.
+ * Destroys str and eliminates the token delimiter.
+ * @param str A pointer to the string to tokenize.
+ * @param delim A pointer to an array of characters that delimit the token
+ * @param ptr A pointer to a string pointer to keep state of the tokenizer
+ * @return Pointer to token
+ */
+
+char* strtok_r(char *str, const char *delim, char **ptr)
+{
+ /* start new tokenizing job or continue existing one? */
+ if (str == NULL)
+ str = *ptr;
+
+ /* skip over prefix delimiters */
+ char *start = str + strspn(str, delim);
+
+ /* find first delimiter character */
+ char *end = start + strcspn(start, delim);
+ end[0] = '\0';
+
+ *ptr = end+1;
+ return start;
+}
+
+static char **strtok_global;
+
+/**
+ * Extract first token in string str that is delimited by a character in tokens.
+ * Destroys str, eliminates the token delimiter and uses global state.
+ * @param str A pointer to the string to tokenize.
+ * @param delim A pointer to an array of characters that delimit the token
+ * @return Pointer to token
+ */
+
+char* strtok(char *str, const char *delim)
+{
+ return strtok_r(str, delim, strtok_global);
+}
+
+/**
+ * Print error message and error number
+ * @param s Error message to print
+ */
+void perror(const char *s)
+{
+ printf("%s: %d\n", s?s:"(none)", errno);
+}