2 * Backchannel functions for CUPS.
4 * Copyright 2007-2014 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * missing or damaged, see the license at "http://www.cups.org/".
13 * This file is subject to the Apple OS-Developed Software exception.
17 * Include necessary headers...
26 # include <sys/time.h>
34 static void cups_setup(fd_set *set, struct timeval *tval,
39 * 'cupsBackChannelRead()' - Read data from the backchannel.
41 * Reads up to "bytes" bytes from the backchannel/backend. The "timeout"
42 * parameter controls how many seconds to wait for the data - use 0.0 to
43 * return immediately if there is no data, -1.0 to wait for data indefinitely.
45 * @since CUPS 1.2/macOS 10.5@
48 ssize_t /* O - Bytes read or -1 on error */
49 cupsBackChannelRead(char *buffer, /* I - Buffer to read into */
50 size_t bytes, /* I - Bytes to read */
51 double timeout) /* I - Timeout in seconds, typically 0.0 to poll */
53 fd_set input; /* Input set */
54 struct timeval tval; /* Timeout value */
55 int status; /* Select status */
59 * Wait for input ready.
64 cups_setup(&input, &tval, timeout);
67 status = select(4, &input, NULL, NULL, NULL);
69 status = select(4, &input, NULL, NULL, &tval);
71 while (status < 0 && errno != EINTR && errno != EAGAIN);
74 return (-1); /* Timeout! */
77 * Read bytes from the pipe...
81 return ((ssize_t)_read(3, buffer, (unsigned)bytes));
83 return (read(3, buffer, bytes));
89 * 'cupsBackChannelWrite()' - Write data to the backchannel.
91 * Writes "bytes" bytes to the backchannel/filter. The "timeout" parameter
92 * controls how many seconds to wait for the data to be written - use
93 * 0.0 to return immediately if the data cannot be written, -1.0 to wait
96 * @since CUPS 1.2/macOS 10.5@
99 ssize_t /* O - Bytes written or -1 on error */
100 cupsBackChannelWrite(
101 const char *buffer, /* I - Buffer to write */
102 size_t bytes, /* I - Bytes to write */
103 double timeout) /* I - Timeout in seconds, typically 1.0 */
105 fd_set output; /* Output set */
106 struct timeval tval; /* Timeout value */
107 int status; /* Select status */
108 ssize_t count; /* Current bytes */
109 size_t total; /* Total bytes */
118 while (total < bytes)
121 * Wait for write-ready...
126 cups_setup(&output, &tval, timeout);
129 status = select(4, NULL, &output, NULL, NULL);
131 status = select(4, NULL, &output, NULL, &tval);
133 while (status < 0 && errno != EINTR && errno != EAGAIN);
136 return (-1); /* Timeout! */
139 * Write bytes to the pipe...
143 count = (ssize_t)_write(3, buffer, (unsigned)(bytes - total));
145 count = write(3, buffer, bytes - total);
151 * Write error - abort on fatal errors...
154 if (errno != EINTR && errno != EAGAIN)
160 * Write succeeded, update buffer pointer and total count...
164 total += (size_t)count;
168 return ((ssize_t)bytes);
173 * 'cups_setup()' - Setup select()
177 cups_setup(fd_set *set, /* I - Set for select() */
178 struct timeval *tval, /* I - Timer value */
179 double timeout) /* I - Timeout in seconds */
181 tval->tv_sec = (int)timeout;
182 tval->tv_usec = (int)(1000000.0 * (timeout - tval->tv_sec));