]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/file.c
2 * "$Id: file.c 7672 2008-06-18 22:03:02Z mike $"
4 * File functions for CUPS.
6 * Since stdio files max out at 256 files on many systems, we have to
7 * write similar functions without this limit. At the same time, using
8 * our own file functions allows us to provide transparent support of
9 * gzip'd print files, PPD files, etc.
11 * Copyright 2007-2010 by Apple Inc.
12 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
14 * These coded instructions, statements, and computer programs are the
15 * property of Apple Inc. and are protected by Federal copyright
16 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
17 * which should have been included with this file. If this file is
18 * file is missing or damaged, see the license at "http://www.cups.org/".
22 * cupsFileClose() - Close a CUPS file.
23 * cupsFileCompression() - Return whether a file is compressed.
24 * cupsFileEOF() - Return the end-of-file status.
25 * cupsFileFind() - Find a file using the specified path.
26 * cupsFileFlush() - Flush pending output.
27 * cupsFileGetChar() - Get a single character from a file.
28 * cupsFileGetConf() - Get a line from a configuration file...
29 * cupsFileGetLine() - Get a CR and/or LF-terminated line that may contain
31 * cupsFileGets() - Get a CR and/or LF-terminated line.
32 * cupsFileLock() - Temporarily lock access to a file.
33 * cupsFileNumber() - Return the file descriptor associated with a CUPS
35 * cupsFileOpen() - Open a CUPS file.
36 * cupsFileOpenFd() - Open a CUPS file using a file descriptor.
37 * cupsFilePeekChar() - Peek at the next character from a file.
38 * cupsFilePrintf() - Write a formatted string.
39 * cupsFilePutChar() - Write a character.
40 * cupsFilePuts() - Write a string.
41 * cupsFileRead() - Read from a file.
42 * cupsFileRewind() - Set the current file position to the beginning of
44 * cupsFileSeek() - Seek in a file.
45 * cupsFileStderr() - Return a CUPS file associated with stderr.
46 * cupsFileStdin() - Return a CUPS file associated with stdin.
47 * cupsFileStdout() - Return a CUPS file associated with stdout.
48 * cupsFileTell() - Return the current file position.
49 * cupsFileUnlock() - Unlock access to a file.
50 * cupsFileWrite() - Write to a file.
51 * cups_compress() - Compress a buffer of data...
52 * cups_fill() - Fill the input buffer...
53 * cups_read() - Read from a file descriptor.
54 * cups_write() - Write to a file descriptor.
58 * Include necessary headers...
61 #include "file-private.h"
63 #include <sys/types.h>
71 static ssize_t
cups_compress(cups_file_t
*fp
, const char *buf
, size_t bytes
);
72 #endif /* HAVE_LIBZ */
73 static ssize_t
cups_fill(cups_file_t
*fp
);
74 static int cups_open(const char *filename
, int mode
);
75 static ssize_t
cups_read(cups_file_t
*fp
, char *buf
, size_t bytes
);
76 static ssize_t
cups_write(cups_file_t
*fp
, const char *buf
, size_t bytes
);
80 * 'cupsFileClose()' - Close a CUPS file.
82 * @since CUPS 1.2/Mac OS X 10.5@
85 int /* O - 0 on success, -1 on error */
86 cupsFileClose(cups_file_t
*fp
) /* I - CUPS file */
88 int fd
; /* File descriptor */
89 char mode
; /* Open mode */
90 int status
; /* Return status */
91 int is_stdio
; /* Is a stdio file? */
94 DEBUG_printf(("cupsFileClose(fp=%p)", fp
));
104 * Flush pending write data...
108 status
= cupsFileFlush(fp
);
113 if (fp
->compressed
&& status
>= 0)
118 * Free decompression data...
121 inflateEnd(&fp
->stream
);
126 * Flush any remaining compressed data...
129 unsigned char trailer
[8]; /* Trailer CRC and length */
130 int done
; /* Done writing... */
133 fp
->stream
.avail_in
= 0;
137 if (fp
->stream
.next_out
> fp
->cbuf
)
139 if (cups_write(fp
, (char *)fp
->cbuf
,
140 fp
->stream
.next_out
- fp
->cbuf
) < 0)
143 fp
->stream
.next_out
= fp
->cbuf
;
144 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
147 if (done
|| status
< 0)
150 done
= deflate(&fp
->stream
, Z_FINISH
) == Z_STREAM_END
&&
151 fp
->stream
.next_out
== fp
->cbuf
;
155 * Write the CRC and length...
158 trailer
[0] = fp
->crc
;
159 trailer
[1] = fp
->crc
>> 8;
160 trailer
[2] = fp
->crc
>> 16;
161 trailer
[3] = fp
->crc
>> 24;
162 trailer
[4] = fp
->pos
;
163 trailer
[5] = fp
->pos
>> 8;
164 trailer
[6] = fp
->pos
>> 16;
165 trailer
[7] = fp
->pos
>> 24;
167 if (cups_write(fp
, (char *)trailer
, 8) < 0)
171 * Free all memory used by the compression stream...
174 deflateEnd(&(fp
->stream
));
177 #endif /* HAVE_LIBZ */
180 * Save the file descriptor we used and free memory...
185 is_stdio
= fp
->is_stdio
;
187 if (fp
->printf_buffer
)
188 free(fp
->printf_buffer
);
193 * Close the file, returning the close status...
198 if (closesocket(fd
) < 0)
212 * 'cupsFileCompression()' - Return whether a file is compressed.
214 * @since CUPS 1.2/Mac OS X 10.5@
217 int /* O - @code CUPS_FILE_NONE@ or @code CUPS_FILE_GZIP@ */
218 cupsFileCompression(cups_file_t
*fp
) /* I - CUPS file */
220 return (fp
? fp
->compressed
: CUPS_FILE_NONE
);
225 * 'cupsFileEOF()' - Return the end-of-file status.
227 * @since CUPS 1.2/Mac OS X 10.5@
230 int /* O - 1 on end of file, 0 otherwise */
231 cupsFileEOF(cups_file_t
*fp
) /* I - CUPS file */
233 return (fp
? fp
->eof
: 1);
238 * 'cupsFileFind()' - Find a file using the specified path.
240 * This function allows the paths in the path string to be separated by
241 * colons (UNIX standard) or semicolons (Windows standard) and stores the
242 * result in the buffer supplied. If the file cannot be found in any of
243 * the supplied paths, @code NULL@ is returned. A @code NULL@ path only
244 * matches the current directory.
246 * @since CUPS 1.2/Mac OS X 10.5@
249 const char * /* O - Full path to file or @code NULL@ if not found */
250 cupsFileFind(const char *filename
, /* I - File to find */
251 const char *path
, /* I - Colon/semicolon-separated path */
252 int executable
, /* I - 1 = executable files, 0 = any file/dir */
253 char *buffer
, /* I - Filename buffer */
254 int bufsize
) /* I - Size of filename buffer */
256 char *bufptr
, /* Current position in buffer */
257 *bufend
; /* End of buffer */
261 * Range check input...
264 DEBUG_printf(("cupsFileFind(filename=\"%s\", path=\"%s\", executable=%d, "
265 "buffer=%p, bufsize=%d)", filename
, path
, executable
, buffer
,
268 if (!filename
|| !buffer
|| bufsize
< 2)
274 * No path, so check current directory...
277 if (!access(filename
, 0))
279 strlcpy(buffer
, filename
, bufsize
);
287 * Now check each path and return the first match...
290 bufend
= buffer
+ bufsize
- 1;
296 if (*path
== ';' || (*path
== ':' && ((bufptr
- buffer
) > 1 || !isalpha(buffer
[0] & 255))))
298 if (*path
== ';' || *path
== ':')
301 if (bufptr
> buffer
&& bufptr
[-1] != '/' && bufptr
< bufend
)
304 strlcpy(bufptr
, filename
, bufend
- bufptr
);
307 if (!access(buffer
, 0))
309 if (!access(buffer
, executable
? X_OK
: 0))
312 DEBUG_printf(("1cupsFileFind: Returning \"%s\"", buffer
));
318 else if (bufptr
< bufend
)
325 * Check the last path...
328 if (bufptr
> buffer
&& bufptr
[-1] != '/' && bufptr
< bufend
)
331 strlcpy(bufptr
, filename
, bufend
- bufptr
);
333 if (!access(buffer
, 0))
335 DEBUG_printf(("1cupsFileFind: Returning \"%s\"", buffer
));
340 DEBUG_puts("1cupsFileFind: Returning NULL");
347 * 'cupsFileFlush()' - Flush pending output.
349 * @since CUPS 1.2/Mac OS X 10.5@
352 int /* O - 0 on success, -1 on error */
353 cupsFileFlush(cups_file_t
*fp
) /* I - CUPS file */
355 ssize_t bytes
; /* Bytes to write */
358 DEBUG_printf(("cupsFileFlush(fp=%p)", fp
));
361 * Range check input...
364 if (!fp
|| fp
->mode
!= 'w')
366 DEBUG_puts("1cupsFileFlush: Attempt to flush a read-only file...");
370 bytes
= (ssize_t
)(fp
->ptr
- fp
->buf
);
372 DEBUG_printf(("2cupsFileFlush: Flushing " CUPS_LLFMT
" bytes...",
379 bytes
= cups_compress(fp
, fp
->buf
, bytes
);
381 #endif /* HAVE_LIBZ */
382 bytes
= cups_write(fp
, fp
->buf
, bytes
);
395 * 'cupsFileGetChar()' - Get a single character from a file.
397 * @since CUPS 1.2/Mac OS X 10.5@
400 int /* O - Character or -1 on end of file */
401 cupsFileGetChar(cups_file_t
*fp
) /* I - CUPS file */
404 * Range check input...
407 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
409 DEBUG_puts("3cupsFileGetChar: Bad arguments!");
414 * If the input buffer is empty, try to read more data...
417 if (fp
->ptr
>= fp
->end
)
418 if (cups_fill(fp
) < 0)
420 DEBUG_puts("3cupsFileGetChar: Unable to fill buffer!");
425 * Return the next character in the buffer...
428 DEBUG_printf(("3cupsFileGetChar: Returning %d...", *(fp
->ptr
) & 255));
432 DEBUG_printf(("4cupsFileGetChar: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
434 return (*(fp
->ptr
)++ & 255);
439 * 'cupsFileGetConf()' - Get a line from a configuration file...
441 * @since CUPS 1.2/Mac OS X 10.5@
444 char * /* O - Line read or @code NULL@ on end of file or error */
445 cupsFileGetConf(cups_file_t
*fp
, /* I - CUPS file */
446 char *buf
, /* O - String buffer */
447 size_t buflen
, /* I - Size of string buffer */
448 char **value
, /* O - Pointer to value */
449 int *linenum
) /* IO - Current line number */
451 char *ptr
; /* Pointer into line */
455 * Range check input...
458 DEBUG_printf(("2cupsFileGetConf(fp=%p, buf=%p, buflen=" CUPS_LLFMT
459 ", value=%p, linenum=%p)", fp
, buf
, CUPS_LLCAST buflen
,
462 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') ||
463 !buf
|| buflen
< 2 || !value
)
472 * Read the next non-comment line...
477 while (cupsFileGets(fp
, buf
, buflen
))
482 * Strip any comments...
485 if ((ptr
= strchr(buf
, '#')) != NULL
)
487 if (ptr
> buf
&& ptr
[-1] == '\\')
490 _cups_strcpy(ptr
- 1, ptr
);
494 // Strip the comment and any trailing whitespace...
497 if (!_cups_isspace(ptr
[-1]))
508 * Strip leading whitespace...
511 for (ptr
= buf
; _cups_isspace(*ptr
); ptr
++);
514 _cups_strcpy(buf
, ptr
);
517 * See if there is anything left...
523 * Yes, grab any value and return...
526 for (ptr
= buf
; *ptr
; ptr
++)
527 if (_cups_isspace(*ptr
))
533 * Have a value, skip any other spaces...
536 while (_cups_isspace(*ptr
))
543 * Strip trailing whitespace and > for lines that begin with <...
546 ptr
+= strlen(ptr
) - 1;
548 if (buf
[0] == '<' && *ptr
== '>')
550 else if (buf
[0] == '<' && *ptr
!= '>')
560 while (ptr
> *value
&& _cups_isspace(*ptr
))
577 * 'cupsFileGetLine()' - Get a CR and/or LF-terminated line that may
578 * contain binary data.
580 * This function differs from @link cupsFileGets@ in that the trailing CR
581 * and LF are preserved, as is any binary data on the line. The buffer is
582 * nul-terminated, however you should use the returned length to determine
583 * the number of bytes on the line.
585 * @since CUPS 1.2/Mac OS X 10.5@
588 size_t /* O - Number of bytes on line or 0 on end of file */
589 cupsFileGetLine(cups_file_t
*fp
, /* I - File to read from */
590 char *buf
, /* I - Buffer */
591 size_t buflen
) /* I - Size of buffer */
593 int ch
; /* Character from file */
594 char *ptr
, /* Current position in line buffer */
595 *end
; /* End of line buffer */
599 * Range check input...
602 DEBUG_printf(("2cupsFileGetLine(fp=%p, buf=%p, buflen=" CUPS_LLFMT
")",
603 fp
, buf
, CUPS_LLCAST buflen
));
605 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') || !buf
|| buflen
< 3)
609 * Now loop until we have a valid line...
612 for (ptr
= buf
, end
= buf
+ buflen
- 2; ptr
< end
;)
614 if (fp
->ptr
>= fp
->end
)
615 if (cups_fill(fp
) <= 0)
618 *ptr
++ = ch
= *(fp
->ptr
)++;
627 if (fp
->ptr
>= fp
->end
)
628 if (cups_fill(fp
) <= 0)
631 if (*(fp
->ptr
) == '\n')
633 *ptr
++ = *(fp
->ptr
)++;
642 * Line feed ends a line...
651 DEBUG_printf(("4cupsFileGetLine: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
658 * 'cupsFileGets()' - Get a CR and/or LF-terminated line.
660 * @since CUPS 1.2/Mac OS X 10.5@
663 char * /* O - Line read or @code NULL@ on end of file or error */
664 cupsFileGets(cups_file_t
*fp
, /* I - CUPS file */
665 char *buf
, /* O - String buffer */
666 size_t buflen
) /* I - Size of string buffer */
668 int ch
; /* Character from file */
669 char *ptr
, /* Current position in line buffer */
670 *end
; /* End of line buffer */
674 * Range check input...
677 DEBUG_printf(("2cupsFileGets(fp=%p, buf=%p, buflen=" CUPS_LLFMT
")", fp
, buf
,
678 CUPS_LLCAST buflen
));
680 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') || !buf
|| buflen
< 2)
684 * Now loop until we have a valid line...
687 for (ptr
= buf
, end
= buf
+ buflen
- 1; ptr
< end
;)
689 if (fp
->ptr
>= fp
->end
)
690 if (cups_fill(fp
) <= 0)
707 if (fp
->ptr
>= fp
->end
)
708 if (cups_fill(fp
) <= 0)
711 if (*(fp
->ptr
) == '\n')
722 * Line feed ends a line...
733 DEBUG_printf(("4cupsFileGets: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
740 * 'cupsFileLock()' - Temporarily lock access to a file.
742 * @since CUPS 1.2/Mac OS X 10.5@
745 int /* O - 0 on success, -1 on error */
746 cupsFileLock(cups_file_t
*fp
, /* I - CUPS file */
747 int block
) /* I - 1 to wait for the lock, 0 to fail right away */
753 if (!fp
|| fp
->mode
== 's')
761 return (_locking(fp
->fd
, block
? _LK_LOCK
: _LK_NBLCK
, 0));
763 return (lockf(fp
->fd
, block
? F_LOCK
: F_TLOCK
, 0));
769 * 'cupsFileNumber()' - Return the file descriptor associated with a CUPS file.
771 * @since CUPS 1.2/Mac OS X 10.5@
774 int /* O - File descriptor */
775 cupsFileNumber(cups_file_t
*fp
) /* I - CUPS file */
785 * 'cupsFileOpen()' - Open a CUPS file.
787 * The "mode" parameter can be "r" to read, "w" to write, overwriting any
788 * existing file, "a" to append to an existing file or create a new file,
789 * or "s" to open a socket connection.
791 * When opening for writing ("w"), an optional number from 1 to 9 can be
792 * supplied which enables Flate compression of the file. Compression is
793 * not supported for the "a" (append) mode.
795 * When opening a socket connection, the filename is a string of the form
796 * "address:port" or "hostname:port". The socket will make an IPv4 or IPv6
797 * connection as needed, generally preferring IPv6 connections when there is
800 * @since CUPS 1.2/Mac OS X 10.5@
803 cups_file_t
* /* O - CUPS file or @code NULL@ if the file or socket cannot be opened */
804 cupsFileOpen(const char *filename
, /* I - Name of file */
805 const char *mode
) /* I - Open mode */
807 cups_file_t
*fp
; /* New CUPS file */
808 int fd
; /* File descriptor */
809 char hostname
[1024], /* Hostname */
810 *portname
; /* Port "name" (number or service) */
811 http_addrlist_t
*addrlist
; /* Host address list */
814 DEBUG_printf(("cupsFileOpen(filename=\"%s\", mode=\"%s\")", filename
,
818 * Range check input...
821 if (!filename
|| !mode
||
822 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's') ||
823 (*mode
== 'a' && isdigit(mode
[1] & 255)))
832 case 'a' : /* Append file */
833 fd
= cups_open(filename
,
834 O_RDWR
| O_CREAT
| O_APPEND
| O_LARGEFILE
| O_BINARY
);
837 case 'r' : /* Read file */
838 fd
= open(filename
, O_RDONLY
| O_LARGEFILE
| O_BINARY
, 0);
841 case 'w' : /* Write file */
842 fd
= cups_open(filename
, O_WRONLY
| O_LARGEFILE
| O_BINARY
);
843 if (fd
< 0 && errno
== ENOENT
)
845 fd
= cups_open(filename
,
846 O_WRONLY
| O_CREAT
| O_EXCL
| O_LARGEFILE
| O_BINARY
);
847 if (fd
< 0 && errno
== EEXIST
)
848 fd
= cups_open(filename
, O_WRONLY
| O_LARGEFILE
| O_BINARY
);
859 case 's' : /* Read/write socket */
860 strlcpy(hostname
, filename
, sizeof(hostname
));
861 if ((portname
= strrchr(hostname
, ':')) != NULL
)
867 * Lookup the hostname and service...
870 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
874 * Connect to the server...
877 if (!httpAddrConnect(addrlist
, &fd
))
879 httpAddrFreeList(addrlist
);
883 httpAddrFreeList(addrlist
);
886 default : /* Remove bogus compiler warning... */
894 * Create the CUPS file structure...
897 if ((fp
= cupsFileOpenFd(fd
, mode
)) == NULL
)
913 * 'cupsFileOpenFd()' - Open a CUPS file using a file descriptor.
915 * The "mode" parameter can be "r" to read, "w" to write, "a" to append,
916 * or "s" to treat the file descriptor as a bidirectional socket connection.
918 * When opening for writing ("w"), an optional number from 1 to 9 can be
919 * supplied which enables Flate compression of the file. Compression is
920 * not supported for the "a" (append) mode.
922 * @since CUPS 1.2/Mac OS X 10.5@
925 cups_file_t
* /* O - CUPS file or @code NULL@ if the file could not be opened */
926 cupsFileOpenFd(int fd
, /* I - File descriptor */
927 const char *mode
) /* I - Open mode */
929 cups_file_t
*fp
; /* New CUPS file */
932 DEBUG_printf(("cupsFileOpenFd(fd=%d, mode=\"%s\")", fd
, mode
));
935 * Range check input...
938 if (fd
< 0 || !mode
||
939 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's') ||
940 (*mode
== 'a' && isdigit(mode
[1] & 255)))
947 if ((fp
= calloc(1, sizeof(cups_file_t
))) == NULL
)
959 fp
->pos
= lseek(fd
, 0, SEEK_END
);
964 fp
->end
= fp
->buf
+ sizeof(fp
->buf
);
967 if (mode
[1] >= '1' && mode
[1] <= '9')
970 * Open a compressed stream, so write the standard gzip file
974 unsigned char header
[10]; /* gzip file header */
975 time_t curtime
; /* Current time */
978 curtime
= time(NULL
);
981 header
[2] = Z_DEFLATED
;
984 header
[5] = curtime
>> 8;
985 header
[6] = curtime
>> 16;
986 header
[7] = curtime
>> 24;
990 cups_write(fp
, (char *)header
, 10);
993 * Initialize the compressor...
996 deflateInit2(&(fp
->stream
), mode
[1] - '0', Z_DEFLATED
, -15, 8,
999 fp
->stream
.next_out
= fp
->cbuf
;
1000 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
1002 fp
->crc
= crc32(0L, Z_NULL
, 0);
1004 #endif /* HAVE_LIBZ */
1015 default : /* Remove bogus compiler warning... */
1020 * Don't pass this file to child processes...
1024 fcntl(fp
->fd
, F_SETFD
, fcntl(fp
->fd
, F_GETFD
) | FD_CLOEXEC
);
1032 * 'cupsFilePeekChar()' - Peek at the next character from a file.
1034 * @since CUPS 1.2/Mac OS X 10.5@
1037 int /* O - Character or -1 on end of file */
1038 cupsFilePeekChar(cups_file_t
*fp
) /* I - CUPS file */
1041 * Range check input...
1044 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
1048 * If the input buffer is empty, try to read more data...
1051 if (fp
->ptr
>= fp
->end
)
1052 if (cups_fill(fp
) < 0)
1056 * Return the next character in the buffer...
1059 return (*(fp
->ptr
) & 255);
1064 * 'cupsFilePrintf()' - Write a formatted string.
1066 * @since CUPS 1.2/Mac OS X 10.5@
1069 int /* O - Number of bytes written or -1 on error */
1070 cupsFilePrintf(cups_file_t
*fp
, /* I - CUPS file */
1071 const char *format
, /* I - Printf-style format string */
1072 ...) /* I - Additional args as necessary */
1074 va_list ap
; /* Argument list */
1075 ssize_t bytes
; /* Formatted size */
1078 DEBUG_printf(("2cupsFilePrintf(fp=%p, format=\"%s\", ...)", fp
, format
));
1080 if (!fp
|| !format
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1083 if (!fp
->printf_buffer
)
1086 * Start with an 1k printf buffer...
1089 if ((fp
->printf_buffer
= malloc(1024)) == NULL
)
1092 fp
->printf_size
= 1024;
1095 va_start(ap
, format
);
1096 bytes
= vsnprintf(fp
->printf_buffer
, fp
->printf_size
, format
, ap
);
1099 if (bytes
>= (ssize_t
)fp
->printf_size
)
1102 * Expand the printf buffer...
1105 char *temp
; /* Temporary buffer pointer */
1111 if ((temp
= realloc(fp
->printf_buffer
, bytes
+ 1)) == NULL
)
1114 fp
->printf_buffer
= temp
;
1115 fp
->printf_size
= bytes
+ 1;
1117 va_start(ap
, format
);
1118 bytes
= vsnprintf(fp
->printf_buffer
, fp
->printf_size
, format
, ap
);
1122 if (fp
->mode
== 's')
1124 if (cups_write(fp
, fp
->printf_buffer
, bytes
) < 0)
1129 DEBUG_printf(("4cupsFilePrintf: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1134 if ((fp
->ptr
+ bytes
) > fp
->end
)
1135 if (cupsFileFlush(fp
))
1140 DEBUG_printf(("4cupsFilePrintf: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1142 if (bytes
> sizeof(fp
->buf
))
1146 return (cups_compress(fp
, fp
->printf_buffer
, bytes
));
1148 #endif /* HAVE_LIBZ */
1149 return (cups_write(fp
, fp
->printf_buffer
, bytes
));
1153 memcpy(fp
->ptr
, fp
->printf_buffer
, bytes
);
1161 * 'cupsFilePutChar()' - Write a character.
1163 * @since CUPS 1.2/Mac OS X 10.5@
1166 int /* O - 0 on success, -1 on error */
1167 cupsFilePutChar(cups_file_t
*fp
, /* I - CUPS file */
1168 int c
) /* I - Character to write */
1171 * Range check input...
1174 if (!fp
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1177 if (fp
->mode
== 's')
1180 * Send character immediately over socket...
1183 char ch
; /* Output character */
1188 if (send(fp
->fd
, &ch
, 1, 0) < 1)
1197 if (fp
->ptr
>= fp
->end
)
1198 if (cupsFileFlush(fp
))
1206 DEBUG_printf(("4cupsFilePutChar: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1213 * 'cupsFilePutConf()' - Write a configuration line.
1215 * This function handles any comment escaping of the value.
1217 * @since CUPS 1.4/Mac OS X 10.6@
1220 ssize_t
/* O - Number of bytes written or -1 on error */
1221 cupsFilePutConf(cups_file_t
*fp
, /* I - CUPS file */
1222 const char *directive
, /* I - Directive */
1223 const char *value
) /* I - Value */
1225 ssize_t bytes
, /* Number of bytes written */
1226 temp
; /* Temporary byte count */
1227 const char *ptr
; /* Pointer into value */
1230 if (!fp
|| !directive
|| !*directive
)
1233 if ((bytes
= cupsFilePuts(fp
, directive
)) < 0)
1236 if (cupsFilePutChar(fp
, ' ') < 0)
1240 if (value
&& *value
)
1242 if ((ptr
= strchr(value
, '#')) != NULL
)
1245 * Need to quote the first # in the info string...
1248 if ((temp
= cupsFileWrite(fp
, value
, ptr
- value
)) < 0)
1252 if (cupsFilePutChar(fp
, '\\') < 0)
1256 if ((temp
= cupsFilePuts(fp
, ptr
)) < 0)
1260 else if ((temp
= cupsFilePuts(fp
, value
)) < 0)
1266 if (cupsFilePutChar(fp
, '\n') < 0)
1274 * 'cupsFilePuts()' - Write a string.
1276 * Like the @code fputs@ function, no newline is appended to the string.
1278 * @since CUPS 1.2/Mac OS X 10.5@
1281 int /* O - Number of bytes written or -1 on error */
1282 cupsFilePuts(cups_file_t
*fp
, /* I - CUPS file */
1283 const char *s
) /* I - String to write */
1285 ssize_t bytes
; /* Bytes to write */
1289 * Range check input...
1292 if (!fp
|| !s
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1296 * Write the string...
1299 bytes
= (int)strlen(s
);
1301 if (fp
->mode
== 's')
1303 if (cups_write(fp
, s
, bytes
) < 0)
1308 DEBUG_printf(("4cupsFilePuts: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1313 if ((fp
->ptr
+ bytes
) > fp
->end
)
1314 if (cupsFileFlush(fp
))
1319 DEBUG_printf(("4cupsFilePuts: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1321 if (bytes
> sizeof(fp
->buf
))
1325 return (cups_compress(fp
, s
, bytes
));
1327 #endif /* HAVE_LIBZ */
1328 return (cups_write(fp
, s
, bytes
));
1332 memcpy(fp
->ptr
, s
, bytes
);
1340 * 'cupsFileRead()' - Read from a file.
1342 * @since CUPS 1.2/Mac OS X 10.5@
1345 ssize_t
/* O - Number of bytes read or -1 on error */
1346 cupsFileRead(cups_file_t
*fp
, /* I - CUPS file */
1347 char *buf
, /* O - Buffer */
1348 size_t bytes
) /* I - Number of bytes to read */
1350 size_t total
; /* Total bytes read */
1351 ssize_t count
; /* Bytes read */
1354 DEBUG_printf(("2cupsFileRead(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", fp
, buf
,
1355 CUPS_LLCAST bytes
));
1358 * Range check input...
1361 if (!fp
|| !buf
|| bytes
< 0 || (fp
->mode
!= 'r' && fp
->mode
!= 's'))
1368 * Loop until all bytes are read...
1374 if (fp
->ptr
>= fp
->end
)
1375 if (cups_fill(fp
) <= 0)
1377 DEBUG_printf(("4cupsFileRead: cups_fill() returned -1, total="
1378 CUPS_LLFMT
, CUPS_LLCAST total
));
1381 return ((ssize_t
)total
);
1386 count
= (ssize_t
)(fp
->end
- fp
->ptr
);
1387 if (count
> (ssize_t
)bytes
)
1388 count
= (ssize_t
)bytes
;
1390 memcpy(buf
, fp
->ptr
, count
);
1394 DEBUG_printf(("4cupsFileRead: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1397 * Update the counts for the last read...
1406 * Return the total number of bytes read...
1409 DEBUG_printf(("3cupsFileRead: total=" CUPS_LLFMT
, CUPS_LLCAST total
));
1411 return ((ssize_t
)total
);
1416 * 'cupsFileRewind()' - Set the current file position to the beginning of the
1419 * @since CUPS 1.2/Mac OS X 10.5@
1422 off_t
/* O - New file position or -1 on error */
1423 cupsFileRewind(cups_file_t
*fp
) /* I - CUPS file */
1426 * Range check input...
1429 DEBUG_printf(("cupsFileRewind(fp=%p)", fp
));
1430 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1432 if (!fp
|| fp
->mode
!= 'r')
1436 * Handle special cases...
1439 if (fp
->bufpos
== 0)
1442 * No seeking necessary...
1453 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1459 * Otherwise, seek in the file and cleanup any compression buffers...
1465 inflateEnd(&fp
->stream
);
1468 #endif /* HAVE_LIBZ */
1470 if (lseek(fp
->fd
, 0, SEEK_SET
))
1472 DEBUG_printf(("1cupsFileRewind: lseek failed: %s", strerror(errno
)));
1482 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1489 * 'cupsFileSeek()' - Seek in a file.
1491 * @since CUPS 1.2/Mac OS X 10.5@
1494 off_t
/* O - New file position or -1 on error */
1495 cupsFileSeek(cups_file_t
*fp
, /* I - CUPS file */
1496 off_t pos
) /* I - Position in file */
1498 ssize_t bytes
; /* Number bytes in buffer */
1501 DEBUG_printf(("cupsFileSeek(fp=%p, pos=" CUPS_LLFMT
")", fp
,
1503 DEBUG_printf(("2cupsFileSeek: fp->pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1504 DEBUG_printf(("2cupsFileSeek: fp->ptr=%p, fp->end=%p", fp
->ptr
, fp
->end
));
1507 * Range check input...
1510 if (!fp
|| pos
< 0 || fp
->mode
!= 'r')
1514 * Handle special cases...
1518 return (cupsFileRewind(fp
));
1522 bytes
= (ssize_t
)(fp
->end
- fp
->buf
);
1524 DEBUG_printf(("2cupsFileSeek: bytes=" CUPS_LLFMT
, CUPS_LLCAST bytes
));
1526 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1529 * No seeking necessary...
1533 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1541 if (!fp
->compressed
&& !fp
->ptr
)
1544 * Preload a buffer to determine whether the file is compressed...
1547 if (cups_fill(fp
) < 0)
1550 #endif /* HAVE_LIBZ */
1553 * Seek forwards or backwards...
1558 if (pos
< fp
->bufpos
)
1561 * Need to seek backwards...
1564 DEBUG_puts("2cupsFileSeek: SEEK BACKWARDS");
1569 inflateEnd(&fp
->stream
);
1571 lseek(fp
->fd
, 0, SEEK_SET
);
1577 while ((bytes
= cups_fill(fp
)) > 0)
1578 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1584 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1588 #endif /* HAVE_LIBZ */
1590 fp
->bufpos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1591 fp
->pos
= fp
->bufpos
;
1595 DEBUG_printf(("2cupsFileSeek: lseek() returned " CUPS_LLFMT
,
1596 CUPS_LLCAST fp
->pos
));
1602 * Need to seek forwards...
1605 DEBUG_puts("2cupsFileSeek: SEEK FORWARDS");
1610 while ((bytes
= cups_fill(fp
)) > 0)
1612 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1619 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1623 #endif /* HAVE_LIBZ */
1625 fp
->bufpos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1626 fp
->pos
= fp
->bufpos
;
1630 DEBUG_printf(("2cupsFileSeek: lseek() returned " CUPS_LLFMT
,
1631 CUPS_LLCAST fp
->pos
));
1635 DEBUG_printf(("2cupsFileSeek: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1642 * 'cupsFileStderr()' - Return a CUPS file associated with stderr.
1644 * @since CUPS 1.2/Mac OS X 10.5@
1647 cups_file_t
* /* O - CUPS file */
1648 cupsFileStderr(void)
1650 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1654 * Open file descriptor 2 as needed...
1657 if (!cg
->stdio_files
[2])
1660 * Flush any pending output on the stdio file...
1666 * Open file descriptor 2...
1669 if ((cg
->stdio_files
[2] = cupsFileOpenFd(2, "w")) != NULL
)
1670 cg
->stdio_files
[2]->is_stdio
= 1;
1673 return (cg
->stdio_files
[2]);
1678 * 'cupsFileStdin()' - Return a CUPS file associated with stdin.
1680 * @since CUPS 1.2/Mac OS X 10.5@
1683 cups_file_t
* /* O - CUPS file */
1686 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1690 * Open file descriptor 0 as needed...
1693 if (!cg
->stdio_files
[0])
1696 * Open file descriptor 0...
1699 if ((cg
->stdio_files
[0] = cupsFileOpenFd(0, "r")) != NULL
)
1700 cg
->stdio_files
[0]->is_stdio
= 1;
1703 return (cg
->stdio_files
[0]);
1708 * 'cupsFileStdout()' - Return a CUPS file associated with stdout.
1710 * @since CUPS 1.2/Mac OS X 10.5@
1713 cups_file_t
* /* O - CUPS file */
1714 cupsFileStdout(void)
1716 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1720 * Open file descriptor 1 as needed...
1723 if (!cg
->stdio_files
[1])
1726 * Flush any pending output on the stdio file...
1732 * Open file descriptor 1...
1735 if ((cg
->stdio_files
[1] = cupsFileOpenFd(1, "w")) != NULL
)
1736 cg
->stdio_files
[1]->is_stdio
= 1;
1739 return (cg
->stdio_files
[1]);
1744 * 'cupsFileTell()' - Return the current file position.
1746 * @since CUPS 1.2/Mac OS X 10.5@
1749 off_t
/* O - File position */
1750 cupsFileTell(cups_file_t
*fp
) /* I - CUPS file */
1752 DEBUG_printf(("2cupsFileTell(fp=%p)", fp
));
1753 DEBUG_printf(("3cupsFileTell: pos=" CUPS_LLFMT
,
1754 CUPS_LLCAST (fp
? fp
->pos
: -1)));
1756 return (fp
? fp
->pos
: 0);
1761 * 'cupsFileUnlock()' - Unlock access to a file.
1763 * @since CUPS 1.2/Mac OS X 10.5@
1766 int /* O - 0 on success, -1 on error */
1767 cupsFileUnlock(cups_file_t
*fp
) /* I - CUPS file */
1773 DEBUG_printf(("cupsFileUnlock(fp=%p)", fp
));
1775 if (!fp
|| fp
->mode
== 's')
1783 return (_locking(fp
->fd
, _LK_UNLCK
, 0));
1785 return (lockf(fp
->fd
, F_ULOCK
, 0));
1791 * 'cupsFileWrite()' - Write to a file.
1793 * @since CUPS 1.2/Mac OS X 10.5@
1796 ssize_t
/* O - Number of bytes written or -1 on error */
1797 cupsFileWrite(cups_file_t
*fp
, /* I - CUPS file */
1798 const char *buf
, /* I - Buffer */
1799 size_t bytes
) /* I - Number of bytes to write */
1802 * Range check input...
1805 DEBUG_printf(("2cupsFileWrite(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")",
1806 fp
, buf
, CUPS_LLCAST bytes
));
1808 if (!fp
|| !buf
|| bytes
< 0 || (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1815 * Write the buffer...
1818 if (fp
->mode
== 's')
1820 if (cups_write(fp
, buf
, bytes
) < 0)
1823 fp
->pos
+= (off_t
)bytes
;
1825 DEBUG_printf(("4cupsFileWrite: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1827 return ((ssize_t
)bytes
);
1830 if ((fp
->ptr
+ bytes
) > fp
->end
)
1831 if (cupsFileFlush(fp
))
1834 fp
->pos
+= (off_t
)bytes
;
1836 DEBUG_printf(("4cupsFileWrite: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1838 if (bytes
> sizeof(fp
->buf
))
1842 return (cups_compress(fp
, buf
, bytes
));
1844 #endif /* HAVE_LIBZ */
1845 return (cups_write(fp
, buf
, bytes
));
1849 memcpy(fp
->ptr
, buf
, bytes
);
1851 return ((ssize_t
)bytes
);
1858 * 'cups_compress()' - Compress a buffer of data...
1861 static ssize_t
/* O - Number of bytes written or -1 */
1862 cups_compress(cups_file_t
*fp
, /* I - CUPS file */
1863 const char *buf
, /* I - Buffer */
1864 size_t bytes
) /* I - Number bytes */
1866 DEBUG_printf(("7cups_compress(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", fp
, buf
,
1867 CUPS_LLCAST bytes
));
1873 fp
->crc
= crc32(fp
->crc
, (const Bytef
*)buf
, bytes
);
1876 * Deflate the bytes...
1879 fp
->stream
.next_in
= (Bytef
*)buf
;
1880 fp
->stream
.avail_in
= bytes
;
1882 while (fp
->stream
.avail_in
> 0)
1885 * Flush the current buffer...
1888 DEBUG_printf(("9cups_compress: avail_in=%d, avail_out=%d",
1889 fp
->stream
.avail_in
, fp
->stream
.avail_out
));
1891 if (fp
->stream
.avail_out
< (int)(sizeof(fp
->cbuf
) / 8))
1893 if (cups_write(fp
, (char *)fp
->cbuf
, fp
->stream
.next_out
- fp
->cbuf
) < 0)
1896 fp
->stream
.next_out
= fp
->cbuf
;
1897 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
1900 deflate(&(fp
->stream
), Z_NO_FLUSH
);
1905 #endif /* HAVE_LIBZ */
1909 * 'cups_fill()' - Fill the input buffer...
1912 static ssize_t
/* O - Number of bytes or -1 */
1913 cups_fill(cups_file_t
*fp
) /* I - CUPS file */
1915 ssize_t bytes
; /* Number of bytes read */
1917 int status
; /* Decompression status */
1918 const unsigned char *ptr
, /* Pointer into buffer */
1919 *end
; /* End of buffer */
1920 #endif /* HAVE_LIBZ */
1923 DEBUG_printf(("7cups_fill(fp=%p)", fp
));
1924 DEBUG_printf(("9cups_fill: fp->ptr=%p, fp->end=%p, fp->buf=%p, "
1925 "fp->bufpos=" CUPS_LLFMT
", fp->eof=%d",
1926 fp
->ptr
, fp
->end
, fp
->buf
, CUPS_LLCAST fp
->bufpos
, fp
->eof
));
1928 if (fp
->ptr
&& fp
->end
)
1929 fp
->bufpos
+= fp
->end
- fp
->buf
;
1932 DEBUG_printf(("9cups_fill: fp->compressed=%d", fp
->compressed
));
1934 while (!fp
->ptr
|| fp
->compressed
)
1937 * Check to see if we have read any data yet; if not, see if we have a
1938 * compressed file...
1944 * Reset the file position in case we are seeking...
1950 * Read the first bytes in the file to determine if we have a gzip'd
1954 if ((bytes
= cups_read(fp
, (char *)fp
->buf
, sizeof(fp
->buf
))) < 0)
1957 * Can't read from file!
1960 DEBUG_printf(("9cups_fill: cups_read() returned " CUPS_LLFMT
,
1961 CUPS_LLCAST bytes
));
1966 if (bytes
< 10 || fp
->buf
[0] != 0x1f ||
1967 (fp
->buf
[1] & 255) != 0x8b ||
1968 fp
->buf
[2] != 8 || (fp
->buf
[3] & 0xe0) != 0)
1971 * Not a gzip'd file!
1975 fp
->end
= fp
->buf
+ bytes
;
1977 DEBUG_printf(("9cups_fill: Returning " CUPS_LLFMT
,
1978 CUPS_LLCAST bytes
));
1984 * Parse header junk: extra data, original name, and comment...
1987 ptr
= (unsigned char *)fp
->buf
+ 10;
1988 end
= (unsigned char *)fp
->buf
+ bytes
;
1990 if (fp
->buf
[3] & 0x04)
1993 * Skip extra data...
1996 if ((ptr
+ 2) > end
)
1999 * Can't read from file!
2005 bytes
= ((unsigned char)ptr
[1] << 8) | (unsigned char)ptr
[0];
2011 * Can't read from file!
2018 if (fp
->buf
[3] & 0x08)
2021 * Skip original name data...
2024 while (ptr
< end
&& *ptr
)
2032 * Can't read from file!
2039 if (fp
->buf
[3] & 0x10)
2042 * Skip comment data...
2045 while (ptr
< end
&& *ptr
)
2053 * Can't read from file!
2060 if (fp
->buf
[3] & 0x02)
2063 * Skip header CRC data...
2071 * Can't read from file!
2079 * Copy the flate-compressed data to the compression buffer...
2082 if ((bytes
= end
- ptr
) > 0)
2083 memcpy(fp
->cbuf
, ptr
, bytes
);
2086 * Setup the decompressor data...
2089 fp
->stream
.zalloc
= (alloc_func
)0;
2090 fp
->stream
.zfree
= (free_func
)0;
2091 fp
->stream
.opaque
= (voidpf
)0;
2092 fp
->stream
.next_in
= (Bytef
*)fp
->cbuf
;
2093 fp
->stream
.next_out
= NULL
;
2094 fp
->stream
.avail_in
= bytes
;
2095 fp
->stream
.avail_out
= 0;
2096 fp
->crc
= crc32(0L, Z_NULL
, 0);
2098 if (inflateInit2(&(fp
->stream
), -15) != Z_OK
)
2107 * If we have reached end-of-file, return immediately...
2114 * Fill the decompression buffer as needed...
2117 if (fp
->stream
.avail_in
== 0)
2119 if ((bytes
= cups_read(fp
, (char *)fp
->cbuf
, sizeof(fp
->cbuf
))) <= 0)
2122 fp
->stream
.next_in
= fp
->cbuf
;
2123 fp
->stream
.avail_in
= bytes
;
2127 * Decompress data from the buffer...
2130 fp
->stream
.next_out
= (Bytef
*)fp
->buf
;
2131 fp
->stream
.avail_out
= sizeof(fp
->buf
);
2133 status
= inflate(&(fp
->stream
), Z_NO_FLUSH
);
2135 if (fp
->stream
.next_out
> (Bytef
*)fp
->buf
)
2136 fp
->crc
= crc32(fp
->crc
, (Bytef
*)fp
->buf
,
2137 fp
->stream
.next_out
- (Bytef
*)fp
->buf
);
2139 if (status
== Z_STREAM_END
)
2142 * Read the CRC and length...
2145 unsigned char trailer
[8]; /* Trailer bytes */
2146 uLong tcrc
; /* Trailer CRC */
2149 if (read(fp
->fd
, trailer
, sizeof(trailer
)) < sizeof(trailer
))
2152 * Can't get it, so mark end-of-file...
2159 tcrc
= (((((trailer
[3] << 8) | trailer
[2]) << 8) | trailer
[1]) << 8) |
2162 if (tcrc
!= fp
->crc
)
2165 * Bad CRC, mark end-of-file...
2168 DEBUG_printf(("9cups_fill: tcrc=%08x, fp->crc=%08x",
2169 (unsigned int)tcrc
, (unsigned int)fp
->crc
));
2177 * Otherwise, reset the compressed flag so that we re-read the
2185 bytes
= sizeof(fp
->buf
) - fp
->stream
.avail_out
;
2188 * Return the decompressed data...
2192 fp
->end
= fp
->buf
+ bytes
;
2198 #endif /* HAVE_LIBZ */
2201 * Read a buffer's full of data...
2204 if ((bytes
= cups_read(fp
, fp
->buf
, sizeof(fp
->buf
))) <= 0)
2207 * Can't read from file!
2218 * Return the bytes we read...
2223 fp
->end
= fp
->buf
+ bytes
;
2230 * 'cups_open()' - Safely open a file for writing.
2232 * We don't allow appending to directories or files that are hard-linked or
2236 static int /* O - File descriptor or -1 otherwise */
2237 cups_open(const char *filename
, /* I - Filename */
2238 int mode
) /* I - Open mode */
2240 int fd
; /* File descriptor */
2241 struct stat fileinfo
; /* File information */
2243 struct stat linkinfo
; /* Link information */
2251 if ((fd
= open(filename
, mode
, 0666)) < 0)
2255 * Then verify that the file descriptor doesn't point to a directory or hard-
2259 if (fstat(fd
, &fileinfo
))
2265 if (fileinfo
.st_nlink
!= 1)
2273 if (fileinfo
.st_mode
& _S_IFDIR
)
2275 if (S_ISDIR(fileinfo
.st_mode
))
2285 * Then use lstat to determine whether the filename is a symlink...
2288 if (lstat(filename
, &linkinfo
))
2294 if (S_ISLNK(linkinfo
.st_mode
) ||
2295 fileinfo
.st_dev
!= linkinfo
.st_dev
||
2296 fileinfo
.st_ino
!= linkinfo
.st_ino
||
2298 fileinfo
.st_gen
!= linkinfo
.st_gen
||
2299 #endif /* HAVE_ST_GEN */
2300 fileinfo
.st_nlink
!= linkinfo
.st_nlink
||
2301 fileinfo
.st_mode
!= linkinfo
.st_mode
)
2318 * 'cups_read()' - Read from a file descriptor.
2321 static ssize_t
/* O - Number of bytes read or -1 */
2322 cups_read(cups_file_t
*fp
, /* I - CUPS file */
2323 char *buf
, /* I - Buffer */
2324 size_t bytes
) /* I - Number bytes */
2326 ssize_t total
; /* Total bytes read */
2329 DEBUG_printf(("7cups_read(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", fp
, buf
,
2330 CUPS_LLCAST bytes
));
2333 * Loop until we read at least 0 bytes...
2339 if (fp
->mode
== 's')
2340 total
= (ssize_t
)recv(fp
->fd
, buf
, (unsigned)bytes
, 0);
2342 total
= (ssize_t
)read(fp
->fd
, buf
, (unsigned)bytes
);
2344 if (fp
->mode
== 's')
2345 total
= recv(fp
->fd
, buf
, bytes
, 0);
2347 total
= read(fp
->fd
, buf
, bytes
);
2350 DEBUG_printf(("9cups_read: total=" CUPS_LLFMT
, CUPS_LLCAST total
));
2356 * Reads can be interrupted by signals and unavailable resources...
2359 if (errno
== EAGAIN
|| errno
== EINTR
)
2366 * Return the total number of bytes read...
2374 * 'cups_write()' - Write to a file descriptor.
2377 static ssize_t
/* O - Number of bytes written or -1 */
2378 cups_write(cups_file_t
*fp
, /* I - CUPS file */
2379 const char *buf
, /* I - Buffer */
2380 size_t bytes
) /* I - Number bytes */
2382 size_t total
; /* Total bytes written */
2383 ssize_t count
; /* Count this time */
2386 DEBUG_printf(("7cups_write(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", fp
, buf
,
2387 CUPS_LLCAST bytes
));
2390 * Loop until all bytes are written...
2397 if (fp
->mode
== 's')
2398 count
= (ssize_t
)send(fp
->fd
, buf
, (unsigned)bytes
, 0);
2400 count
= (ssize_t
)write(fp
->fd
, buf
, (unsigned)bytes
);
2402 if (fp
->mode
== 's')
2403 count
= send(fp
->fd
, buf
, bytes
, 0);
2405 count
= write(fp
->fd
, buf
, bytes
);
2408 DEBUG_printf(("9cups_write: count=" CUPS_LLFMT
, CUPS_LLCAST count
));
2413 * Writes can be interrupted by signals and unavailable resources...
2416 if (errno
== EAGAIN
|| errno
== EINTR
)
2423 * Update the counts for the last write call...
2432 * Return the total number of bytes written...
2435 return ((ssize_t
)total
);
2440 * End of "$Id: file.c 7672 2008-06-18 22:03:02Z mike $".