home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / gnulib / libsrc98.zoo / write.c < prev   
Encoding:
C/C++ Source or Header  |  1992-01-29  |  2.1 KB  |  116 lines

  1. /*
  2.  *        Cross Development System for Atari ST 
  3.  *     Copyright (c) 1988, Memorial University of Newfoundland
  4.  *
  5.  * See Changelog for further history    ++jrb
  6.  *
  7.  * 1.5  ERS -- modified for 16 bit compilers
  8.  * 1.4  ERS -- added errno handling, fixed up calls to console_write_byte
  9.  *
  10.  * 1.3    jrd
  11.  *
  12.  * Revision 1.2  88/02/03  22:56:45  m68k
  13.  * Added unix like tty driver stuff
  14.  * 
  15.  * Revision 1.1  88/01/29  17:32:06  m68k
  16.  * Initial revision
  17.  * 
  18.  */
  19. #include    <stddef.h>
  20. #include    <errno.h>
  21. #include    <osbind.h>
  22. #include    <ioctl.h>
  23. #include    <tchars.h>
  24. #include    <unistd.h>
  25. #include    <string.h>
  26. #include    <device.h>
  27. #include    <limits.h>
  28. #include     <fcntl.h>
  29. #include    "lib.h"
  30.  
  31. int    __col_pos = 0;
  32.  
  33. #ifdef __MSHORT__
  34. int
  35. write(fd, buf, nbytes)
  36.     int        fd;
  37.     const void    *buf;
  38.     unsigned int    nbytes;
  39. {
  40.        return _write(fd, buf, (unsigned long)nbytes);
  41. }
  42. #else
  43.  
  44. asm(".stabs \"_write\",5,0,0,__write"); /* dept of clean tricks */
  45.  
  46. #endif
  47. long
  48. _write(fd, buf, n)
  49.       int     fd;
  50.       const void *buf;
  51.       unsigned long  n;
  52. {
  53.   long nbytes;
  54.   const char    *s;
  55.   long i;
  56.   long result;
  57.   struct _device *dev;
  58.  
  59.   if(n > ((unsigned long)LONG_MAX))
  60.   {
  61.     errno = EBADARG;
  62.     return -1L;
  63.   }
  64.   nbytes = (long)n;
  65.   if ((dev = _dev_fd(fd)) && dev->write)
  66.     return (*dev->write)(fd, buf, nbytes);
  67.  
  68.   if (!isatty(fd)) {
  69.     int f = __OPEN_INDEX(fd);
  70.     
  71.     if( (f >= __NHANDLES) || (f < 0) )
  72.     {
  73.         errno = EBADF;
  74.         return -1;
  75.     }
  76.  
  77.     if (__open_stat[f].append) { /* if O_APPEND */
  78.         result = Fseek(0L, fd, SEEK_END);
  79.             if (result < 0) {
  80.                 errno = -result;
  81.                 return(-1L);
  82.             }
  83.     }
  84.  
  85.         result = Fwrite(fd, nbytes, buf);
  86.     if (result < 0) {
  87.         errno = -result;
  88.         return -1L;
  89.     }
  90.     else return result;
  91.   }
  92.  
  93.   i = nbytes;
  94.   for (s = buf; i > 0 ; s++, i-- ) {
  95.     if (*s == '\n' && (__ttymode & CRMOD) && !(__ttymode & RAW)) 
  96.         {
  97.         console_write_byte(fd, '\r');
  98.         console_write_byte(fd, '\n');
  99.         __col_pos = 0;
  100.         } 
  101.         else
  102.         {
  103.         if (*s == '\r')
  104.             __col_pos = 0;
  105.             else
  106.         if (*(unsigned char *)s >= ' ')
  107.             __col_pos++;
  108.             else
  109.         if (*s == '\t')
  110.             __col_pos = (__col_pos | 7) + 1;
  111.         console_write_byte(fd, *s);
  112.         }
  113.   }
  114.   return nbytes;
  115. }
  116.