home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 239.lha / Unix2src.shar / dnet / net.c < prev   
C/C++ Source or Header  |  1989-05-02  |  2KB  |  104 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.     if (n >= 0)
  18.     RcvData += n;
  19.     if (n <= 0)         /* disallow infinite fast-timeout select loops */
  20.     RExpect = 0;
  21.     if (DDebug && n < 0) {
  22.     write(2, "RcvInt ERR\n", 11);
  23.     }
  24. }
  25.  
  26. static struct sgttyb    ttym;
  27. static struct stat    Stat;
  28.  
  29. NetOpen()
  30. {
  31.     int async = 1;
  32.  
  33.     fstat(0, &Stat);
  34.     fchmod(0, 0600);
  35.     /*
  36.     signal(SIGIO, RcvInt);
  37.     */
  38.     ioctl (0, TIOCGETP, &ttym);
  39.     ttym.sg_flags |= RAW;
  40.     ttym.sg_flags &= ~CBREAK;
  41.     ttym.sg_flags &= ~ECHO;
  42.     ioctl (0, TIOCSETP, &ttym);
  43.     /*
  44.     ioctl (0, FIOASYNC, &async);
  45.     */
  46.     ioctl (0, FIONBIO, &async);
  47. }
  48.  
  49. NetClose()
  50. {
  51.     int async = 0;
  52.  
  53.     fchmod(0, Stat.st_mode);
  54.     ioctl (0, FIONBIO, &async);
  55.     /*
  56.     ioctl (0, FIOASYNC, &async);
  57.     */
  58.     ioctl (0, TIOCGETP, &ttym);
  59.     ttym.sg_flags &= ~RAW;
  60.     ttym.sg_flags |= ECHO;
  61.     ioctl (0, TIOCSETP, &ttym);
  62. }
  63.  
  64. NetWrite(buf, bytes, expectreply)
  65. ubyte *buf;
  66. {
  67.     if (expectreply)
  68.     DidWrite = 1;
  69.     if (DDebug)
  70.     fprintf(stderr, "NETWRITE %08lx %ld\n", buf, bytes);
  71.     gwrite(0, buf, bytes);
  72. }
  73.  
  74. gwrite(fd, buf, bytes)
  75. register char *buf;
  76. register long bytes;
  77. {
  78.     register long n;
  79.     while (bytes) {
  80.     n = write(fd, buf, bytes);
  81.     if (n > 0) {
  82.         bytes -= n;
  83.         buf += n;
  84.         continue;
  85.     }
  86.     if (errno == EINTR)
  87.         continue;
  88.     if (errno == EWOULDBLOCK) {
  89.         fd_set fd_wr;
  90.         fd_set fd_ex;
  91.         FD_ZERO(&fd_wr);
  92.         FD_ZERO(&fd_ex);
  93.         FD_SET(fd, &fd_wr);
  94.         FD_SET(fd, &fd_ex);
  95.         select(fd+1, NULL, &fd_wr, &fd_ex, NULL);
  96.         continue;
  97.     }
  98.     if (errno == EPIPE)
  99.         return;
  100.     dneterror("gwrite");
  101.     }
  102. }
  103.  
  104.