From e62b35169cdcd13632ae353b1e5ffde7dec44201 Mon Sep 17 00:00:00 2001 From: Robert de Bath Date: Sat, 23 Jan 1999 13:29:22 +0100 Subject: Import Dev86src-0.14.7.tar.gz --- libc/Makefile | 14 +- libc/Pre_main | 2 +- libc/README | 3 +- libc/bcc/__ldivmod.c | 40 ++--- libc/bcc/bcc_bsw.c | 82 +++++----- libc/bcc/bcc_int.c | 32 ++-- libc/bcc/bcc_io.c | 34 ++--- libc/bcc/bcc_long.c | 58 +++---- libc/bios/Makefile | 4 +- libc/bios/bios.c | 81 +++++----- libc/bios/bios_vid.c | 4 +- libc/bios/cprintf.c | 128 ++++++++++++++++ libc/bios/fileops.c | 168 ++++++++++++++++++++ libc/bios/fs_dos.c | 421 +++++++++++++++++++++++++++++++++++++++++++++++++++ libc/bios/io.h | 22 +++ libc/bios/rawio.c | 327 +++++++++++++++++++++++++++++++++++++++ libc/bios/rawio.h | 16 ++ libc/include/bios.h | 39 +++++ libc/include/dos.h | 21 +-- libc/msdos/TODO | 2 + libc/msdos/msdos.c | 4 + libc/stdio2/printf.c | 6 +- libc/stdio2/scanf.c | 15 ++ libc/stdio2/stdio.c | 16 +- 24 files changed, 1333 insertions(+), 206 deletions(-) create mode 100644 libc/bios/cprintf.c create mode 100644 libc/bios/fileops.c create mode 100644 libc/bios/fs_dos.c create mode 100644 libc/bios/io.h create mode 100644 libc/bios/rawio.c create mode 100644 libc/bios/rawio.h create mode 100644 libc/include/bios.h (limited to 'libc') diff --git a/libc/Makefile b/libc/Makefile index 640c468..2e1ffd3 100644 --- a/libc/Makefile +++ b/libc/Makefile @@ -11,7 +11,7 @@ endif VERMAJOR=0 VERMINOR=14 -VERPATCH=5 +VERPATCH=7 VER=$(VERMAJOR).$(VERMINOR).$(VERPATCH) CC=bcc @@ -76,13 +76,11 @@ clean: ############################################################################ install_incl: - # rm -rf $(BCCHOME)/include - rm -rf $(BCCHOME)/include/linuxmt $(BCCHOME)/include/arch - cp -pr include $(BCCHOME)/include - if [ -f kinclude/Used ] ; \ - then cp -pr kinclude/arch $(BCCHOME)/include/arch ; \ - cp -pr kinclude/linuxmt $(BCCHOME)/include/linuxmt ; \ - else rm -rf $(BCCHOME)/include/linuxmt $(BCCHOME)/include/arch ; \ + install -d $(BCCHOME)/include + rm -f $(BCCHOME)/include/linuxmt $(BCCHOME)/include/arch ||: + cp -pr include/* $(BCCHOME)/include + if [ ! -f kinclude/Used ] ; \ + then rm -rf $(BCCHOME)/include/linuxmt $(BCCHOME)/include/arch ; \ ln -s $(ELKSSRC)/include/linuxmt $(BCCHOME)/include ; \ ln -s $(ELKSSRC)/include/arch $(BCCHOME)/include ; \ fi diff --git a/libc/Pre_main b/libc/Pre_main index 0b3bec2..f0a5b83 100644 --- a/libc/Pre_main +++ b/libc/Pre_main @@ -1,5 +1,5 @@ -There is now support for calling functions before main and from inside the +There is support for calling functions before main and from inside the exit() function. The exit processing uses the standard 'atexit' and 'on_exit' functions diff --git a/libc/README b/libc/README index 6f45f26..653ed06 100644 --- a/libc/README +++ b/libc/README @@ -12,7 +12,8 @@ are in this version of the combined development environment. Some other versions will work but often they'll just appear to work or not work at all. The original bcc-cc1 won't pickup the right header files, the original ld86 won't generate COM files or 386-Linux files and looks in -the wrong place for crt0.o and libc.a. +the wrong place for crt0.o and libc.a. The original as is just plain +broken! Main Subdirectories. diff --git a/libc/bcc/__ldivmod.c b/libc/bcc/__ldivmod.c index 596d022..359c692 100644 --- a/libc/bcc/__ldivmod.c +++ b/libc/bcc/__ldivmod.c @@ -8,35 +8,35 @@ .text .even -| ldivmod.s - 32 over 32 to 32 bit division and remainder for 8086 +! ldivmod.s - 32 over 32 to 32 bit division and remainder for 8086 -| ldivmod( dividend bx:ax, divisor di:cx ) [ signed quot di:cx, rem bx:ax ] -| ludivmod( dividend bx:ax, divisor di:cx ) [ unsigned quot di:cx, rem bx:ax ] +! ldivmod( dividend bx:ax, divisor di:cx ) [ signed quot di:cx, rem bx:ax ] +! ludivmod( dividend bx:ax, divisor di:cx ) [ unsigned quot di:cx, rem bx:ax ] -| dx is not preserved +! dx is not preserved -| NB negatives are handled correctly, unlike by the processor -| divison by zero does not trap +! NB negatives are handled correctly, unlike by the processor +! divison by zero does not trap -| let dividend = a, divisor = b, quotient = q, remainder = r -| a = b * q + r mod 2^32 -| where: +! let dividend = a, divisor = b, quotient = q, remainder = r +! a = b * q + r mod 2^32 +! where: -| if b = 0, q = 0 and r = a +! if b = 0, q = 0 and r = a -| otherwise, q and r are uniquely determined by the requirements: -| r has the same sign as b and absolute value smaller than that of b, i.e. -| if b > 0, then 0 <= r < b -| if b < 0, then 0 >= r > b -| (the absoulute value and its comparison depend on signed/unsigned) +! otherwise, q and r are uniquely determined by the requirements: +! r has the same sign as b and absolute value smaller than that of b, i.e. +! if b > 0, then 0 <= r < b +! if b < 0, then 0 >= r > b +! (the absoulute value and its comparison depend on signed/unsigned) -| the rule for the sign of r means that the quotient is truncated towards -| negative infinity in the usual case of a positive divisor +! the rule for the sign of r means that the quotient is truncated towards +! negative infinity in the usual case of a positive divisor -| if the divisor is negative, the division is done by negating a and b, -| doing the division, then negating q and r +! if the divisor is negative, the division is done by negating a and b, +! doing the division, then negating q and r .globl ldivmod @@ -105,7 +105,7 @@ divlarge: cmp dx,ax ja zdivu -| rotate w (= b) to greatest dyadic multiple of b <= r +! rotate w (= b) to greatest dyadic multiple of b <= r loop1: shl dx,*1 ! w = 2*w diff --git a/libc/bcc/bcc_bsw.c b/libc/bcc/bcc_bsw.c index a80a679..1693f5a 100644 --- a/libc/bcc/bcc_bsw.c +++ b/libc/bcc/bcc_bsw.c @@ -18,7 +18,7 @@ #ifdef L___laddb #asm -| laddb.s +! laddb.s .globl laddb .globl laddub @@ -37,7 +37,7 @@ laddub: #ifdef L___landb #asm -| landb.s +! landb.s .globl landb .globl landub @@ -56,15 +56,15 @@ landub: #ifdef L___lcmpb #asm -| lcmpb.s -| lcmpb, lcmpub don`t preserve ax +! lcmpb.s +! lcmpb, lcmpub don`t preserve ax .globl lcmpb .globl lcmpub lcmpb: lcmpub: - sub ax,(di) | don`t need to preserve ax + sub ax,(di) ! don`t need to preserve ax je LCMP_NOT_SURE ret @@ -72,17 +72,17 @@ lcmpub: LCMP_NOT_SURE: cmp bx,2(di) - jb LCMP_B_AND_LT | b (below) becomes lt (less than) as well - jge LCMP_EXIT | ge and already ae - | else make gt as well as a (above) - inc ax | clear ov and mi, set ne for greater than + jb LCMP_B_AND_LT ! b (below) becomes lt (less than) as well + jge LCMP_EXIT ! ge and already ae + ! else make gt as well as a (above) + inc ax ! clear ov and mi, set ne for greater than LCMP_EXIT: ret .even LCMP_B_AND_LT: - dec ax | clear ov, set mi and ne for less than + dec ax ! clear ov, set mi and ne for less than ret #endasm #endif @@ -93,7 +93,7 @@ LCMP_B_AND_LT: #ifdef L___lcomb #asm -| lcomb.s +! lcomb.s .globl lcomb .globl lcomub @@ -112,7 +112,7 @@ lcomub: #ifdef L___ldecb #asm -| ldecb.s +! ldecb.s .globl ldecb .globl ldecub @@ -139,8 +139,8 @@ LDEC_BOTH: #ifdef L___ldivb #asm -| ldivb.s -| ax:bx / (di):2(di), quotient ax:bx, remainder cx:di, dx not preserved +! ldivb.s +! ax:bx / (di):2(di), quotient ax:bx, remainder cx:di, dx not preserved .globl ldivb .extern ldivmod @@ -149,7 +149,7 @@ ldivb: xchg ax,bx mov cx,2(di) mov di,(di) - call ldivmod | bx:ax / di:cx, quot di:cx, rem bx:ax + call ldivmod ! bx:ax / di:cx, quot di:cx, rem bx:ax xchg ax,di xchg bx,cx ret @@ -162,8 +162,8 @@ ldivb: #ifdef L___ldivub #asm -| ldivub.s -| unsigned ax:bx / (di):2(di), quotient ax:bx,remainder cx:di, dx not preserved +! ldivub.s +! unsigned ax:bx / (di):2(di), quotient ax:bx,remainder cx:di, dx not preserved .globl ldivub .extern ludivmod @@ -172,7 +172,7 @@ ldivub: xchg ax,bx mov cx,2(di) mov di,(di) - call ludivmod | unsigned bx:ax / di:cx, quot di:cx, rem bx:ax + call ludivmod ! unsigned bx:ax / di:cx, quot di:cx, rem bx:ax xchg ax,di xchg bx,cx ret @@ -185,7 +185,7 @@ ldivub: #ifdef L___leorb #asm -| leorb.s +! leorb.s .globl leorb .globl leorub @@ -204,7 +204,7 @@ leorub: #ifdef L___lincb #asm -| lincb.s +! lincb.s .globl lincb .globl lincub @@ -229,8 +229,8 @@ LINC_HIGH_WORD: #ifdef L___lmodb #asm -| lmodb.s -| ax:bx % (di):2(di), remainder ax:bx, quotient cx:di, dx not preserved +! lmodb.s +! ax:bx % (di):2(di), remainder ax:bx, quotient cx:di, dx not preserved .globl lmodb .extern ldivmod @@ -239,7 +239,7 @@ lmodb: xchg ax,bx mov cx,2(di) mov di,(di) - call ldivmod | bx:ax / di:cx, quot di:cx, rem bx:ax + call ldivmod ! bx:ax / di:cx, quot di:cx, rem bx:ax xchg ax,bx xchg cx,di ret @@ -252,8 +252,8 @@ lmodb: #ifdef L___lmodub #asm -| lmodub.s -| unsigned ax:bx / (di):2(di), remainder ax:bx,quotient cx:di, dx not preserved +! lmodub.s +! unsigned ax:bx / (di):2(di), remainder ax:bx,quotient cx:di, dx not preserved .globl lmodub .extern ludivmod @@ -262,7 +262,7 @@ lmodub: xchg ax,bx mov cx,2(di) mov di,(di) - call ludivmod | unsigned bx:ax / di:cx, quot di:cx, rem bx:ax + call ludivmod ! unsigned bx:ax / di:cx, quot di:cx, rem bx:ax xchg ax,bx xchg cx,di ret @@ -275,8 +275,8 @@ lmodub: #ifdef L___lmulb #asm -| lmulb.s -| lmulb, lmulub don`t preserve cx, dx +! lmulb.s +! lmulb, lmulub don`t preserve cx, dx .globl lmulb .globl lmulub @@ -302,7 +302,7 @@ lmulub: #ifdef L___lnegb #asm -| lnegb.s +! lnegb.s .globl lnegb .globl lnegub @@ -322,7 +322,7 @@ lnegub: #ifdef L___lorb #asm -| lorb.s +! lorb.s .globl lorb .globl lorub @@ -341,8 +341,8 @@ lorub: #ifdef L___lslb #asm -| lslb.s -| lslb, lslub don`t preserve cx +! lslb.s +! lslb, lslub don`t preserve cx .globl lslb .globl lslub @@ -375,8 +375,8 @@ LSL_ZERO: #ifdef L___lsrb #asm -| lsrb.s -| lsrb doesn`t preserve cx +! lsrb.s +! lsrb doesn`t preserve cx .globl lsrb @@ -395,7 +395,7 @@ LSR_EXIT: .even LSR_SIGNBIT: - mov cx,*32 | equivalent to +infinity in this context + mov cx,*32 ! equivalent to +infinity in this context j LSR_LOOP #endasm #endif @@ -406,8 +406,8 @@ LSR_SIGNBIT: #ifdef L___lsrub #asm -| lsrub.s -| lsrub doesn`t preserve cx +! lsrub.s +! lsrub doesn`t preserve cx .globl lsrub @@ -438,7 +438,7 @@ LSRU_ZERO: #ifdef L___lsubb #asm -| lsubb.s +! lsubb.s .globl lsubb .globl lsubub @@ -457,8 +457,8 @@ lsubub: #ifdef L___ltstb #asm -| ltstb.s -| ltstb, ltstub don`t preserve ax +! ltstb.s +! ltstb, ltstub don`t preserve ax .globl ltstb .globl ltstub @@ -479,7 +479,7 @@ LTST_NOT_SURE: .even LTST_FIX_SIGN: - inc ax | clear ov and mi, set ne for greater than + inc ax ! clear ov and mi, set ne for greater than ret #endasm #endif diff --git a/libc/bcc/bcc_int.c b/libc/bcc/bcc_int.c index 028a3f1..ae02a8a 100644 --- a/libc/bcc/bcc_int.c +++ b/libc/bcc/bcc_int.c @@ -17,8 +17,8 @@ #ifdef L___idiv #asm -| idiv.s -| idiv_ doesn`t preserve dx (returns remainder in it) +! idiv.s +! idiv_ doesn`t preserve dx (returns remainder in it) .globl idiv_ @@ -35,8 +35,8 @@ idiv_: #ifdef L___idivu #asm -| idivu.s -| idiv_u doesn`t preserve dx (returns remainder in it) +! idivu.s +! idiv_u doesn`t preserve dx (returns remainder in it) .globl idiv_u @@ -53,8 +53,8 @@ idiv_u: #ifdef L___imod #asm -| imod.s -| imod doesn`t preserve dx (returns quotient in it) +! imod.s +! imod doesn`t preserve dx (returns quotient in it) .globl imod @@ -72,8 +72,8 @@ imod: #ifdef L___imodu #asm -| imodu.s -| imodu doesn`t preserve dx (returns quotient in it) +! imodu.s +! imodu doesn`t preserve dx (returns quotient in it) .globl imodu @@ -91,8 +91,8 @@ imodu: #ifdef L___imul #asm -| imul.s -| imul_, imul_u don`t preserve dx +! imul.s +! imul_, imul_u don`t preserve dx .globl imul_ .globl imul_u @@ -110,8 +110,8 @@ imul_u: #ifdef L___isl #asm -| isl.s -| isl, islu don`t preserve cl +! isl.s +! isl, islu don`t preserve cl .globl isl .globl islu @@ -130,8 +130,8 @@ islu: #ifdef L___isr #asm -| isr.s -| isr doesn`t preserve cl +! isr.s +! isr doesn`t preserve cl .globl isr @@ -148,8 +148,8 @@ isr: #ifdef L___isru #asm -| isru.s -| isru doesn`t preserve cl +! isru.s +! isru doesn`t preserve cl .globl isru diff --git a/libc/bcc/bcc_io.c b/libc/bcc/bcc_io.c index d7af86f..8f7285e 100644 --- a/libc/bcc/bcc_io.c +++ b/libc/bcc/bcc_io.c @@ -19,8 +19,8 @@ #ifdef L___inport #asm -| int inport( int port ); -| reads a word from the i/o port port and returns it +! int inport( int port ); +! reads a word from the i/o port port and returns it .globl _inport _inport: @@ -39,8 +39,8 @@ _inport: #ifdef L___inportb #asm -| int inportb( int port ); -| reads a byte from the i/o port port and returns it +! int inportb( int port ); +! reads a byte from the i/o port port and returns it .globl _inportb _inportb: @@ -60,8 +60,8 @@ _inportb: #ifdef L___outport #asm -| void outport( int port, int value ); -| writes the word value to the i/o port port +! void outport( int port, int value ); +! writes the word value to the i/o port port .globl _outport _outport: @@ -80,9 +80,9 @@ _outport: #ifdef L___outportb #asm -| void oportb( int port, char value ); -| writes the byte value to the i/o port port -| this would be outportb except for feeble linkers +! void oportb( int port, char value ); +! writes the byte value to the i/o port port +! this would be outportb except for feeble linkers .globl _oportb _oportb: @@ -101,8 +101,8 @@ _oportb: #ifdef L___peekb #asm -| int peekb( unsigned segment, char *offset ); -| returns the (unsigned) byte at the far pointer segment:offset +! int peekb( unsigned segment, char *offset ); +! returns the (unsigned) byte at the far pointer segment:offset .define _peekb _peekb: @@ -124,8 +124,8 @@ _peekb: #ifdef L___peekw #asm -| int peekw( unsigned segment, int *offset ); -| returns the word at the far pointer segment:offset +! int peekw( unsigned segment, int *offset ); +! returns the word at the far pointer segment:offset .define _peekw _peekw: @@ -146,8 +146,8 @@ _peekw: #ifdef L___pokeb #asm -| void pokeb( unsigned segment, char *offset, char value ); -| writes the byte value at the far pointer segment:offset +! void pokeb( unsigned segment, char *offset, char value ); +! writes the byte value at the far pointer segment:offset .define _pokeb _pokeb: @@ -169,8 +169,8 @@ _pokeb: #ifdef L___pokew #asm -| void pokew( unsigned segment, int *offset, int value ); -| writes the word value at the far pointer segment:offset +! void pokew( unsigned segment, int *offset, int value ); +! writes the word value at the far pointer segment:offset .define _pokew _pokew: diff --git a/libc/bcc/bcc_long.c b/libc/bcc/bcc_long.c index 4d1f7d8..3d59457 100644 --- a/libc/bcc/bcc_long.c +++ b/libc/bcc/bcc_long.c @@ -19,7 +19,7 @@ #ifdef L___laddl #asm -| laddl.s +! laddl.s .globl laddl .globl laddul @@ -38,7 +38,7 @@ laddul: #ifdef L___landl #asm -| landl.s +! landl.s .globl landl .globl landul @@ -57,8 +57,8 @@ landul: #ifdef L___lcmpl #asm -| lcmpl.s -| lcmpl, lcmpul don`t preserve bx +! lcmpl.s +! lcmpl, lcmpul don`t preserve bx .globl lcmpl .globl lcmpul @@ -94,7 +94,7 @@ LCMP_B_AND_LT: #ifdef L___lcoml #asm -| lcoml.s +! lcoml.s .globl lcoml .globl lcomul @@ -113,7 +113,7 @@ lcomul: #ifdef L___ldecl #asm -| ldecl.s +! ldecl.s .globl ldecl .globl ldecul @@ -140,8 +140,8 @@ LDEC_BOTH: #ifdef L___ldivl #asm -| ldivl.s -| bx:ax / 2(di):(di), quotient bx:ax, remainder di:cx, dx not preserved +! ldivl.s +! bx:ax / 2(di):(di), quotient bx:ax, remainder di:cx, dx not preserved .globl ldivl .extern ldivmod @@ -163,8 +163,8 @@ ldivl: #ifdef L___ldivul #asm -| ldivul.s -| unsigned bx:ax / 2(di):(di), quotient bx:ax,remainder di:cx, dx not preserved +! ldivul.s +! unsigned bx:ax / 2(di):(di), quotient bx:ax,remainder di:cx, dx not preserved .globl ldivul .extern ludivmod @@ -185,7 +185,7 @@ ldivul: #ifdef L___leorl #asm -| leorl.s +! leorl.s .globl leorl .globl leorul @@ -204,7 +204,7 @@ leorul: #ifdef L___lincl #asm -| lincl.s +! lincl.s .globl lincl .globl lincul @@ -229,8 +229,8 @@ LINC_HIGH_WORD: #ifdef L___lmodl #asm -| lmodl.s -| bx:ax % 2(di):(di), remainder bx:ax, quotient di:cx, dx not preserved +! lmodl.s +! bx:ax % 2(di):(di), remainder bx:ax, quotient di:cx, dx not preserved .globl lmodl .extern ldivmod @@ -249,8 +249,8 @@ lmodl: #ifdef L___lmodul #asm -| lmodul.s -| unsigned bx:ax / 2(di):(di), remainder bx:ax,quotient di:cx, dx not preserved +! lmodul.s +! unsigned bx:ax / 2(di):(di), remainder bx:ax,quotient di:cx, dx not preserved .globl lmodul .extern ludivmod @@ -269,8 +269,8 @@ lmodul: #ifdef L___lmull #asm -| lmull.s -| lmull, lmulul don`t preserve cx, dx +! lmull.s +! lmull, lmulul don`t preserve cx, dx .globl lmull .globl lmulul @@ -295,7 +295,7 @@ lmulul: #ifdef L___lnegl #asm -| lnegl.s +! lnegl.s .globl lnegl .globl lnegul @@ -315,7 +315,7 @@ lnegul: #ifdef L___lorl #asm -| lorl.s +! lorl.s .globl lorl .globl lorul @@ -334,8 +334,8 @@ lorul: #ifdef L___lsll #asm -| lsll.s -| lsll, lslul don`t preserve cx +! lsll.s +! lsll, lslul don`t preserve cx .globl lsll .globl lslul @@ -368,8 +368,8 @@ LSL_ZERO: #ifdef L___lsrl #asm -| lsrl.s -| lsrl doesn`t preserve cx +! lsrl.s +! lsrl doesn`t preserve cx .globl lsrl @@ -399,8 +399,8 @@ LSR_SIGNBIT: #ifdef L___lsrul #asm -| lsrul.s -| lsrul doesn`t preserve cx +! lsrul.s +! lsrul doesn`t preserve cx .globl lsrul @@ -431,7 +431,7 @@ LSRU_ZERO: #ifdef L___lsubl #asm -| lsubl.s +! lsubl.s .globl lsubl .globl lsubul @@ -450,8 +450,8 @@ lsubul: #ifdef L___ltstl #asm -| ltstl.s -| ltstl, ltstul don`t preserve bx +! ltstl.s +! ltstl, ltstul don`t preserve bx .globl ltstl .globl ltstul diff --git a/libc/bios/Makefile b/libc/bios/Makefile index b617e50..8436846 100644 --- a/libc/bios/Makefile +++ b/libc/bios/Makefile @@ -4,13 +4,13 @@ ifeq ($(LIB_OS),BIOS) ASRC=bios.c -AOBJ=bios_start.o bios_isatty.o \ +AOBJ=bios_start.o bios_isatty.o bios_nofiles.o \ bios_open.o bios_read.o bios_write.o bios_lseek.o bios_close.o BSRC=bios_vid.c BOBJ=bios_putc.o bios_getc.o bios_khit.o bios_rdline.o -OBJ=$(AOBJ) $(BOBJ) time.o +OBJ=$(AOBJ) $(BOBJ) time.o fileops.o fs_dos.o rawio.o CFLAGS=$(ARCH) $(CCFLAGS) $(DEFS) diff --git a/libc/bios/bios.c b/libc/bios/bios.c index a8d2079..2290472 100644 --- a/libc/bios/bios.c +++ b/libc/bios/bios.c @@ -7,13 +7,14 @@ #ifdef __AS386_16__ #ifdef __STANDALONE__ -#include +#include #include #include -int errno; #ifdef L_bios_start + char ** environ = { 0 }; +int errno; void (*__cleanup)() = 0; @@ -119,27 +120,6 @@ reti_ins: /****************************************************************************/ -#ifdef L___file_3 - -/* If the block function does track buffering this should be ok ... */ -struct { - int (*block_rw)(); /* Args (rwoc, &buffer, blockno) 1k blocks */ - /* 0 = read, 1 = write */ - /* 2 = open, buffer is fname ptr */ - /* 3 = close, other args ignored */ - long offset; - - int flags; - long block_num; - char buffer[1024]; -} __file_3_data; - -#define FILE3_OPEN 1 /* File is open */ -#define FILE3_DATA 2 /* buffer has valid contents */ -#define FILE3_DIRTY 4 /* buffer has been modified */ - -#endif - #ifdef L_bios_write write(fd,buf,len) int fd,len; @@ -155,9 +135,8 @@ char * buf; bios_putc(c); } return len; - } - errno = EBADF; - return -1; + } + return (*__files)(CMD_WRITE, fd, buf, len); } #endif @@ -169,8 +148,7 @@ int fd,len; char * buf; { if(fd == 0) return bios_rdline(buf, len); - errno = EBADF; - return -1; + return (*__files)(CMD_READ, fd, buf, len); } #endif @@ -183,33 +161,41 @@ int fd, whence; long offt; { if( fd >= 0 && fd <= 2 ) errno = ESPIPE; - else errno = EBADF; + else + { + if( (*__files)(CMD_LSEEK, fd, &offt, whence) >= 0 ) + return offt; + } return -1L; } #endif /****************************************************************************/ -#ifdef L_bios_open -open(name, flags, mode) -char * name; -int flags, mode; +#ifdef L_bios_close +close(fd) +int fd; { - errno = ENOENT; + if( fd >= 0 && fd <= 2 ) errno = ENOSYS; + else + return (*__files)(CMD_CLOSE, fd); return -1; } #endif /****************************************************************************/ -#ifdef L_bios_close -close(fd) -int fd; -{ - if( fd >= 0 && fd <= 2 ) errno = ENOSYS; - else errno = EBADF; +#ifdef L_bios_nofiles +int (*__files)() = __nofiles; + +int __nofiles(cmd, fd, buf, len) +int cmd, fd, len; +char * buf; +{ + errno = EBADF; return -1; } + #endif /****************************************************************************/ @@ -225,6 +211,21 @@ int fd; /****************************************************************************/ +#ifdef L_bios_open +extern int __fileops(); + +open(name, flags, mode) +char * name; +int flags, mode; +{ + __files = __fileops; + return (*__files)(CMD_OPEN, flags, name, mode); +} + +#endif + +/****************************************************************************/ + #endif #endif #endif diff --git a/libc/bios/bios_vid.c b/libc/bios/bios_vid.c index cc49736..8bf375a 100644 --- a/libc/bios/bios_vid.c +++ b/libc/bios/bios_vid.c @@ -4,7 +4,7 @@ */ /* Various possible console types */ -#define VT52_CON /* IMO the best, no clear to EOS/EOL yet */ +#define VT52_CON /* IMO the best, no clear to EOS/EOL tho */ #define XANSI_CON /* Largest but still not complete */ #define XDUMB_CON /* Can't do much */ #define XSPEC_CON /* Incomplete, best for slow links */ @@ -13,7 +13,7 @@ #ifdef __AS386_16__ #ifdef __STANDALONE__ -#include +#include #include int errno; diff --git a/libc/bios/cprintf.c b/libc/bios/cprintf.c new file mode 100644 index 0000000..5f8beaa --- /dev/null +++ b/libc/bios/cprintf.c @@ -0,0 +1,128 @@ + +#include +#define wchar(ch) putch(ch) + +cprintf(char * fmt, va_list ap) +{ + register int c; + int count = 0; + int type, base; + long val; + char * cp; + char padch=' '; + int minsize = 0; + + while(c=*fmt++) + { + if(c!='%') + { + wchar(c); + count++; + } + else + { + type=1; + do { c=*fmt++; } while( c>='0' && c<='9'); + + padch = *fmt; + minsize=0; + if(padch == '-') fmt++; + + for(;;) + { + c=*fmt++; + if( c<'0' || c>'9' ) break; + minsize*=10; minsize+=c-'0'; + } + + while( c=='.' || (c>='0' && c<='9')) { c=*fmt++; } + + if( padch == '-' ) minsize = -minsize; + else + if( padch == '0' ) padch='0'; else padch=' '; + + if( c == 0 ) break; + if(c=='h') + { + c=*fmt++; + type = 0; + } + else if(c=='l') + { + c=*fmt++; + type = 2; + } + + switch(c) + { + case 'x': base=16; if(0) { + case 'o': base= 8; } if(0) { + case 'd': base=10; } + val=0; + switch(type) + { + case 0: val=va_arg(ap, short); break; + case 1: val=va_arg(ap, int); break; + case 2: val=va_arg(ap, long); break; + } + cp = __numout(val,base); + if(0) { + case 's': + cp=va_arg(ap, char *); + } + if( minsize > 0 ) + { + minsize -= strlen(cp); + while(minsize>0) { wchar(padch); minsize--; } + minsize=0; + } + if( minsize < 0 ) minsize= -minsize-strlen(cp); + while(*cp) + wchar(*cp++); + while(minsize>0) { wchar(' '); minsize--; } + break; + case 'c': + wchar(va_arg(ap, int)); + break; + default: + wchar(c); + break; + } + } + } + return count; +} + + + +static char nstring[]="0123456789ABCDEF"; + +static unsigned char * +__numout(long i, int base) +{ + static unsigned char out[16]; + int n; + int flg = 0; + unsigned long val; + + if (i<0 && base==10) + { + flg = 1; + i = -i; + } + val = i; + + for (n = 0; n < 15; n++) + out[n] = ' '; + out[15] = '\0'; + n = 14; + do + { + out[n] = nstring[val % base]; + n--; + val /= base; + } + while(val); + if(flg) out[n--] = '-'; + return &out[n+1]; +} diff --git a/libc/bios/fileops.c b/libc/bios/fileops.c new file mode 100644 index 0000000..5c71a37 --- /dev/null +++ b/libc/bios/fileops.c @@ -0,0 +1,168 @@ + +#include +#include +#include + +#include "io.h" +#include "rawio.h" + +static int op_open(); +static int op_close(); +static int op_read(); +static int op_write(); +static long op_lseek(); + +int +__fileops(cmd, fd, buf, len) +int cmd, fd, len; +char * buf; +{ + switch(cmd) + { + case CMD_OPEN: return op_open(buf, fd, len); + case CMD_READ: return op_read(fd, buf, len); +/* + case CMD_WRITE: return op_write(fd, buf, len); + case CMD_LSEEK: rv = op_lseek(fd, *(long*)buf, len); + (*(long*)buf) = rv; + if( rv == -1 ) return -1; + else return 0; + */ + case CMD_CLOSE: return op_close(fd); + } + + errno=EINVAL; + return -1; +} + +#define MAX_OPEN_FILES 5 +ioblock _iob[MAX_OPEN_FILES]; + +static int +op_read(fd,buf,len) +int fd,len; +char * buf; +{ + ioblock* cur = &_iob[fd]; + int amount_read = 0; + int amount_left_in_buffer; + int amount_to_copy; + + if (fd >= MAX_OPEN_FILES || _iob[fd].block_read == 0) + { + errno = EBADF; + return -1; + } + + while (len > 0) + { + /* pull in next block as required */ + if (cur->amount_left <= 0) + { + int read_len = cur->block_read(cur, + cur->buffer, + (long) cur->offset / sizeof(cur->buffer) + ); +#ifdef DEBUG + fprintf(stderr, "br: returned %d\n", read_len); +#endif + if (read_len <= 0) + break; + cur->amount_left = read_len; + } + if (cur->amount_left > len) + amount_to_copy = len; + else + amount_to_copy = cur->amount_left; + +#ifdef DEBUG + fprintf(stderr, "r: len=%d, amount_left=%ld, offset=%ld, buf=%x\n", + len, cur->amount_left, cur->offset, + (int) cur->buffer[cur->offset % sizeof(cur->buffer)]); +#endif + memcpy(buf, + &cur->buffer[cur->offset % sizeof(cur->buffer)], + amount_to_copy); + amount_read += amount_to_copy; + len -= amount_to_copy; + cur->offset += amount_to_copy; + buf += amount_to_copy; + cur->amount_left -= amount_to_copy; + } + return amount_read; +} + +/****************************************************************************/ + +static int +op_open(name, flags, mode) +char * name; +int flags, mode; +{ + int fd; + ioblock* cur; + + /* + * discover whether the iob has been initialised or not + */ + if (_iob[0].flags == 0) + { + _iob[0].flags = O_RDONLY; + _iob[1].flags = O_WRONLY; + _iob[2].flags = O_WRONLY; + } + /* + * discover next free iob + */ + for (fd = 3; fd < MAX_OPEN_FILES; ++fd) + { + if (_iob[fd].block_read == NULL && _iob[fd].block_write == NULL) + break; + } + if (fd >= MAX_OPEN_FILES) + { + errno = EMFILE; /* too many open files */ + return -1; + } + + /* + * try and find the file + */ + cur = &_iob[fd]; + if (fsdos_open_file(cur, name, flags, mode) >= 0) + { + cur->amount_left = 0; + cur->offset = 0; + return fd; + } + cur->block_read = NULL; /* ensure that the file is closed */ + cur->block_write = NULL; + errno = ENOENT; + return -1; +} + +/****************************************************************************/ + +static int +op_close(fd) +int fd; +{ + if (fd >= MAX_OPEN_FILES || _iob[0].flags == 0) + { + errno = EBADF; + return -1; + } + else + { + ioblock* cur = &_iob[fd]; + cur->close(cur); + cur->block_read = NULL; + cur->block_write = NULL; + cur->close = NULL; + cur->flags = 0; + return 0; + } +} + +/****************************************************************************/ + diff --git a/libc/bios/fs_dos.c b/libc/bios/fs_dos.c new file mode 100644 index 0000000..28ee962 --- /dev/null +++ b/libc/bios/fs_dos.c @@ -0,0 +1,421 @@ + +#include +#include +#include +#include +#include "io.h" +#include "rawio.h" + +#define DONT_BUFFER_FAT + +#define DOS_SECT(P) get_uint(P,0x0B) +#define DOS_CLUST(P) get_byte(P,0x0D) +#define DOS_RESV(P) get_uint(P,0x0E) +#define DOS_NFAT(P) get_byte(P,0x10) +#define DOS_NROOT(P) get_uint(P,0x11) +#define DOS_MAXSECT(P) get_uint(P,0x13) +#define DOS_MEDIA(P) get_byte(P,0x15) +#define DOS_FATLEN(P) get_uint(P,0x16) +#define DOS_SPT(P) get_uint(P,0x18) +#define DOS_HEADS(P) get_uint(P,0x1A) +#define DOS_HIDDEN(P) get_long(P,0x1C) +#define DOS4_MAXSECT(P) get_long(P,0x20) +#define DOS4_PHY_DRIVE(P) get_byte(P,0x24) +#define DOS4_SERIAL(P) get_long(P,0x27) + +/* These assume alignment is not a problem */ +#define get_byte(P,Off) *((unsigned char*)((char*)(P)+(Off))) +#define get_uint(P,Off) *((unsigned short*)((char*)(P)+(Off))) +#define get_long(P,Off) *((long*)((char*)(P)+(Off))) + +static int read_bootblock(); +static int dir_nentry, dir_sect; +static int dos_clust0, dos_spc, dos_fatpos; +static int last_serial = 0; + +#ifdef BUFFER_FAT +static char * fat_buf = 0; +#endif + +struct filestatus { + char fname[12]; + unsigned short first_cluster; + unsigned short cur_cluster; + unsigned short sector_no; + long file_length; +}; + +static int fsdos_read_block(); +static int fsdos_close_file(); + +/* buffer to read into */ +static char sect[512]; + +fsdos_open_file(iob, fname, flags, mode) +ioblock* iob; +char * fname; +int flags; +int mode; +{ + extern union REGS __argr; + char conv_name[12]; + char *d, *s; + int i; + int dodir = 0; + struct filestatus* cur_file; + +#ifdef DEBUG + printf("fsdos_open_file(%x, %s, %d, %d, %d)\n", + iob, fname, flags, mode, sizeof(iob)); +#endif + iob->block_read = fsdos_read_block; + iob->close = fsdos_close_file; + + /* Get the superblock */ + if( read_bootblock() < 0 ) return -1; + + if(strcmp(fname, ".") == 0) + dodir = 1; + else + { + /* Convert the name to MSDOS directory format */ + strcpy(conv_name, " "); + for(s=fname, d=conv_name; *s && *d && *s != '.' && *s != ' '; s++) + { + if( islower(*s) ) *d++ = toupper(*s); + else *d++ = *s; + } + while( *s && *s != '.' ) s++; + strcpy(d=(conv_name+8), " "); + if( *s == '.' ) + { + for(s++; *s && *d; s++) + { + if( islower(*s) ) *d++ = toupper(*s); + else *d++ = *s; + } + } + } +#ifdef DEBUG + printf("fsdos_open_file: converted filename=<%s>\n", conv_name); +#endif + +#ifdef BUFFER_FAT + rawio_read_sector(0, sect); + + if( !dodir ) + { + /* Read in and buffer the FAT */ + if( fat_buf ) free(fat_buf); + fat_buf = malloc(DOS_FATLEN(sect) * 512); + if( fat_buf == 0 ) + { + errno = ENOMEM; + return -1; + } + else + { + int fatsec = DOS_RESV(sect); + int nsec = DOS_FATLEN(sect); + + for(i=0; i>5)&0xF), + ((get_uint(d,24)>>9)&0x7F)+1980, + ((get_uint(d,22)>>11)&0x1F), + ((get_uint(d,22)>>5)&0x3F) + ); + if( *d > ' ' && *d <= '~' ) switch(d[11]&0x18) + { + case 0: + printf("%-8.8s %-3.3s %10ld%s\n", d, d+8, get_long(d,28), dtime); + break; + case 0x10: + printf("%-8.8s %-3.3s %s\n", d, d+8, dtime); + break; + case 8: + if( (d[11] & 7) == 0 ) + printf("%-11.11s %s\n", d, dtime); + break; + } +#if 0 + if( more_strn(lbuf, sizeof(lbuf)) < 0 ) break; +#endif + } + else if( memcmp(d, conv_name, 11) == 0 && (d[11]&0x18) == 0 ) + { /* Name matches and is normal file */ + +#ifdef DEBUG + fprintf(stderr, "dos_open_file: %s worked\n", fname); +#endif + cur_file = malloc(sizeof(*cur_file)); + + iob->context = cur_file; + if (iob->context == NULL) + { + errno = ENOMEM; + return -1; + } + memset(cur_file, '\0', sizeof(*cur_file)); + strcpy(cur_file->fname, conv_name); + cur_file->first_cluster = get_uint(d,26); + cur_file->file_length = get_long(d,28); + + cur_file->cur_cluster = cur_file->first_cluster; + cur_file->sector_no = 0; + + return 0; + } + } + return -1; +} + +fsdos_rewind_file(iob) +ioblock* iob; +{ + struct filestatus* cur_file; + + if (iob == NULL || iob->context == NULL) + { + errno = EBADF; + return -1; + } + + cur_file = (struct filestatus*) iob->context; + /* Is there an opened file ? */ + if( cur_file->fname[0] == 0 ) return -1; + + cur_file->sector_no = 0; + cur_file->cur_cluster = cur_file->first_cluster; + return 0; +} + +static int fsdos_close_file(iob) +ioblock* iob; +{ + struct filestatus* cur_file; + + if (iob == NULL || iob->context == NULL) + { + errno = EBADF; + return -1; + } + cur_file = (struct filestatus*) iob->context; + free(cur_file); + iob->context = NULL; + +#ifdef BUFFER_FAT + if( fat_buf ) free(fat_buf); + fat_buf = 0; +#endif + + rawio_reset_disk(); + return 0; +} + +long +fsdos_file_length(iob) +ioblock* iob; +{ + struct filestatus* cur_file; + + if (iob == NULL || iob->context == NULL) + { + errno = EBADF; + return -1; + } + cur_file = (struct filestatus*) iob->context; + + /* Is there an opened file ? */ + if( cur_file->fname[0] == 0 ) return -1; + + return cur_file->file_length; +} + +static int fsdos_read_block(iob, buffer, block) +ioblock* iob; +char * buffer; +long block; /* ignored for now */ +{ + int s; + char * ptr = buffer; + struct filestatus* cur_file; + long amount_left_in_file; + long amount_to_read; + +#ifdef DEBUG + fprintf(stderr, "rb: iob = %x, buf = %x, block = %ld\n", + iob, buffer, block); +#endif + if (iob == NULL || iob->context == NULL) + { + fprintf(stderr, "rb: no context\n"); + errno = EBADF; + return -1; + } + cur_file = (struct filestatus*) iob->context; + + /* Is there an opened file ? */ + if( cur_file->fname[0] == 0 ) + { +#ifdef DEBUG + fprintf(stderr, "fsdos_read_block: File is not currently open!\n"); +#endif + errno = EBADF; + return -1; + } + + amount_left_in_file = cur_file->file_length - block * 1024; + + if (amount_left_in_file < 0) + { +#ifdef DEBUG + fprintf(stderr, "EOF\n"); +#endif + return 0; + } + + /* Are we before the EOF ? NB: FAT12 ONLY! */ + if( cur_file->cur_cluster >= 0xFF0 || cur_file->cur_cluster < 2 ) + { +#ifdef DEBUG + fprintf(stderr, "Hit end of file; cluster 0x%03x\n", + cur_file->cur_cluster); +#endif + return 0; + } + + for(s=0; s<2; s++) + { + unsigned int sectno; + + if (amount_left_in_file <= 0) + { +#ifdef DEBUG + fprintf(stderr, "r2: Hit end of file\n"); +#endif + break; + } + + sectno = dos_clust0 + + cur_file->cur_cluster * dos_spc + + cur_file->sector_no % dos_spc; + + if (rawio_read_sector(sectno, ptr) <= 0) + { +#ifdef DEBUG + fprintf(stderr, "r2: rawio failed\n"); +#endif + return -1; + } + + cur_file->sector_no++; + if( cur_file->sector_no % dos_spc == 0 ) + { + int odd = (cur_file->cur_cluster&1); + unsigned int val, val2; + + val = cur_file->cur_cluster + (cur_file->cur_cluster>>1); +#ifdef BUFFER_FAT + val2 = get_uint(fat_buf, val); +#else + if (rawio_read_sector(dos_fatpos+(val/512), sect) <= 0) return -1; + if( val%512 == 511 ) + { + val2 = sect[511] & 0xFF; + if (rawio_read_sector(dos_fatpos+(val/512)+1, sect) <= 0) return -1; + val2 |= (sect[0]<<8); + } + else + val2 = get_uint(sect, (val%512)); +#endif + + if( odd ) val2>>=4; + + val2 &= 0xFFF; + + cur_file->cur_cluster = val2; + } + + amount_to_read = amount_left_in_file; + if (amount_to_read > 512) amount_to_read = 512; + ptr += amount_to_read; + amount_left_in_file -= amount_to_read; + } + + return ptr - buffer; +} + +static int read_bootblock() +{ + int rv, media_byte = 0; + +#ifdef DEBUG + printf("fs_dos:read_bootblock:\n"); +#endif + if (rawio_read_sector(1, sect) <= 0) return -1; + media_byte = *(unsigned char*)sect; + + /* Valid media byte ? */ + if( (media_byte & 0xF0) != 0xF0 ) return -1; + if (rawio_read_sector(0, sect) <= 0) return -1; + + if( DOS_MEDIA(sect) != media_byte ) return -1; + if( DOS_SPT(sect) > 63 ) return -1; + if( DOS_SECT(sect) != 512 ) return -1; + + if( last_serial != DOS4_SERIAL(sect) ) fsdos_close_file(); + last_serial = DOS4_SERIAL(sect); + + /* Collect important data */ + dir_sect = DOS_RESV(sect) + DOS_NFAT(sect)*DOS_FATLEN(sect); + dir_nentry = DOS_NROOT(sect); + + dos_fatpos = DOS_RESV(sect); + dos_spc = DOS_CLUST(sect); + if( dos_spc < 1 ) dos_spc = 1; + dos_clust0 = dir_sect + (dir_nentry+15)/16 - 2*dos_spc; + + if( rawio_disk_cyls == 0 ) + { + rawio_disk_spt = DOS_SPT(sect); + rawio_disk_heads = DOS_HEADS(sect); + } + +#ifdef DEBUG + printf("read_bootblock: heads(%d), spt(%d), dir_sect(%d)\n", + rawio_disk_heads, + rawio_disk_spt, + dir_sect); +#endif + return 0; +} diff --git a/libc/bios/io.h b/libc/bios/io.h new file mode 100644 index 0000000..a098e03 --- /dev/null +++ b/libc/bios/io.h @@ -0,0 +1,22 @@ +#ifndef __io_h__ +#define __io_h__ + +typedef struct { + /* + * both block_read/block_write should be defined as + * int x(ioblock* iob, char* buffer, int blockno) + * and it reads/writes 1k blocks + * + * close should be defined as int x(ioblock* ioblock); + */ + int (*block_read)(); /* read routine */ + int (*block_write)(); /* write routine - not supported yet*/ + int (*close)(); /* close routine */ + long offset; /* current offset in file to read/write */ + int flags; + long amount_left; /* amount left in buffer */ + char buffer[1024]; + void* context; +} ioblock; + +#endif diff --git a/libc/bios/rawio.c b/libc/bios/rawio.c new file mode 100644 index 0000000..1bb0e4d --- /dev/null +++ b/libc/bios/rawio.c @@ -0,0 +1,327 @@ +/* + * rawio.c - plagiarised from ../../bootblocks/trk_buf.c + */ + +#include +#include +#include +#include +#include "rawio.h" + +int rawio_disk_drive = 0; +int rawio_disk_spt = 7; +int rawio_disk_heads = 2; +int rawio_disk_cyls = 0; + +static int last_drive = 0; +static int data_len = 0; +static long data_trk1 = 0; +static char * data_buf1 = 0; +static long data_trk2 = 0; +static char * data_buf2 = 0; + +static long bad_track = -1; /* Track number of last unsuccesful read */ + +static long rawio_get_dpt(); + +void rawio_reset_disk() +{ + if( data_buf1 ) free(data_buf1); + if( data_buf2 ) free(data_buf2); + data_buf1 = data_buf2 = 0; + last_drive = rawio_disk_drive; + + if( !(rawio_disk_drive & 0x80 ) ) + { + rawio_disk_spt = 7; /* Defaults for reading Boot area. */ + rawio_disk_heads = 2; + rawio_disk_cyls = 0; +#ifdef DEBUG + fprintf(stderr, "reset_disk (hard): spt = %d, heads = %d, cyls = %d\n", + rawio_disk_spt, + rawio_disk_heads, + rawio_disk_cyls); +#endif + } +#if defined(__MSDOS__) || defined(__STANDALONE__) + else + { + /* Hard disk, get parameters from bios */ + long dpt; + int v; + + rawio_disk_spt = 17; /* Defaults for reading Boot area. */ + rawio_disk_heads = 1; + rawio_disk_cyls = 0; + + dpt = rawio_get_dpt(rawio_disk_drive); + v = ((dpt>>16) & 0xFF); + if( v == 0xFF || v <= (rawio_disk_drive&0x7F) ) return; /* Bad dpt */ + + rawio_disk_spt = (dpt & 0x3F); /* Max sector number 1-63 */ + if( rawio_disk_spt == 0 ) rawio_disk_spt = 64; /* 1-64 ? */ + rawio_disk_heads = ((dpt>>24) & 0xFF) + 1; /* Head count 1-256 */ + rawio_disk_cyls = ((dpt>>8) & 0xFF) + ((dpt<<2) & 0x300) + 1; + + /* Cyls count, unchecked, only needs != 0, if AMI 386 bios can be + * upto 4096 cylinder, otherwise BIOS limit is 1024 cyl. + */ +#ifdef DEBUG + fprintf(stderr, "reset_disk (soft): spt = %d, heads = %d, cyls = %d\n", + rawio_disk_spt, + rawio_disk_heads, + rawio_disk_cyls); +#endif + } +#endif +} + +int rawio_read_lsector(sectno, buffer) +long sectno; +char* buffer; +{ + int tries = 6; + int rv; + + int phy_s = 1; + int phy_h = 0; + int phy_c = 0; + + if( rawio_disk_drive != last_drive ) rawio_reset_disk(); + + if( rawio_disk_spt < 0 || rawio_disk_spt > 63 || rawio_disk_heads < 1 ) + { + phy_s = sectno; + rawio_reset_disk(); + +#ifdef DEBUG + fprintf(stderr, "read_sector(%ld = %d,%d,%d)\n", + sectno, phy_c, phy_h, phy_s+1); +#endif + } + else + { + phy_s = sectno%rawio_disk_spt; + phy_h = sectno/rawio_disk_spt%rawio_disk_heads; + phy_c = sectno/rawio_disk_spt/rawio_disk_heads; + +#ifdef DEBUG + fprintf(stderr, "read_sector(%ld = %d,%d,%d)\n", + sectno, phy_c, phy_h, phy_s+1); +#endif + if( rawio_fetch_track_buf(phy_c, phy_h, phy_s) >= 0 ) + { +#ifdef DEBUG + fprintf(stderr, "read_sector: ftb worked\n"); +#endif + memcpy(buffer, data_buf1 + (phy_s % data_len) * 512, 512); + return 512; + } + } + + data_len = -1; /* Zap the cache */ + if( data_buf1 == 0 ) + data_buf1 = malloc(512); + if( data_buf1 == 0 ) + { + fprintf(stderr, "Cannot allocate memory for disk read!!!\n"); + return 0; + } + + fprintf(stderr, "WARNING: Single sector read\n"); + + do + { + rv = rawio_phy_read(rawio_disk_drive, phy_c, phy_h, phy_s+1, 1, data_buf1); + tries--; + if( rv ) fprintf(stderr, "Error in phy_read(%d,%d,%d,%d,%d,%d);\n", + rawio_disk_drive, phy_c, phy_h, phy_s+1, 1, data_buf1); + } + while(rv && tries > 0); + + if(rv) + { +#ifdef DEBUG + fprintf(stderr, "rawio failed\n"); +#endif + return 0; + } + else + { +#ifdef DEBUG + fprintf(stderr, "rawio worked\n"); +#endif + memcpy(buffer, data_buf1, 512); + return 512; + } +} + +rawio_fetch_track_buf(phy_c, phy_h, phy_s) +int phy_c, phy_h, phy_s; +{ + long trk_no, t; + char * p; + int tries = 3; + int rv, nlen; + + /* Big tracks get us short of memory so limit it. */ + nlen = (rawio_disk_spt-1)/22; + nlen = (rawio_disk_spt+nlen)/(nlen+1); + trk_no = (long)phy_c*rawio_disk_heads*4+phy_h*4+phy_s/nlen+1; + + if( data_len != nlen ) + { + if( data_buf1 ) free(data_buf1); + if( data_buf2 ) free(data_buf2); + data_buf1 = data_buf2 = 0; + data_len = rawio_disk_spt; + } +#ifdef DEBUG + fprintf(stderr, + "ftb: trk_no=%ld, data_trk1=%ld, data_buf1=%x\n", + trk_no, + data_trk1, + data_buf1); +#endif + if( trk_no == bad_track ) return -1; + + if( data_buf1 && trk_no == data_trk1 ) return 0; + + /* Two cases: + * 1) buffer2 has the one we want, need to swap to make it most recent + * 2) Neither has it, need to swap to overwrite least recent. + */ + + /* So we always swap */ + p = data_buf1; data_buf1 = data_buf2; data_buf2 = p; + t = data_trk1; data_trk1 = data_trk2; data_trk2 = t; + +#ifdef DEBUG + fprintf(stderr, "ftb swap: trk_no=%ld, data_trk1=%ld, data_buf1=%x\n", + trk_no, + data_trk1, + data_buf1); +#endif + /* The other one right ? */ + if( data_buf1 && trk_no == data_trk1 ) return 0; + + /* If we get here we have to do a physical read ... */ + /* into data_buf1. */ + + if( data_buf1 == 0 ) + { + data_buf1 = malloc(rawio_disk_spt*512); + +#ifdef __ELKS__ + fprintf(stderr, "Allocated buffer to %d\n", data_buf1); +#endif + } + if( data_buf1 == 0 ) + { + /* Is buf2 allocated ? Yes take it! */ + data_buf1 = data_buf2; data_buf2 = 0; data_trk2 = -1; + } + + bad_track = -1; + data_trk1 = -1; + +#ifdef DEBUG + fprintf(stderr, "ftb buf: trk_no=%ld, data_trk1=%ld, data_buf1=%x\n", + trk_no, + data_trk1, + data_buf1); +#endif + /* Not enough memory for track read. */ + if( data_buf1 == 0 ) return -1; + + do /* the physical read */ + { + rv = rawio_phy_read(rawio_disk_drive, phy_c, phy_h, phy_s/data_len+1, data_len, + data_buf1); + tries--; + if( rv ) fprintf(stderr, "Error in phy_read(%d,%d,%d,%d,%d,%d);\n", + rawio_disk_drive, phy_c, phy_h, phy_s/data_len+1, data_len, data_buf1); + } + while(rv && tries > 0); + + /* Disk error, it'll try one at a time, _very_ slowly! */ + if(rv) + { + bad_track = trk_no; + return -1; + } + + /* Yes! */ + data_trk1 = trk_no; + return 0; +} + +#if defined(__MSDOS__) || defined(__STANDALONE__) +rawio_phy_read(drive, cyl, head, sect, length, buffer) +{ +#asm + push bp + mov bp,sp + + push es + push ds + pop es + + mov dl,[bp+2+_rawio_phy_read.drive] + mov ch,[bp+2+_rawio_phy_read.cyl] + mov dh,[bp+2+_rawio_phy_read.head] + mov bx,[bp+2+_rawio_phy_read.buffer] + + mov ax,[bp+2+_rawio_phy_read.cyl] ! Bits 10-11 of cylinder, AMI BIOS. + mov cl,#4 + sar ax,cl + and al,#$C0 + xor dh,al + + mov cl,[bp+2+_rawio_phy_read.sect] + and cl,#$3F + mov ax,[bp+2+_rawio_phy_read.cyl] ! Bits 8-9 of cylinder. + sar ax,#1 + sar ax,#1 + and al,#$C0 + or cl,al + + mov al,[bp+2+_rawio_phy_read.length] + mov ah,#$02 + int $13 + jc read_err + mov ax,#0 +read_err: + + pop es + pop bp +#endasm +} + +long +rawio_get_dpt(drive) +{ +#asm + push bp + mov bp,sp + + push di + push es + + mov dl,[bp+2+_rawio_get_dpt.drive] + + mov ah,#$08 + int $13 + jnc func_ok + mov cx,ax + mov dx,#-1 +func_ok: + mov ax,cx + + pop es + pop di + pop bp +#endasm +} +#endif + diff --git a/libc/bios/rawio.h b/libc/bios/rawio.h new file mode 100644 index 0000000..3e378d7 --- /dev/null +++ b/libc/bios/rawio.h @@ -0,0 +1,16 @@ +#ifndef __readfs_h__ /* multi-inclusion protection */ +#define __readfs_h__ + +#ifdef __STDC__ +#define P(x) x +#else +#define P(x) () +#endif + +#define rawio_read_sector(__sect, __buffer) rawio_read_lsector((unsigned long)__sect, __buffer) +int rawio_read_lsector P((long sector, char* buffer)); + +extern int rawio_disk_cyls; +extern int rawio_disk_heads; +extern int rawio_disk_spt; +#endif diff --git a/libc/include/bios.h b/libc/include/bios.h new file mode 100644 index 0000000..4cfcd6a --- /dev/null +++ b/libc/include/bios.h @@ -0,0 +1,39 @@ + +#ifndef __BIOS_H +#define __BIOS_H +#include + +union REGS +{ + struct { unsigned int ax, bx, cx, dx, si, di, cflag; } x; + struct { unsigned char al, ah, bl, bh, cl, ch, dl, dh; } h; +}; + +struct SREGS +{ + unsigned int es, cs, ss, ds; +}; + +unsigned int __get_cs __P((void)); +unsigned int __get_ds __P((void)); +unsigned int __get_es __P((void)); +void __set_es __P((unsigned int seg)); +int __peek_es __P((unsigned int off)); +int __deek_es __P((unsigned int off)); + +#define movedata __movedata + +#ifdef __LIBC__ + +extern int __nofiles(); +extern int (*__files)(); + +#define CMD_OPEN 0 +#define CMD_READ 1 +#define CMD_WRITE 2 +#define CMD_LSEEK 3 +#define CMD_CLOSE 4 + +#endif +#endif + diff --git a/libc/include/dos.h b/libc/include/dos.h index d67e27f..4ff9e6e 100644 --- a/libc/include/dos.h +++ b/libc/include/dos.h @@ -2,17 +2,7 @@ #ifndef __DOS_H #define __DOS_H #include - -union REGS -{ - struct { unsigned int ax, bx, cx, dx, si, di, cflag; } x; - struct { unsigned char al, ah, bl, bh, cl, ch, dl, dh; } h; -}; - -struct SREGS -{ - unsigned int es, cs, ss, ds; -}; +#include #ifdef __MSDOS__ extern unsigned int __envseg; @@ -24,14 +14,5 @@ void __setvect __P((int i, long j)); long __getvect __P((int vecno)); #endif -unsigned int __get_cs __P((void)); -unsigned int __get_ds __P((void)); -unsigned int __get_es __P((void)); -void __set_es __P((unsigned int seg)); -int __peek_es __P((unsigned int off)); -int __deek_es __P((unsigned int off)); - -#define movedata __movedata - #endif diff --git a/libc/msdos/TODO b/libc/msdos/TODO index 7d3eb12..f2664a2 100644 --- a/libc/msdos/TODO +++ b/libc/msdos/TODO @@ -1 +1,3 @@ #define remove(x) unlink(x) + +See also the other files, many parts are incomplete. diff --git a/libc/msdos/msdos.c b/libc/msdos/msdos.c index 99e8835..77100ae 100644 --- a/libc/msdos/msdos.c +++ b/libc/msdos/msdos.c @@ -190,8 +190,12 @@ char ** __argv; for(i=0; i John Stanley has again been a great help in debugging, particularly + * > with the printf/scanf functions which are his creation. + * * Dale Schumacher 399 Beacon Ave. * (alias: Dalnefre') St. Paul, MN 55104 * dal@syntel.UUCP United States of America * "It's not reality that's important, but how you perceive things." + * */ /* Altered to use stdarg, made the core function vfprintf. diff --git a/libc/stdio2/scanf.c b/libc/stdio2/scanf.c index 2d61ab2..1e0f282 100644 --- a/libc/stdio2/scanf.c +++ b/libc/stdio2/scanf.c @@ -1,3 +1,17 @@ +/* + * This file based on scanf.c from 'Dlibs' on the atari ST (RdeBath) + * + * 19-OCT-88: Dale Schumacher + * > John Stanley has again been a great help in debugging, particularly + * > with the printf/scanf functions which are his creation. + * + * Dale Schumacher 399 Beacon Ave. + * (alias: Dalnefre') St. Paul, MN 55104 + * dal@syntel.UUCP United States of America + * "It's not reality that's important, but how you perceive things." + * + */ + #include #include #include @@ -519,3 +533,4 @@ va_list ap; } #endif + diff --git a/libc/stdio2/stdio.c b/libc/stdio2/stdio.c index bc96100..c317859 100644 --- a/libc/stdio2/stdio.c +++ b/libc/stdio2/stdio.c @@ -130,11 +130,11 @@ FILE *fp; register int v; Inline_init; - v = fp->mode; - /* If last op was a read ... */ - if ((v & __MODE_READING) && fflush(fp)) + /* If last op was a read ... note fflush may change fp->mode and ret OK */ + if ((fp->mode & __MODE_READING) && fflush(fp)) return EOF; + v = fp->mode; /* Can't write if there's been an EOF or error then return EOF */ if ((v & (__MODE_WRITE | __MODE_EOF | __MODE_ERR)) != __MODE_WRITE) return EOF; @@ -304,7 +304,7 @@ FILE *f; register int ch; ret = s; - for (i = count; i > 0; i--) + for (i = count-1; i > 0; i--) { ch = getc(f); if (ch == EOF) @@ -456,11 +456,11 @@ FILE *fp; int len; unsigned bytes, put; - v = fp->mode; - /* If last op was a read ... */ - if ((v & __MODE_READING) && fflush(fp)) + /* If last op was a read ... note fflush may change fp->mode and ret OK */ + if ((fp->mode & __MODE_READING) && fflush(fp)) return 0; + v = fp->mode; /* Can't write or there's been an EOF or error then return 0 */ if ((v & (__MODE_WRITE | __MODE_EOF | __MODE_ERR)) != __MODE_WRITE) return 0; @@ -537,7 +537,6 @@ int ref; { #if 1 /* if __MODE_READING and no ungetc ever done can just move pointer */ - /* This needs testing! */ if ( (fp->mode &(__MODE_READING | __MODE_UNGOT)) == __MODE_READING && ( ref == SEEK_SET || ref == SEEK_CUR )) @@ -823,6 +822,7 @@ size_t size; { fp->bufstart = buf; fp->bufend = buf+size; + fp->mode &= ~__MODE_BUF; fp->mode |= mode; } } -- cgit v1.2.1