-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
173 lines (145 loc) · 3.52 KB
/
shell.c
File metadata and controls
173 lines (145 loc) · 3.52 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <stdint.h>
#include "shell.h"
typedef void (*CommandFunc)();
CommandFunc cmdl[] = {add,freee,print_s/*,allocate*/};
char* commands[] ={"ADD","FREE","PRINTS"/*,"ALLOCATEM"*/};
/*
void allocate(char* arg){
int out[10];
int count = turntointarr(arg,out,10);
int* pointer = (int*) alloc(out[0]);
char prt[20];
for(int i =1;i<count;i++){
pointer[i-1] = out[i];
inttostr(pointer[i-1],prt);
print_s(prt);
}
}
*/
void inttostr(int num, char* str) {
int i = 0, temp, len = 0;
char buffer[20]; // Sufficient for up to 32-bit integer
if (num == 0) {
str[i++] = '0';
str[i] = '\0';
return;
}
while (num > 0) {
buffer[len++] = (num % 10) + '0';
num /= 10;
}
for (int j = 0; j < len; j++) {
str[j] = buffer[len - 1 - j];
}
str[len] = '\0';
return;
}
int checkifcmdoropr(char* in, int* funcno, int* indx) {
for (int i = 0; i < sizeof(commands) / sizeof(commands[0]); i++) {
int j = 0;
while (in[j] != ' ' && in[j] != '\0' && commands[i][j] != '\0') {
if (commands[i][j] != in[j]) {
break;
}
j++;
}
if (in[j] == ' ' && commands[i][j] == '\0') {
*funcno = i;
*indx = j + 1;
return 1;
}
}
return 0;
}
void exec(char* in){
if (in[0]=='\0')return ;
int funcno;
int indx;
if(!checkifcmdoropr(in,&funcno,&indx))return;
char* arg1 = in+indx;
cmdl[funcno](arg1);
return;
}
int is_digit(char c) {
return c >= '0' && c <= '9';
}
// Convert a numeric string to an integer from scratch
int string_to_int(const char *str, int *result) {
int sign = 1;
int value = 0;
// Check for optional sign
if (*str == '-') {
sign = -1;
str++;
} else if (*str == '+') {
str++;
}
// Convert character digits to integer
while (*str) {
if (!is_digit(*str)) {
return 0; // Not a valid number
}
value = value * 10 + (*str - '0');
str++;
}
*result = value * sign;
return 1; // Success
}
int turntointarr(const char *input, int *output, int max_size) {
int count = 0;
char temp[20]; // Temporary buffer for numbers
int i = 0;
while (*input) {
if (*input == ' ') {
if (i > 0) {
temp[i] = '\0';
int num;
if (string_to_int(temp, &num)) {
if (count < max_size) {
output[count++] = num;
} else {
return -1;
}
} else {
return -1;
}
i = 0;
}
} else {
if (i < sizeof(temp) - 1) {
temp[i++] = *input;
} else {
return -1;
}
}
input++;
}
if (i > 0) {
temp[i] = '\0';
int num;
if (string_to_int(temp, &num)) {
if (count < max_size) {
output[count++] = num;
} else {
return -1;
}
} else {
return -1;
}
}
return count;
}
void add(char* arg) {
int sum = 0;
int out[10];
int count = turntointarr(arg, out, 10);
if (count == -1) {
return;
}
for (int i = 0; i < count; i++) {
sum += out[i];
}
char str[20];
inttostr(sum, str);
print_s(str);
}