SimpleKernel 1.17.0
Loading...
Searching...
No Matches
sk_stdlib.c File Reference
#include "sk_stdlib.h"
#include <limits.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "sk_ctype.h"
Include dependency graph for sk_stdlib.c:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void __stack_chk_fail ()
 栈保护检查失败后进入死循环
 
static unsigned long long strtox_main (const char *nptr, char **endptr, int base, int *sign_out, int *overflow)
 
unsigned long long int strtoull (const char *nptr, char **endptr, int base)
 将字符串按指定进制转换为无符号长长整数,并可以获取转换结束位置
 
long long int strtoll (const char *nptr, char **endptr, int base)
 将字符串按指定进制转换为长长整数,并可以获取转换结束位置
 
long int strtol (const char *nptr, char **endptr, int base)
 将字符串按指定进制转换为长整数,并可以获取转换结束位置
 
unsigned long int strtoul (const char *nptr, char **endptr, int base)
 将字符串按指定进制转换为无符号长整数,并可以获取转换结束位置
 
int atoi (const char *nptr)
 将字符串转换为整数
 
long int atol (const char *nptr)
 将字符串转换为长整数
 
long long int atoll (const char *nptr)
 将字符串转换为长长整数
 

Variables

uint64_t __stack_chk_guard = 0x595E9FBD94FDA766
 栈保护
 

Function Documentation

◆ __stack_chk_fail()

void __stack_chk_fail ( )

栈保护检查失败后进入死循环

Definition at line 22 of file sk_stdlib.c.

22{ while (true); }

◆ atoi()

int atoi ( const char *  nptr)

将字符串转换为整数

Parameters
nptr指向要转换的字符串的指针
Returns
转换后的整数值

Definition at line 153 of file sk_stdlib.c.

153{ return (int)strtol(nptr, NULL, 10); }
#define strtol

◆ atol()

long int atol ( const char *  nptr)

将字符串转换为长整数

Parameters
nptr指向要转换的字符串的指针
Returns
转换后的长整数值

Definition at line 155 of file sk_stdlib.c.

155{ return strtol(nptr, NULL, 10); }

◆ atoll()

long long int atoll ( const char *  nptr)

将字符串转换为长长整数

Parameters
nptr指向要转换的字符串的指针
Returns
转换后的长长整数值

Definition at line 157 of file sk_stdlib.c.

157{ return strtoll(nptr, NULL, 10); }
#define strtoll

◆ strtol()

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

将字符串按指定进制转换为长整数,并可以获取转换结束位置

Parameters
nptr指向要转换的字符串的指针
endptr指向指针的指针,用于存储转换结束位置
base转换的进制(2-36,0表示自动检测)
Returns
转换后的长整数值

Definition at line 136 of file sk_stdlib.c.

136 {
137 long long int val = strtoll(nptr, endptr, base);
138#if LONG_MAX != LLONG_MAX
139 if (val > LONG_MAX) return LONG_MAX;
140 if (val < LONG_MIN) return LONG_MIN;
141#endif
142 return (long int)val;
143}

◆ strtoll()

long long int strtoll ( const char *  nptr,
char **  endptr,
int  base 
)

将字符串按指定进制转换为长长整数,并可以获取转换结束位置

Parameters
nptr指向要转换的字符串的指针
endptr指向指针的指针,用于存储转换结束位置
base转换的进制(2-36,0表示自动检测)
Returns
转换后的长长整数值

Definition at line 117 of file sk_stdlib.c.

117 {
118 int negative;
119 int overflow;
120 unsigned long long acc =
121 strtox_main(nptr, endptr, base, &negative, &overflow);
122
123 if (overflow) {
124 return negative ? LLONG_MIN : LLONG_MAX;
125 }
126
127 if (negative) {
128 if (acc > (unsigned long long)LLONG_MAX + 1) return LLONG_MIN;
129 return -(long long)acc;
130 } else {
131 if (acc > LLONG_MAX) return LLONG_MAX;
132 return (long long)acc;
133 }
134}
static unsigned long long strtox_main(const char *nptr, char **endptr, int base, int *sign_out, int *overflow)
Definition sk_stdlib.c:27
Here is the call graph for this function:

◆ strtoul()

unsigned long int strtoul ( const char *  nptr,
char **  endptr,
int  base 
)

将字符串按指定进制转换为无符号长整数,并可以获取转换结束位置

Parameters
nptr指向要转换的字符串的指针
endptr指向指针的指针,用于存储转换结束位置
base转换的进制(2-36,0表示自动检测)
Returns
转换后的无符号长整数值

Definition at line 145 of file sk_stdlib.c.

145 {
146 unsigned long long int val = strtoull(nptr, endptr, base);
147#if ULONG_MAX != ULLONG_MAX
148 if (val > ULONG_MAX) return ULONG_MAX;
149#endif
150 return (unsigned long int)val;
151}
#define strtoull

◆ strtoull()

unsigned long long int strtoull ( const char *  nptr,
char **  endptr,
int  base 
)

将字符串按指定进制转换为无符号长长整数,并可以获取转换结束位置

Parameters
nptr指向要转换的字符串的指针
endptr指向指针的指针,用于存储转换结束位置
base转换的进制(2-36,0表示自动检测)
Returns
转换后的无符号长长整数值

Definition at line 107 of file sk_stdlib.c.

107 {
108 int negative;
109 int overflow;
110 unsigned long long acc =
111 strtox_main(nptr, endptr, base, &negative, &overflow);
112
113 if (overflow) return ULLONG_MAX;
114 return negative ? -acc : acc;
115}
Here is the call graph for this function:

◆ strtox_main()

static unsigned long long strtox_main ( const char *  nptr,
char **  endptr,
int  base,
int *  sign_out,
int *  overflow 
)
static

Definition at line 27 of file sk_stdlib.c.

28 {
29 const char* s = nptr;
30 unsigned long long acc = 0;
31 int c;
32 unsigned long long cutoff;
33 int cutlim;
34 int any = 0;
35 int negative = 0;
36
37 *overflow = 0;
38
39 // Skip whitespace
40 while (isspace(*s)) s++;
41
42 // Check sign
43 if (*s == '-') {
44 negative = 1;
45 s++;
46 } else if (*s == '+') {
47 s++;
48 }
49 if (sign_out) *sign_out = negative;
50
51 // Detect base
52 if ((base == 0 || base == 16) && *s == '0' && (s[1] == 'x' || s[1] == 'X')) {
53 // Hex prefix
54 // We speculatively consume it.
55 if (isxdigit(s[2])) {
56 s += 2;
57 base = 16;
58 } else {
59 // '0x' followed by non-hex.
60 // if base==0, it's octal 0.
61 if (base == 0) base = 8;
62 }
63 }
64 if (base == 0) {
65 base = *s == '0' ? 8 : 10;
66 }
67
68 if (base < 2 || base > 36) {
69 if (endptr) *endptr = (char*)nptr; // Invalid base
70 return 0;
71 }
72
73 cutoff = ULLONG_MAX / (unsigned long long)base;
74 cutlim = ULLONG_MAX % (unsigned long long)base;
75
76 for (;; s++) {
77 c = *s;
78 if (isdigit(c))
79 c -= '0';
80 else if (isalpha(c))
81 c = toupper(c) - 'A' + 10;
82 else
83 break;
84
85 if (c >= base) break;
86
87 if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) {
88 any = -1; // overflow
89 } else {
90 any = 1;
91 acc = acc * base + c;
92 }
93 }
94
95 if (any < 0) {
96 *overflow = 1;
97 acc = ULLONG_MAX;
98 }
99
100 // Set endptr
101 if (endptr) {
102 *endptr = (char*)(any ? s : nptr);
103 }
104 return acc;
105}
#define isxdigit
#define isdigit
#define isspace
#define toupper
#define isalpha
Here is the caller graph for this function:

Variable Documentation

◆ __stack_chk_guard

uint64_t __stack_chk_guard = 0x595E9FBD94FDA766

栈保护

Definition at line 19 of file sk_stdlib.c.