home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / CPM / BDSC / BDSC-4 / BDSLIB.ARK / FGETC.C < prev    next >
Text File  |  1983-07-15  |  3KB  |  120 lines

  1. /*
  2.  * fgetc
  3.  * Conforms closely to the standard i/o function used in the Version 7 
  4.  * U**X C Library but takes careful note of the CP/M console and reader
  5.  * devices. The structure of the hypothetical getc macro corresponding
  6.  * to this function is as follows:
  7.  * #define getc(p) ((p)->_nleft-- ? *(p)->_nextp++ : _filbuf(p))
  8.  * Last Edit 7/3/83
  9.  */
  10.  
  11. int
  12. fgetc(stream)
  13. FILE *stream;
  14. {
  15.     int nsecs;
  16.     char c;
  17.     if (stream == stdin) {
  18.         c = bdos(1,0);
  19.         return ((c == '\r') ? '\n' : c) ;
  20.     }
  21.     else if (stream == DEV_RDR)
  22.         return bdos(3,0);
  23.     else if (!stream -> _nleft--) {
  24.         if((nsecs = read(stream->_fd, stream->_base, NSECTS)) <= 0){
  25.             if (stream -> _nleft == EOF)
  26.                 stream -> _flag = _IOEOF ;
  27.             if (nsecs == ERROR)
  28.                 stream -> _flag |= _IOERR ;
  29.             return stream -> _nleft++;
  30.         }
  31.         stream -> _nleft = nsecs * SECSIZ - 1;
  32.         stream -> _nextp = stream -> _base;
  33.     }
  34.     return *stream->_nextp++;
  35. }
  36.  
  37. /*
  38.  * fputc
  39.  * This function conforms closely to the function in the Version
  40.  * 7 U**X C Library with consideraton of the CPM console and reader
  41.  * devices. The hypothetical corresponding putc macro follows:
  42.  * #define putc(c,p) ((p)->_nleft--?*(p)->_nextp++ = (c):_flsbuf((x),p))
  43.  * Last Edit 7/1/83
  44.  */
  45.  
  46. void
  47. fputc(c,stream)
  48. char c;
  49. FILE *stream;
  50. {
  51.     if (stream==stdout) {
  52.         if (c == '\n')
  53.             bdos(2,'\r');
  54.         bdos(2,c);
  55.         return ;
  56.     }
  57.     else if (stream==DEV_LST) {
  58.         bdos(5,c);
  59.         return ;
  60.     }
  61.     else if (stream==DEV_PUN) {
  62.         bdos(4,c);
  63.         return ;
  64.     }
  65.     else if (stream==stderr) {
  66.         if (c == '\n')
  67.             bios(4,'\r');
  68.         bios(4,c);
  69.         return ;
  70.     }
  71.     if ((stream -> _flag & _IOWRT) == 0) { /* can't write on stream    */
  72.         stream -> _flag |= _IOERR ;
  73.         return ;
  74.     }
  75.     if (!stream -> _nleft--) {    /* if buffer full then flush it    */
  76.         if ((write(stream->_fd, stream->_base, NSECTS)) != NSECTS) {
  77.             stream -> _flag |= _IOERR ;
  78.             return ;
  79.         }
  80.         stream -> _nleft = (BUFSIZ - 1);
  81.         stream -> _nextp = stream -> _base;
  82.     }
  83.     *stream -> _nextp++ = c;
  84.     return ;
  85. }
  86.  
  87. /*
  88.  * ungetc
  89.  * This function conforms closely to the macro used in the Version
  90.  * 7 U**X Standard C Libary. It is a buffered "unget" character
  91.  * routine. Only ONE char may be "ungotten" between consecutive
  92.  * getc()/fgetc() calls. Note that any ungotten character is lost
  93.  * if a call subsquently made to fseek(). The macro corresponding
  94.  * the function is as follows:
  95.  * #define ungetc(c,p) ((p)->_nleft==BUFSIZ ? -1 : *--(p)->_nextp=(c))
  96.  * Last Edit 7/3/83
  97.  */
  98.  
  99. void
  100. ungetc(c, stream)
  101. FILE *stream;
  102. char c;
  103. {
  104.     if (stream == stdin) {
  105.         ungetch(c);            /* stdin is special    */
  106.         return ;
  107.     }
  108.     else if (stream <= DEV_PUN && stream > stdin) {
  109.         /* stream -> _flag |= _IOERR ;     no pushbacks here    */
  110.         return ;
  111.     }
  112.     if (stream -> _nleft == BUFSIZ) {
  113.         stream -> _flag |= _IOERR ;    /* too many pushed back    */
  114.         return ;
  115.     }
  116.     *--stream -> _nextp = c ;
  117.     stream -> _nleft++ ;
  118.     return ;
  119. }
  120.