]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/backchannel.c
4 * Backchannel functions for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 by Easy Software Products.
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
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * This file is subject to the Apple OS-Developed Software exception.
28 * cupsBackChannelRead() - Read data from the backchannel.
29 * cupsBackChannelWrite() - Write data to the backchannel.
30 * cups_setup() - Setup select()
34 * Include necessary headers...
43 # include <sys/time.h>
51 static void cups_setup(fd_set
*set
, struct timeval
*tval
,
56 * 'cupsBackChannelRead()' - Read data from the backchannel.
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.
66 ssize_t
/* O - Bytes read or -1 on error */
67 cupsBackChannelRead(char *buffer
, /* I - Buffer to read */
68 size_t bytes
, /* I - Bytes to read */
69 double timeout
) /* I - Timeout in seconds */
71 fd_set input
; /* Input set */
72 struct timeval tval
; /* Timeout value */
73 int status
; /* Select status */
77 * Wait for input ready.
82 cups_setup(&input
, &tval
, timeout
);
85 status
= select(4, &input
, NULL
, NULL
, NULL
);
87 status
= select(4, &input
, NULL
, NULL
, &tval
);
89 while (status
< 0 && errno
!= EINTR
);
92 return (-1); /* Timeout! */
95 * Read bytes from the pipe...
98 return (read(3, buffer
, bytes
));
103 * 'cupsBackChannelWrite()' - Write data to the backchannel.
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
113 ssize_t
/* O - Bytes written or -1 on error */
114 cupsBackChannelWrite(
115 const char *buffer
, /* I - Buffer to write */
116 size_t bytes
, /* I - Bytes to write */
117 double timeout
) /* I - Timeout in seconds */
119 fd_set output
; /* Output set */
120 struct timeval tval
; /* Timeout value */
121 int status
; /* Select status */
122 ssize_t count
; /* Current bytes */
123 size_t total
; /* Total bytes */
132 while (total
< bytes
)
135 * Wait for write-ready...
140 cups_setup(&output
, &tval
, timeout
);
143 status
= select(4, NULL
, &output
, NULL
, NULL
);
145 status
= select(4, NULL
, &output
, NULL
, &tval
);
147 while (status
< 0 && errno
!= EINTR
);
150 return (-1); /* Timeout! */
153 * Write bytes to the pipe...
156 count
= write(3, buffer
, bytes
- total
);
161 * Write error - abort on fatal errors...
164 if (errno
!= EINTR
&& errno
!= EAGAIN
)
170 * Write succeeded, update buffer pointer and total count...
183 * 'cups_setup()' - Setup select()
187 cups_setup(fd_set
*set
, /* I - Set for select() */
188 struct timeval
*tval
, /* I - Timer value */
189 double timeout
) /* I - Timeout in seconds */
191 tval
->tv_sec
= (int)timeout
;
192 tval
->tv_usec
= (int)(1000000.0 * (timeout
- tval
->tv_sec
));