Home | History | Annotate | Download | only in lib
      1 #include <string.h>
      2 #include <stdlib.h>
      3 #include <limits.h>
      4 
      5 #include "strntol.h"
      6 
      7 long strntol(const char *str, size_t sz, char **end, int base)
      8 {
      9 	/* Expect that digit representation of LONG_MAX/MIN
     10 	 * not greater than this buffer */
     11 	char buf[24];
     12 	long ret;
     13 	const char *beg = str;
     14 
     15 	/* Catch up leading spaces */
     16 	for (; beg && sz && *beg == ' '; beg++, sz--)
     17 		;
     18 
     19 	if (!sz || sz >= sizeof(buf)) {
     20 		if (end)
     21 			*end = (char *)str;
     22 		return 0;
     23 	}
     24 
     25 	memcpy(buf, beg, sz);
     26 	buf[sz] = '\0';
     27 	ret = strtol(buf, end, base);
     28 	if (ret == LONG_MIN || ret == LONG_MAX)
     29 		return ret;
     30 	if (end)
     31 		*end = (char *)str + (*end - buf);
     32 	return ret;
     33 }
     34