home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / dev / gcc / ixemulsrc.lha / ixemul / library / __write.c < prev    next >
C/C++ Source or Header  |  1996-12-11  |  3KB  |  113 lines

  1. /*
  2.  *  This file is part of ixemul.library for the Amiga.
  3.  *  Copyright (C) 1991, 1992  Markus M. Wild
  4.  *
  5.  *  This library is free software; you can redistribute it and/or
  6.  *  modify it under the terms of the GNU Library General Public
  7.  *  License as published by the Free Software Foundation; either
  8.  *  version 2 of the License, or (at your option) any later version.
  9.  *
  10.  *  This library is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  *  Library General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU Library General Public
  16.  *  License along with this library; if not, write to the Free
  17.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  *  __swrite.c,v 1.1.1.1 1994/04/04 04:30:12 amiga Exp
  20.  *
  21.  *  __swrite.c,v
  22.  * Revision 1.1.1.1  1994/04/04  04:30:12  amiga
  23.  * Initial CVS check in.
  24.  *
  25.  *  Revision 1.1  1992/05/14  19:55:40  mwild
  26.  *  Initial revision
  27.  *
  28.  */
  29.  
  30. #define _KERNEL
  31. #include "ixemul.h"
  32. #include "kprintf.h"
  33.  
  34. static int __do_sync_write(struct file *f, char *buf, int len)
  35. {
  36.   int err=errno, res = 0;
  37.   int omask;
  38.  
  39.   omask = syscall (SYS_sigsetmask, ~0);
  40.   __get_file (f);
  41.  
  42.   if (len > 0)
  43.     {
  44.       /* full append-mode means, before each write do an explicit
  45.        * seek to eof */
  46.       if (f->f_flags & FAPPEND)
  47.     {
  48.       SendPacket3(f,__srwport,ACTION_SEEK,f->f_fh->fh_Arg1,0,OFFSET_END);
  49.           __wait_sync_packet(&f->f_sp);
  50.     }
  51.  
  52.       SendPacket3(f,__srwport,ACTION_WRITE,f->f_fh->fh_Arg1,(long)buf,len);
  53.       __wait_sync_packet(&f->f_sp);
  54.       res = LastResult(f);
  55.       if (res == -1) err = __ioerr_to_errno(LastError(f));
  56.     }
  57.  
  58.   LastResult(f) = 0;
  59.   __release_file (f);
  60.   syscall (SYS_sigsetmask, omask);
  61.   errno = err;
  62.   KPRINTF (("&errno = %lx, errno = %ld\n", &errno, errno));
  63.   return res;
  64. }
  65.  
  66. int __write(struct file *f, char *buf, int len)
  67. {
  68.   char *p;
  69.   int l, bytes, res = 0, tmp;
  70.  
  71.   if (HANDLER_NIL(f))
  72.     return len;
  73.   if (!f->f_fh->fh_Port)    /* if not interactive */
  74.     return __do_sync_write(f, buf, len);
  75.  
  76.   /* write the buffer line by line, otherwise the user isn't able to stop
  77.      the console output until the whole buffer was flushed to the console */
  78.   /* also check the f->f_ttyflags */
  79.  
  80. #define TTY_NLCR_ENABLE (IXTTY_RAW | IXTTY_OPOST | IXTTY_ONLCR)
  81.  
  82.   for (p = buf, l = 0; l < len; p += bytes, l += bytes)
  83.     {
  84.       if (p[0] == '\n')
  85.         {
  86.           bytes = 1;
  87.           if ((f->f_ttyflags & TTY_NLCR_ENABLE) == TTY_NLCR_ENABLE)
  88.             {
  89.               tmp = __do_sync_write(f, "\r\n", 2);
  90.               if (tmp == -1)
  91.                 return tmp;
  92.               tmp = (tmp == 2 ? 1 : 0);
  93.               res += tmp;
  94.               if (tmp != bytes)
  95.                 return res;
  96.               continue;
  97.             }
  98.         }
  99.       else
  100.         for (bytes = 0; l + bytes < len && p[bytes] != '\n' && bytes < 256; bytes++) ;
  101.       if (bytes)
  102.         {
  103.           tmp = __do_sync_write(f, p, bytes);
  104.           if (tmp == -1)
  105.             return tmp;
  106.           res += tmp;
  107.           if (tmp != bytes)
  108.             return res;
  109.         }
  110.     }
  111.   return res;
  112. }
  113.