home *** CD-ROM | disk | FTP | other *** search
- /*
- Standard library of C functions
- for C/80 (Software Toolworks)
-
-
-
- NOTE: C/80 requires that you pass the
- number of arguments to a function that
- the function expects. If you only need
- to pass one argument, but the function
- was written for two, pass a null for
- the other. Without the second argument
- C/80 bombs.
- Harvey G. Lord
- */
-
- /* Contributed by Les Johnson of Rochester,
- New York, 12/21/82.
-
- Some string functions
- */
-
- int atoi(n) /* convert ascii string to integer */
- char *n;
- {
- int val;
- char c;
- int sign;
- val=0;
- sign=1;
- while ((c = *n) == '\t' || c== ' ') ++n;
- if (c== '-') {sign = -1; n++;}
- while ( isdigit(c = *n++)) val = val * 10 + c - '0';
- return sign*val;
- }
-
- char *strcat(s1,s2) /* concatenate two strings */
- char *s1, *s2;
- {
- char *temp; temp=s1;
- while(*s1) s1++;
- do *s1++ = *s2; while (*s2++);
- return temp;
- }
-
- int strcmp(s,t) /* compare two strings */
- char s[], t[];
- {
- int i;
- i = 0;
- while (s[i] == t[i])
- if (s[i++] == '\0')
- return 0;
- return s[i] - t[i];
- }
-
- char *strcpy(s1,s2) /* copy first string into second */
- char *s1, *s2;
- {
- char *temp; temp=s1;
- while (*s1++ = *s2++);
- return temp;
- }
-
- int strlen(s) /* return length of a string */
- char *s;
- {
- int len;
- len=0;
- while (*s++) len++;
- return len;
- }
-
- /*
- Some character diddling functions
- */
-
- int isalpha(c) /* return true if alphabetic (not number) */
- char c;
- {
- return isupper(c) || islower(c);
- }
-
- int isupper(c) /* return true if upper case ascii */
- char c;
- {
- return c>='A' && c<='Z';
- }
-
- int islower(c) /* return true if lower case ascii */
- char c;
- {
- return c>='a' && c<='z';
- }
-
- int isdigit(c) /* return true if ascii decimal digit */
- char c;
- {
- return c>='0' && c<='9';
- }
-
- int isspace(c) /* return true if "white space" */
- char c;
- {
- return c==' ' || c=='\t' || c=='\n';
- }
-
- char toupper(c) /* convert to upper case */
- char c;
- {
- return islower(c) ? c-32 : c;
- }
-
- char tolower(c) /* convert to lower case */
- char c;
- {
- return isupper(c) ? c+32 : c;
- }
-
- int abs(n) /* return absolute value of n */
- {
- return (n<0) ? -n : n;
- }
-
- int max(a,b) /* return larger of two */
- {
- return (a > b) ? a : b;
- }
-
- int min(a,b) /* return smaller of two */
- {
- return (a <= b) ? a : b;
- }
-
- /*
- functions added by Les Johnson - 12/19/82
- */
- bdos(n,de) /* calls the bdos function n, with the value de */
- int n,de; {
-
- #asm
- POP H ; return address
- POP D ; second parameter
- POP B ; first parameter
- PUSH B ; restore stack
- PUSH D
- PUSH H
-
- CALL 5 ; bdos
-
- MOV L,A
- MOV H,B
- #endasm
- }
-
- char bios(n,c) /* calls bios function number n */
- int n,c;
- {
- /* get bios address + function # times 3 */
- #asm
- JMP .begin
- .addr: DW 0
-
- .begin: POP H ; save return address
- SHLD .addr
-
- POP B ; 1st arg into c
- POP D ; get function #
- LXI H,.retadd
- PUSH H ; put return addr on stack
-
- LHLD 1 ; get bios vector
- DCX H
- DCX H
- DCX H
- DAD D ; times 3, add to vector
- DAD D
- DAD D
- PCHL ; jump to bios vector
-
- .retadd: LHLD .addr ; restore stack
- PUSH B
- PUSH B
- PUSH H
- MOV L,A ; return argument in hl
- MVI H,0
- #endasm
- }
-
- char peek(n) /* return the contents of address n */
- char *n; {
- return(*n);
- }
-
- poke(n,b) /* "poke" byte value b into address n */
- char *n,b; {
- *n = b;
- }
-
- char inp(n) /* return byte value from port n */
- int n; {
- #asm
- INX SP ; past return address
- INX SP
- POP H ; port number
- MOV H,L
- MVI L,0DBH ; input op code
- SHLD .port
-
- .port: DW 0 ; port number goes here
-
- MVI H,0
- MOV A,L
- PUSH H
- DCX SP ; return address
- DCX SP
- #endasm
- }
-
- outp(n,b) /* send byte value b to port n */
- int n,b; {
- #asm
- INX SP ; past return addr
- INX SP
-
- POP H ; value
- MOV A,H
- POP H ; port
- MOV H,L
- MVI L,0D3H ; out op code
- SHLD .oport
-
- .oport: DW 0
-
- PUSH H ; restore stack
- PUSH H
- DCX SP
- DCX SP
- #endasm
- }
-
- pause() /* sit and wait until the keyboard is hit */
- {
- while(!kbhit());
- }
-
- sleep(n) /* sleep for n/10 seconds */
- int n; {
- int i,j,k;
- for(i=0; i!=n; ++i){
- for(j=0; j!=10; ++j){
- for(k=0; k!=0xaf; ++k);
- if(kbhit()){ getchar(); exit();}
- }
- }
- }
-
- kbhit() /* return true if a character is waiting at
- the console */
- {
- return(bdos(11,0)); /* console status */
-
- /*
-
- kbhit() also works as a bios call. In that case it's
-
- return(bios(2,0));
-
- H.G.L.
- */
-
- }