"Fossies" - the Fresh Open Source Software Archive

Member "HTTPing-2.9/io.c" (29 Oct 2022, 3345 Bytes) of package /linux/www/HTTPing-2.9.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "io.c" see the Fossies "Dox" file reference documentation.

    1 /* Released under AGPL v3 with exception for the OpenSSL library. See license.txt */
    2 
    3 #include <errno.h>
    4 #include <libintl.h>
    5 #include <fcntl.h>
    6 #include <stdlib.h>
    7 #include <stdio.h>
    8 #include <string.h>
    9 #include <syslog.h>
   10 #include <sys/time.h>
   11 #include <sys/types.h>
   12 #include <sys/stat.h>
   13 #include <unistd.h>
   14 
   15 #include "gen.h"
   16 #include "error.h"
   17 
   18 ssize_t read_to(int fd, char *whereto, size_t len, double timeout)
   19 {
   20     for(;;)
   21     {
   22         ssize_t rc;
   23         struct timeval to;
   24         fd_set rfds;
   25 
   26         FD_ZERO(&rfds);
   27         FD_SET(fd, &rfds);
   28 
   29         to.tv_sec  = (long)(timeout / 1000.0);
   30         to.tv_usec = (long)(timeout * 1000.0) % 1000000;
   31 
   32         rc = select(fd + 1, &rfds, NULL, NULL, &to);
   33         if (rc == 0)
   34             return RC_TIMEOUT;
   35         else if (rc == -1)
   36         {
   37             if (errno == EAGAIN)
   38                 continue;
   39             if (errno == EINTR)
   40                 return RC_CTRLC;
   41 
   42             set_error(gettext("read_to::select failed: %s"), strerror(errno));
   43 
   44             return RC_SHORTREAD;
   45         }
   46 
   47         return read(fd, whereto, len);
   48     }
   49 }
   50 
   51 ssize_t myread(int fd, char *whereto, size_t len, double timeout)
   52 {
   53     ssize_t cnt=0;
   54 
   55     while(len>0)
   56     {
   57         ssize_t rc;
   58         struct timeval to;
   59         fd_set rfds;
   60 
   61         FD_ZERO(&rfds);
   62         FD_SET(fd, &rfds);
   63 
   64         to.tv_sec  = (long)(timeout / 1000.0);
   65         to.tv_usec = (long)(timeout * 1000.0) % 1000000;
   66 
   67         rc = select(fd + 1, &rfds, NULL, NULL, &to);
   68         if (rc == 0)
   69             return RC_TIMEOUT;
   70         else if (rc == -1)
   71         {
   72             if (errno == EAGAIN)
   73                 continue;
   74             if (errno == EINTR)
   75                 return RC_CTRLC;
   76 
   77             set_error(gettext("myread::select failed: %s"), strerror(errno));
   78 
   79             return RC_SHORTREAD;
   80         }
   81 
   82         if (FD_ISSET(fd, &rfds))
   83         {
   84             rc = read(fd, whereto, len);
   85 
   86             if (rc == -1)
   87             {
   88                 if (errno == EAGAIN)
   89                     continue;
   90                 if (errno == EINTR)
   91                     return RC_CTRLC;
   92 
   93                 set_error(gettext("myread::read failed: %s"), strerror(errno));
   94 
   95                 return RC_SHORTREAD;
   96             }
   97             else if (rc == 0)
   98                 break;
   99             else
  100             {
  101                 whereto += rc;
  102                 len -= rc;
  103                 cnt += rc;
  104             }
  105         }
  106     }
  107 
  108     return cnt;
  109 }
  110 
  111 ssize_t mywrite(int fd, char *wherefrom, size_t len, double timeout)
  112 {
  113     ssize_t cnt=0;
  114 
  115     while(len>0)
  116     {
  117         ssize_t rc;
  118         struct timeval to;
  119         fd_set wfds;
  120 
  121         FD_ZERO(&wfds);
  122         FD_SET(fd, &wfds);
  123 
  124         to.tv_sec  = (long)(timeout / 1000.0);
  125         to.tv_usec = (long)(timeout * 1000.0) % 1000000;
  126 
  127         rc = select(fd + 1, NULL, &wfds, NULL, &to);
  128         if (rc == 0)
  129             return RC_TIMEOUT;
  130         else if (rc == -1)
  131         {
  132             if (errno == EAGAIN)
  133                 continue;
  134             if (errno == EINTR)
  135                 return RC_CTRLC;
  136 
  137             set_error(gettext("mywrite::select failed: %s"), strerror(errno));
  138 
  139             return RC_SHORTWRITE;
  140         }
  141 
  142         rc = write(fd, wherefrom, len);
  143 
  144         if (rc == -1)
  145         {
  146             if (errno == EAGAIN)
  147                 continue;
  148             if (errno == EINTR)
  149                 return RC_CTRLC;
  150 
  151             set_error(gettext("mywrite::write failed: %s"), strerror(errno));
  152 
  153             return RC_SHORTWRITE;
  154         }
  155         else if (rc == 0)
  156             break;
  157         else
  158         {
  159             wherefrom += rc;
  160             len -= rc;
  161             cnt += rc;
  162         }
  163     }
  164 
  165     return cnt;
  166 }
  167 
  168 int set_fd_nonblocking(int fd)
  169 {
  170         /* set fd to non-blocking */
  171         if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1)
  172     {
  173         fprintf(stderr, gettext("set_fd_nonblocking failed! (%s)\n"), strerror(errno));
  174 
  175                 return -1;
  176     }
  177 
  178         return 0;
  179 }
  180 
  181 int set_fd_blocking(int fd)
  182 {
  183         /* set fd to blocking */
  184         if (fcntl(fd, F_SETFL, 0) == -1)
  185     {
  186         fprintf(stderr, gettext("set_fd_blocking failed! (%s)\n"), strerror(errno));
  187 
  188                 return -1;
  189     }
  190 
  191         return 0;
  192 }