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