home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff294.lzh / DNet / unix / dnet / net.c < prev    next >
C/C++ Source or Header  |  1989-12-11  |  2KB  |  112 lines

  1.  
  2. /*
  3.  *  NET.C
  4.  *
  5.  *    DNET (c)Copyright 1988, Matthew Dillon, All Rights Reserved
  6.  *
  7.  *  NetWork raw device interface.  Replace with whatever interface you
  8.  *  want.
  9.  */
  10.  
  11. #include "dnet.h"
  12. #include <sys/stat.h>
  13.  
  14. RcvInt()
  15. {
  16.     int n = read(0, RcvBuf + RcvData, RCVBUF - RcvData);
  17.     int i;
  18.     extern int errno;
  19.  
  20.     errno = 0;
  21.     if (n >= 0)
  22.     RcvData += n;
  23.     if (n <= 0)         /* disallow infinite fast-timeout select loops */
  24.     RExpect = 0;
  25.     if (DDebug)
  26.     printf("read(%d,%d)\n", n, errno);
  27. }
  28.  
  29. static struct sgttyb    ttym;
  30. static struct stat    Stat;
  31.  
  32. NetOpen()
  33. {
  34.     int async = 1;
  35.  
  36.     fstat(0, &Stat);
  37.     fchmod(0, 0600);
  38.     /*
  39.     signal(SIGIO, RcvInt);
  40.     */
  41.     ioctl (0, TIOCGETP, &ttym);
  42.     if (Mode7) {
  43.         ttym.sg_flags &= ~RAW;
  44.         ttym.sg_flags |= CBREAK;
  45.     } else {
  46.         ttym.sg_flags |= RAW;
  47.         ttym.sg_flags &= ~CBREAK;
  48.     }
  49.     ttym.sg_flags &= ~ECHO;
  50.     ioctl (0, TIOCSETP, &ttym);
  51.     /*
  52.     ioctl (0, FIOASYNC, &async);
  53.     */
  54.     ioctl (0, FIONBIO, &async);
  55. }
  56.  
  57. NetClose()
  58. {
  59.     int async = 0;
  60.  
  61.     fchmod(0, Stat.st_mode);
  62.     ioctl (0, FIONBIO, &async);
  63.     /*
  64.     ioctl (0, FIOASYNC, &async);
  65.     */
  66.     ioctl (0, TIOCGETP, &ttym);
  67.     ttym.sg_flags &= ~RAW;
  68.     ttym.sg_flags |= ECHO;
  69.     ioctl (0, TIOCSETP, &ttym);
  70. }
  71.  
  72. NetWrite(buf, bytes)
  73. ubyte *buf;
  74. {
  75.     if (DDebug)
  76.     fprintf(stderr, "NETWRITE %08lx %ld\n", buf, bytes);
  77.     gwrite(0, buf, bytes);
  78. }
  79.  
  80. gwrite(fd, buf, bytes)
  81. register char *buf;
  82. register long bytes;
  83. {
  84.     register long n;
  85.     while (bytes) {
  86.     n = write(fd, buf, bytes);
  87.     if (n > 0) {
  88.         bytes -= n;
  89.         buf += n;
  90.         continue;
  91.     }
  92.     if (errno == EINTR)
  93.         continue;
  94.     if (errno == EWOULDBLOCK) {
  95.         fd_set fd_wr;
  96.         fd_set fd_ex;
  97.         FD_ZERO(&fd_wr);
  98.         FD_ZERO(&fd_ex);
  99.         FD_SET(fd, &fd_wr);
  100.         FD_SET(fd, &fd_ex);
  101.         if (select(fd+1, NULL, &fd_wr, &fd_ex, NULL) < 0) {
  102.          perror("select");
  103.         }
  104.         continue;
  105.     }
  106.     if (errno == EPIPE)
  107.         return;
  108.     dneterror("gwrite");
  109.     }
  110. }
  111.  
  112.