]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/backchannel.c
2 * "$Id: backchannel.c 6649 2007-07-11 21:46:42Z mike $"
4 * Backchannel functions for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 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. The "timeout"
50 * parameter controls how many seconds to wait for the data - use
51 * 0.0 to return immediately if there is no data, -1.0 to wait
52 * for data indefinitely.
57 ssize_t
/* O - Bytes read or -1 on error */
58 cupsBackChannelRead(char *buffer
, /* I - Buffer to read */
59 size_t bytes
, /* I - Bytes to read */
60 double timeout
) /* I - Timeout in seconds */
62 fd_set input
; /* Input set */
63 struct timeval tval
; /* Timeout value */
64 int status
; /* Select status */
68 * Wait for input ready.
73 cups_setup(&input
, &tval
, timeout
);
76 status
= select(4, &input
, NULL
, NULL
, NULL
);
78 status
= select(4, &input
, NULL
, NULL
, &tval
);
80 while (status
< 0 && errno
!= EINTR
);
83 return (-1); /* Timeout! */
86 * Read bytes from the pipe...
90 return ((ssize_t
)read(3, buffer
, (unsigned)bytes
));
92 return (read(3, buffer
, bytes
));
98 * 'cupsBackChannelWrite()' - Write data to the backchannel.
100 * Writes "bytes" bytes to the backchannel. The "timeout" parameter
101 * controls how many seconds to wait for the data to be written - use
102 * 0.0 to return immediately if the data cannot be written, -1.0 to wait
108 ssize_t
/* O - Bytes written or -1 on error */
109 cupsBackChannelWrite(
110 const char *buffer
, /* I - Buffer to write */
111 size_t bytes
, /* I - Bytes to write */
112 double timeout
) /* I - Timeout in seconds */
114 fd_set output
; /* Output set */
115 struct timeval tval
; /* Timeout value */
116 int status
; /* Select status */
117 ssize_t count
; /* Current bytes */
118 size_t total
; /* Total bytes */
127 while (total
< bytes
)
130 * Wait for write-ready...
135 cups_setup(&output
, &tval
, timeout
);
138 status
= select(4, NULL
, &output
, NULL
, NULL
);
140 status
= select(4, NULL
, &output
, NULL
, &tval
);
142 while (status
< 0 && errno
!= EINTR
);
145 return (-1); /* Timeout! */
148 * Write bytes to the pipe...
152 count
= (ssize_t
)write(3, buffer
, (unsigned)(bytes
- total
));
154 count
= write(3, buffer
, bytes
- total
);
160 * Write error - abort on fatal errors...
169 * Write succeeded, update buffer pointer and total count...
177 return ((ssize_t
)bytes
);
182 * 'cups_setup()' - Setup select()
186 cups_setup(fd_set
*set
, /* I - Set for select() */
187 struct timeval
*tval
, /* I - Timer value */
188 double timeout
) /* I - Timeout in seconds */
190 tval
->tv_sec
= (int)timeout
;
191 tval
->tv_usec
= (int)(1000000.0 * (timeout
- tval
->tv_sec
));
199 * End of "$Id: backchannel.c 6649 2007-07-11 21:46:42Z mike $".