home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / C80TCOG.ZIP / C80LIB.C < prev    next >
Encoding:
Text File  |  1988-08-08  |  4.4 KB  |  273 lines

  1. /*
  2.        Standard library of C functions
  3.         for C/80 (Software Toolworks)
  4.  
  5.  
  6.  
  7.     NOTE:  C/80 requires that you pass the
  8.     number of arguments to a function that
  9.     the function expects. If you only need
  10.     to pass one argument, but the function
  11.     was  written for two,  pass a null for
  12.     the other. Without the second argument
  13.     C/80 bombs.
  14.                 Harvey G. Lord
  15. */
  16.  
  17. /*    Contributed by Les Johnson of Rochester, 
  18.     New York, 12/21/82.
  19.  
  20.     Some string functions
  21. */
  22.  
  23. int atoi(n) /* convert ascii string to integer */
  24. char *n;
  25. {
  26.     int val; 
  27.     char c;
  28.     int sign;
  29.     val=0;
  30.     sign=1;
  31.     while ((c = *n) == '\t' || c== ' ') ++n;
  32.     if (c== '-') {sign = -1; n++;}
  33.     while (  isdigit(c = *n++)) val = val * 10 + c - '0';
  34.     return sign*val;
  35. }
  36.  
  37. char *strcat(s1,s2) /* concatenate two strings */
  38. char *s1, *s2;
  39. {
  40.     char *temp; temp=s1;
  41.     while(*s1) s1++;
  42.     do *s1++ = *s2; while (*s2++);
  43.     return temp;
  44. }
  45.  
  46. int strcmp(s,t) /* compare two strings */
  47. char s[], t[];
  48. {
  49.     int i;
  50.     i = 0;
  51.     while (s[i] == t[i])
  52.         if (s[i++] == '\0')
  53.             return 0;
  54.     return s[i] - t[i];
  55. }
  56.  
  57. char *strcpy(s1,s2) /* copy first string into second */
  58. char *s1, *s2;
  59. {
  60.     char *temp; temp=s1;
  61.     while (*s1++ = *s2++);
  62.     return temp;
  63. }
  64.  
  65. int strlen(s) /* return length of a string */
  66. char *s;
  67. {
  68.     int len;
  69.     len=0;
  70.     while (*s++) len++;
  71.     return len;
  72. }
  73.  
  74. /*
  75.     Some character diddling functions
  76. */
  77.  
  78. int isalpha(c) /* return true if alphabetic (not number) */
  79. char c;
  80. {
  81.     return isupper(c) || islower(c);
  82. }
  83.  
  84. int isupper(c) /* return true if upper case ascii */
  85. char c;
  86. {
  87.     return c>='A' && c<='Z';
  88. }
  89.  
  90. int islower(c) /* return true if lower case ascii */
  91. char c;
  92. {
  93.     return c>='a' && c<='z';
  94. }
  95.  
  96. int isdigit(c) /* return true if ascii decimal digit */
  97. char c;
  98. {
  99.     return c>='0' && c<='9';
  100. }
  101.  
  102. int isspace(c) /* return true if "white space" */
  103. char c;
  104. {
  105.     return c==' ' || c=='\t' || c=='\n';
  106. }
  107.  
  108. char toupper(c) /* convert to upper case */
  109. char c;
  110. {
  111.     return islower(c) ? c-32 : c;
  112. }
  113.  
  114. char tolower(c) /* convert to lower case */
  115. char c;
  116. {
  117.     return isupper(c) ? c+32 : c;
  118. }
  119.  
  120. int abs(n) /* return absolute value of n */
  121. {
  122.     return (n<0) ? -n : n;
  123. }
  124.  
  125. int max(a,b) /* return larger of two */
  126. {
  127.     return (a > b) ? a : b;
  128. }
  129.  
  130. int min(a,b) /* return smaller of two */
  131. {
  132.     return (a <= b) ? a : b;
  133. }
  134.  
  135. /*
  136.     functions added by Les Johnson - 12/19/82
  137. */
  138. bdos(n,de) /* calls the bdos function n, with the value de */
  139. int n,de; {
  140.  
  141. #asm
  142.     POP H    ; return address
  143.     POP D    ; second parameter
  144.     POP B    ; first parameter
  145.     PUSH B    ; restore stack
  146.     PUSH D
  147.     PUSH H
  148.  
  149.     CALL 5    ; bdos
  150.  
  151.     MOV L,A
  152.     MOV H,B
  153. #endasm
  154. }
  155.  
  156. char bios(n,c) /* calls bios function number n */
  157. int n,c;
  158. {
  159.     /* get bios address + function # times 3 */
  160. #asm
  161.     JMP .begin
  162. .addr:    DW 0
  163.  
  164. .begin:    POP H    ; save return address
  165.     SHLD .addr
  166.  
  167.     POP B    ; 1st arg into c
  168.     POP D    ; get function #
  169.     LXI H,.retadd
  170.     PUSH H    ; put return addr on stack
  171.  
  172.     LHLD 1    ; get bios vector
  173.     DCX H
  174.     DCX H
  175.     DCX H
  176.     DAD D    ; times 3, add to vector
  177.     DAD D
  178.     DAD D
  179.     PCHL    ; jump to bios vector
  180.  
  181. .retadd: LHLD .addr ; restore stack
  182.     PUSH B
  183.     PUSH B
  184.     PUSH H
  185.     MOV L,A    ; return argument in hl
  186.     MVI H,0
  187. #endasm
  188. }
  189.  
  190. char peek(n) /* return the contents of address n */
  191.     char *n; {
  192.     return(*n);
  193. }
  194.  
  195. poke(n,b) /* "poke" byte value b into address n */
  196.     char *n,b; {
  197.     *n = b;
  198. }
  199.  
  200. char inp(n) /* return byte value from port n */
  201. int n; {
  202. #asm
  203.     INX SP ; past return address
  204.     INX SP
  205.     POP H  ; port number
  206.     MOV H,L
  207.     MVI L,0DBH ; input op code
  208.     SHLD .port
  209.  
  210. .port:    DW 0   ; port number goes here
  211.  
  212.     MVI H,0
  213.     MOV A,L
  214.     PUSH H
  215.     DCX SP ; return address
  216.     DCX SP
  217. #endasm
  218. }
  219.  
  220. outp(n,b) /* send byte value b to port n */
  221.     int n,b; {
  222. #asm
  223.     INX SP ; past return addr
  224.     INX SP
  225.  
  226.     POP H ; value
  227.     MOV A,H
  228.     POP H ; port
  229.     MOV H,L
  230.     MVI L,0D3H ; out op code
  231.     SHLD .oport
  232.  
  233. .oport: DW 0
  234.  
  235.     PUSH H ; restore stack
  236.     PUSH H
  237.     DCX SP
  238.     DCX SP
  239. #endasm
  240. }
  241.  
  242. pause() /* sit and wait until the keyboard is hit */
  243. {
  244.     while(!kbhit());
  245. }
  246.  
  247. sleep(n) /* sleep for n/10 seconds */
  248. int n; {
  249.     int i,j,k;
  250.     for(i=0; i!=n; ++i){
  251.         for(j=0; j!=10; ++j){
  252.             for(k=0; k!=0xaf; ++k);
  253.             if(kbhit()){ getchar(); exit();}
  254.         }
  255.     }
  256. }
  257.  
  258. kbhit() /* return true if a character is waiting at 
  259.     the console */
  260. {
  261.     return(bdos(11,0)); /* console status */
  262.  
  263. /* 
  264.  
  265. kbhit() also works as a bios call. In that case it's
  266.  
  267.     return(bios(2,0));
  268.  
  269.                 H.G.L.
  270. */
  271.  
  272. }
  273.