2 * "$Id: backchannel.c 11558 2014-02-06 18:33:34Z msweet $"
4 * Backchannel functions for CUPS.
6 * Copyright 2007-2014 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products.
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/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * Include necessary headers...
28 # include <sys/time.h>
36 static void cups_setup(fd_set *set, struct timeval *tval,
41 * 'cupsBackChannelRead()' - Read data from the backchannel.
43 * Reads up to "bytes" bytes from the backchannel/backend. The "timeout"
44 * parameter controls how many seconds to wait for the data - use 0.0 to
45 * return immediately if there is no data, -1.0 to wait for data indefinitely.
47 * @since CUPS 1.2/OS X 10.5@
50 ssize_t /* O - Bytes read or -1 on error */
51 cupsBackChannelRead(char *buffer, /* I - Buffer to read into */
52 size_t bytes, /* I - Bytes to read */
53 double timeout) /* I - Timeout in seconds, typically 0.0 to poll */
55 fd_set input; /* Input set */
56 struct timeval tval; /* Timeout value */
57 int status; /* Select status */
61 * Wait for input ready.
66 cups_setup(&input, &tval, timeout);
69 status = select(4, &input, NULL, NULL, NULL);
71 status = select(4, &input, NULL, NULL, &tval);
73 while (status < 0 && errno != EINTR && errno != EAGAIN);
76 return (-1); /* Timeout! */
79 * Read bytes from the pipe...
83 return ((ssize_t)_read(3, buffer, (unsigned)bytes));
85 return (read(3, buffer, bytes));
91 * 'cupsBackChannelWrite()' - Write data to the backchannel.
93 * Writes "bytes" bytes to the backchannel/filter. The "timeout" parameter
94 * controls how many seconds to wait for the data to be written - use
95 * 0.0 to return immediately if the data cannot be written, -1.0 to wait
98 * @since CUPS 1.2/OS X 10.5@
101 ssize_t /* O - Bytes written or -1 on error */
102 cupsBackChannelWrite(
103 const char *buffer, /* I - Buffer to write */
104 size_t bytes, /* I - Bytes to write */
105 double timeout) /* I - Timeout in seconds, typically 1.0 */
107 fd_set output; /* Output set */
108 struct timeval tval; /* Timeout value */
109 int status; /* Select status */
110 ssize_t count; /* Current bytes */
111 size_t total; /* Total bytes */
120 while (total < bytes)
123 * Wait for write-ready...
128 cups_setup(&output, &tval, timeout);
131 status = select(4, NULL, &output, NULL, NULL);
133 status = select(4, NULL, &output, NULL, &tval);
135 while (status < 0 && errno != EINTR && errno != EAGAIN);
138 return (-1); /* Timeout! */
141 * Write bytes to the pipe...
145 count = (ssize_t)_write(3, buffer, (unsigned)(bytes - total));
147 count = write(3, buffer, bytes - total);
153 * Write error - abort on fatal errors...
156 if (errno != EINTR && errno != EAGAIN)
162 * Write succeeded, update buffer pointer and total count...
166 total += (size_t)count;
170 return ((ssize_t)bytes);
175 * 'cups_setup()' - Setup select()
179 cups_setup(fd_set *set, /* I - Set for select() */
180 struct timeval *tval, /* I - Timer value */
181 double timeout) /* I - Timeout in seconds */
183 tval->tv_sec = (int)timeout;
184 tval->tv_usec = (int)(1000000.0 * (timeout - tval->tv_sec));
192 * End of "$Id: backchannel.c 11558 2014-02-06 18:33:34Z msweet $".