strclinonb.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* include nonb1 */
  2. #include "unp.h"
  3. void
  4. str_cli(FILE *fp, int sockfd)
  5. {
  6. int maxfdp1, val, stdineof;
  7. ssize_t n, nwritten;
  8. fd_set rset, wset;
  9. char to[MAXLINE], fr[MAXLINE];
  10. char *toiptr, *tooptr, *friptr, *froptr;
  11. val = Fcntl(sockfd, F_GETFL, 0);
  12. Fcntl(sockfd, F_SETFL, val | O_NONBLOCK);
  13. val = Fcntl(STDIN_FILENO, F_GETFL, 0);
  14. Fcntl(STDIN_FILENO, F_SETFL, val | O_NONBLOCK);
  15. val = Fcntl(STDOUT_FILENO, F_GETFL, 0);
  16. Fcntl(STDOUT_FILENO, F_SETFL, val | O_NONBLOCK);
  17. toiptr = tooptr = to; /* initialize buffer pointers */
  18. friptr = froptr = fr;
  19. stdineof = 0;
  20. maxfdp1 = max(max(STDIN_FILENO, STDOUT_FILENO), sockfd) + 1;
  21. for ( ; ; ) {
  22. FD_ZERO(&rset);
  23. FD_ZERO(&wset);
  24. if (stdineof == 0 && toiptr < &to[MAXLINE])
  25. FD_SET(STDIN_FILENO, &rset); /* read from stdin */
  26. if (friptr < &fr[MAXLINE])
  27. FD_SET(sockfd, &rset); /* read from socket */
  28. if (tooptr != toiptr)
  29. FD_SET(sockfd, &wset); /* data to write to socket */
  30. if (froptr != friptr)
  31. FD_SET(STDOUT_FILENO, &wset); /* data to write to stdout */
  32. Select(maxfdp1, &rset, &wset, NULL, NULL);
  33. /* end nonb1 */
  34. /* include nonb2 */
  35. if (FD_ISSET(STDIN_FILENO, &rset)) {
  36. if ( (n = read(STDIN_FILENO, toiptr, &to[MAXLINE] - toiptr)) < 0) {
  37. if (errno != EWOULDBLOCK)
  38. err_sys("read error on stdin");
  39. } else if (n == 0) {
  40. #ifdef VOL2
  41. fprintf(stderr, "%s: EOF on stdin\n", gf_time());
  42. #endif
  43. stdineof = 1; /* all done with stdin */
  44. if (tooptr == toiptr)
  45. Shutdown(sockfd, SHUT_WR);/* send FIN */
  46. } else {
  47. #ifdef VOL2
  48. fprintf(stderr, "%s: read %d bytes from stdin\n", gf_time(), n);
  49. #endif
  50. toiptr += n; /* # just read */
  51. FD_SET(sockfd, &wset); /* try and write to socket below */
  52. }
  53. }
  54. if (FD_ISSET(sockfd, &rset)) {
  55. if ( (n = read(sockfd, friptr, &fr[MAXLINE] - friptr)) < 0) {
  56. if (errno != EWOULDBLOCK)
  57. err_sys("read error on socket");
  58. } else if (n == 0) {
  59. #ifdef VOL2
  60. fprintf(stderr, "%s: EOF on socket\n", gf_time());
  61. #endif
  62. if (stdineof)
  63. return; /* normal termination */
  64. else
  65. err_quit("str_cli: server terminated prematurely");
  66. } else {
  67. #ifdef VOL2
  68. fprintf(stderr, "%s: read %d bytes from socket\n",
  69. gf_time(), n);
  70. #endif
  71. friptr += n; /* # just read */
  72. FD_SET(STDOUT_FILENO, &wset); /* try and write below */
  73. }
  74. }
  75. /* end nonb2 */
  76. /* include nonb3 */
  77. if (FD_ISSET(STDOUT_FILENO, &wset) && ( (n = friptr - froptr) > 0)) {
  78. if ( (nwritten = write(STDOUT_FILENO, froptr, n)) < 0) {
  79. if (errno != EWOULDBLOCK)
  80. err_sys("write error to stdout");
  81. } else {
  82. #ifdef VOL2
  83. fprintf(stderr, "%s: wrote %d bytes to stdout\n",
  84. gf_time(), nwritten);
  85. #endif
  86. froptr += nwritten; /* # just written */
  87. if (froptr == friptr)
  88. froptr = friptr = fr; /* back to beginning of buffer */
  89. }
  90. }
  91. if (FD_ISSET(sockfd, &wset) && ( (n = toiptr - tooptr) > 0)) {
  92. if ( (nwritten = write(sockfd, tooptr, n)) < 0) {
  93. if (errno != EWOULDBLOCK)
  94. err_sys("write error to socket");
  95. } else {
  96. #ifdef VOL2
  97. fprintf(stderr, "%s: wrote %d bytes to socket\n",
  98. gf_time(), nwritten);
  99. #endif
  100. tooptr += nwritten; /* # just written */
  101. if (tooptr == toiptr) {
  102. toiptr = tooptr = to; /* back to beginning of buffer */
  103. if (stdineof)
  104. Shutdown(sockfd, SHUT_WR); /* send FIN */
  105. }
  106. }
  107. }
  108. }
  109. }
  110. /* end nonb3 */