"Fossies" - the Fresh Open Source Software Archive

Member "amavisd-milter-1.7.2/compat/write_sock.c" (27 Jan 2019, 3167 Bytes) of package /linux/privat/amavisd-milter-1.7.2.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.

    1 /*
    2  * Copyright (c) 2005, Petr Rehor <rx@rx.cz>. All rights reserved.
    3  *
    4  * Redistribution and use in source and binary forms, with or without
    5  * modification, are permitted provided that the following conditions
    6  * are met:
    7  * 1. Redistributions of source code must retain the above copyright
    8  *    notice, this list of conditions and the following disclaimer.
    9  * 2. Redistributions in binary form must reproduce the above copyright
   10  *    notice, this list of conditions and the following disclaimer in the
   11  *    documentation and/or other materials provided with the distribution.
   12  * 3. Neither the name of the copyright holders nor the names of its
   13  *    contributors may be used to endorse or promote products derived from
   14  *    this software without specific prior written permission.
   15  *
   16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
   20  * OWNERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
   22  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
   23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
   24  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   25  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   27  */
   28 
   29 #include "compat.h"
   30 
   31 #include <errno.h>
   32 
   33 
   34 /*
   35 ** WRITE_SOCK - write N bytes to socket
   36 */
   37 ssize_t
   38 write_sock(int sd, void *buf, size_t nbytes, long timeout)
   39 {
   40     int         ret;
   41     char       *b = (char *) buf;
   42     fd_set      wfds;
   43     size_t      n = 0;
   44     ssize_t     m = 0;
   45     struct      timeval tv;
   46 
   47     /* Set timeout */
   48     tv.tv_sec = timeout;
   49     tv.tv_usec = 0;
   50 
   51     /* Check socket descriptor */
   52     if (sd >= (int) FD_SETSIZE) {
   53         /* sd is larger than FD_SETSIZE */
   54         errno = EBADF;
   55         return -1;
   56     }
   57 
   58     /* Write N bytes to socket */
   59     while (n < nbytes) {
   60         FD_ZERO(&wfds);
   61         FD_SET((unsigned int)sd, &wfds);
   62 
   63         /* Wait for socket */
   64         ret = select(sd + 1, NULL, &wfds, NULL, &tv);
   65         if (ret == -1) {
   66             if (errno == EINTR) {
   67                 /* A signal was delivered, continue */
   68                 continue;
   69             } else {
   70                 /* An error occured */
   71                 return -1;
   72             }
   73         } else if (ret == 0) {
   74             /* Timeout */
   75             errno = ETIMEDOUT;
   76             return -1;
   77         }
   78 
   79         /* Write data to socket */
   80         m = write(sd, b, nbytes - n);
   81         if (m == -1) {
   82             if (errno == EINTR) {
   83                 /* A signal was delivered, continue */
   84                 continue;
   85             } else {
   86                 /* An error occured */
   87                 return -1;
   88             }
   89         } else {
   90             /* Write data */
   91             n += m;
   92             b += m;
   93         }
   94     }
   95 
   96     /* Return number of bytes */
   97     return nbytes;
   98 }