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