]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
36080486 | 2 | * "$Id: backchannel.c 5551 2006-05-19 20:20:32Z mike $" |
ef416fc2 | 3 | * |
4 | * Backchannel functions for the Common UNIX Printing System (CUPS). | |
5 | * | |
ecdc0628 | 6 | * Copyright 1997-2006 by Easy Software Products. |
ef416fc2 | 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 | ||
ecdc0628 | 66 | ssize_t /* O - Bytes read or -1 on error */ |
bd7854cb | 67 | cupsBackChannelRead(char *buffer, /* I - Buffer to read */ |
ecdc0628 | 68 | size_t bytes, /* I - Bytes to read */ |
ef416fc2 | 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 | ||
ecdc0628 | 113 | ssize_t /* O - Bytes written or -1 on error */ |
bd7854cb | 114 | cupsBackChannelWrite( |
ef416fc2 | 115 | const char *buffer, /* I - Buffer to write */ |
ecdc0628 | 116 | size_t bytes, /* I - Bytes to write */ |
ef416fc2 | 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 */ | |
ecdc0628 | 122 | ssize_t count; /* Current bytes */ |
123 | size_t total; /* Total bytes */ | |
ef416fc2 | 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 | ||
f301802f | 164 | if (errno != EINTR) |
ef416fc2 | 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 | /* | |
36080486 | 200 | * End of "$Id: backchannel.c 5551 2006-05-19 20:20:32Z mike $". |
ef416fc2 | 201 | */ |