2 * "$Id: backchannel.c 9042 2010-03-24 00:45:34Z mike $"
4 * Backchannel functions for CUPS.
6 * Copyright 2007-2010 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 * cupsBackChannelRead() - Read data from the backchannel.
20 * cupsBackChannelWrite() - Write data to the backchannel.
21 * cups_setup() - Setup select()
25 * Include necessary headers...
34 # include <sys/time.h>
42 static void cups_setup(fd_set *set, struct timeval *tval,
47 * 'cupsBackChannelRead()' - Read data from the backchannel.
49 * Reads up to "bytes" bytes from the backchannel/backend. The "timeout"
50 * parameter controls how many seconds to wait for the data - use 0.0 to
51 * return immediately if there is no data, -1.0 to wait for data indefinitely.
53 * @since CUPS 1.2/Mac OS X 10.5@
56 ssize_t /* O - Bytes read or -1 on error */
57 cupsBackChannelRead(char *buffer, /* I - Buffer to read into */
58 size_t bytes, /* I - Bytes to read */
59 double timeout) /* I - Timeout in seconds, typically 0.0 to poll */
61 fd_set input; /* Input set */
62 struct timeval tval; /* Timeout value */
63 int status; /* Select status */
67 * Wait for input ready.
72 cups_setup(&input, &tval, timeout);
75 status = select(4, &input, NULL, NULL, NULL);
77 status = select(4, &input, NULL, NULL, &tval);
79 while (status < 0 && errno != EINTR && errno != EAGAIN);
82 return (-1); /* Timeout! */
85 * Read bytes from the pipe...
89 return ((ssize_t)_read(3, buffer, (unsigned)bytes));
91 return (read(3, buffer, bytes));
97 * 'cupsBackChannelWrite()' - Write data to the backchannel.
99 * Writes "bytes" bytes to the backchannel/filter. The "timeout" parameter
100 * controls how many seconds to wait for the data to be written - use
101 * 0.0 to return immediately if the data cannot be written, -1.0 to wait
104 * @since CUPS 1.2/Mac OS X 10.5@
107 ssize_t /* O - Bytes written or -1 on error */
108 cupsBackChannelWrite(
109 const char *buffer, /* I - Buffer to write */
110 size_t bytes, /* I - Bytes to write */
111 double timeout) /* I - Timeout in seconds, typically 1.0 */
113 fd_set output; /* Output set */
114 struct timeval tval; /* Timeout value */
115 int status; /* Select status */
116 ssize_t count; /* Current bytes */
117 size_t total; /* Total bytes */
126 while (total < bytes)
129 * Wait for write-ready...
134 cups_setup(&output, &tval, timeout);
137 status = select(4, NULL, &output, NULL, NULL);
139 status = select(4, NULL, &output, NULL, &tval);
141 while (status < 0 && errno != EINTR && errno != EAGAIN);
144 return (-1); /* Timeout! */
147 * Write bytes to the pipe...
151 count = (ssize_t)_write(3, buffer, (unsigned)(bytes - total));
153 count = write(3, buffer, bytes - total);
159 * Write error - abort on fatal errors...
162 if (errno != EINTR && errno != EAGAIN)
168 * Write succeeded, update buffer pointer and total count...
176 return ((ssize_t)bytes);
181 * 'cups_setup()' - Setup select()
185 cups_setup(fd_set *set, /* I - Set for select() */
186 struct timeval *tval, /* I - Timer value */
187 double timeout) /* I - Timeout in seconds */
189 tval->tv_sec = (int)timeout;
190 tval->tv_usec = (int)(1000000.0 * (timeout - tval->tv_sec));
198 * End of "$Id: backchannel.c 9042 2010-03-24 00:45:34Z mike $".