]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/backchannel.c
Full sweep of all Clang warnings, plus some bug fixes for incorrect memcpy usage.
[thirdparty/cups.git] / cups / backchannel.c
CommitLineData
ef416fc2 1/*
f2d18633 2 * "$Id$"
ef416fc2 3 *
7e86f2f6 4 * Backchannel functions for CUPS.
ef416fc2 5 *
7e86f2f6
MS
6 * Copyright 2007-2014 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products.
ef416fc2 8 *
7e86f2f6
MS
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
ef416fc2 14 *
7e86f2f6 15 * This file is subject to the Apple OS-Developed Software exception.
ef416fc2 16 */
17
18/*
19 * Include necessary headers...
20 */
21
22#include "cups.h"
23#include <errno.h>
24#ifdef WIN32
25# include <io.h>
26# include <fcntl.h>
27#else
28# include <sys/time.h>
29#endif /* WIN32 */
30
31
32/*
33 * Local functions...
34 */
35
36static void cups_setup(fd_set *set, struct timeval *tval,
37 double timeout);
38
39
40/*
41 * 'cupsBackChannelRead()' - Read data from the backchannel.
42 *
79e1d494
MS
43 * Reads up to "bytes" bytes from the backchannel/backend. The "timeout"
44 * parameter controls how many seconds to wait for the data - use 0.0 to
45 * return immediately if there is no data, -1.0 to wait for data indefinitely.
ef416fc2 46 *
f3c17241 47 * @since CUPS 1.2/OS X 10.5@
ef416fc2 48 */
49
ecdc0628 50ssize_t /* O - Bytes read or -1 on error */
79e1d494 51cupsBackChannelRead(char *buffer, /* I - Buffer to read into */
ecdc0628 52 size_t bytes, /* I - Bytes to read */
79e1d494 53 double timeout) /* I - Timeout in seconds, typically 0.0 to poll */
ef416fc2 54{
55 fd_set input; /* Input set */
56 struct timeval tval; /* Timeout value */
57 int status; /* Select status */
58
59
60 /*
61 * Wait for input ready.
62 */
63
64 do
65 {
66 cups_setup(&input, &tval, timeout);
67
68 if (timeout < 0.0)
69 status = select(4, &input, NULL, NULL, NULL);
70 else
71 status = select(4, &input, NULL, NULL, &tval);
72 }
e07d4801 73 while (status < 0 && errno != EINTR && errno != EAGAIN);
ef416fc2 74
75 if (status < 0)
76 return (-1); /* Timeout! */
77
78 /*
79 * Read bytes from the pipe...
80 */
81
b86bc4cf 82#ifdef WIN32
536bc2c6 83 return ((ssize_t)_read(3, buffer, (unsigned)bytes));
b86bc4cf 84#else
ef416fc2 85 return (read(3, buffer, bytes));
b86bc4cf 86#endif /* WIN32 */
ef416fc2 87}
88
89
90/*
91 * 'cupsBackChannelWrite()' - Write data to the backchannel.
92 *
79e1d494 93 * Writes "bytes" bytes to the backchannel/filter. The "timeout" parameter
ef416fc2 94 * controls how many seconds to wait for the data to be written - use
95 * 0.0 to return immediately if the data cannot be written, -1.0 to wait
96 * indefinitely.
97 *
f3c17241 98 * @since CUPS 1.2/OS X 10.5@
ef416fc2 99 */
100
ecdc0628 101ssize_t /* O - Bytes written or -1 on error */
bd7854cb 102cupsBackChannelWrite(
ef416fc2 103 const char *buffer, /* I - Buffer to write */
ecdc0628 104 size_t bytes, /* I - Bytes to write */
79e1d494 105 double timeout) /* I - Timeout in seconds, typically 1.0 */
ef416fc2 106{
107 fd_set output; /* Output set */
108 struct timeval tval; /* Timeout value */
109 int status; /* Select status */
ecdc0628 110 ssize_t count; /* Current bytes */
111 size_t total; /* Total bytes */
ef416fc2 112
113
114 /*
115 * Write all bytes...
116 */
117
118 total = 0;
119
120 while (total < bytes)
121 {
122 /*
123 * Wait for write-ready...
124 */
125
126 do
127 {
128 cups_setup(&output, &tval, timeout);
129
130 if (timeout < 0.0)
131 status = select(4, NULL, &output, NULL, NULL);
132 else
133 status = select(4, NULL, &output, NULL, &tval);
134 }
e07d4801 135 while (status < 0 && errno != EINTR && errno != EAGAIN);
ef416fc2 136
e07d4801 137 if (status <= 0)
ef416fc2 138 return (-1); /* Timeout! */
139
140 /*
141 * Write bytes to the pipe...
142 */
143
b86bc4cf 144#ifdef WIN32
536bc2c6 145 count = (ssize_t)_write(3, buffer, (unsigned)(bytes - total));
b86bc4cf 146#else
ef416fc2 147 count = write(3, buffer, bytes - total);
b86bc4cf 148#endif /* WIN32 */
ef416fc2 149
150 if (count < 0)
151 {
152 /*
153 * Write error - abort on fatal errors...
154 */
155
e07d4801 156 if (errno != EINTR && errno != EAGAIN)
ef416fc2 157 return (-1);
158 }
159 else
160 {
161 /*
162 * Write succeeded, update buffer pointer and total count...
163 */
164
165 buffer += count;
7e86f2f6 166 total += (size_t)count;
ef416fc2 167 }
168 }
169
b86bc4cf 170 return ((ssize_t)bytes);
ef416fc2 171}
172
173
174/*
f3c17241 175 * 'cups_setup()' - Setup select()
ef416fc2 176 */
177
178static void
179cups_setup(fd_set *set, /* I - Set for select() */
180 struct timeval *tval, /* I - Timer value */
181 double timeout) /* I - Timeout in seconds */
182{
183 tval->tv_sec = (int)timeout;
184 tval->tv_usec = (int)(1000000.0 * (timeout - tval->tv_sec));
185
186 FD_ZERO(set);
187 FD_SET(3, set);
188}
189
190
191/*
f2d18633 192 * End of "$Id$".
ef416fc2 193 */