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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
|
/* Copyright (C) 1995,1996 Robert de Bath <rdebath@cix.compulink.co.uk>
* This file is part of the Linux-8086 C library and is distributed
* under the GNU Library General Public License.
*/
#if !__FIRST_ARG_IN_AX__
#ifdef __AS386_16__
#ifdef __MSDOS__
#include <dos.h>
#include <fcntl.h>
#include <errno.h>
int errno;
unsigned _doserrno;
#ifdef L_dos_start
static char * defarg[2] = { "C" };
static char ** def_environ =defarg+1;
void (*__cleanup)() = 0;
#asm
.data
export ___envseg
___envseg:
.word 0
export ___psp
___psp:
.word 0
.text
export _exit
export __exit
_exit: ! exit(rv) function
mov bx,sp
push [bx+2] ! Copy the `rv` for the exit fuctions.
mov bx,[___cleanup] ! Call exit, normally this is `__do_exit`
test bx,bx
je no_clean ! But it`s default is null
call bx
no_clean:
inc sp
inc sp
__exit: ! _exit(rv)
mov bx,sp
mov ax,[bx+2]
mov ah,#$4c
int #$21
dos_1_exit:
int #$20
.text
export ___cstartup ! Crt0 startup
___cstartup:
mov ax,#$3000 ! Get DOS version
int $21
cmp al,#2 ! DOS 2+ is Ok
jb dos_1_exit
mov dx,cs ! Current CS
add dx,#__segoff ! This var generated by the linker
mov ds,dx ! Correct DS
mov [___psp],es ! PSP segment
seg es
mov ax,[$2c]
mov [___envseg],ax ! Enviroment Segment
! Now need to free some RAM
seg es
mov bx,[2] ! Top of Ram
mov ax,ds
add ax,#4096 ! Top of 64k data seg
jc use_tor ! Oops, wrapped
cmp ax,bx
jnc use_tor ! Bigger than tor
mov bx,ax
use_tor:
mov ax,cs ! Work out how big the memseg is needed
sub bx,ax
mov ah,#$4A ! Set it
int $21
jnc set_stack ! Good.
! Ooops, problem..
! BX is now how big it can be so set that.
! FIXME should check for BSS v Stack overlap
mov ah,#$4A
int $21
set_stack: ! Now set SS to the same as DS
sub bx,#__segoff ! And SP to the top of available memory.
mov cl,#4
shl bx,cl
sub bx,#2
mov ss,dx
mov sp,bx
zap_bss: ! Clear the BSS
mov es,dx ! ES now data seg
mov di,#__edata
mov cx,#__end
sub cx,di
xor ax,ax
cld
rep
stosb
push [_def_environ] ! Defaults for when nothing is used.
mov ax,#_defarg
push ax
mov ax,#1
push ax
mov si,#auto_start ! Pointer to first autostart function
auto_run:
mov bx,[si]
test bx,bx
jz no_entry
call bx ! Call the function
no_entry:
inc si ! SI at next
inc si
jmp auto_run ! And round for the next.
call_exit: ! Last item called by above.
pop bx ! Be tidy.
push ax ! At the end the last called was main() push it`s
call _exit ! return val and call exit();
bad_exit:
jmp bad_exit ! Exit returned !!
loc 2
.word _main ! Segment 2 is the trailing pointers, main and the
.word call_exit ! routine to call exit.
data_start:
.text
#endasm
__E_nosys()
{
#asm
.text
export sys_call5
export sys_call4
export sys_call3
export sys_call2
export sys_call1
export sys_call0
sys_call5: ! Trap the unemulated Linux86 syscalls
sys_call4:
sys_call3:
sys_call2:
sys_call1:
sys_call0:
#endasm
errno = ENOSYS;
return -1;
}
#endif
#ifdef L___mkargv
#ifdef __AS386_16__
#asm
loc 1 ! Make sure the pointer is in the correct segment
auto_func: ! Label for bcc -M to work.
.word ___mkargv ! Pointer to the autorun function
.text ! So the function after is also in the correct seg.
#endasm
#endif
__mkargv(__argc, __argv)
int __argc;
char ** __argv;
{
int length, i, argc=1, s=0;
unsigned char *ptr, *p;
__set_es(__psp); /* Pointer to the args */
length = __peek_es(0x80); /* Length of cmd line */
if( length > 0 )
{
ptr = (char*) sbrk(length+1); /* Allocate some space */
for(i=0; i<length; i++) /* Copy it in. */
{
ptr[i] = __peek_es(0x81+i);
if( ptr[i] > ' ' && s == 0 ) { argc++; s=1; }
if( ptr[i] <= ' ' && s == 1 ) s=0;
}
ptr[length]=0;
p= __argv[0];
__argv = (char**) sbrk((argc+1)*sizeof(char*));
__argv[0] = p; /* TODO: The real command can be found */
__argc=argc;
/*
* TODO: This needs to understand quoting and wildcards
*/
argc=1; s=0;
for(i=0; i<length; i++)
{
if( ptr[i] > ' ' && s == 0 ) { __argv[argc++] = ptr+i; s=1; }
if( ptr[i] <= ' ' && s == 1 ) { ptr[i] = '\0'; s=0; }
}
__argv[argc] = 0;
}
}
#endif
#ifdef L___mkenvp
#ifdef __AS386_16__
#asm
loc 1 ! Make sure the pointer is in the correct segment
auto_func: ! Label for bcc -M to work.
.word ___mkenvp ! Pointer to the autorun function
.text ! So the function after is also in the correct seg.
#endasm
#endif
char ** environ = 0;
__mkenvp(__argc, __argv, __envp)
int __argc;
char ** __argv;
char ** __envp;
{
/* FIXME !!!
*
* Note must write to __envp argument but not touch __argv or __argc
*/
}
#endif
#ifdef L_dos__fconv
/* This function converts filenames from unix like to DOS. */
char *
__fconv(fname)
char * fname;
{
static char buf1[66], buf2[66], *str = 0;
register char *p, ch;
int dot = 0;
if( strcmp("/dev/tty", fname) == 0 ) return "CON:";
if( str == buf1 ) str = buf2; else str = buf1;
p = str;
if( strncmp("/mnt/", fname, 5) == 0 )
{
strcpy(p, "A:"); p+=2; fname+=4;
}
/*
* POSS:
* open("/name/*", ...); looks for an environ var PATH_name=c:\x\y\z
*/
while((ch = *fname++) && p < str+65)
{
if( ( ch >= 'a' && ch <= 'z' )
|| ( ch >= '0' && ch <= '9' )
|| ch == ':' || ch == '%' || ch == '-' || ch == '$' )
;
else if( ch >= 'A' && ch <= 'Z' )
ch = ch-'A'+'a';
else if( ch == '.' && dot == 0 )
dot = 1;
else if( ch == '/' || ch == '\\' )
{
dot = 0; ch = '\\';
}
else ch = '_';
*p++ = ch;
}
*p++ = '\0';
return str;
}
#endif
#ifdef L___exterror
static char errno_xlate[] = {
0, EINVAL, ENOENT, ENOENT, EMFILE, EACCES, EBADF, EFAULT, ENOMEM,
EFAULT, ENOMEM, EINVAL, EINVAL, EINVAL, EINVAL, ENODEV, EPERM, EXDEV,
ENFILE, EROFS, ENODEV, ENXIO, EINVAL, EIO, EINVAL, ESPIPE, EIO, EIO,
EAGAIN, EIO, EIO, EIO, EBUSY, EBUSY, ENODEV, EFAULT, ENOLCK, EFAULT,
EFAULT, ENOSPC
};
__exterror()
{
#asm
export exterror
exterror:
push ds
push es
push di
push si
push bp
xor bx,bx
mov ah,#$59
int $21
pop bp
pop si
pop di
pop es
pop ds
mov __doserrno,ax
#endasm
{
int nerrno;
extern unsigned _doserrno;
if( _doserrno == 0 )
/* No error? No change. */;
else if( _doserrno >= sizeof(errno_xlate)
|| errno_xlate[_doserrno] == EFAULT )
errno = 16384+_doserrno;
else
errno = errno_xlate[_doserrno];
}
return -1;
}
#endif
#ifdef L_dos_read
int
read(fd, ptr, len)
int fd;
char *ptr;
unsigned len;
{
#asm
mov bx,sp
mov cx,[bx+6]
mov dx,[bx+4]
mov bx,[bx+2]
mov ah,#$3f
int #$21
jnc readok
br exterror
readok:
#endasm
}
#endif
#ifdef L_dos_write
int
write(fd, ptr, len)
int fd;
char *ptr;
unsigned len;
{
#asm
mov bx,sp
mov cx,[bx+6]
mov dx,[bx+4]
mov bx,[bx+2]
mov ah,#$40
int #$21
jnc writeok
br exterror
writeok:
#endasm
}
#endif
#ifdef L_dos_open
int
open(fname, type, cmode)
char * fname;
int type;
int cmode;
{
register char * nname = __fconv(fname);
int creat_mode = 0;
int rv;
static int xlate_mode[] = {
#ifdef OPEN_LIKE_UNIX
O_RDONLY|O_DENYNONE,
O_WRONLY|O_DENYNONE,
O_RDWR|O_DENYNONE,
#else
O_RDONLY|O_DENYNONE,
O_WRONLY|O_DENYWRITE,
O_RDWR|O_DENYALL,
#endif
3
};
if( (cmode & 0222) == 0 ) creat_mode = 1;
/* BzzzT. Assume these flags both mean the merge of them */
/* BzzzT. Also ignore O_EXCL */
if( type & (O_TRUNC|O_CREAT) )
rv = __dos_creat(nname, creat_mode);
else
{
/* If we would open in compatibility mode make it a little more unixy */
if( type & O_DENYMODE )
rv = __dos_open(nname, type&(O_ACCMODE|O_DENYMODE|O_SETFD));
else
rv = __dos_open(nname, xlate_mode[type&O_ACCMODE]);
}
return rv;
}
__dos_open(fname, mode)
{
#asm
mov bx,sp
mov dx,[bx+2] ;ds:dx points to source string
mov al,[bx+4] ;access code
mov ah,#$3d ;ask for a open
int #$21
jnc openok ;return handle if no error
br exterror
openok:
#endasm
}
__dos_creat(fname)
char * fname;
{
#asm
mov bx,sp
mov dx,[bx+2] ;ds:dx points to source string
xor cx,cx ;normal attributes
mov ah,#$3c ;ask for a create
int #$21
jnc creok ;return handle if no error
br exterror
creok:
#endasm
}
#endif
#ifdef L_dos_close
close(fd)
{
#asm
mov bx,sp
mov bx,[bx+2] ;file handle
mov ah,#$3e ;ask for a close
int #$21
mov ax,0 ;return 0 if no error
jnc closeok
br exterror
closeok:
#endasm
}
#endif
#ifdef L_dos_unlink
unlink(fname)
char * fname;
{
#asm
mov bx,sp
push [bx+2]
call ___fconv
inc sp
inc sp
mov dx,ax ;ds:dx points to source string
mov ah,#$41 ;ask for a unlink
int #$21
mov ax,0 ;assume no errors
jnc unlok
br exterror
unlok:
#endasm
}
#endif
#ifdef L_dos_lseek
long
lseek(fd, offset, mode)
int fd, mode;
long offset;
{
#asm
mov bx,sp
mov al,[bx+8] ;mode of seek (0 to 2)
mov dx,[bx+4] ;cx:dx is long offset
mov cx,[bx+6]
mov bx,[bx+2] ;file handle
mov ah,#$42
int #$21 ;do the lseek
jnc seekok
call exterror
mov dx,ax
seekok:
#endasm
}
#endif
#ifdef L_dos_segalloc
unsigned int
__segalloc(paracount)
unsigned int paracount;
{
#asm
mov bx,sp
mov bx,[bx+2]
mov ah,#$48
int $21
jnc ok
mov ax,#0
ok:
#endasm
}
#endif
#ifdef L_dos_segfree
unsigned int
__segfree(segno)
unsigned int segno;
{
#asm
push es
mov bx,sp
mov es,[bx+4]
mov ah,#$49
int $21
jc err
mov ax,#0
err:
pop es
#endasm
}
#endif
#ifdef L_dos_setvect
void
__setvect(i,j)
int i;
long j;
{
#asm
mov bx,sp
mov ax,[bx+2]
mov dx,[bx+4]
mov bx,[bx+6]
push ds
test bx,bx
jnz got_seg
mov bx,cs
got_seg:
mov ds,bx
mov ah,#$25
int $21
pop ds
#endasm
}
#endif
#ifdef L_dos_getvect
long
__getvect(vecno)
int vecno;
{
#asm
mov bx,sp
mov ax,[bx+2]
mov ah,#$35
push es
int #$21
mov dx,es
mov ax,bx
pop es
#endasm
}
#endif
#ifdef L_dos_getmod
int
__dos_getmod(fname)
{
#asm
#if __FIRST_ARG_IN_AX__
mov dx,ax
#else
mov bx,sp
mov dx,[bx+2]
#endif
mov ax,#$4300
int #$21
jnc statok
br exterror
statok:
mov ax,cx
#endasm
}
#endif
#ifdef L_dos_stat
int
__dos_stat(fname, dtaptr)
{
#asm
mov bx,sp
#if __FIRST_ARG_IN_AX__
mov cx,ax
mov dx,[bx+2]
#else
mov dx,[bx+4]
#endif
mov ah,#$1A ; Set DTA to requested
int #$21
#if __FIRST_ARG_IN_AX__
mov ax,cx
#else
mov dx,[bx+2]
#endif
mov ax,#$4300 ; Locate the file
int #$21
jc nonesuch
mov ax,#$4e00 ; Get all the available information.
int #$21
jc nonesuch
xor ax,ax
ret
nonesuch:
mov ax,#2
mov _errno,ax
mov ax,#-1
#endasm
}
#endif
#ifdef L_dos_isatty
isatty(fd)
int fd;
{
#asm
mov bx,sp
mov bx,[bx+2]
mov ah,#$44
mov al,#0
int #$21
xor ax,ax
test dx,#$80
jz not_tty
inc ax
not_tty:
#endasm
}
#endif
#ifdef L_dos_abort
abort()
{
write(2, "Abnormal program termination\r\n", 30);
_exit(3);
}
#endif
#ifdef L_bdos
bdos(dosfn, dosdx, dosal)
int dosfn;
unsigned dosdx, dosal;
{
#asm
_bdosptr = _bdos
mov bx,sp
push si
push di
mov dx,_bdos.dosdx[bx]
mov cx,_bdos.dosfn[bx]
mov ax,_bdos.dosal[bx]
mov ah,cl
int $21
pop di
pop si
#endasm
}
#endif
#endif /* __MSDOS__ */
#endif /* __AS386_16__ */
#endif /* !__FIRST_ARG_IN_AX__ */
/*
# Name No Args Flag, comment
CHDIR 12 1
TIME 13 1 * No long return val, arg _must_ exist.
MKNOD 14 3
CHMOD 15 2
CHOWN 16 3
BRK 17 1 * This is only to tell the system
STAT 18 2
GETPID 20 1 * This gets both pid & ppid
MOUNT 21 3 * Need more args & no ELKS
UMOUNT 22 1 . No ELKS
SETUID 23 1
GETUID 24 1 * This gets both uid and euid
STIME 25 2 . No ELKS should be 1 LONG arg
PTRACE 26 X +
ALARM 27 2 ? No unused return.
FSTAT 28 2
PAUSE 29 0
UTIME 30 2
STTY 31 2 . ELKS ain't got this and it'll probably change
GTTY 32 2 . ELKS ain't got this and it'll probably change
ACCESS 33 2
NICE 34 1
FTIME 35 1 . ELKS ain't got this.
SYNC 36 0
KILL 37 2
RENAME 38 2
MKDIR 39 2
RMDIR 40 1
DUP 41 X - Using nasty fcntl function
PIPE 42 1
TIMES 43 1
PROF 44 X +
SETGID 46 1
GETGID 47 1 * This gets both gid and egid
SIGNAL 48 2 +
ACCT 51 X +
PLOCK 53 X +
IOCTL 54 3
FCNTL 55 3
EXEC 59 2 ?
UMASK 60 1
CHROOT 61 1
SIGACTION 71 X
SIGSUSPEND 72 X
SIGPENDING 73 X
SIGPROCMASK 74 X
SIGRETURN 75 X
REBOOT 76 3 . No ELKS and the magic number will be diff.
*/
|