-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibc.c
More file actions
114 lines (87 loc) · 1.18 KB
/
libc.c
File metadata and controls
114 lines (87 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* libc.c
*/
#include <libc.h>
#include <types.h>
int errno;
void perror()
{
write(1, "Error #: ", 7);
char buff[16];
itoa(errno, buff);
write(1, buff, strlen(buff));
write(1, "\n", 1);
}
void itoa(int a, char *b)
{
int i, i1;
char c;
if (a==0) { b[0]='0'; b[1]=0; return ;}
i=0;
while (a>0)
{
b[i]=(a%10)+'0';
a=a/10;
i++;
}
for (i1=0; i1<i/2; i1++)
{
c=b[i1];
b[i1]=b[i-i1-1];
b[i-i1-1]=c;
}
b[i]=0;
}
int strlen(char *a)
{
int i;
i=0;
while (a[i]!=0) i++;
return i;
}
void ctox(char a, char *b)
{
int i, i1;
char c;
if (a==0) { b[0]='0'; b[1]=0; return ;}
i=0;
while (a>0)
{
int num = a%16;
if (num < 10)
b[i]=num+'0';
else
b[i]=num-10+'A';
a=a/16;
i++;
}
for (i1=0; i1<i/2; i1++)
{
c=b[i1];
b[i1]=b[i-i1-1];
b[i-i1-1]=c;
}
b[i]=0;
}
int seed = 0;
void srand(int s)
{
seed = s;
}
unsigned int rand()
{
seed = (seed * 1103515245 + 12345) % 2147483648;
return seed;
}
int atoi(char *a)
{
int i;
int res = 0;
i=0;
while (a[i]!=0 && a[i] != '\n')
{
res = res * 10 + a[i] - '0';
i++;
}
return res;
}