1 /* SPDX-License-Identifier: MIT */
2 
3 /*
4  * Copyright © 2005-2014 Rich Felker, et al.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 
26 /* From: http://fossies.org/dox/musl-1.0.5/atoi_8c_source.html */
27 
28 #include <stdlib.h>
29 #include <ctype.h>
30 
atoi(const char * s)31 int atoi(const char *s)
32 {
33 	int n = 0;
34 	int neg = 0;
35 
36 	while (isspace((unsigned char)*s) != 0) {
37 		s++;
38 	}
39 	switch (*s) {
40 	case '-':
41 		neg = 1;
42 		s++;
43 		break;	/* artifact to quiet coverity warning */
44 	case '+':
45 		s++;
46 		break;
47 	default:
48 		/* Add an empty default with break, this is a defensive programming.
49 		 * Static analysis tool won't raise a violation if default is empty,
50 		 * but has that comment.
51 		 */
52 		break;
53 	}
54 	/* Compute n as a negative number to avoid overflow on INT_MIN */
55 	while (isdigit((unsigned char)*s) != 0) {
56 		n = 10*n - (*s++ - '0');
57 	}
58 	return neg ? n : -n;
59 }
60