home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mntlib32.zoo / write.c < prev    next >
C/C++ Source or Header  |  1993-06-17  |  1KB  |  75 lines

  1. /*
  2.  * _write: like write, but takes a long instead of an int. Written by
  3.  * Eric R. Smith and placed in the public domain.
  4.  */
  5.  
  6. /* BUG: under TOS, CRMOD doesn't work unless RAW is on or SIGINT is
  7.  * being caught
  8.  */
  9.  
  10. #include <osbind.h>
  11. #include <fcntl.h>
  12. #include <ioctl.h>
  13. #include <errno.h>
  14. #include <unistd.h>
  15. #include <signal.h>
  16. #include <stat.h>
  17. #include "lib.h"
  18.  
  19. extern __Sigfunc _sig_handler[]; /* TOS fakes for signal handling */
  20.  
  21. long
  22. _write(fd, buf, size)
  23.     int fd;
  24.     const void *buf;
  25.     unsigned long size;
  26. {
  27.     unsigned char c, *foo;
  28.     unsigned flags;
  29.     long r;
  30.     extern int __mint;
  31.     struct stat statbuf;
  32.  
  33.     if (__mint == 0 && isatty(fd)) {
  34.         r = __OPEN_INDEX(fd);
  35.         if (r < 0 || r >= __NHANDLES)
  36.             r = __NHANDLES - 1;
  37.         flags = __open_stat[r].flags;
  38.         if ((flags & RAW) || _sig_handler[SIGINT] != SIG_DFL) {
  39.             foo = (unsigned char *) buf;
  40.             r = size;
  41.             while (r-- > 0) {
  42.                 c = *foo++;
  43.                 if (c == '\n' && (flags & CRMOD))
  44.                     _console_write_byte(fd, '\r');
  45.                 _console_write_byte(fd, c);
  46.             }
  47.             return size;
  48.         }
  49.     }
  50.  
  51.     r = Fwrite(fd, size, buf);
  52.     if (r < 0) {
  53.         errno = (int) -r;
  54.         return -1;
  55.     }
  56.     if (size && r == 0) {
  57.         if (fstat(fd, &statbuf) < 0) return -1;
  58.         if ((statbuf.st_mode & S_IFMT) == S_IFREG) {
  59.             errno = ENOSPC;
  60.             return -1;
  61.         }
  62.     }
  63.  
  64.     return r;
  65. }
  66.  
  67. int
  68. write(fd, buf, size)
  69.     int fd;
  70.     const void *buf;
  71.     unsigned size;
  72. {
  73.     return (int) _write(fd, buf, (unsigned long)size);
  74. }
  75.