]>
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 the Common UNIX Printing System (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-2008 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...
65 #include <sys/types.h>
67 #include "http-private.h"
73 #endif /* HAVE_LIBZ */
76 # include <sys/locking.h>
81 * Some operating systems support large files via open flag O_LARGEFILE...
85 # define O_LARGEFILE 0
86 #endif /* !O_LARGEFILE */
90 * Some operating systems don't define O_BINARY, which is used by Microsoft
91 * and IBM to flag binary files...
96 #endif /* !O_BINARY */
100 * Types and structures...
103 struct _cups_file_s
/**** CUPS file structure... ****/
106 int fd
; /* File descriptor */
107 char mode
, /* Mode ('r' or 'w') */
108 compressed
, /* Compression used? */
109 is_stdio
, /* stdin/out/err? */
110 eof
, /* End of file? */
111 buf
[4096], /* Buffer */
112 *ptr
, /* Pointer into buffer */
113 *end
; /* End of buffer data */
114 off_t pos
, /* Position in file */
115 bufpos
; /* File position for start of buffer */
118 z_stream stream
; /* (De)compression stream */
119 Bytef cbuf
[4096]; /* (De)compression buffer */
120 uLong crc
; /* (De)compression CRC */
121 #endif /* HAVE_LIBZ */
123 char *printf_buffer
; /* cupsFilePrintf buffer */
124 size_t printf_size
; /* Size of cupsFilePrintf buffer */
133 static ssize_t
cups_compress(cups_file_t
*fp
, const char *buf
, size_t bytes
);
134 #endif /* HAVE_LIBZ */
135 static ssize_t
cups_fill(cups_file_t
*fp
);
136 static ssize_t
cups_read(cups_file_t
*fp
, char *buf
, size_t bytes
);
137 static ssize_t
cups_write(cups_file_t
*fp
, const char *buf
, size_t bytes
);
141 * 'cupsFileClose()' - Close a CUPS file.
146 int /* O - 0 on success, -1 on error */
147 cupsFileClose(cups_file_t
*fp
) /* I - CUPS file */
149 int fd
; /* File descriptor */
150 char mode
; /* Open mode */
151 int status
; /* Return status */
152 int is_stdio
; /* Is a stdio file? */
155 DEBUG_printf(("cupsFileClose(fp=%p)\n", fp
));
165 * Flush pending write data...
169 status
= cupsFileFlush(fp
);
174 if (fp
->compressed
&& status
>= 0)
179 * Free decompression data...
182 inflateEnd(&fp
->stream
);
187 * Flush any remaining compressed data...
190 unsigned char trailer
[8]; /* Trailer CRC and length */
191 int done
; /* Done writing... */
194 fp
->stream
.avail_in
= 0;
198 if (fp
->stream
.next_out
> fp
->cbuf
)
200 if (cups_write(fp
, (char *)fp
->cbuf
,
201 fp
->stream
.next_out
- fp
->cbuf
) < 0)
204 fp
->stream
.next_out
= fp
->cbuf
;
205 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
208 if (done
|| status
< 0)
211 done
= deflate(&fp
->stream
, Z_FINISH
) == Z_STREAM_END
&&
212 fp
->stream
.next_out
== fp
->cbuf
;
216 * Write the CRC and length...
219 trailer
[0] = fp
->crc
;
220 trailer
[1] = fp
->crc
>> 8;
221 trailer
[2] = fp
->crc
>> 16;
222 trailer
[3] = fp
->crc
>> 24;
223 trailer
[4] = fp
->pos
;
224 trailer
[5] = fp
->pos
>> 8;
225 trailer
[6] = fp
->pos
>> 16;
226 trailer
[7] = fp
->pos
>> 24;
228 if (cups_write(fp
, (char *)trailer
, 8) < 0)
232 * Free all memory used by the compression stream...
235 deflateEnd(&(fp
->stream
));
238 #endif /* HAVE_LIBZ */
241 * Save the file descriptor we used and free memory...
246 is_stdio
= fp
->is_stdio
;
248 if (fp
->printf_buffer
)
249 free(fp
->printf_buffer
);
254 * Close the file, returning the close status...
259 if (closesocket(fd
) < 0)
273 * 'cupsFileCompression()' - Return whether a file is compressed.
278 int /* O - @code CUPS_FILE_NONE@ or @code CUPS_FILE_GZIP@ */
279 cupsFileCompression(cups_file_t
*fp
) /* I - CUPS file */
281 return (fp
? fp
->compressed
: CUPS_FILE_NONE
);
286 * 'cupsFileEOF()' - Return the end-of-file status.
291 int /* O - 1 on end of file, 0 otherwise */
292 cupsFileEOF(cups_file_t
*fp
) /* I - CUPS file */
294 return (fp
? fp
->eof
: 1);
299 * 'cupsFileFind()' - Find a file using the specified path.
301 * This function allows the paths in the path string to be separated by
302 * colons (UNIX standard) or semicolons (Windows standard) and stores the
303 * result in the buffer supplied. If the file cannot be found in any of
304 * the supplied paths, @code NULL@ is returned. A @code NULL@ path only
305 * matches the current directory.
310 const char * /* O - Full path to file or @code NULL@ if not found */
311 cupsFileFind(const char *filename
, /* I - File to find */
312 const char *path
, /* I - Colon/semicolon-separated path */
313 int executable
, /* I - 1 = executable files, 0 = any file/dir */
314 char *buffer
, /* I - Filename buffer */
315 int bufsize
) /* I - Size of filename buffer */
317 char *bufptr
, /* Current position in buffer */
318 *bufend
; /* End of buffer */
322 * Range check input...
325 if (!filename
|| !buffer
|| bufsize
< 2)
331 * No path, so check current directory...
334 if (!access(filename
, 0))
336 strlcpy(buffer
, filename
, bufsize
);
344 * Now check each path and return the first match...
347 bufend
= buffer
+ bufsize
- 1;
353 if (*path
== ';' || (*path
== ':' && ((bufptr
- buffer
) > 1 || !isalpha(buffer
[0] & 255))))
355 if (*path
== ';' || *path
== ':')
358 if (bufptr
> buffer
&& bufptr
[-1] != '/' && bufptr
< bufend
)
361 strlcpy(bufptr
, filename
, bufend
- bufptr
);
364 if (!access(buffer
, 0))
366 if (!access(buffer
, executable
? X_OK
: 0))
369 DEBUG_printf(("cupsFileFind: Returning \"%s\"\n", buffer
));
375 else if (bufptr
< bufend
)
382 * Check the last path...
385 if (bufptr
> buffer
&& bufptr
[-1] != '/' && bufptr
< bufend
)
388 strlcpy(bufptr
, filename
, bufend
- bufptr
);
390 if (!access(buffer
, 0))
392 DEBUG_printf(("cupsFileFind: Returning \"%s\"\n", buffer
));
397 DEBUG_puts("cupsFileFind: Returning NULL");
404 * 'cupsFileFlush()' - Flush pending output.
409 int /* O - 0 on success, -1 on error */
410 cupsFileFlush(cups_file_t
*fp
) /* I - CUPS file */
412 ssize_t bytes
; /* Bytes to write */
415 DEBUG_printf(("cupsFileFlush(fp=%p)\n", fp
));
418 * Range check input...
421 if (!fp
|| fp
->mode
!= 'w')
423 DEBUG_puts("cupsFileFlush: Attempt to flush a read-only file...");
427 bytes
= (ssize_t
)(fp
->ptr
- fp
->buf
);
429 DEBUG_printf(("cupsFileFlush: Flushing " CUPS_LLFMT
" bytes...\n",
436 bytes
= cups_compress(fp
, fp
->buf
, bytes
);
438 #endif /* HAVE_LIBZ */
439 bytes
= cups_write(fp
, fp
->buf
, bytes
);
452 * 'cupsFileGetChar()' - Get a single character from a file.
457 int /* O - Character or -1 on end of file */
458 cupsFileGetChar(cups_file_t
*fp
) /* I - CUPS file */
461 * Range check input...
464 DEBUG_printf(("cupsFileGetChar(fp=%p)\n", fp
));
466 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
468 DEBUG_puts("cupsFileGetChar: Bad arguments!");
473 * If the input buffer is empty, try to read more data...
476 if (fp
->ptr
>= fp
->end
)
477 if (cups_fill(fp
) < 0)
479 DEBUG_puts("cupsFileGetChar: Unable to fill buffer!");
484 * Return the next character in the buffer...
487 DEBUG_printf(("cupsFileGetChar: Returning %d...\n", *(fp
->ptr
) & 255));
491 DEBUG_printf(("cupsFileGetChar: pos=" CUPS_LLFMT
"\n", CUPS_LLCAST fp
->pos
));
493 return (*(fp
->ptr
)++ & 255);
498 * 'cupsFileGetConf()' - Get a line from a configuration file...
503 char * /* O - Line read or @code NULL@ on end of file or error */
504 cupsFileGetConf(cups_file_t
*fp
, /* I - CUPS file */
505 char *buf
, /* O - String buffer */
506 size_t buflen
, /* I - Size of string buffer */
507 char **value
, /* O - Pointer to value */
508 int *linenum
) /* IO - Current line number */
510 char *ptr
; /* Pointer into line */
514 * Range check input...
517 DEBUG_printf(("cupsFileGetConf(fp=%p, buf=%p, buflen=" CUPS_LLFMT
518 ", value=%p, linenum=%p)\n", fp
, buf
, CUPS_LLCAST buflen
,
521 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') ||
522 !buf
|| buflen
< 2 || !value
)
531 * Read the next non-comment line...
536 while (cupsFileGets(fp
, buf
, buflen
))
541 * Strip any comments...
544 if ((ptr
= strchr(buf
, '#')) != NULL
)
546 if (ptr
> buf
&& ptr
[-1] == '\\')
549 _cups_strcpy(ptr
- 1, ptr
);
553 // Strip the comment and any trailing whitespace...
556 if (!isspace(ptr
[-1] & 255))
567 * Strip leading whitespace...
570 for (ptr
= buf
; isspace(*ptr
& 255); ptr
++);
573 _cups_strcpy(buf
, ptr
);
576 * See if there is anything left...
582 * Yes, grab any value and return...
585 for (ptr
= buf
; *ptr
; ptr
++)
586 if (isspace(*ptr
& 255))
592 * Have a value, skip any other spaces...
595 while (isspace(*ptr
& 255))
602 * Strip trailing whitespace and > for lines that begin with <...
605 ptr
+= strlen(ptr
) - 1;
607 if (buf
[0] == '<' && *ptr
== '>')
609 else if (buf
[0] == '<' && *ptr
!= '>')
619 while (ptr
> *value
&& isspace(*ptr
& 255))
636 * 'cupsFileGetLine()' - Get a CR and/or LF-terminated line that may
637 * contain binary data.
639 * This function differs from @link cupsFileGets@ in that the trailing CR
640 * and LF are preserved, as is any binary data on the line. The buffer is
641 * nul-terminated, however you should use the returned length to determine
642 * the number of bytes on the line.
647 size_t /* O - Number of bytes on line or 0 on end of file */
648 cupsFileGetLine(cups_file_t
*fp
, /* I - File to read from */
649 char *buf
, /* I - Buffer */
650 size_t buflen
) /* I - Size of buffer */
652 int ch
; /* Character from file */
653 char *ptr
, /* Current position in line buffer */
654 *end
; /* End of line buffer */
658 * Range check input...
661 DEBUG_printf(("cupsFileGetLine(fp=%p, buf=%p, buflen=" CUPS_LLFMT
")\n",
662 fp
, buf
, CUPS_LLCAST buflen
));
664 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') || !buf
|| buflen
< 3)
668 * Now loop until we have a valid line...
671 for (ptr
= buf
, end
= buf
+ buflen
- 2; ptr
< end
;)
673 if (fp
->ptr
>= fp
->end
)
674 if (cups_fill(fp
) <= 0)
677 *ptr
++ = ch
= *(fp
->ptr
)++;
686 if (fp
->ptr
>= fp
->end
)
687 if (cups_fill(fp
) <= 0)
690 if (*(fp
->ptr
) == '\n')
692 *ptr
++ = *(fp
->ptr
)++;
701 * Line feed ends a line...
710 DEBUG_printf(("cupsFileGetLine: pos=" CUPS_LLFMT
"\n", CUPS_LLCAST fp
->pos
));
717 * 'cupsFileGets()' - Get a CR and/or LF-terminated line.
722 char * /* O - Line read or @code NULL@ on end of file or error */
723 cupsFileGets(cups_file_t
*fp
, /* I - CUPS file */
724 char *buf
, /* O - String buffer */
725 size_t buflen
) /* I - Size of string buffer */
727 int ch
; /* Character from file */
728 char *ptr
, /* Current position in line buffer */
729 *end
; /* End of line buffer */
733 * Range check input...
736 DEBUG_printf(("cupsFileGets(fp=%p, buf=%p, buflen=" CUPS_LLFMT
")\n", fp
, buf
,
737 CUPS_LLCAST buflen
));
739 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') || !buf
|| buflen
< 2)
743 * Now loop until we have a valid line...
746 for (ptr
= buf
, end
= buf
+ buflen
- 1; ptr
< end
;)
748 if (fp
->ptr
>= fp
->end
)
749 if (cups_fill(fp
) <= 0)
766 if (fp
->ptr
>= fp
->end
)
767 if (cups_fill(fp
) <= 0)
770 if (*(fp
->ptr
) == '\n')
781 * Line feed ends a line...
792 DEBUG_printf(("cupsFileGets: pos=" CUPS_LLFMT
"\n", CUPS_LLCAST fp
->pos
));
799 * 'cupsFileLock()' - Temporarily lock access to a file.
804 int /* O - 0 on success, -1 on error */
805 cupsFileLock(cups_file_t
*fp
, /* I - CUPS file */
806 int block
) /* I - 1 to wait for the lock, 0 to fail right away */
812 if (!fp
|| fp
->mode
== 's')
820 return (locking(fp
->fd
, block
? _LK_LOCK
: _LK_NBLCK
, 0));
822 return (lockf(fp
->fd
, block
? F_LOCK
: F_TLOCK
, 0));
828 * 'cupsFileNumber()' - Return the file descriptor associated with a CUPS file.
833 int /* O - File descriptor */
834 cupsFileNumber(cups_file_t
*fp
) /* I - CUPS file */
844 * 'cupsFileOpen()' - Open a CUPS file.
846 * The "mode" parameter can be "r" to read, "w" to write, overwriting any
847 * existing file, "a" to append to an existing file or create a new file,
848 * or "s" to open a socket connection.
850 * When opening for writing ("w"), an optional number from 1 to 9 can be
851 * supplied which enables Flate compression of the file. Compression is
852 * not supported for the "a" (append) mode.
854 * When opening a socket connection, the filename is a string of the form
855 * "address:port" or "hostname:port". The socket will make an IPv4 or IPv6
856 * connection as needed, generally preferring IPv6 connections when there is
862 cups_file_t
* /* O - CUPS file or @code NULL@ if the file or socket cannot be opened */
863 cupsFileOpen(const char *filename
, /* I - Name of file */
864 const char *mode
) /* I - Open mode */
866 cups_file_t
*fp
; /* New CUPS file */
867 int fd
; /* File descriptor */
868 char hostname
[1024], /* Hostname */
869 *portname
; /* Port "name" (number or service) */
870 http_addrlist_t
*addrlist
; /* Host address list */
873 DEBUG_printf(("cupsFileOpen(filename=\"%s\", mode=\"%s\")\n", filename
,
877 * Range check input...
880 if (!filename
|| !mode
||
881 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's') ||
882 (*mode
== 'a' && isdigit(mode
[1] & 255)))
891 case 'a' : /* Append file */
892 fd
= open(filename
, O_RDWR
| O_CREAT
| O_APPEND
| O_LARGEFILE
| O_BINARY
, 0666);
895 case 'r' : /* Read file */
896 fd
= open(filename
, O_RDONLY
| O_LARGEFILE
| O_BINARY
, 0);
899 case 'w' : /* Write file */
900 fd
= open(filename
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_LARGEFILE
| O_BINARY
, 0666);
903 case 's' : /* Read/write socket */
904 strlcpy(hostname
, filename
, sizeof(hostname
));
905 if ((portname
= strrchr(hostname
, ':')) != NULL
)
911 * Lookup the hostname and service...
914 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
918 * Connect to the server...
921 if (!httpAddrConnect(addrlist
, &fd
))
923 httpAddrFreeList(addrlist
);
927 httpAddrFreeList(addrlist
);
930 default : /* Remove bogus compiler warning... */
938 * Create the CUPS file structure...
941 if ((fp
= cupsFileOpenFd(fd
, mode
)) == NULL
)
957 * 'cupsFileOpenFd()' - Open a CUPS file using a file descriptor.
959 * The "mode" parameter can be "r" to read, "w" to write, "a" to append,
960 * or "s" to treat the file descriptor as a bidirectional socket connection.
962 * When opening for writing ("w"), an optional number from 1 to 9 can be
963 * supplied which enables Flate compression of the file. Compression is
964 * not supported for the "a" (append) mode.
969 cups_file_t
* /* O - CUPS file or @code NULL@ if the file could not be opened */
970 cupsFileOpenFd(int fd
, /* I - File descriptor */
971 const char *mode
) /* I - Open mode */
973 cups_file_t
*fp
; /* New CUPS file */
976 DEBUG_printf(("cupsFileOpenFd(fd=%d, mode=\"%s\")\n", fd
, mode
));
979 * Range check input...
982 if (fd
< 0 || !mode
||
983 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's') ||
984 (*mode
== 'a' && isdigit(mode
[1] & 255)))
991 if ((fp
= calloc(1, sizeof(cups_file_t
))) == NULL
)
1003 fp
->pos
= lseek(fd
, 0, SEEK_END
);
1008 fp
->end
= fp
->buf
+ sizeof(fp
->buf
);
1011 if (mode
[1] >= '1' && mode
[1] <= '9')
1014 * Open a compressed stream, so write the standard gzip file
1018 unsigned char header
[10]; /* gzip file header */
1019 time_t curtime
; /* Current time */
1022 curtime
= time(NULL
);
1025 header
[2] = Z_DEFLATED
;
1027 header
[4] = curtime
;
1028 header
[5] = curtime
>> 8;
1029 header
[6] = curtime
>> 16;
1030 header
[7] = curtime
>> 24;
1034 cups_write(fp
, (char *)header
, 10);
1037 * Initialize the compressor...
1040 deflateInit2(&(fp
->stream
), mode
[1] - '0', Z_DEFLATED
, -15, 8,
1041 Z_DEFAULT_STRATEGY
);
1043 fp
->stream
.next_out
= fp
->cbuf
;
1044 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
1046 fp
->crc
= crc32(0L, Z_NULL
, 0);
1048 #endif /* HAVE_LIBZ */
1059 default : /* Remove bogus compiler warning... */
1064 * Don't pass this file to child processes...
1068 fcntl(fp
->fd
, F_SETFD
, fcntl(fp
->fd
, F_GETFD
) | FD_CLOEXEC
);
1076 * 'cupsFilePeekChar()' - Peek at the next character from a file.
1081 int /* O - Character or -1 on end of file */
1082 cupsFilePeekChar(cups_file_t
*fp
) /* I - CUPS file */
1085 * Range check input...
1088 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
1092 * If the input buffer is empty, try to read more data...
1095 if (fp
->ptr
>= fp
->end
)
1096 if (cups_fill(fp
) < 0)
1100 * Return the next character in the buffer...
1103 return (*(fp
->ptr
) & 255);
1108 * 'cupsFilePrintf()' - Write a formatted string.
1113 int /* O - Number of bytes written or -1 on error */
1114 cupsFilePrintf(cups_file_t
*fp
, /* I - CUPS file */
1115 const char *format
, /* I - Printf-style format string */
1116 ...) /* I - Additional args as necessary */
1118 va_list ap
; /* Argument list */
1119 ssize_t bytes
; /* Formatted size */
1122 DEBUG_printf(("cupsFilePrintf(fp=%p, format=\"%s\", ...)\n", fp
, format
));
1124 if (!fp
|| !format
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1127 if (!fp
->printf_buffer
)
1130 * Start with an 1k printf buffer...
1133 if ((fp
->printf_buffer
= malloc(1024)) == NULL
)
1136 fp
->printf_size
= 1024;
1139 va_start(ap
, format
);
1140 bytes
= vsnprintf(fp
->printf_buffer
, fp
->printf_size
, format
, ap
);
1143 if (bytes
>= fp
->printf_size
)
1146 * Expand the printf buffer...
1149 char *temp
; /* Temporary buffer pointer */
1155 if ((temp
= realloc(fp
->printf_buffer
, bytes
+ 1)) == NULL
)
1158 fp
->printf_buffer
= temp
;
1159 fp
->printf_size
= bytes
+ 1;
1161 va_start(ap
, format
);
1162 bytes
= vsnprintf(fp
->printf_buffer
, fp
->printf_size
, format
, ap
);
1166 if (fp
->mode
== 's')
1168 if (cups_write(fp
, fp
->printf_buffer
, bytes
) < 0)
1173 DEBUG_printf(("cupsFilePrintf: pos=" CUPS_LLFMT
"\n", CUPS_LLCAST fp
->pos
));
1178 if ((fp
->ptr
+ bytes
) > fp
->end
)
1179 if (cupsFileFlush(fp
))
1184 DEBUG_printf(("cupsFilePrintf: pos=" CUPS_LLFMT
"\n", CUPS_LLCAST fp
->pos
));
1186 if (bytes
> sizeof(fp
->buf
))
1190 return (cups_compress(fp
, fp
->printf_buffer
, bytes
));
1192 #endif /* HAVE_LIBZ */
1193 return (cups_write(fp
, fp
->printf_buffer
, bytes
));
1197 memcpy(fp
->ptr
, fp
->printf_buffer
, bytes
);
1205 * 'cupsFilePutChar()' - Write a character.
1210 int /* O - 0 on success, -1 on error */
1211 cupsFilePutChar(cups_file_t
*fp
, /* I - CUPS file */
1212 int c
) /* I - Character to write */
1215 * Range check input...
1218 if (!fp
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1221 if (fp
->mode
== 's')
1224 * Send character immediately over socket...
1227 char ch
; /* Output character */
1232 if (send(fp
->fd
, &ch
, 1, 0) < 1)
1241 if (fp
->ptr
>= fp
->end
)
1242 if (cupsFileFlush(fp
))
1250 DEBUG_printf(("cupsFilePutChar: pos=" CUPS_LLFMT
"\n", CUPS_LLCAST fp
->pos
));
1257 * 'cupsFilePuts()' - Write a string.
1259 * Like the @code fputs@ function, no newline is appended to the string.
1264 int /* O - Number of bytes written or -1 on error */
1265 cupsFilePuts(cups_file_t
*fp
, /* I - CUPS file */
1266 const char *s
) /* I - String to write */
1268 ssize_t bytes
; /* Bytes to write */
1272 * Range check input...
1275 if (!fp
|| !s
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1279 * Write the string...
1282 bytes
= (int)strlen(s
);
1284 if (fp
->mode
== 's')
1286 if (cups_write(fp
, s
, bytes
) < 0)
1291 DEBUG_printf(("cupsFilePuts: pos=" CUPS_LLFMT
"\n", CUPS_LLCAST fp
->pos
));
1296 if ((fp
->ptr
+ bytes
) > fp
->end
)
1297 if (cupsFileFlush(fp
))
1302 DEBUG_printf(("cupsFilePuts: pos=" CUPS_LLFMT
"\n", CUPS_LLCAST fp
->pos
));
1304 if (bytes
> sizeof(fp
->buf
))
1308 return (cups_compress(fp
, s
, bytes
));
1310 #endif /* HAVE_LIBZ */
1311 return (cups_write(fp
, s
, bytes
));
1315 memcpy(fp
->ptr
, s
, bytes
);
1323 * 'cupsFileRead()' - Read from a file.
1328 ssize_t
/* O - Number of bytes read or -1 on error */
1329 cupsFileRead(cups_file_t
*fp
, /* I - CUPS file */
1330 char *buf
, /* O - Buffer */
1331 size_t bytes
) /* I - Number of bytes to read */
1333 size_t total
; /* Total bytes read */
1334 ssize_t count
; /* Bytes read */
1337 DEBUG_printf(("cupsFileRead(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")\n", fp
, buf
,
1338 CUPS_LLCAST bytes
));
1341 * Range check input...
1344 if (!fp
|| !buf
|| bytes
< 0 || (fp
->mode
!= 'r' && fp
->mode
!= 's'))
1351 * Loop until all bytes are read...
1357 if (fp
->ptr
>= fp
->end
)
1358 if (cups_fill(fp
) <= 0)
1360 DEBUG_printf(("cupsFileRead: cups_fill() returned -1, total=" CUPS_LLFMT
"\n",
1361 CUPS_LLCAST total
));
1364 return ((ssize_t
)total
);
1369 count
= (ssize_t
)(fp
->end
- fp
->ptr
);
1370 if (count
> (ssize_t
)bytes
)
1371 count
= (ssize_t
)bytes
;
1373 memcpy(buf
, fp
->ptr
, count
);
1377 DEBUG_printf(("cupsFileRead: pos=" CUPS_LLFMT
"\n", CUPS_LLCAST fp
->pos
));
1380 * Update the counts for the last read...
1389 * Return the total number of bytes read...
1392 DEBUG_printf(("cupsFileRead: total=%d\n", (int)total
));
1394 return ((ssize_t
)total
);
1399 * 'cupsFileRewind()' - Set the current file position to the beginning of the
1405 off_t
/* O - New file position or -1 on error */
1406 cupsFileRewind(cups_file_t
*fp
) /* I - CUPS file */
1409 * Range check input...
1412 DEBUG_printf(("cupsFileRewind(fp=%p)\n", fp
));
1413 DEBUG_printf(("cupsFileRewind: pos=" CUPS_LLFMT
"\n", CUPS_LLCAST fp
->pos
));
1415 if (!fp
|| fp
->mode
!= 'r')
1419 * Handle special cases...
1422 if (fp
->bufpos
== 0)
1425 * No seeking necessary...
1436 DEBUG_printf(("cupsFileRewind: pos=" CUPS_LLFMT
"\n", CUPS_LLCAST fp
->pos
));
1442 * Otherwise, seek in the file and cleanup any compression buffers...
1448 inflateEnd(&fp
->stream
);
1451 #endif /* HAVE_LIBZ */
1453 if (lseek(fp
->fd
, 0, SEEK_SET
))
1455 DEBUG_printf(("cupsFileRewind: lseek failed: %s\n", strerror(errno
)));
1465 DEBUG_printf(("cupsFileRewind: pos=" CUPS_LLFMT
"\n", CUPS_LLCAST fp
->pos
));
1472 * 'cupsFileSeek()' - Seek in a file.
1477 off_t
/* O - New file position or -1 on error */
1478 cupsFileSeek(cups_file_t
*fp
, /* I - CUPS file */
1479 off_t pos
) /* I - Position in file */
1481 ssize_t bytes
; /* Number bytes in buffer */
1484 DEBUG_printf(("cupsFileSeek(fp=%p, pos=" CUPS_LLFMT
")\n", fp
,
1486 DEBUG_printf(("cupsFileSeek: fp->pos=" CUPS_LLFMT
"\n", CUPS_LLCAST fp
->pos
));
1487 DEBUG_printf(("cupsFileSeek: fp->ptr=%p, fp->end=%p\n", fp
->ptr
, fp
->end
));
1490 * Range check input...
1493 if (!fp
|| pos
< 0 || fp
->mode
!= 'r')
1497 * Handle special cases...
1501 return (cupsFileRewind(fp
));
1505 bytes
= (ssize_t
)(fp
->end
- fp
->buf
);
1507 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1510 * No seeking necessary...
1514 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1522 if (!fp
->compressed
&& !fp
->ptr
)
1525 * Preload a buffer to determine whether the file is compressed...
1528 if (cups_fill(fp
) < 0)
1531 #endif /* HAVE_LIBZ */
1534 * Seek forwards or backwards...
1539 DEBUG_printf(("cupsFileSeek: bytes=" CUPS_LLFMT
"\n", CUPS_LLCAST bytes
));
1541 if (pos
< fp
->bufpos
)
1544 * Need to seek backwards...
1547 DEBUG_puts("cupsFileSeek: SEEK BACKWARDS");
1552 inflateEnd(&fp
->stream
);
1554 lseek(fp
->fd
, 0, SEEK_SET
);
1560 while ((bytes
= cups_fill(fp
)) > 0)
1561 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1567 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1571 #endif /* HAVE_LIBZ */
1573 fp
->bufpos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1574 fp
->pos
= fp
->bufpos
;
1578 DEBUG_printf(("cupsFileSeek: lseek() returned " CUPS_LLFMT
"...\n",
1579 CUPS_LLCAST fp
->pos
));
1585 * Need to seek forwards...
1588 DEBUG_puts("cupsFileSeek: SEEK FORWARDS");
1593 while ((bytes
= cups_fill(fp
)) > 0)
1595 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1602 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1606 #endif /* HAVE_LIBZ */
1608 fp
->bufpos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1609 fp
->pos
= fp
->bufpos
;
1613 DEBUG_printf(("cupsFileSeek: lseek() returned " CUPS_LLFMT
"...\n",
1614 CUPS_LLCAST fp
->pos
));
1618 DEBUG_printf(("cupsFileSeek: pos=" CUPS_LLFMT
"\n", CUPS_LLCAST fp
->pos
));
1625 * 'cupsFileStderr()' - Return a CUPS file associated with stderr.
1630 cups_file_t
* /* O - CUPS file */
1631 cupsFileStderr(void)
1633 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1637 * Open file descriptor 2 as needed...
1640 if (!cg
->stdio_files
[2])
1643 * Flush any pending output on the stdio file...
1649 * Open file descriptor 2...
1652 if ((cg
->stdio_files
[2] = cupsFileOpenFd(2, "w")) != NULL
)
1653 cg
->stdio_files
[2]->is_stdio
= 1;
1656 return (cg
->stdio_files
[2]);
1661 * 'cupsFileStdin()' - Return a CUPS file associated with stdin.
1666 cups_file_t
* /* O - CUPS file */
1669 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1673 * Open file descriptor 0 as needed...
1676 if (!cg
->stdio_files
[0])
1679 * Open file descriptor 0...
1682 if ((cg
->stdio_files
[0] = cupsFileOpenFd(0, "r")) != NULL
)
1683 cg
->stdio_files
[0]->is_stdio
= 1;
1686 return (cg
->stdio_files
[0]);
1691 * 'cupsFileStdout()' - Return a CUPS file associated with stdout.
1696 cups_file_t
* /* O - CUPS file */
1697 cupsFileStdout(void)
1699 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1703 * Open file descriptor 1 as needed...
1706 if (!cg
->stdio_files
[1])
1709 * Flush any pending output on the stdio file...
1715 * Open file descriptor 1...
1718 if ((cg
->stdio_files
[1] = cupsFileOpenFd(1, "w")) != NULL
)
1719 cg
->stdio_files
[1]->is_stdio
= 1;
1722 return (cg
->stdio_files
[1]);
1727 * 'cupsFileTell()' - Return the current file position.
1732 off_t
/* O - File position */
1733 cupsFileTell(cups_file_t
*fp
) /* I - CUPS file */
1735 DEBUG_printf(("cupsFileTell(fp=%p)\n", fp
));
1736 DEBUG_printf(("cupsFileTell: pos=" CUPS_LLFMT
"\n", CUPS_LLCAST (fp
? fp
->pos
: -1)));
1738 return (fp
? fp
->pos
: 0);
1743 * 'cupsFileUnlock()' - Unlock access to a file.
1748 int /* O - 0 on success, -1 on error */
1749 cupsFileUnlock(cups_file_t
*fp
) /* I - CUPS file */
1755 DEBUG_printf(("cupsFileUnlock(fp=%p)\n", fp
));
1757 if (!fp
|| fp
->mode
== 's')
1765 return (locking(fp
->fd
, _LK_UNLCK
, 0));
1767 return (lockf(fp
->fd
, F_ULOCK
, 0));
1773 * 'cupsFileWrite()' - Write to a file.
1778 ssize_t
/* O - Number of bytes written or -1 on error */
1779 cupsFileWrite(cups_file_t
*fp
, /* I - CUPS file */
1780 const char *buf
, /* I - Buffer */
1781 size_t bytes
) /* I - Number of bytes to write */
1784 * Range check input...
1787 DEBUG_printf(("cupsFileWrite(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")\n",
1788 fp
, buf
, CUPS_LLCAST bytes
));
1790 if (!fp
|| !buf
|| bytes
< 0 || (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1797 * Write the buffer...
1800 if (fp
->mode
== 's')
1802 if (cups_write(fp
, buf
, bytes
) < 0)
1805 fp
->pos
+= (off_t
)bytes
;
1807 DEBUG_printf(("cupsFileWrite: pos=" CUPS_LLFMT
"\n", CUPS_LLCAST fp
->pos
));
1809 return ((ssize_t
)bytes
);
1812 if ((fp
->ptr
+ bytes
) > fp
->end
)
1813 if (cupsFileFlush(fp
))
1816 fp
->pos
+= (off_t
)bytes
;
1818 DEBUG_printf(("cupsFileWrite: pos=" CUPS_LLFMT
"\n", CUPS_LLCAST fp
->pos
));
1820 if (bytes
> sizeof(fp
->buf
))
1824 return (cups_compress(fp
, buf
, bytes
));
1826 #endif /* HAVE_LIBZ */
1827 return (cups_write(fp
, buf
, bytes
));
1831 memcpy(fp
->ptr
, buf
, bytes
);
1833 return ((ssize_t
)bytes
);
1840 * 'cups_compress()' - Compress a buffer of data...
1843 static ssize_t
/* O - Number of bytes written or -1 */
1844 cups_compress(cups_file_t
*fp
, /* I - CUPS file */
1845 const char *buf
, /* I - Buffer */
1846 size_t bytes
) /* I - Number bytes */
1848 DEBUG_printf(("cups_compress(fp=%p, buf=%p, bytes=" CUPS_LLFMT
"\n", fp
, buf
,
1849 CUPS_LLCAST bytes
));
1855 fp
->crc
= crc32(fp
->crc
, (const Bytef
*)buf
, bytes
);
1858 * Deflate the bytes...
1861 fp
->stream
.next_in
= (Bytef
*)buf
;
1862 fp
->stream
.avail_in
= bytes
;
1864 while (fp
->stream
.avail_in
> 0)
1867 * Flush the current buffer...
1870 DEBUG_printf(("cups_compress: avail_in=%d, avail_out=%d\n",
1871 fp
->stream
.avail_in
, fp
->stream
.avail_out
));
1873 if (fp
->stream
.avail_out
< (int)(sizeof(fp
->cbuf
) / 8))
1875 if (cups_write(fp
, (char *)fp
->cbuf
, fp
->stream
.next_out
- fp
->cbuf
) < 0)
1878 fp
->stream
.next_out
= fp
->cbuf
;
1879 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
1882 deflate(&(fp
->stream
), Z_NO_FLUSH
);
1887 #endif /* HAVE_LIBZ */
1891 * 'cups_fill()' - Fill the input buffer...
1894 static ssize_t
/* O - Number of bytes or -1 */
1895 cups_fill(cups_file_t
*fp
) /* I - CUPS file */
1897 ssize_t bytes
; /* Number of bytes read */
1899 int status
; /* Decompression status */
1900 const unsigned char *ptr
, /* Pointer into buffer */
1901 *end
; /* End of buffer */
1902 #endif /* HAVE_LIBZ */
1905 DEBUG_printf(("cups_fill(fp=%p)\n", fp
));
1906 DEBUG_printf(("cups_fill: fp->ptr=%p, fp->end=%p, fp->buf=%p, "
1907 "fp->bufpos=" CUPS_LLFMT
", fp->eof=%d\n",
1908 fp
->ptr
, fp
->end
, fp
->buf
, CUPS_LLCAST fp
->bufpos
, fp
->eof
));
1910 if (fp
->ptr
&& fp
->end
)
1911 fp
->bufpos
+= fp
->end
- fp
->buf
;
1914 DEBUG_printf(("cups_fill: fp->compressed=%d\n", fp
->compressed
));
1916 while (!fp
->ptr
|| fp
->compressed
)
1919 * Check to see if we have read any data yet; if not, see if we have a
1920 * compressed file...
1926 * Reset the file position in case we are seeking...
1932 * Read the first bytes in the file to determine if we have a gzip'd
1936 if ((bytes
= cups_read(fp
, (char *)fp
->buf
, sizeof(fp
->buf
))) < 0)
1939 * Can't read from file!
1942 DEBUG_printf(("cups_fill: cups_read() returned " CUPS_LLFMT
"!\n",
1943 CUPS_LLCAST bytes
));
1948 if (bytes
< 10 || fp
->buf
[0] != 0x1f ||
1949 (fp
->buf
[1] & 255) != 0x8b ||
1950 fp
->buf
[2] != 8 || (fp
->buf
[3] & 0xe0) != 0)
1953 * Not a gzip'd file!
1957 fp
->end
= fp
->buf
+ bytes
;
1959 DEBUG_printf(("cups_fill: Returning " CUPS_LLFMT
"!\n",
1960 CUPS_LLCAST bytes
));
1966 * Parse header junk: extra data, original name, and comment...
1969 ptr
= (unsigned char *)fp
->buf
+ 10;
1970 end
= (unsigned char *)fp
->buf
+ bytes
;
1972 if (fp
->buf
[3] & 0x04)
1975 * Skip extra data...
1978 if ((ptr
+ 2) > end
)
1981 * Can't read from file!
1987 bytes
= ((unsigned char)ptr
[1] << 8) | (unsigned char)ptr
[0];
1993 * Can't read from file!
2000 if (fp
->buf
[3] & 0x08)
2003 * Skip original name data...
2006 while (ptr
< end
&& *ptr
)
2014 * Can't read from file!
2021 if (fp
->buf
[3] & 0x10)
2024 * Skip comment data...
2027 while (ptr
< end
&& *ptr
)
2035 * Can't read from file!
2042 if (fp
->buf
[3] & 0x02)
2045 * Skip header CRC data...
2053 * Can't read from file!
2061 * Copy the flate-compressed data to the compression buffer...
2064 if ((bytes
= end
- ptr
) > 0)
2065 memcpy(fp
->cbuf
, ptr
, bytes
);
2068 * Setup the decompressor data...
2071 fp
->stream
.zalloc
= (alloc_func
)0;
2072 fp
->stream
.zfree
= (free_func
)0;
2073 fp
->stream
.opaque
= (voidpf
)0;
2074 fp
->stream
.next_in
= (Bytef
*)fp
->cbuf
;
2075 fp
->stream
.next_out
= NULL
;
2076 fp
->stream
.avail_in
= bytes
;
2077 fp
->stream
.avail_out
= 0;
2078 fp
->crc
= crc32(0L, Z_NULL
, 0);
2080 if (inflateInit2(&(fp
->stream
), -15) != Z_OK
)
2089 * If we have reached end-of-file, return immediately...
2096 * Fill the decompression buffer as needed...
2099 if (fp
->stream
.avail_in
== 0)
2101 if ((bytes
= cups_read(fp
, (char *)fp
->cbuf
, sizeof(fp
->cbuf
))) <= 0)
2104 fp
->stream
.next_in
= fp
->cbuf
;
2105 fp
->stream
.avail_in
= bytes
;
2109 * Decompress data from the buffer...
2112 fp
->stream
.next_out
= (Bytef
*)fp
->buf
;
2113 fp
->stream
.avail_out
= sizeof(fp
->buf
);
2115 status
= inflate(&(fp
->stream
), Z_NO_FLUSH
);
2117 if (fp
->stream
.next_out
> (Bytef
*)fp
->buf
)
2118 fp
->crc
= crc32(fp
->crc
, (Bytef
*)fp
->buf
,
2119 fp
->stream
.next_out
- (Bytef
*)fp
->buf
);
2121 if (status
== Z_STREAM_END
)
2124 * Read the CRC and length...
2127 unsigned char trailer
[8]; /* Trailer bytes */
2128 uLong tcrc
; /* Trailer CRC */
2131 if (read(fp
->fd
, trailer
, sizeof(trailer
)) < sizeof(trailer
))
2134 * Can't get it, so mark end-of-file...
2141 tcrc
= (((((trailer
[3] << 8) | trailer
[2]) << 8) | trailer
[1]) << 8) |
2144 if (tcrc
!= fp
->crc
)
2147 * Bad CRC, mark end-of-file...
2150 DEBUG_printf(("cups_fill: tcrc=%08x, fp->crc=%08x\n",
2151 (unsigned int)tcrc
, (unsigned int)fp
->crc
));
2159 * Otherwise, reset the compressed flag so that we re-read the
2167 bytes
= sizeof(fp
->buf
) - fp
->stream
.avail_out
;
2170 * Return the decompressed data...
2174 fp
->end
= fp
->buf
+ bytes
;
2180 #endif /* HAVE_LIBZ */
2183 * Read a buffer's full of data...
2186 if ((bytes
= cups_read(fp
, fp
->buf
, sizeof(fp
->buf
))) <= 0)
2189 * Can't read from file!
2200 * Return the bytes we read...
2205 fp
->end
= fp
->buf
+ bytes
;
2212 * 'cups_read()' - Read from a file descriptor.
2215 static ssize_t
/* O - Number of bytes read or -1 */
2216 cups_read(cups_file_t
*fp
, /* I - CUPS file */
2217 char *buf
, /* I - Buffer */
2218 size_t bytes
) /* I - Number bytes */
2220 ssize_t total
; /* Total bytes read */
2223 DEBUG_printf(("cups_read(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")\n", fp
, buf
,
2224 CUPS_LLCAST bytes
));
2227 * Loop until we read at least 0 bytes...
2233 if (fp
->mode
== 's')
2234 total
= (ssize_t
)recv(fp
->fd
, buf
, (unsigned)bytes
, 0);
2236 total
= (ssize_t
)read(fp
->fd
, buf
, (unsigned)bytes
);
2238 if (fp
->mode
== 's')
2239 total
= recv(fp
->fd
, buf
, bytes
, 0);
2241 total
= read(fp
->fd
, buf
, bytes
);
2244 DEBUG_printf(("cups_read: total=" CUPS_LLFMT
"\n", CUPS_LLCAST total
));
2250 * Reads can be interrupted by signals and unavailable resources...
2253 if (errno
== EAGAIN
|| errno
== EINTR
)
2260 * Return the total number of bytes read...
2268 * 'cups_write()' - Write to a file descriptor.
2271 static ssize_t
/* O - Number of bytes written or -1 */
2272 cups_write(cups_file_t
*fp
, /* I - CUPS file */
2273 const char *buf
, /* I - Buffer */
2274 size_t bytes
) /* I - Number bytes */
2276 size_t total
; /* Total bytes written */
2277 ssize_t count
; /* Count this time */
2280 DEBUG_printf(("cups_write(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")\n", fp
, buf
,
2281 CUPS_LLCAST bytes
));
2284 * Loop until all bytes are written...
2291 if (fp
->mode
== 's')
2292 count
= (ssize_t
)send(fp
->fd
, buf
, (unsigned)bytes
, 0);
2294 count
= (ssize_t
)write(fp
->fd
, buf
, (unsigned)bytes
);
2296 if (fp
->mode
== 's')
2297 count
= send(fp
->fd
, buf
, bytes
, 0);
2299 count
= write(fp
->fd
, buf
, bytes
);
2302 DEBUG_printf(("cups_write: count=" CUPS_LLFMT
"\n", CUPS_LLCAST count
));
2307 * Writes can be interrupted by signals and unavailable resources...
2310 if (errno
== EAGAIN
|| errno
== EINTR
)
2317 * Update the counts for the last write call...
2326 * Return the total number of bytes written...
2329 return ((ssize_t
)total
);
2334 * End of "$Id: file.c 7672 2008-06-18 22:03:02Z mike $".