]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/backchannel.c
Migrate Windows conditional code to _WIN32 define.
[thirdparty/cups.git] / cups / backchannel.c
CommitLineData
ef416fc2 1/*
7e86f2f6 2 * Backchannel functions for CUPS.
ef416fc2 3 *
7e86f2f6
MS
4 * Copyright 2007-2014 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products.
ef416fc2 6 *
e3101897 7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
ef416fc2 8 */
9
10/*
11 * Include necessary headers...
12 */
13
14#include "cups.h"
15#include <errno.h>
19dc16f7 16#ifdef _WIN32
ef416fc2 17# include <io.h>
18# include <fcntl.h>
19#else
20# include <sys/time.h>
19dc16f7 21#endif /* _WIN32 */
ef416fc2 22
23
24/*
25 * Local functions...
26 */
27
28static void cups_setup(fd_set *set, struct timeval *tval,
29 double timeout);
30
31
32/*
33 * 'cupsBackChannelRead()' - Read data from the backchannel.
34 *
79e1d494
MS
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.
ef416fc2 38 *
8072030b 39 * @since CUPS 1.2/macOS 10.5@
ef416fc2 40 */
41
ecdc0628 42ssize_t /* O - Bytes read or -1 on error */
79e1d494 43cupsBackChannelRead(char *buffer, /* I - Buffer to read into */
ecdc0628 44 size_t bytes, /* I - Bytes to read */
79e1d494 45 double timeout) /* I - Timeout in seconds, typically 0.0 to poll */
ef416fc2 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 }
e07d4801 65 while (status < 0 && errno != EINTR && errno != EAGAIN);
ef416fc2 66
67 if (status < 0)
68 return (-1); /* Timeout! */
69
70 /*
71 * Read bytes from the pipe...
72 */
73
19dc16f7 74#ifdef _WIN32
536bc2c6 75 return ((ssize_t)_read(3, buffer, (unsigned)bytes));
b86bc4cf 76#else
ef416fc2 77 return (read(3, buffer, bytes));
19dc16f7 78#endif /* _WIN32 */
ef416fc2 79}
80
81
82/*
83 * 'cupsBackChannelWrite()' - Write data to the backchannel.
84 *
79e1d494 85 * Writes "bytes" bytes to the backchannel/filter. The "timeout" parameter
ef416fc2 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 *
8072030b 90 * @since CUPS 1.2/macOS 10.5@
ef416fc2 91 */
92
ecdc0628 93ssize_t /* O - Bytes written or -1 on error */
bd7854cb 94cupsBackChannelWrite(
ef416fc2 95 const char *buffer, /* I - Buffer to write */
ecdc0628 96 size_t bytes, /* I - Bytes to write */
79e1d494 97 double timeout) /* I - Timeout in seconds, typically 1.0 */
ef416fc2 98{
99 fd_set output; /* Output set */
100 struct timeval tval; /* Timeout value */
101 int status; /* Select status */
ecdc0628 102 ssize_t count; /* Current bytes */
103 size_t total; /* Total bytes */
ef416fc2 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 }
e07d4801 127 while (status < 0 && errno != EINTR && errno != EAGAIN);
ef416fc2 128
e07d4801 129 if (status <= 0)
ef416fc2 130 return (-1); /* Timeout! */
131
132 /*
133 * Write bytes to the pipe...
134 */
135
19dc16f7 136#ifdef _WIN32
536bc2c6 137 count = (ssize_t)_write(3, buffer, (unsigned)(bytes - total));
b86bc4cf 138#else
ef416fc2 139 count = write(3, buffer, bytes - total);
19dc16f7 140#endif /* _WIN32 */
ef416fc2 141
142 if (count < 0)
143 {
144 /*
145 * Write error - abort on fatal errors...
146 */
147
e07d4801 148 if (errno != EINTR && errno != EAGAIN)
ef416fc2 149 return (-1);
150 }
151 else
152 {
153 /*
154 * Write succeeded, update buffer pointer and total count...
155 */
156
157 buffer += count;
7e86f2f6 158 total += (size_t)count;
ef416fc2 159 }
160 }
161
b86bc4cf 162 return ((ssize_t)bytes);
ef416fc2 163}
164
165
166/*
f3c17241 167 * 'cups_setup()' - Setup select()
ef416fc2 168 */
169
170static void
171cups_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}