]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
bc44d920 | 2 | * "$Id: backchannel.c 6649 2007-07-11 21:46:42Z mike $" |
ef416fc2 | 3 | * |
4 | * Backchannel functions for the Common UNIX Printing System (CUPS). | |
5 | * | |
bc44d920 | 6 | * Copyright 2007 by Apple Inc. |
b86bc4cf | 7 | * Copyright 1997-2007 by Easy Software Products. |
ef416fc2 | 8 | * |
9 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 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 | * |
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 | ||
ecdc0628 | 57 | ssize_t /* O - Bytes read or -1 on error */ |
bd7854cb | 58 | cupsBackChannelRead(char *buffer, /* I - Buffer to read */ |
ecdc0628 | 59 | size_t bytes, /* I - Bytes to read */ |
ef416fc2 | 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 | ||
b86bc4cf | 89 | #ifdef WIN32 |
90 | return ((ssize_t)read(3, buffer, (unsigned)bytes)); | |
91 | #else | |
ef416fc2 | 92 | return (read(3, buffer, bytes)); |
b86bc4cf | 93 | #endif /* WIN32 */ |
ef416fc2 | 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 | ||
ecdc0628 | 108 | ssize_t /* O - Bytes written or -1 on error */ |
bd7854cb | 109 | cupsBackChannelWrite( |
ef416fc2 | 110 | const char *buffer, /* I - Buffer to write */ |
ecdc0628 | 111 | size_t bytes, /* I - Bytes to write */ |
ef416fc2 | 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 */ | |
ecdc0628 | 117 | ssize_t count; /* Current bytes */ |
118 | size_t total; /* Total bytes */ | |
ef416fc2 | 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 | ||
b86bc4cf | 151 | #ifdef WIN32 |
152 | count = (ssize_t)write(3, buffer, (unsigned)(bytes - total)); | |
153 | #else | |
ef416fc2 | 154 | count = write(3, buffer, bytes - total); |
b86bc4cf | 155 | #endif /* WIN32 */ |
ef416fc2 | 156 | |
157 | if (count < 0) | |
158 | { | |
159 | /* | |
160 | * Write error - abort on fatal errors... | |
161 | */ | |
162 | ||
f301802f | 163 | if (errno != EINTR) |
ef416fc2 | 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 | ||
b86bc4cf | 177 | return ((ssize_t)bytes); |
ef416fc2 | 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 | /* | |
bc44d920 | 199 | * End of "$Id: backchannel.c 6649 2007-07-11 21:46:42Z mike $". |
ef416fc2 | 200 | */ |