Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  2. #include <libc/stubs.h>
  3. #include <stdio.h>
  4. #include <sys/types.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <libc/file.h>
  8.  
  9. int
  10. _flsbuf(int c, FILE *f)
  11. {
  12.   char *base;
  13.   int n, rn;
  14.   char c1;
  15.   int size;
  16.  
  17.   if (f->_flag & _IORW)
  18.   {
  19.     f->_flag |= _IOWRT;
  20.     f->_flag &= ~(_IOEOF|_IOREAD);
  21.   }
  22.  
  23.   if ((f->_flag&_IOWRT)==0)
  24.     return EOF;
  25.  
  26.  tryagain:
  27.   if (f->_flag&_IOLBF)
  28.   {
  29.     base = f->_base;
  30.     *f->_ptr++ = c;
  31.     if ((rn = f->_ptr - base) >= f->_bufsiz || c == '\n')
  32.     {
  33.       f->_ptr = base;
  34.       f->_cnt = 0;
  35.     }
  36.     else
  37.     {
  38.       /* we got here because _cnt is wrong, so fix it */
  39.       f->_cnt = -rn;
  40.       rn = n = 0;
  41.     }
  42.   }
  43.   else
  44.     if (f->_flag&_IONBF)
  45.     {
  46.       c1 = c;
  47.       rn = 1;
  48.       base = &c1;
  49.       f->_cnt = 0;
  50.     }
  51.     else
  52.     {
  53.       if ((base=f->_base)==NULL)
  54.       {
  55.         size = 512;
  56.         if ((f->_base=base=malloc(size)) == NULL)
  57.         {
  58.           f->_flag |= _IONBF;
  59.           goto tryagain;
  60.         }
  61.         f->_flag |= _IOMYBUF;
  62.         f->_bufsiz = size;
  63.         if (f==stdout)
  64.         {
  65.           f->_flag |= _IOLBF;
  66.           f->_ptr = base;
  67.           goto tryagain;
  68.         }
  69.         rn = n = 0;
  70.       }
  71.       else
  72.         rn = f->_ptr - base;
  73.       f->_ptr = base;
  74.       f->_cnt = f->_bufsiz;
  75.     }
  76.   while (rn > 0)
  77.   {
  78.    if(f->std_ops && STM_OP(f,write))
  79.    {
  80.     n=STM_OP(f,write)(f,base,rn);
  81.    } else {
  82.     n = write(fileno(f), base, rn);
  83.    }
  84.    if (n <= 0)
  85.    {
  86.     f->_flag |= _IOERR;
  87.     return EOF;
  88.    }
  89.    rn -= n;
  90.   base += n;
  91.  }
  92.   if ((f->_flag&(_IOLBF|_IONBF)) == 0)
  93.   {
  94.     f->_cnt--;
  95.     *f->_ptr++ = c;
  96.   }
  97.   return c;
  98. }
  99.