"Fossies" - the Fresh Open Source Software Archive 
Member "amavisd-milter-1.7.2/compat/read_sock.c" (27 Jan 2019, 3468 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 ** READ_SOCK - read N bytes from socket
36 */
37 ssize_t
38 read_sock(int sd, void *buf, size_t nbytes, long timeout)
39 {
40 int ret;
41 char *b = (char *) buf;
42 fd_set rfds, efds;
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 /* Read N bytes from socket */
59 while (n < nbytes) {
60 FD_ZERO(&rfds);
61 FD_ZERO(&efds);
62 FD_SET((unsigned int)sd, &rfds);
63 FD_SET((unsigned int)sd, &efds);
64
65 /* Wait for socket */
66 ret = select(sd + 1, &rfds, NULL, &efds, &tv);
67 if (ret == -1) {
68 if (errno == EINTR) {
69 /* A signal was delivered, continue */
70 continue;
71 } else {
72 /* An error occured */
73 return -1;
74 }
75 } else if (ret == 0) {
76 /* Timeout */
77 errno = ETIMEDOUT;
78 return -1;
79 }
80 if (FD_ISSET(sd, &efds)) {
81 /* Out-of-band data received on socket */
82 errno = EIO;
83 return -1;
84 }
85
86 /* Read data from socket */
87 m = read(sd, b, nbytes - n);
88 if (m == -1) {
89 if (errno == EINTR) {
90 /* A signal was delivered, continue */
91 continue;
92 } else {
93 /* An error occured */
94 return -1;
95 }
96 } else if (m == 0) {
97 /* End of file */
98 nbytes = n;
99 } else {
100 /* Read data */
101 n += m;
102 b += m;
103 }
104 }
105
106 /* Return number of bytes */
107 return nbytes;
108 }