-
Notifications
You must be signed in to change notification settings - Fork 39
/
os.c
515 lines (429 loc) · 11.3 KB
/
os.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
#include "lemon.h"
#include "larray.h"
#include "lmodule.h"
#include "lstring.h"
#include "linteger.h"
#include <time.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <limits.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#ifdef WINDOWS
#include <windows.h>
#include <winsock2.h>
#else
#include <sys/select.h>
#endif
static struct lobject *
os_open(struct lemon *lemon, struct lobject *self, int argc, struct lobject *argv[])
{
int fd;
int oflag;
int mode;
#if WINDOWS
mode = S_IREAD | S_IWRITE;
#else
mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
#endif
if (argc == 3) {
mode = linteger_to_long(lemon, argv[2]);
}
if (argc >= 2) {
oflag = linteger_to_long(lemon, argv[1]);
} else {
oflag = O_RDONLY;
}
if (oflag & O_CREAT) {
fd = open(lstring_to_cstr(lemon, argv[0]), oflag, mode);
} else {
fd = open(lstring_to_cstr(lemon, argv[0]), oflag);
}
if (fd == -1) {
return lobject_error_type(lemon, "open '%@' fail: %s", argv[0], strerror(errno));
}
return linteger_create_from_long(lemon, fd);
}
static struct lobject *
os_read(struct lemon *lemon, struct lobject *self, int argc, struct lobject *argv[])
{
int fd;
long pos;
long end;
size_t size;
struct stat stat;
struct lobject *string;
fd = linteger_to_long(lemon, argv[0]);
if (fd < 0) {
return lobject_error_type(lemon, "can't read '%@'", argv[0]);
}
pos = lseek(fd, 0, SEEK_CUR);
if (pos < 0) {
return lobject_error_type(lemon, "can't read '%@'", argv[0]);
}
if (fstat(fd, &stat) < 0) {
return lobject_error_type(lemon, "can't read '%@'", argv[0]);
}
end = stat.st_size;
string = lstring_create(lemon, NULL, end - pos);
size = read(fd, lstring_buffer(lemon, string), end - pos);
if (size < (size_t)(end - pos)) {
return lstring_create(lemon,
lstring_buffer(lemon, string),
size);
}
return string;
}
static struct lobject *
os_write(struct lemon *lemon, struct lobject *self, int argc, struct lobject *argv[])
{
int fd;
size_t size;
fd = linteger_to_long(lemon, argv[0]);
size = write(fd,
lstring_to_cstr(lemon, argv[1]),
lstring_length(lemon, argv[1]));
if (size) {
return lemon->l_true;
}
return lemon->l_false;
}
static struct lobject *
os_close(struct lemon *lemon, struct lobject *self, int argc, struct lobject *argv[])
{
int fd;
fd = linteger_to_long(lemon, argv[0]);
close(fd);
return lemon->l_nil;
}
#ifndef WINDOWS
static struct lobject *
os_fcntl(struct lemon *lemon, struct lobject *self, int argc, struct lobject *argv[])
{
int fd;
int cmd;
int flags;
int value;
if (argc < 2 || argc > 3) {
return lobject_error_argument(lemon,
"fcntl() require fd, cmd and optional value");
}
value = 0;
if (argc == 3 && lobject_is_integer(lemon, argv[2])) {
value = linteger_to_long(lemon, argv[2]);
}
fd = linteger_to_long(lemon, argv[0]);
cmd = linteger_to_long(lemon, argv[1]);
flags = fcntl(fd, cmd, value);
if (flags == -1) {
perror("fcntl");
return linteger_create_from_long(lemon, -1);
}
return linteger_create_from_long(lemon, flags);
}
#endif
static struct lobject *
os_select(struct lemon *lemon, struct lobject *self, int argc, struct lobject *argv[])
{
int i;
int n;
int fd;
int count;
int maxfd;
fd_set rfds;
fd_set wfds;
fd_set xfds;
struct timeval tv;
struct timeval *timeout;
int rlen;
int wlen;
int xlen;
struct lobject *rlist[256];
struct lobject *wlist[256];
struct lobject *xlist[256];
struct lobject *item;
struct lobject *items[3];
if (argc != 3 && argc != 4) {
return lobject_error_argument(lemon, "select() required 3 arrays");
}
if (!lobject_is_array(lemon, argv[0]) ||
!lobject_is_array(lemon, argv[1]) ||
!lobject_is_array(lemon, argv[2]))
{
return lobject_error_argument(lemon, "select() required 3 arrays");
}
if (argc == 4 && !lobject_is_integer(lemon, argv[3])) {
return lobject_error_argument(lemon, "select() 4th not integer");
}
FD_ZERO(&rfds);
FD_ZERO(&wfds);
FD_ZERO(&xfds);
maxfd = 0;
count = larray_length(lemon, argv[0]);
for (i = 0; i < count; i++) {
item = larray_get_item(lemon, argv[0], i);
fd = linteger_to_long(lemon, item);
if (maxfd < fd) {
maxfd = fd;
}
FD_SET(fd, &rfds);
}
count = larray_length(lemon, argv[1]);
for (i = 0; i < count; i++) {
item = larray_get_item(lemon, argv[1], i);
fd = linteger_to_long(lemon, item);
if (maxfd < fd) {
maxfd = fd;
}
FD_SET(fd, &wfds);
}
count = larray_length(lemon, argv[2]);
for (i = 0; i < count; i++) {
item = larray_get_item(lemon, argv[2], i);
fd = linteger_to_long(lemon, item);
if (maxfd < fd) {
maxfd = fd;
}
FD_SET(fd, &xfds);
}
timeout = NULL;
if (argc == 4) {
long us = linteger_to_long(lemon, argv[3]);
tv.tv_sec = us / 1000000;
tv.tv_usec = us % 1000000;
timeout = &tv;
}
n = select(maxfd + 1, &rfds, &wfds, &xfds, timeout);
if (n == 0) {
/* timeout */
items[0] = larray_create(lemon, 0, NULL);
items[1] = larray_create(lemon, 0, NULL);
items[2] = larray_create(lemon, 0, NULL);
return larray_create(lemon, 3, items);
}
if (n < 0 && errno != EINTR) {
/* error */
return lobject_error_type(lemon, "select() %s", strerror(errno));
}
rlen = 0;
wlen = 0;
xlen = 0;
items[0] = larray_create(lemon, rlen, rlist);
items[1] = larray_create(lemon, wlen, wlist);
items[2] = larray_create(lemon, xlen, xlist);
if (n > 0) {
for (i = 0; i < maxfd + 1; i++) {
if (FD_ISSET(i, &rfds)) {
item = linteger_create_from_long(lemon, i);
rlist[rlen++] = item;
if (rlen == 256) {
larray_append(lemon, items[0], rlen, rlist);
rlen = 0;
}
}
if (FD_ISSET(i, &wfds)) {
item = linteger_create_from_long(lemon, i);
wlist[wlen++] = item;
if (wlen == 256) {
larray_append(lemon, items[1], wlen, wlist);
wlen = 0;
}
}
if (FD_ISSET(i, &xfds)) {
item = linteger_create_from_long(lemon, i);
xlist[xlen++] = item;
if (xlen == 256) {
larray_append(lemon, items[2], xlen, xlist);
xlen = 0;
}
}
}
}
larray_append(lemon, items[0], rlen, rlist);
larray_append(lemon, items[1], wlen, wlist);
larray_append(lemon, items[2], xlen, xlist);
return larray_create(lemon, 3, items);
}
static struct lobject *
os_realpath(struct lemon *lemon, struct lobject *self, int argc, struct lobject *argv[])
{
const char *path;
char buffer[PATH_MAX];
#ifdef WINDOWS
path = lstring_to_cstr(lemon, argv[0]);
if (GetFullPathName(path, PATH_MAX, buffer, NULL)) {
return lstring_create(lemon, buffer, strlen(buffer));
}
#else
path = realpath(lstring_to_cstr(lemon, argv[0]), buffer);
if (path) {
return lstring_create(lemon, path, strlen(path));
}
#endif
return lemon->l_nil;
}
static struct lobject *
os_time(struct lemon *lemon, struct lobject *self, int argc, struct lobject *argv[])
{
time_t t;
time(&t);
return linteger_create_from_long(lemon, t);
}
static struct lobject *
os_ctime(struct lemon *lemon, struct lobject *self, int argc, struct lobject *argv[])
{
time_t t;
char *ptr;
char buf[26];
if (argc != 1 || lobject_is_integer(lemon, argv[0])) {
return lobject_error_argument(lemon, "required 1 integer argument");
}
t = linteger_to_long(lemon, argv[0]);
#ifdef WINDOWS
(void)buf;
ptr = ctime(&t);
#else
ptr = ctime_r(&t, buf);
#endif
return lstring_create(lemon, ptr, strlen(ptr));
}
struct ltmobject {
struct lobject object;
struct tm tm;
};
static struct lobject *
ltmobject_method(struct lemon *lemon,
struct lobject *self,
int method, int argc, struct lobject *argv[])
{
return lobject_default(lemon, self, method, argc, argv);
}
static struct lobject *
os_gmtime(struct lemon *lemon, struct lobject *self, int argc, struct lobject *argv[])
{
time_t t;
struct tm *tm;
struct ltmobject *tmobject;
tmobject = lobject_create(lemon, sizeof(struct ltmobject), ltmobject_method);
if (argc != 1 || !lobject_is_integer(lemon, argv[0])) {
return lobject_error_argument(lemon, "required 1 integer argument");
}
t = linteger_to_long(lemon, argv[0]);
#ifdef WINDOWS
tm = gmtime(&t);
if (!tm) {
return NULL;
}
memcpy(&tmobject->tm, tm, sizeof(*tm));
#else
tm = gmtime_r(&t, &tmobject->tm);
if (!tm) {
return NULL;
}
#endif
return (struct lobject *)tmobject;
}
static struct lobject *
os_strftime(struct lemon *lemon, struct lobject *self, int argc, struct lobject *argv[])
{
char buf[256];
const char *fmt;
struct ltmobject *tm;
if (argc != 2 || !lobject_is_string(lemon, argv[0]) || argv[1]->l_method != ltmobject_method) {
return lobject_error_argument(lemon, "required 2 integer arguments");
}
tm = (struct ltmobject *)argv[1];
fmt = lstring_to_cstr(lemon, argv[0]);
strftime(buf, sizeof(buf), fmt, &tm->tm);
return lstring_create(lemon, buf, strlen(buf));
}
static struct lobject *
os_exit(struct lemon *lemon, struct lobject *self, int argc, struct lobject *argv[])
{
int status;
status = 0;
if (argc == 1) {
if (!lobject_is_integer(lemon, argv[0])) {
return lobject_error_argument(lemon, "required 1 integer argument");
}
status = linteger_to_long(lemon, argv[0]);
}
exit(status);
return lemon->l_nil;
}
struct lobject *
os_module(struct lemon *lemon)
{
char *cstr;
struct lobject *name;
struct lobject *module;
#define SET_FUNCTION(value) do { \
cstr = #value ; \
name = lstring_create(lemon, cstr, strlen(cstr)); \
lobject_set_attr(lemon, \
module, \
name, \
lfunction_create(lemon, name, NULL, os_ ## value)); \
} while(0)
#define SET_INTEGER(value) do { \
cstr = #value ; \
name = lstring_create(lemon, cstr, strlen(cstr)); \
lobject_set_attr(lemon, \
module, \
name, \
linteger_create_from_long(lemon, value)); \
} while(0)
module = lmodule_create(lemon, lstring_create(lemon, "os", 2));
SET_FUNCTION(open);
SET_FUNCTION(read);
SET_FUNCTION(write);
SET_FUNCTION(close);
SET_FUNCTION(time);
SET_FUNCTION(ctime);
SET_FUNCTION(gmtime);
SET_FUNCTION(strftime);
SET_FUNCTION(select);
SET_FUNCTION(realpath);
SET_FUNCTION(exit);
#ifndef WINDOWS
SET_FUNCTION(fcntl);
#endif
/* common */
SET_INTEGER(EXIT_SUCCESS);
SET_INTEGER(EXIT_FAILURE);
SET_INTEGER(O_RDONLY);
SET_INTEGER(O_WRONLY);
SET_INTEGER(O_RDWR);
SET_INTEGER(O_APPEND);
SET_INTEGER(O_CREAT);
SET_INTEGER(O_TRUNC);
SET_INTEGER(O_EXCL);
#ifdef WINDOWS
SET_INTEGER(O_TEXT);
SET_INTEGER(O_BINARY);
SET_INTEGER(S_IREAD);
SET_INTEGER(S_IWRITE);
#else
SET_INTEGER(O_NONBLOCK);
SET_INTEGER(O_NOFOLLOW);
SET_INTEGER(O_CLOEXEC);
SET_INTEGER(S_IRWXU);
SET_INTEGER(S_IRUSR);
SET_INTEGER(S_IWUSR);
SET_INTEGER(S_IXUSR);
SET_INTEGER(S_IRWXG);
SET_INTEGER(S_IRGRP);
SET_INTEGER(S_IWGRP);
SET_INTEGER(S_IXGRP);
SET_INTEGER(S_IRWXO);
SET_INTEGER(S_IROTH);
SET_INTEGER(S_IWOTH);
SET_INTEGER(S_IXOTH);
SET_INTEGER(F_GETFL);
SET_INTEGER(O_NONBLOCK);
#endif
return module;
}