/
utils.c
executable file
·246 lines (207 loc) · 4.73 KB
/
utils.c
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>
#include <assert.h>
#include <arith.h>
#include "utils.h"
#include "svc.h"
#include "alloc.h"
#include "fd.h"
/*
* wrapper function around strtol call; converts and checks for error
*
* num - string to convert
* res - storage location of the converted value
*
* returns SUCCESS on success and < 0 on error
*/
int
strtol_wrap(const char *num, long *res, int base) {
char fbuf[256];
if (num == NULL) {
sprintf(fbuf, "Parse error: %s\r\n", "passed NULL pointer");
eputs(STDOUT, fbuf);
return -3;
}
int rv = SUCCESS;
char *end = NULL;
*res = strtol(num, &end, base);
if (errno) { /* value out of range: under or over flow */
sprintf(fbuf, "Parse error: %s\r\n", strerror(errno));
eputs(STDOUT, fbuf);
errno = 0;
rv = -1;
}
if (*end) {
sprintf(fbuf, "Parse error: non-parsing part: %s\r\n", end);
eputs(STDOUT, fbuf);
rv = -2;
}
return rv;
}
int
strtoul_wrap(const char *num, unsigned long *res, int base) {
char fbuf[256];
if (num == NULL) {
sprintf(fbuf, "Parse error: %s\r\n", "passed NULL pointer");
eputs(STDOUT, fbuf);
return -3;
}
int rv = SUCCESS;
char *end = NULL;
*res = strtoul(num, &end, base);
if (errno) { /* value out of range: under or over flow */
sprintf(fbuf, "Parse error: %s\r\n", strerror(errno));
eputs(STDOUT, fbuf);
errno = 0;
rv = -1;
}
if (*end) {
sprintf(fbuf, "Parse error: non-parsing part: %s\r\n", end);
eputs(STDOUT, fbuf);
rv = -2;
}
return rv;
}
int
strtoull_wrap(const char *num, unsigned long long *res, int base) {
char fbuf[256];
if (num == NULL) {
sprintf(fbuf, "Parse error: %s\r\n", "passed NULL pointer");
eputs(STDOUT, fbuf);
return -3;
}
int rv = SUCCESS;
char *end = NULL;
*res = strtoull(num, &end, base);
if (errno) { /* value out of range: under or over flow */
sprintf(fbuf, "Parse error: %s\r\n", strerror(errno));
eputs(STDOUT, fbuf);
errno = 0;
rv = -1;
}
if (*end) {
sprintf(fbuf, "Parse error: non-parsing part: %s\r\n", end);
eputs(STDOUT, fbuf);
rv = -2;
}
return rv;
}
/*
* almost a replica of strdup, didn't know whether we are allowed to use it
* (it's not in the standard, but part of POSIX)
*
* arg_str - string to duplicate
* len - length of the string
*
* returns pointer to the newly allocated string
*/
char
*newstr(const char *arg_str, size_t len) {
char *rv = emalloc(sizeof (char) * (len+1));
if (rv == NULL) return NULL;
strncpy(rv, arg_str, len);
rv[len] = '\0';
return rv;
}
/*
* malloc wrapper which checks for errors and fails immideately
*
* n - size to allocate
*
* returns void
*/
void
*emalloc(size_t n) {
void *rv;
if ((rv = svc_malloc(n)) == NULL)
eputs(STDOUT, "out of memory");
return rv;
}
void
eputs(int fd, const char *str) {
while(*str) svc_write(fd, *str++);
}
void
eputc(int fd, const char ch) {
svc_write(fd, ch);
}
char
egetc(int fd) {
char ch;
svc_read(fd, &ch);
return ch;
}
void mmap(void) {
svc_mmap();
}
void
efree(void *ptr) {
svc_free(ptr);
}
int
eopen(const char *path) {
return svc_open(path);
}
int
eclose(int fd) {
return svc_close(fd);
}
int
ecreate(const char *path) {
return svc_create(path);
}
int
eremove(const char *path) {
return svc_remove(path);
}
/*
* initializes input_t object with size string to read input
*
* returns pointer to the allocated object
*/
input_t*
input_t_init(size_t size) {
input_t *buf = emalloc(sizeof *buf);
buf->line = emalloc(sizeof (char) * size + 1);
buf->argv = NULL;
buf->argc = 0;
return buf;
}
/*
* frees memory of the input_t object
*
* returns void
*/
void
freecmd(input_t *cmd_list) {
const char **argv_ptr = cmd_list->argv;
while(*argv_ptr)
efree((void *)*argv_ptr++);
efree(cmd_list->argv);
//efree(cmd_list->line); /* freed in parseline */
efree(cmd_list);
}
void
panic(void) {
__asm("bkpt");
}
int ls(void) {
return svc_ls();
}
int cd(const char *dir) {
return svc_cd(dir);
}
int pwd(void) {
return svc_pwd();
}
void
char2ascii(unsigned char ch, char *string) {
string[0] = (char) (ch/100 + '0');
ch -= ch/100 * 100;
string[1] = (char) (ch/10 + '0');
ch -= ch/10 * 10;
string[2] = (char) (ch + '0');
}