CHDK_DE Vorschauversion  Trunk Rev. 5291
 Alle Datenstrukturen Dateien Funktionen Variablen Typdefinitionen Aufzählungen Aufzählungswerte Makrodefinitionen
strtol.c-Dateireferenz

gehe zum Quellcode dieser Datei

Funktionen

long strtol (const char *nptr, char **endptr, register int base)
 

Variablen

int errno
 

Dokumentation der Funktionen

long strtol ( const char *  nptr,
char **  endptr,
register int  base 
)

Definiert in Zeile 98 der Datei strtol.c.

99 {
100  register const char *s = nptr;
101  register unsigned long acc;
102  register int c;
103  register unsigned long cutoff;
104  register int neg = 0, any, cutlim;
105 
106  /*
107  * Skip white space and pick up leading +/- sign if any.
108  * If base is 0, allow 0x for hex and 0 for octal, else
109  * assume decimal; if base is already 16, allow 0x.
110  */
111  do {
112  c = *s++;
113  } while (ISSPACE(c));
114  if (c == '-') {
115  neg = 1;
116  c = *s++;
117  } else if (c == '+')
118  c = *s++;
119  if ((base == 0 || base == 16) &&
120  c == '0' && (*s == 'x' || *s == 'X')) {
121  c = s[1];
122  s += 2;
123  base = 16;
124  }
125  if (base == 0)
126  base = c == '0' ? 8 : 10;
127 
128  /*
129  * Compute the cutoff value between legal numbers and illegal
130  * numbers. That is the largest legal value, divided by the
131  * base. An input number that is greater than this value, if
132  * followed by a legal input character, is too big. One that
133  * is equal to this value may be valid or not; the limit
134  * between valid and invalid numbers is then based on the last
135  * digit. For instance, if the range for longs is
136  * [-2147483648..2147483647] and the input base is 10,
137  * cutoff will be set to 214748364 and cutlim to either
138  * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
139  * a value > 214748364, or equal but the next digit is > 7 (or 8),
140  * the number is too big, and we will return a range error.
141  *
142  * Set any if any `digits' consumed; make it negative to indicate
143  * overflow.
144  */
145  cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
146  cutlim = cutoff % (unsigned long)base;
147  cutoff /= (unsigned long)base;
148  for (acc = 0, any = 0;; c = *s++) {
149  if (ISDIGIT(c))
150  c -= '0';
151  else if (ISALPHA(c))
152  c -= ISUPPER(c) ? 'A' - 10 : 'a' - 10;
153  else
154  break;
155  if (c >= base)
156  break;
157  if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
158  any = -1;
159  else {
160  any = 1;
161  acc *= base;
162  acc += c;
163  }
164  }
165  if (any < 0) {
166  acc = neg ? LONG_MIN : LONG_MAX;
167 #if 0
168  errno = ERANGE;
169 #endif
170  } else if (neg)
171  acc = -acc;
172  if (endptr != 0)
173  *endptr = (char *) (any ? s - 1 : nptr);
174  return (acc);
175 }

Variablen-Dokumentation

int errno