home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume26 / socket / io.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-07  |  3.0 KB  |  132 lines

  1. /*
  2.  
  3. $Header: io.c[1.10] Sun Aug 30 19:21:18 1992 nickel@cs.tu-berlin.de proposed $
  4. This file is part of socket(1).
  5. Copyright (C) 1992 by Juergen Nickelsen <nickel@cs.tu-berlin.de>
  6. Please read the file COPYRIGHT for further details.
  7.  
  8. */
  9.  
  10. #define _BSD            /* AIX *loves* this */
  11.  
  12. #include <sys/types.h>
  13. #include <sys/time.h>
  14. #ifdef ISC
  15. #include <sys/bsdtypes.h>
  16. #endif
  17. #include <errno.h>
  18. #include <stdio.h>
  19. #include "globals.h"
  20.  
  21. /* read from from, write to to. select(2) has returned, so input
  22.  * must be available. */
  23. int do_read_write(from, to)
  24. int from, to ;
  25. {
  26.     int size ;
  27.     char input_buffer[BUFSIZ] ;
  28.     
  29.     if ((size = read(from, input_buffer, BUFSIZ)) == -1) {
  30.     perror2("read") ;
  31.     return -1 ;
  32.     }
  33.     if (size == 0) {        /* end-of-file condition */
  34.     if (from == active_socket) {
  35.         /* if it was the socket, the connection is closed */
  36.         if (verboseflag) {
  37.         fprintf(stderr, "connection closed by peer\n") ;
  38.         }
  39.         return -1 ;
  40.     } else {
  41.         if (quitflag) {
  42.         /* we close connection later */
  43.         if (verboseflag) {
  44.             fprintf(stderr, "connection closed\n") ;
  45.         }
  46.         return -1 ;
  47.         } else if (verboseflag) {
  48.         fprintf(stderr, "end of input on stdin\n") ;
  49.         }
  50.         readonlyflag = 1 ;
  51.         return 1 ;
  52.     }
  53.     }
  54.     return do_write(input_buffer, size, to) ;
  55.  
  56. }
  57.  
  58. /* write the buffer; in successive pieces, if necessary. */
  59. int do_write(buffer, size, to)
  60. char *buffer ;
  61. int size, to ;
  62. {
  63.     char buffer2[2 * BUFSIZ] ;    /* expanding lf's to crlf's can
  64.                  * make the block twice as big at most */
  65.     int written ;
  66.  
  67.     if (crlfflag) {
  68.     if (to == active_socket) {
  69.         add_crs(buffer, buffer2, &size) ;
  70.     } else {
  71.         strip_crs(buffer, buffer2, &size) ;
  72.     }
  73.     } else {
  74.     bcopy(buffer, buffer2, size) ;
  75.     }
  76.     while (size > 0) {
  77.     written = write(to, buffer2, size) ;
  78.     if (written == -1) {
  79.         /* this should not happen */
  80.         perror2("write") ;
  81.         fprintf(stderr, "%s: error writing to %s\n",
  82.             progname,
  83.             to == active_socket ? "socket" : "stdout") ;
  84.         return -1 ;
  85.     }
  86.     size -= written ;
  87.     }
  88.     return 1 ;
  89. }
  90.  
  91. /* all IO to and from the socket is handled here. The main part is
  92.  * a loop around select(2). */
  93. do_io()
  94. {
  95.     fd_set readfds ;
  96.     int fdset_width ;
  97.     int selret ;
  98.  
  99.     fdset_width = (IN > active_socket ? IN : active_socket) + 1 ;
  100.     while (1) {            /* this loop is exited sideways */
  101.     /* set up file descriptor set for select(2) */
  102.     FD_ZERO(&readfds) ;
  103.     if (!readonlyflag) {
  104.         FD_SET(IN, &readfds) ;
  105.     }
  106.     if (!writeonlyflag) {
  107.         FD_SET(active_socket, &readfds) ;
  108.     }
  109.  
  110.     do {
  111.         /* wait until input is available */
  112.         selret = select(fdset_width, &readfds, NULL, NULL, NULL) ;
  113.         /* EINTR happens when the process is stopped */
  114.         if (selret < 0 && errno != EINTR) {
  115.         perror2("select") ;
  116.         exit(1) ;
  117.         }
  118.     } while (selret <= 0) ;
  119.  
  120.     /* do the appropriate read and write */
  121.     if (FD_ISSET(active_socket, &readfds)) {
  122.         if (do_read_write(active_socket, OUT) < 0) {
  123.         break ;
  124.         }
  125.     } else {
  126.         if (do_read_write(IN, active_socket) < 0) {
  127.         break ;
  128.         }
  129.     }
  130.     }
  131. }
  132.