?? clib.arc
字號:
>>> CLIB.H 1617
/*
** CLIB.H -- Definitions for Small-C library functions.
**
** Copyright 1983 L. E. Payne and J. E. Hendrix
*/
/*
** Misc parameters
*/
#define MAXFILES 20 /* maximum open files */
#define DOSEOF 26 /* DOS end-of-file byte */
#define ARCHIVE 32 /* file archive bit */
/*
** DOS function calls
*/
#define CREATE 60 /* make file */
#define OPEN 61 /* open file */
#define CLOSE 62 /* close file or device */
#define READ 63 /* read from a file */
#define WRITE 64 /* write to a file */
#define DELETE 65 /* delete file */
#define SEEK 66 /* seek within a file */
#define CONTROL 68 /* control device */
#define FORCE 70 /* force use of a handle */
#define RETDOS 76 /* close files and return to DOS */
#define FNDFIL 78 /* find first occurrence of a file */
#define FNDNXT 79 /* find next occurrence of a file */
#define RENAME 86 /* rename file */
/*
** File status bits
*/
#define OPNBIT 1 /* open condition */
#define EOFBIT 2 /* end-of-file condition */
#define ERRBIT 4 /* error condition */
/*
** File positioning origins
*/
#define FROM_BEG 0 /* from beginning of file */
#define FROM_CUR 1 /* from current position */
#define FROM_END 2 /* from end of file */
/*
** Buffer usage codes
** NULL means the buffer is not used.
*/
#define EMPTY 1 /* buffer is currently empty */
#define IN 2 /* buffer is currently holding input data */
#define OUT 3 /* buffer is currently holding output data */
/*
** ASCII characters
*/
#define ABORT 3
#define RUB 8
#define PAUSE 19
#define WIPE 24
#define DEL 127
>>> ABS.C 117
/*
** abs -- returns absolute value of nbr
*/
abs(nbr) int nbr; {
if(nbr < 0) return (-nbr);
return (nbr);
}
>>> ATOI.C 259
/*
** atoi(s) - convert s to integer.
*/
atoi(s) char *s; {
int sign, n;
while(isspace(*s)) ++s;
sign = 1;
switch(*s) {
case '-': sign = -1;
case '+': ++s;
}
n = 0;
while(isdigit(*s)) n = 10 * n + *s++ - '0';
return (sign * n);
}
>>> ATOIB.C 435
/*
** atoib(s,b) - Convert s to "unsigned" integer in base b.
** NOTE: This is a non-standard function.
*/
atoib(s, b) char *s; int b; {
int n, digit;
n = 0;
while(isspace(*s)) ++s;
while((digit = (127 & *s++)) >= '0') {
if(digit >= 'a') digit -= 87;
else if(digit >= 'A') digit -= 55;
else digit -= '0';
if(digit >= b) break;
n = b * n + digit;
}
return (n);
}
>>> AUXBUF.C 786
#include "stdio.h"
#include "clib.h"
extern int
_bufsiz[MAXFILES], /* size of buffer */
_bufptr[MAXFILES]; /* aux buffer address */
/*
** auxbuf -- allocate an auxiliary input buffer for fd
** fd = file descriptor of an open file
** size = size of buffer to be allocated
** Returns NULL on success, else ERR.
** Note: Ungetc() still works.
** A 2nd call allocates a new buffer replacing old one.
** If fd is a device, buffer is allocated but ignored.
** Buffer stays allocated when fd is closed or new one is allocated.
** May be used on a closed fd.
*/
auxbuf(fd, size) int fd; char *size; { /* fake unsigned */
if(!size || avail(NO) < size) return (ERR);
_bufptr[fd] = malloc(size);
_bufsiz[fd] = size;
_empty(fd, NO);
return (NULL);
}
>>> AVAIL.C 347
extern char *_memptr;
/*
** Return the number of bytes of available memory.
** In case of a stack overflow condition, if 'abort'
** is non-zero the program aborts with an 'S' clue,
** otherwise zero is returned.
*/
avail(abort) int abort; {
char x;
if(&x < _memptr) {
if(abort) exit(1);
return (0);
}
return (&x - _memptr);
}
>>> BSEEK.C 727
#include "stdio.h"
#include "clib.h"
extern int _nextc[], _bufuse[];
/*
** Position fd to the character in file indicated by "offset."
** "Offset" is the address of a long integer or array of two
** integers containing the offset, low word first.
**
** BASE OFFSET-RELATIVE-TO
** 0 beginning of file
** 1 current byte in file
** 2 end of file (minus offset)
**
** Returns NULL on success, else EOF.
*/
bseek(fd, offset, base) int fd, offset[], base; {
int hi, lo;
if(!_mode(fd) || !_bufuse[fd]) return (EOF);
if(_adjust(fd)) return (EOF);
lo = offset[0];
hi = offset[1];
if(!_seek(base, fd, &hi, &lo)) return (EOF);
_nextc[fd] = EOF;
_clreof(fd);
return (NULL);
}
>>> BTELL.C 447
#include "stdio.h"
#include "clib.h"
extern int _bufuse[];
/*
** Retrieve offset to next character in fd.
** "Offset" must be the address of a long int or
** a 2-element int array. The offset is placed in
** offset in low, high order.
*/
btell(fd, offset) int fd, offset[]; {
if(!_mode(fd) || !_bufuse[fd]) return (EOF);
if(_adjust(fd)) return (EOF);
offset[0] = offset[1] = 0;
_seek(FROM_CUR, fd, offset+1, offset);
return (NULL);
}
>>> CALL.ASM 3939
;
; Small-C Run Time Library for MS/PC-DOS
;
extrn __main: near
extrn _exit: near
extrn __memptr: word
data segment public
dw 1
data ends
stack segment stack
dw 32 dup(?)
stack ends
code segment public
assume cs:code
start:
mov ax,data ; set data segment for program
mov ds,ax
mov ax,es:[2] ; paragraphs of memory on system
sub ax,data ; paragraphs beyond code segment
cmp ah,10h ; more than 64K?
jb start_1 ; no
mov ax,1000h ; only use 64K
start_1:
mov cl,4
shl ax,cl ; byte offset to end of data/free/stack
cli ; disable interrupts
mov bx,ds
mov ss,bx ; make data and stack segments coincide
mov sp,ax ; top of stack = end of data/free/stack
push ax ; force sp non-zero (if 64K used)
sti ; reenable interrupts
mov ax,stack ; paragraph following data
sub ax,data ; number of data paragraphs
shl ax,cl ; number of data bytes (offset to free/stack)
mov bx,ax
inc bh ; adjust for minimum stack space
cmp bx,sp ; enough memory?
jb start_2 ; yes
mov ax,1 ; no, terminate with exit code 1
push ax
call _exit
start_2:
mov __memptr,ax ; set memory allocation pointer
;
; ------------ release unused memory -----------
; ------ cannot run debug with this code -------
; mov bx,sp
; mov ah,4AH
; int 21H
; ----------------------------------------------
;
; make sure that es -> psp, because __main requires it
;
jmp __main ; __main never returns
public _ccargc
_ccargc:
mov al,cl
xor ah,ah
ret
;
; Test if Secondary (BX) <oper> Primary (AX)
;-------------------------- MASM version
;compare macro name, cond
; public __&name
;__&name:
; cmp ax,bx
; j&cond true
; xor ax,ax ; returns zero
; ret
; endm
;-------------------------- ASM version
compare macro name, cond
public __?1
__?1:
cmp ax,bx
j?2 true
xor ax,ax ; returns zero
ret
endm
;--------------------------
compare ult,a
compare ugt,b
compare ule,ae
compare uge,be
compare eq,e
compare ne,ne
compare lt,g
compare gt,l
compare le,ge
compare ge,le
;
; Logical Negate of Primary
;
public __lneg
__lneg:
or ax,ax
jnz false
true: mov ax,1 ; returns one
ret
false: xor ax,ax ; returns zero
ret
;
;
; execute "switch" statement
;
; ax = switch value
; (sp) -> switch table
; dw addr1, value1
; dw addr2, value2
; ...
; dw 0
; [jmp default]
; continuation
;
public __switch
__switch:
pop bx ; bx -> switch table
jmp skip ; skip the pre-increment
back:
add bx,4
skip: mov cx,cs:[bx]
jcxz default ; end of table -- jump out
cmp ax,cs:[bx+2]
jnz back
jmp cx ; match -- jump to case
default:
inc bx
inc bx
jmp bx ; jump to default/continuation
;
; dummy entry point to resolve the external reference _LINK
; which is no longer generated by Small-C but which exists in
; library modules and .OBJ files compiled by earlier versions
; of Small-C
public __link
__link: ret
code ends
end start
>>> CALLOC.C 315
#include "stdio.h"
/*
** Cleared-memory allocation of n items of size bytes.
** n = Number of items to allocate space for.
** size = Size of the items in bytes.
** Returns the address of the allocated block,
** else NULL for failure.
*/
calloc(n, size) unsigned n, size; {
return (_alloc(n*size, YES));
}
>>> CLEARERR.C 152
#include "stdio.h"
#include "clib.h"
extern int _status[];
/*
** Clear error status for fd.
*/
clearerr(fd) int fd; {
if(_mode(fd)) _clrerr(fd);
}
>>> CSEEK.C 1539
#include "stdio.h"
#include "clib.h"
extern int _nextc[], _bufuse[];
/*
** Position fd to the 128-byte record indicated by
** "offset" relative to the point indicated by "base."
**
** BASE OFFSET-RELATIVE-TO
** 0 first record
** 1 current record
** 2 end of file (last record + 1)
** (offset should be minus)
**
** Returns NULL on success, else EOF.
*/
cseek(fd, offset, base) int fd, offset, base; {
int newrec, oldrec, hi, lo;
if(!_mode(fd) || !_bufuse[fd]) return (EOF);
if(_adjust(fd)) return (EOF);
switch (base) {
case 0: newrec = offset;
break;
case 1: oldrec = ctell(fd);
goto calc;
case 2: hi = lo = 0;
if(!_seek(FROM_END, fd, &hi, &lo)) return (EOF);
oldrec = ((lo >> 7) & 511) | (hi << 9);
calc:
newrec = oldrec + offset;
break;
default: return (EOF);
}
lo = (newrec << 7); /* convert newrec to long int */
hi = (newrec >> 9) & 127;
if(!_seek(FROM_BEG, fd, &hi, &lo)) return (EOF);
_nextc[fd] = EOF;
_clreof(fd);
return (NULL);
}
/*
** Position fd to the character indicated by
** "offset" within current 128-byte record.
** Must be on record boundary.
**
** Returns NULL on success, else EOF.
*/
cseekc(fd, offset) int fd, offset; {
int hi, lo;
if(!_mode(fd) || isatty(fd) ||
ctellc(fd) || offset < 0 || offset > 127) return (EOF);
hi = 0; lo = offset;
if(!_seek(FROM_CUR, fd, &hi, &lo)) return (EOF);
return (NULL);
}
>>> CSYSLIB.C 9708
/*
** CSYSLIB -- System-Level Library Functions
*/
#include "stdio.h"
#include "clib.h"
/*
****************** System Variables ********************
*/
int
_cnt=1, /* arg count for main */
_vec[20], /* arg vectors for main */
_status[MAXFILES] = {OPNBIT, OPNBIT, OPNBIT, OPNBIT, OPNBIT},
_cons [MAXFILES], /* fast way to tell if file is the console */
_nextc [MAXFILES] = {EOF, EOF, EOF, EOF, EOF},
_bufuse[MAXFILES], /* current buffer usage: NULL, EMPTY, IN, OUT */
_bufsiz[MAXFILES], /* size of buffer */
_bufptr[MAXFILES], /* aux buffer address */
_bufnxt[MAXFILES], /* address of next byte in buffer */
_bufend[MAXFILES], /* address of end-of-data in buffer */
_bufeof[MAXFILES]; /* true if current buffer ends file */
char
*_memptr, /* pointer to free memory. */
_arg1[]="*"; /* first arg for main */
/*
*************** System-Level Functions *****************
*/
/*
** Process command line, allocate default buffer to each fd,
** execute main(), and exit to DOS. Must be executed with es=psp.
** Small default buffers are allocated because a high price is paid for
** byte-by-byte calls to DOS. Tests gave these results for a simple
** copy program:
**
** chunk size copy time in seconds
** 1 36
** 5 12
** 25 6
** 50 6
*/
_main() {
int fd;
_parse();
for(fd = 0; fd < MAXFILES; ++fd) auxbuf(fd, 32);
if(!isatty(stdin)) _bufuse[stdin] = EMPTY;
if(!isatty(stdout)) _bufuse[stdout] = EMPTY;
main(_cnt, _vec);
exit(0);
}
/*
** Parse command line and setup argc and argv.
** Must be executed with es == psp
*/
_parse() {
char *ptr;
#asm
mov cl,es:[80h] ; get parameter string length
mov ch,0
push cx ; save it
inc cx
push cx ; 1st __alloc() arg
mov ax,1
push ax ; 2nd __alloc() arg
call __alloc ; allocate zeroed memory for args
add sp,4
mov [bp-2],ax ; ptr = addr of allocated memory
pop cx
push es ; exchange
push ds ; es (source)
pop es ; and
pop ds ; ds (destination)
mov si,81h ; source offset
mov di,[bp-2] ; destination offset
rep movsb ; move string
mov al,0
stosb ; terminate with null byte
push es
pop ds ; restore ds
#endasm
_vec[0]=_arg1; /* first arg = "*" */
while (*ptr) {
if(isspace(*ptr)) {++ptr; continue;}
if(_cnt < 20) _vec[_cnt++] = ptr;
while(*ptr) {
if(isspace(*ptr)) {*ptr = NULL; ++ptr; break;}
++ptr;
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -