]> 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 5099 2006-02-13 02:46:10Z mike $"
3 *
4 * Backchannel functions for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2005 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 int /* O - Bytes read or -1 on error */
67 cupsBackChannelRead(char *buffer, /* I - Buffer to read */
68 int 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 return (read(3, buffer, bytes));
99 }
100
101
102 /*
103 * 'cupsBackChannelWrite()' - Write data to the backchannel.
104 *
105 * Writes "bytes" bytes to the backchannel. The "timeout" parameter
106 * controls how many seconds to wait for the data to be written - use
107 * 0.0 to return immediately if the data cannot be written, -1.0 to wait
108 * indefinitely.
109 *
110 * @since CUPS 1.2@
111 */
112
113 int /* O - Bytes written or -1 on error */
114 cupsBackChannelWrite(
115 const char *buffer, /* I - Buffer to write */
116 int bytes, /* I - Bytes to write */
117 double timeout) /* I - Timeout in seconds */
118 {
119 fd_set output; /* Output set */
120 struct timeval tval; /* Timeout value */
121 int status; /* Select status */
122 int count, /* Current bytes */
123 total; /* Total bytes */
124
125
126 /*
127 * Write all bytes...
128 */
129
130 total = 0;
131
132 while (total < bytes)
133 {
134 /*
135 * Wait for write-ready...
136 */
137
138 do
139 {
140 cups_setup(&output, &tval, timeout);
141
142 if (timeout < 0.0)
143 status = select(4, NULL, &output, NULL, NULL);
144 else
145 status = select(4, NULL, &output, NULL, &tval);
146 }
147 while (status < 0 && errno != EINTR);
148
149 if (status < 0)
150 return (-1); /* Timeout! */
151
152 /*
153 * Write bytes to the pipe...
154 */
155
156 count = write(3, buffer, bytes - total);
157
158 if (count < 0)
159 {
160 /*
161 * Write error - abort on fatal errors...
162 */
163
164 if (errno != EINTR && errno != EAGAIN)
165 return (-1);
166 }
167 else
168 {
169 /*
170 * Write succeeded, update buffer pointer and total count...
171 */
172
173 buffer += count;
174 total += count;
175 }
176 }
177
178 return (bytes);
179 }
180
181
182 /*
183 * 'cups_setup()' - Setup select()
184 */
185
186 static void
187 cups_setup(fd_set *set, /* I - Set for select() */
188 struct timeval *tval, /* I - Timer value */
189 double timeout) /* I - Timeout in seconds */
190 {
191 tval->tv_sec = (int)timeout;
192 tval->tv_usec = (int)(1000000.0 * (timeout - tval->tv_sec));
193
194 FD_ZERO(set);
195 FD_SET(3, set);
196 }
197
198
199 /*
200 * End of "$Id: backchannel.c 5099 2006-02-13 02:46:10Z mike $".
201 */