Advertisement
Mukmin039

nf_getc.c

Apr 8th, 2022
1,238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pic 16 0.95 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #include "stdio_impl.h"
  4.  
  5. #ifdef STDIO_NO_FILE_IO
  6. /* "No file system" fgetc, ungetc */
  7.  
  8. static char ungetbuf[UNGET];
  9. static char ungetcnt = 0;
  10.  
  11. int fgetc(FILE *fp)
  12. {
  13.     int c;
  14.      extern int getch(void);
  15.  
  16.     if (fp == stdin) {
  17.         if (ungetcnt) {
  18.             c = ungetbuf[--ungetcnt];
  19.         } else {
  20.             c = getch();
  21.         }
  22.     } else {
  23.         if (fp->ungetcnt) {
  24.             c = fp->ungetbuf[--fp->ungetcnt];
  25.         } else {
  26.             c = fp->source[fp->count];
  27.             if (c == '\0') {
  28.                 c = EOF;
  29.             } else {
  30.                 ++fp->count;
  31.             }
  32.         }
  33.     }
  34.     return c;
  35. }
  36.  
  37. int ungetc(int c, FILE *fp)
  38. {
  39.     if (c == EOF) {
  40.         return EOF;
  41.     }
  42.     if (fp == stdin) {
  43.         if (ungetcnt != UNGET) {
  44.             ungetbuf[ungetcnt++] = (char)c;
  45.         }
  46.         else {
  47.             return EOF;
  48.         }
  49.     } else {
  50.         if (fp->ungetcnt != UNGET) {
  51.             fp->ungetbuf[fp->ungetcnt++] = (char)c;
  52.         } else {
  53.             return EOF;
  54.         }
  55.     }
  56.     return (unsigned char)c;
  57. }
  58.  
  59. #endif
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement