]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/backchannel.c
Migrate Windows conditional code to _WIN32 define.
[thirdparty/cups.git] / cups / backchannel.c
1 /*
2 * Backchannel functions for CUPS.
3 *
4 * Copyright 2007-2014 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products.
6 *
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
8 */
9
10 /*
11 * Include necessary headers...
12 */
13
14 #include "cups.h"
15 #include <errno.h>
16 #ifdef _WIN32
17 # include <io.h>
18 # include <fcntl.h>
19 #else
20 # include <sys/time.h>
21 #endif /* _WIN32 */
22
23
24 /*
25 * Local functions...
26 */
27
28 static void cups_setup(fd_set *set, struct timeval *tval,
29 double timeout);
30
31
32 /*
33 * 'cupsBackChannelRead()' - Read data from the backchannel.
34 *
35 * Reads up to "bytes" bytes from the backchannel/backend. The "timeout"
36 * parameter controls how many seconds to wait for the data - use 0.0 to
37 * return immediately if there is no data, -1.0 to wait for data indefinitely.
38 *
39 * @since CUPS 1.2/macOS 10.5@
40 */
41
42 ssize_t /* O - Bytes read or -1 on error */
43 cupsBackChannelRead(char *buffer, /* I - Buffer to read into */
44 size_t bytes, /* I - Bytes to read */
45 double timeout) /* I - Timeout in seconds, typically 0.0 to poll */
46 {
47 fd_set input; /* Input set */
48 struct timeval tval; /* Timeout value */
49 int status; /* Select status */
50
51
52 /*
53 * Wait for input ready.
54 */
55
56 do
57 {
58 cups_setup(&input, &tval, timeout);
59
60 if (timeout < 0.0)
61 status = select(4, &input, NULL, NULL, NULL);
62 else
63 status = select(4, &input, NULL, NULL, &tval);
64 }
65 while (status < 0 && errno != EINTR && errno != EAGAIN);
66
67 if (status < 0)
68 return (-1); /* Timeout! */
69
70 /*
71 * Read bytes from the pipe...
72 */
73
74 #ifdef _WIN32
75 return ((ssize_t)_read(3, buffer, (unsigned)bytes));
76 #else
77 return (read(3, buffer, bytes));
78 #endif /* _WIN32 */
79 }
80
81
82 /*
83 * 'cupsBackChannelWrite()' - Write data to the backchannel.
84 *
85 * Writes "bytes" bytes to the backchannel/filter. The "timeout" parameter
86 * controls how many seconds to wait for the data to be written - use
87 * 0.0 to return immediately if the data cannot be written, -1.0 to wait
88 * indefinitely.
89 *
90 * @since CUPS 1.2/macOS 10.5@
91 */
92
93 ssize_t /* O - Bytes written or -1 on error */
94 cupsBackChannelWrite(
95 const char *buffer, /* I - Buffer to write */
96 size_t bytes, /* I - Bytes to write */
97 double timeout) /* I - Timeout in seconds, typically 1.0 */
98 {
99 fd_set output; /* Output set */
100 struct timeval tval; /* Timeout value */
101 int status; /* Select status */
102 ssize_t count; /* Current bytes */
103 size_t total; /* Total bytes */
104
105
106 /*
107 * Write all bytes...
108 */
109
110 total = 0;
111
112 while (total < bytes)
113 {
114 /*
115 * Wait for write-ready...
116 */
117
118 do
119 {
120 cups_setup(&output, &tval, timeout);
121
122 if (timeout < 0.0)
123 status = select(4, NULL, &output, NULL, NULL);
124 else
125 status = select(4, NULL, &output, NULL, &tval);
126 }
127 while (status < 0 && errno != EINTR && errno != EAGAIN);
128
129 if (status <= 0)
130 return (-1); /* Timeout! */
131
132 /*
133 * Write bytes to the pipe...
134 */
135
136 #ifdef _WIN32
137 count = (ssize_t)_write(3, buffer, (unsigned)(bytes - total));
138 #else
139 count = write(3, buffer, bytes - total);
140 #endif /* _WIN32 */
141
142 if (count < 0)
143 {
144 /*
145 * Write error - abort on fatal errors...
146 */
147
148 if (errno != EINTR && errno != EAGAIN)
149 return (-1);
150 }
151 else
152 {
153 /*
154 * Write succeeded, update buffer pointer and total count...
155 */
156
157 buffer += count;
158 total += (size_t)count;
159 }
160 }
161
162 return ((ssize_t)bytes);
163 }
164
165
166 /*
167 * 'cups_setup()' - Setup select()
168 */
169
170 static void
171 cups_setup(fd_set *set, /* I - Set for select() */
172 struct timeval *tval, /* I - Timer value */
173 double timeout) /* I - Timeout in seconds */
174 {
175 tval->tv_sec = (int)timeout;
176 tval->tv_usec = (int)(1000000.0 * (timeout - tval->tv_sec));
177
178 FD_ZERO(set);
179 FD_SET(3, set);
180 }