]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/file.c
2 * "$Id: file.c 5812 2006-08-09 22:42:51Z 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 1997-2006 by Easy Software Products, all rights reserved.
13 * These coded instructions, statements, and computer programs are the
14 * property of Easy Software Products and are protected by Federal
15 * copyright law. Distribution and use rights are outlined in the file
16 * "LICENSE.txt" which should have been included with this file. If this
17 * file is missing or damaged please contact Easy Software Products
20 * Attn: CUPS Licensing Information
21 * Easy Software Products
22 * 44141 Airport View Drive, Suite 204
23 * Hollywood, Maryland 20636 USA
25 * Voice: (301) 373-9600
26 * EMail: cups-info@cups.org
27 * WWW: http://www.cups.org
31 * cupsFileClose() - Close a CUPS file.
32 * cupsFileCompression() - Return whether a file is compressed.
33 * cupsFileEOF() - Return the end-of-file status.
34 * cupsFileFind() - Find a file using the specified path.
35 * cupsFileFlush() - Flush pending output.
36 * cupsFileGetChar() - Get a single character from a file.
37 * cupsFileGetConf() - Get a line from a configuration file...
38 * cupsFileGetLine() - Get a CR and/or LF-terminated line that may
39 * contain binary data.
40 * cupsFileGets() - Get a CR and/or LF-terminated line.
41 * cupsFileLock() - Temporarily lock access to a file.
42 * cupsFileNumber() - Return the file descriptor associated with a CUPS
44 * cupsFileOpen() - Open a CUPS file.
45 * cupsFileOpenFd() - Open a CUPS file using a file descriptor.
46 * cupsFilePeekChar() - Peek at the next character from a file.
47 * cupsFilePrintf() - Write a formatted string.
48 * cupsFilePutChar() - Write a character.
49 * cupsFilePuts() - Write a string.
50 * cupsFileRead() - Read from a file.
51 * cupsFileRewind() - Rewind a file.
52 * cupsFileSeek() - Seek in a file.
53 * cupsFileStderr() - Return a CUPS file associated with stderr.
54 * cupsFileStdin() - Return a CUPS file associated with stdin.
55 * cupsFileStdout() - Return a CUPS file associated with stdout.
56 * cupsFileTell() - Return the current file position.
57 * cupsFileUnlock() - Unlock access to a file.
58 * cupsFileWrite() - Write to a file.
59 * cups_compress() - Compress a buffer of data...
60 * cups_fill() - Fill the input buffer...
61 * cups_read() - Read from a file descriptor.
62 * cups_write() - Write to a file descriptor.
66 * Include necessary headers...
73 #include <sys/types.h>
75 #include "http-private.h"
81 #endif /* HAVE_LIBZ */
84 # include <sys/locking.h>
89 * Some operating systems support large files via open flag O_LARGEFILE...
93 # define O_LARGEFILE 0
94 #endif /* !O_LARGEFILE */
98 * Types and structures...
101 struct _cups_file_s
/**** CUPS file structure... ****/
104 int fd
; /* File descriptor */
105 char mode
, /* Mode ('r' or 'w') */
106 compressed
, /* Compression used? */
107 is_stdio
, /* stdin/out/err? */
108 eof
, /* End of file? */
109 buf
[4096], /* Buffer */
110 *ptr
, /* Pointer into buffer */
111 *end
; /* End of buffer data */
112 off_t pos
; /* File position for start of buffer */
115 z_stream stream
; /* (De)compression stream */
116 Bytef cbuf
[4096]; /* (De)compression buffer */
117 uLong crc
; /* (De)compression CRC */
118 #endif /* HAVE_LIBZ */
127 static ssize_t
cups_compress(cups_file_t
*fp
, const char *buf
, size_t bytes
);
128 #endif /* HAVE_LIBZ */
129 static ssize_t
cups_fill(cups_file_t
*fp
);
130 static ssize_t
cups_read(cups_file_t
*fp
, char *buf
, size_t bytes
);
131 static ssize_t
cups_write(cups_file_t
*fp
, const char *buf
, size_t bytes
);
135 * 'cupsFileClose()' - Close a CUPS file.
138 int /* O - 0 on success, -1 on error */
139 cupsFileClose(cups_file_t
*fp
) /* I - CUPS file */
141 int fd
; /* File descriptor */
142 char mode
; /* Open mode */
143 int status
; /* Return status */
144 int is_stdio
; /* Is a stdio file? */
147 DEBUG_printf(("cupsFileClose(fp=%p)\n", fp
));
157 * Flush pending write data...
161 status
= cupsFileFlush(fp
);
166 if (fp
->compressed
&& status
>= 0)
171 * Free decompression data...
174 inflateEnd(&fp
->stream
);
179 * Flush any remaining compressed data...
182 unsigned char trailer
[8]; /* Trailer CRC and length */
183 int done
; /* Done writing... */
186 fp
->stream
.avail_in
= 0;
190 if (fp
->stream
.next_out
> fp
->cbuf
)
192 if (cups_write(fp
, (char *)fp
->cbuf
,
193 fp
->stream
.next_out
- fp
->cbuf
) < 0)
196 fp
->stream
.next_out
= fp
->cbuf
;
197 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
200 if (done
|| status
< 0)
203 done
= deflate(&fp
->stream
, Z_FINISH
) == Z_STREAM_END
&&
204 fp
->stream
.next_out
== fp
->cbuf
;
208 * Write the CRC and length...
211 trailer
[0] = fp
->crc
;
212 trailer
[1] = fp
->crc
>> 8;
213 trailer
[2] = fp
->crc
>> 16;
214 trailer
[3] = fp
->crc
>> 24;
215 trailer
[4] = fp
->pos
;
216 trailer
[5] = fp
->pos
>> 8;
217 trailer
[6] = fp
->pos
>> 16;
218 trailer
[7] = fp
->pos
>> 24;
220 if (cups_write(fp
, (char *)trailer
, 8) < 0)
224 * Free all memory used by the compression stream...
227 deflateEnd(&(fp
->stream
));
230 #endif /* HAVE_LIBZ */
233 * Save the file descriptor we used and free memory...
238 is_stdio
= fp
->is_stdio
;
243 * Close the file, returning the close status...
248 if (closesocket(fd
) < 0)
262 * 'cupsFileCompression()' - Return whether a file is compressed.
265 int /* O - CUPS_FILE_NONE or CUPS_FILE_GZIP */
266 cupsFileCompression(cups_file_t
*fp
) /* I - CUPS file */
268 return (fp
? fp
->compressed
: CUPS_FILE_NONE
);
273 * 'cupsFileEOF()' - Return the end-of-file status.
276 int /* O - 1 on EOF, 0 otherwise */
277 cupsFileEOF(cups_file_t
*fp
) /* I - CUPS file */
279 return (fp
? fp
->eof
: 1);
284 * 'cupsFileFind()' - Find a file using the specified path.
286 * This function allows the paths in the path string to be separated by
287 * colons (UNIX standard) or semicolons (Windows standard) and stores the
288 * result in the buffer supplied. If the file cannot be found in any of
289 * the supplied paths, NULL is returned. A NULL path only matches the
293 const char * /* O - Full path to file or NULL */
294 cupsFileFind(const char *filename
, /* I - File to find */
295 const char *path
, /* I - Colon/semicolon-separated path */
296 int executable
, /* I - 1 = executable files, 0 = any file/dir */
297 char *buffer
, /* I - Filename buffer */
298 int bufsize
) /* I - Size of filename buffer */
300 char *bufptr
, /* Current position in buffer */
301 *bufend
; /* End of buffer */
305 * Range check input...
308 if (!filename
|| !buffer
|| bufsize
< 2)
314 * No path, so check current directory...
317 if (!access(filename
, 0))
319 strlcpy(buffer
, filename
, bufsize
);
327 * Now check each path and return the first match...
330 bufend
= buffer
+ bufsize
- 1;
335 if (*path
== ';' || *path
== ':')
337 if (bufptr
> buffer
&& bufptr
[-1] != '/' && bufptr
< bufend
)
340 strlcpy(bufptr
, filename
, bufend
- bufptr
);
343 if (!access(buffer
, 0))
345 if (!access(buffer
, executable
? X_OK
: 0))
351 else if (bufptr
< bufend
)
358 * Check the last path...
361 if (bufptr
> buffer
&& bufptr
[-1] != '/' && bufptr
< bufend
)
364 strlcpy(bufptr
, filename
, bufend
- bufptr
);
366 if (!access(buffer
, 0))
374 * 'cupsFileFlush()' - Flush pending output.
377 int /* O - 0 on success, -1 on error */
378 cupsFileFlush(cups_file_t
*fp
) /* I - CUPS file */
380 size_t bytes
; /* Bytes to write */
383 DEBUG_printf(("cupsFileFlush(fp=%p)\n", fp
));
386 * Range check input...
389 if (!fp
|| fp
->mode
!= 'w')
391 DEBUG_puts(" Attempt to flush a read-only file...");
395 bytes
= fp
->ptr
- fp
->buf
;
397 DEBUG_printf((" Flushing %ld bytes...\n", (long)bytes
));
403 bytes
= cups_compress(fp
, fp
->buf
, bytes
);
405 #endif /* HAVE_LIBZ */
406 bytes
= cups_write(fp
, fp
->buf
, bytes
);
419 * 'cupsFileGetChar()' - Get a single character from a file.
422 int /* O - Character or -1 on EOF */
423 cupsFileGetChar(cups_file_t
*fp
) /* I - CUPS file */
426 * Range check input...
429 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
433 * If the input buffer is empty, try to read more data...
436 if (fp
->ptr
>= fp
->end
)
437 if (cups_fill(fp
) < 0)
441 * Return the next character in the buffer...
444 return (*(fp
->ptr
)++ & 255);
449 * 'cupsFileGetConf()' - Get a line from a configuration file...
452 char * /* O - Line read or NULL on eof/error */
453 cupsFileGetConf(cups_file_t
*fp
, /* I - CUPS file */
454 char *buf
, /* O - String buffer */
455 size_t buflen
, /* I - Size of string buffer */
456 char **value
, /* O - Pointer to value */
457 int *linenum
) /* IO - Current line number */
459 char *ptr
; /* Pointer into line */
463 * Range check input...
466 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') ||
467 !buf
|| buflen
< 2 || !value
)
476 * Read the next non-comment line...
481 while (cupsFileGets(fp
, buf
, buflen
))
486 * Strip any comments...
489 if ((ptr
= strchr(buf
, '#')) != NULL
)
493 if (!isspace(ptr
[-1] & 255))
503 * Strip leading whitespace...
506 for (ptr
= buf
; isspace(*ptr
& 255); ptr
++);
509 _cups_strcpy(buf
, ptr
);
512 * See if there is anything left...
518 * Yes, grab any value and return...
521 for (ptr
= buf
; *ptr
; ptr
++)
522 if (isspace(*ptr
& 255))
528 * Have a value, skip any other spaces...
531 while (isspace(*ptr
& 255))
538 * Strip trailing whitespace and > for lines that begin with <...
541 ptr
+= strlen(ptr
) - 1;
543 if (buf
[0] == '<' && *ptr
== '>')
545 else if (buf
[0] == '<' && *ptr
!= '>')
555 while (ptr
> *value
&& isspace(*ptr
& 255))
572 * 'cupsFileGetLine()' - Get a CR and/or LF-terminated line that may
573 * contain binary data.
575 * This function differs from cupsFileGets() in that the trailing CR and LF
576 * are preserved, as is any binary data on the line. The buffer is nul-
577 * terminated, however you should use the returned length to determine
578 * the number of bytes on the line.
581 size_t /* O - Number of bytes on line or 0 on EOF */
582 cupsFileGetLine(cups_file_t
*fp
, /* I - File to read from */
583 char *buf
, /* I - Buffer */
584 size_t buflen
) /* I - Size of buffer */
586 int ch
; /* Character from file */
587 char *ptr
, /* Current position in line buffer */
588 *end
; /* End of line buffer */
592 * Range check input...
595 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') || !buf
|| buflen
< 3)
599 * Now loop until we have a valid line...
602 for (ptr
= buf
, end
= buf
+ buflen
- 2; ptr
< end
;)
604 if (fp
->ptr
>= fp
->end
)
605 if (cups_fill(fp
) <= 0)
608 *ptr
++ = ch
= *(fp
->ptr
)++;
616 if (fp
->ptr
>= fp
->end
)
617 if (cups_fill(fp
) <= 0)
620 if (*(fp
->ptr
) == '\n')
621 *ptr
++ = *(fp
->ptr
)++;
628 * Line feed ends a line...
642 * 'cupsFileGets()' - Get a CR and/or LF-terminated line.
645 char * /* O - Line read or NULL on eof/error */
646 cupsFileGets(cups_file_t
*fp
, /* I - CUPS file */
647 char *buf
, /* O - String buffer */
648 size_t buflen
) /* I - Size of string buffer */
650 int ch
; /* Character from file */
651 char *ptr
, /* Current position in line buffer */
652 *end
; /* End of line buffer */
656 * Range check input...
659 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') || !buf
|| buflen
< 2)
663 * Now loop until we have a valid line...
666 for (ptr
= buf
, end
= buf
+ buflen
- 1; ptr
< end
;)
668 if (fp
->ptr
>= fp
->end
)
669 if (cups_fill(fp
) <= 0)
685 if (fp
->ptr
>= fp
->end
)
686 if (cups_fill(fp
) <= 0)
689 if (*(fp
->ptr
) == '\n')
697 * Line feed ends a line...
713 * 'cupsFileLock()' - Temporarily lock access to a file.
716 int /* O - 0 on success, -1 on error */
717 cupsFileLock(cups_file_t
*fp
, /* I - File to lock */
718 int block
) /* I - 1 to wait for the lock, 0 to fail right away */
724 if (!fp
|| fp
->mode
== 's')
732 return (locking(fp
->fd
, block
? _LK_LOCK
: _LK_NBLCK
, 0));
734 return (lockf(fp
->fd
, block
? F_LOCK
: F_TLOCK
, 0));
740 * 'cupsFileNumber()' - Return the file descriptor associated with a CUPS file.
743 int /* O - File descriptor */
744 cupsFileNumber(cups_file_t
*fp
) /* I - CUPS file */
751 * 'cupsFileOpen()' - Open a CUPS file.
754 cups_file_t
* /* O - CUPS file or NULL */
755 cupsFileOpen(const char *filename
, /* I - Name of file */
756 const char *mode
) /* I - Open mode */
758 cups_file_t
*fp
; /* New CUPS file */
759 int fd
; /* File descriptor */
760 char hostname
[1024], /* Hostname */
761 *portname
; /* Port "name" (number or service) */
762 http_addrlist_t
*addrlist
; /* Host address list */
765 DEBUG_printf(("cupsFileOpen(filename=\"%s\", mode=\"%s\")\n", filename
,
769 * Range check input...
772 if (!filename
|| !mode
||
773 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's'))
782 case 'a' : /* Append file */
783 fd
= open(filename
, O_RDWR
| O_CREAT
| O_APPEND
| O_LARGEFILE
, 0666);
786 case 'r' : /* Read file */
787 fd
= open(filename
, O_RDONLY
| O_LARGEFILE
, 0);
790 case 'w' : /* Write file */
791 fd
= open(filename
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_LARGEFILE
, 0666);
794 case 's' : /* Read/write socket */
795 strlcpy(hostname
, filename
, sizeof(hostname
));
796 if ((portname
= strrchr(hostname
, ':')) != NULL
)
802 * Lookup the hostname and service...
805 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
809 * Connect to the server...
812 if (!httpAddrConnect(addrlist
, &fd
))
814 httpAddrFreeList(addrlist
);
818 httpAddrFreeList(addrlist
);
821 default : /* Remove bogus compiler warning... */
829 * Create the CUPS file structure...
832 if ((fp
= cupsFileOpenFd(fd
, mode
)) == NULL
)
848 * 'cupsFileOpenFd()' - Open a CUPS file using a file descriptor.
851 cups_file_t
* /* O - CUPS file or NULL */
852 cupsFileOpenFd(int fd
, /* I - File descriptor */
853 const char *mode
) /* I - Open mode */
855 cups_file_t
*fp
; /* New CUPS file */
858 DEBUG_printf(("cupsFileOpenFd(fd=%d, mode=\"%s\")\n", fd
, mode
));
861 * Range check input...
864 if (fd
< 0 || !mode
||
865 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's'))
872 if ((fp
= calloc(1, sizeof(cups_file_t
))) == NULL
)
887 fp
->end
= fp
->buf
+ sizeof(fp
->buf
);
890 if (mode
[1] >= '1' && mode
[1] <= '9')
893 * Open a compressed stream, so write the standard gzip file
897 unsigned char header
[10]; /* gzip file header */
898 time_t curtime
; /* Current time */
901 curtime
= time(NULL
);
904 header
[2] = Z_DEFLATED
;
907 header
[5] = curtime
>> 8;
908 header
[6] = curtime
>> 16;
909 header
[7] = curtime
>> 24;
913 cups_write(fp
, (char *)header
, 10);
916 * Initialize the compressor...
919 deflateInit2(&(fp
->stream
), mode
[1] - '0', Z_DEFLATED
, -15, 8,
922 fp
->stream
.next_out
= fp
->cbuf
;
923 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
925 fp
->crc
= crc32(0L, Z_NULL
, 0);
927 #endif /* HAVE_LIBZ */
938 default : /* Remove bogus compiler warning... */
943 * Don't pass this file to child processes...
947 fcntl(fp
->fd
, F_SETFD
, fcntl(fp
->fd
, F_GETFD
) | FD_CLOEXEC
);
955 * 'cupsFilePeekChar()' - Peek at the next character from a file.
958 int /* O - Character or -1 on EOF */
959 cupsFilePeekChar(cups_file_t
*fp
) /* I - CUPS file */
962 * Range check input...
965 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
969 * If the input buffer is empty, try to read more data...
972 if (fp
->ptr
>= fp
->end
)
973 if (cups_fill(fp
) < 0)
977 * Return the next character in the buffer...
980 return (*(fp
->ptr
) & 255);
985 * 'cupsFilePrintf()' - Write a formatted string.
988 int /* O - Number of bytes written or -1 */
989 cupsFilePrintf(cups_file_t
*fp
, /* I - CUPS file */
990 const char *format
, /* I - Printf-style format string */
991 ...) /* I - Additional args as necessary */
993 va_list ap
; /* Argument list */
994 size_t bytes
; /* Formatted size */
995 char buf
[8192]; /* Formatted text */
998 DEBUG_printf(("cupsFilePrintf(fp=%p, format=\"%s\", ...)\n", fp
, format
));
1000 if (!fp
|| !format
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1003 va_start(ap
, format
);
1004 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
1007 if (bytes
>= sizeof(buf
))
1010 if (fp
->mode
== 's')
1011 return (cups_write(fp
, buf
, bytes
));
1013 if ((fp
->ptr
+ bytes
) > fp
->end
)
1014 if (cupsFileFlush(fp
))
1019 if (bytes
> sizeof(fp
->buf
))
1023 return (cups_compress(fp
, buf
, bytes
));
1025 #endif /* HAVE_LIBZ */
1026 return (cups_write(fp
, buf
, bytes
));
1030 memcpy(fp
->ptr
, buf
, bytes
);
1038 * 'cupsFilePutChar()' - Write a character.
1041 int /* O - 0 on success, -1 on error */
1042 cupsFilePutChar(cups_file_t
*fp
, /* I - CUPS file */
1043 int c
) /* I - Character to write */
1046 * Range check input...
1049 if (!fp
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1052 if (fp
->mode
== 's')
1055 * Send character immediately over socket...
1058 char ch
; /* Output character */
1063 if (send(fp
->fd
, &ch
, 1, 0) < 1)
1072 if (fp
->ptr
>= fp
->end
)
1073 if (cupsFileFlush(fp
))
1086 * 'cupsFilePuts()' - Write a string.
1089 int /* O - Number of bytes written or -1 */
1090 cupsFilePuts(cups_file_t
*fp
, /* I - CUPS file */
1091 const char *s
) /* I - String to write */
1093 size_t bytes
; /* Bytes to write */
1097 * Range check input...
1100 if (!fp
|| !s
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1104 * Write the string...
1109 if (fp
->mode
== 's')
1111 if (cups_write(fp
, s
, bytes
) < 0)
1119 if ((fp
->ptr
+ bytes
) > fp
->end
)
1120 if (cupsFileFlush(fp
))
1125 if (bytes
> sizeof(fp
->buf
))
1129 return (cups_compress(fp
, s
, bytes
));
1131 #endif /* HAVE_LIBZ */
1132 return (cups_write(fp
, s
, bytes
));
1136 memcpy(fp
->ptr
, s
, bytes
);
1144 * 'cupsFileRead()' - Read from a file.
1147 ssize_t
/* O - Number of bytes read or -1 */
1148 cupsFileRead(cups_file_t
*fp
, /* I - CUPS file */
1149 char *buf
, /* O - Buffer */
1150 size_t bytes
) /* I - Number of bytes to read */
1152 size_t total
, /* Total bytes read */
1153 count
; /* Bytes read */
1156 DEBUG_printf(("cupsFileRead(fp=%p, buf=%p, bytes=%ld)\n", fp
, buf
,
1160 * Range check input...
1163 if (!fp
|| !buf
|| bytes
< 0 || (fp
->mode
!= 'r' && fp
->mode
!= 's'))
1170 * Loop until all bytes are read...
1176 if (fp
->ptr
>= fp
->end
)
1177 if (cups_fill(fp
) <= 0)
1179 DEBUG_printf((" cups_fill() returned -1, total=%d\n", total
));
1187 count
= fp
->end
- fp
->ptr
;
1191 memcpy(buf
, fp
->ptr
, count
);
1195 * Update the counts for the last read...
1204 * Return the total number of bytes read...
1207 DEBUG_printf((" total=%d\n", total
));
1214 * 'cupsFileRewind()' - Rewind a file.
1217 off_t
/* O - New file position or -1 */
1218 cupsFileRewind(cups_file_t
*fp
) /* I - CUPS file */
1221 * Range check input...
1224 if (!fp
|| fp
->mode
!= 'r')
1228 * Handle special cases...
1234 * No seeking necessary...
1247 * Otherwise, seek in the file and cleanup any compression buffers...
1253 inflateEnd(&fp
->stream
);
1256 #endif /* HAVE_LIBZ */
1258 lseek(fp
->fd
, 0, SEEK_SET
);
1270 * 'cupsFileSeek()' - Seek in a file.
1273 off_t
/* O - New file position or -1 */
1274 cupsFileSeek(cups_file_t
*fp
, /* I - CUPS file */
1275 off_t pos
) /* I - Position in file */
1277 size_t bytes
; /* Number bytes in buffer */
1280 DEBUG_printf(("cupsFileSeek(fp=%p, pos=" CUPS_LLFMT
")\n", fp
, pos
));
1281 DEBUG_printf((" fp->pos=" CUPS_LLFMT
"\n", fp
->pos
));
1282 DEBUG_printf((" fp->ptr=%p, fp->end=%p\n", fp
->ptr
, fp
->end
));
1285 * Range check input...
1288 if (!fp
|| pos
< 0 || fp
->mode
!= 'r')
1292 * Handle special cases...
1296 return (cupsFileRewind(fp
));
1301 * No seeking necessary...
1314 if (!fp
->compressed
&& !fp
->ptr
)
1317 * Preload a buffer to determine whether the file is compressed...
1320 if (cups_fill(fp
) < 0)
1323 #endif /* HAVE_LIBZ */
1326 * Figure out the number of bytes in the current buffer, and then
1327 * see if we are outside of it...
1331 bytes
= fp
->end
- fp
->buf
;
1337 DEBUG_printf((" bytes=" CUPS_LLFMT
"\n", CUPS_LLCAST bytes
));
1342 * Need to seek backwards...
1345 DEBUG_puts(" SEEK BACKWARDS");
1350 inflateEnd(&fp
->stream
);
1352 lseek(fp
->fd
, 0, SEEK_SET
);
1357 while ((bytes
= cups_fill(fp
)) > 0)
1358 if (pos
>= fp
->pos
&& pos
< (fp
->pos
+ bytes
))
1364 fp
->ptr
= fp
->buf
+ pos
- fp
->pos
;
1367 #endif /* HAVE_LIBZ */
1369 fp
->pos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1373 DEBUG_printf((" lseek() returned %ld...\n", (long)fp
->pos
));
1376 else if (pos
>= (fp
->pos
+ bytes
))
1379 * Need to seek forwards...
1382 DEBUG_puts(" SEEK FORWARDS");
1387 while ((bytes
= cups_fill(fp
)) > 0)
1389 if (pos
>= fp
->pos
&& pos
< (fp
->pos
+ bytes
))
1396 fp
->ptr
= fp
->buf
+ pos
- fp
->pos
;
1399 #endif /* HAVE_LIBZ */
1401 fp
->pos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1405 DEBUG_printf((" lseek() returned " CUPS_LLFMT
"...\n", fp
->pos
));
1411 * Just reposition the current pointer, since we have the right
1415 DEBUG_puts(" SEEK INSIDE BUFFER");
1417 fp
->ptr
= fp
->buf
+ pos
- fp
->pos
;
1425 * 'cupsFileStderr()' - Return a CUPS file associated with stderr.
1429 cupsFileStderr(void)
1431 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1435 * Open file descriptor 2 as needed...
1438 if (!cg
->stdio_files
[2])
1441 * Flush any pending output on the stdio file...
1447 * Open file descriptor 2...
1450 if ((cg
->stdio_files
[2] = cupsFileOpenFd(2, "w")) != NULL
)
1451 cg
->stdio_files
[2]->is_stdio
= 1;
1454 return (cg
->stdio_files
[2]);
1459 * 'cupsFileStdin()' - Return a CUPS file associated with stdin.
1465 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1469 * Open file descriptor 0 as needed...
1472 if (!cg
->stdio_files
[0])
1475 * Open file descriptor 0...
1478 if ((cg
->stdio_files
[0] = cupsFileOpenFd(0, "r")) != NULL
)
1479 cg
->stdio_files
[0]->is_stdio
= 1;
1482 return (cg
->stdio_files
[0]);
1487 * 'cupsFileStdout()' - Return a CUPS file associated with stdout.
1491 cupsFileStdout(void)
1493 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1497 * Open file descriptor 1 as needed...
1500 if (!cg
->stdio_files
[1])
1503 * Flush any pending output on the stdio file...
1509 * Open file descriptor 1...
1512 if ((cg
->stdio_files
[1] = cupsFileOpenFd(1, "w")) != NULL
)
1513 cg
->stdio_files
[1]->is_stdio
= 1;
1516 return (cg
->stdio_files
[1]);
1521 * 'cupsFileTell()' - Return the current file position.
1524 off_t
/* O - File position */
1525 cupsFileTell(cups_file_t
*fp
) /* I - CUPS file */
1527 return (fp
? fp
->pos
: 0);
1532 * 'cupsFileUnlock()' - Unlock access to a file.
1535 int /* O - 0 on success, -1 on error */
1536 cupsFileUnlock(cups_file_t
*fp
) /* I - File to lock */
1542 if (!fp
|| fp
->mode
== 's')
1550 return (locking(fp
->fd
, _LK_UNLCK
, 0));
1552 return (lockf(fp
->fd
, F_ULOCK
, 0));
1558 * 'cupsFileWrite()' - Write to a file.
1561 ssize_t
/* O - Number of bytes written */
1562 cupsFileWrite(cups_file_t
*fp
, /* I - CUPS file */
1563 const char *buf
, /* I - Buffer */
1564 size_t bytes
) /* I - Number of bytes to write */
1567 * Range check input...
1570 if (!fp
|| !buf
|| bytes
< 0 || (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1577 * Write the buffer...
1580 if (fp
->mode
== 's')
1582 if (cups_write(fp
, buf
, bytes
) < 0)
1590 if ((fp
->ptr
+ bytes
) > fp
->end
)
1591 if (cupsFileFlush(fp
))
1596 if (bytes
> sizeof(fp
->buf
))
1600 return (cups_compress(fp
, buf
, bytes
));
1602 #endif /* HAVE_LIBZ */
1603 return (cups_write(fp
, buf
, bytes
));
1607 memcpy(fp
->ptr
, buf
, bytes
);
1616 * 'cups_compress()' - Compress a buffer of data...
1619 static ssize_t
/* O - Number of bytes written or -1 */
1620 cups_compress(cups_file_t
*fp
, /* I - CUPS file */
1621 const char *buf
, /* I - Buffer */
1622 size_t bytes
) /* I - Number bytes */
1624 DEBUG_printf(("cups_compress(fp=%p, buf=%p, bytes=%ld)\n", fp
, buf
,
1631 fp
->crc
= crc32(fp
->crc
, (const Bytef
*)buf
, bytes
);
1634 * Deflate the bytes...
1637 fp
->stream
.next_in
= (Bytef
*)buf
;
1638 fp
->stream
.avail_in
= bytes
;
1640 while (fp
->stream
.avail_in
> 0)
1643 * Flush the current buffer...
1646 DEBUG_printf((" avail_in=%d, avail_out=%d\n", fp
->stream
.avail_in
,
1647 fp
->stream
.avail_out
));
1649 if (fp
->stream
.avail_out
< (int)(sizeof(fp
->cbuf
) / 8))
1651 if (cups_write(fp
, (char *)fp
->cbuf
, fp
->stream
.next_out
- fp
->cbuf
) < 0)
1654 fp
->stream
.next_out
= fp
->cbuf
;
1655 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
1658 deflate(&(fp
->stream
), Z_NO_FLUSH
);
1663 #endif /* HAVE_LIBZ */
1667 * 'cups_fill()' - Fill the input buffer...
1670 static ssize_t
/* O - Number of bytes or -1 */
1671 cups_fill(cups_file_t
*fp
) /* I - CUPS file */
1673 ssize_t bytes
; /* Number of bytes read */
1675 const unsigned char *ptr
, /* Pointer into buffer */
1676 *end
; /* End of buffer */
1677 #endif /* HAVE_LIBZ */
1680 DEBUG_printf(("cups_fill(fp=%p)\n", fp
));
1681 DEBUG_printf((" fp->ptr=%p, fp->end=%p, fp->buf=%p, "
1682 "fp->pos=" CUPS_LLFMT
", fp->eof=%d\n",
1683 fp
->ptr
, fp
->end
, fp
->buf
, fp
->pos
, fp
->eof
));
1686 * Update the "pos" element as needed...
1689 if (fp
->ptr
&& fp
->end
)
1690 fp
->pos
+= fp
->end
- fp
->buf
;
1693 DEBUG_printf((" fp->compressed=%d\n", fp
->compressed
));
1695 while (!fp
->ptr
|| fp
->compressed
)
1698 * Check to see if we have read any data yet; if not, see if we have a
1699 * compressed file...
1705 * Reset the file position in case we are seeking...
1711 * Read the first bytes in the file to determine if we have a gzip'd
1715 if ((bytes
= cups_read(fp
, (char *)fp
->buf
, sizeof(fp
->buf
))) < 0)
1718 * Can't read from file!
1721 DEBUG_printf((" cups_read() returned " CUPS_LLFMT
"!\n",
1722 CUPS_LLCAST bytes
));
1727 if (bytes
< 10 || fp
->buf
[0] != 0x1f ||
1728 (fp
->buf
[1] & 255) != 0x8b ||
1729 fp
->buf
[2] != 8 || (fp
->buf
[3] & 0xe0) != 0)
1732 * Not a gzip'd file!
1736 fp
->end
= fp
->buf
+ bytes
;
1738 DEBUG_printf((" returning " CUPS_LLFMT
"!\n", CUPS_LLCAST bytes
));
1744 * Parse header junk: extra data, original name, and comment...
1747 ptr
= (unsigned char *)fp
->buf
+ 10;
1748 end
= (unsigned char *)fp
->buf
+ bytes
;
1750 if (fp
->buf
[3] & 0x04)
1753 * Skip extra data...
1756 if ((ptr
+ 2) > end
)
1759 * Can't read from file!
1765 bytes
= ((unsigned char)ptr
[1] << 8) | (unsigned char)ptr
[0];
1771 * Can't read from file!
1778 if (fp
->buf
[3] & 0x08)
1781 * Skip original name data...
1784 while (ptr
< end
&& *ptr
)
1792 * Can't read from file!
1799 if (fp
->buf
[3] & 0x10)
1802 * Skip comment data...
1805 while (ptr
< end
&& *ptr
)
1813 * Can't read from file!
1820 if (fp
->buf
[3] & 0x02)
1823 * Skip header CRC data...
1831 * Can't read from file!
1839 * Copy the flate-compressed data to the compression buffer...
1842 if ((bytes
= end
- ptr
) > 0)
1843 memcpy(fp
->cbuf
, ptr
, bytes
);
1846 * Setup the decompressor data...
1849 fp
->stream
.zalloc
= (alloc_func
)0;
1850 fp
->stream
.zfree
= (free_func
)0;
1851 fp
->stream
.opaque
= (voidpf
)0;
1852 fp
->stream
.next_in
= (Bytef
*)fp
->cbuf
;
1853 fp
->stream
.next_out
= NULL
;
1854 fp
->stream
.avail_in
= bytes
;
1855 fp
->stream
.avail_out
= 0;
1856 fp
->crc
= crc32(0L, Z_NULL
, 0);
1858 if (inflateInit2(&(fp
->stream
), -15) != Z_OK
)
1867 * If we have reached end-of-file, return immediately...
1874 * Fill the decompression buffer as needed...
1877 if (fp
->stream
.avail_in
== 0)
1879 if ((bytes
= cups_read(fp
, (char *)fp
->cbuf
, sizeof(fp
->cbuf
))) <= 0)
1882 fp
->stream
.next_in
= fp
->cbuf
;
1883 fp
->stream
.avail_in
= bytes
;
1887 * Decompress data from the buffer...
1890 fp
->stream
.next_out
= (Bytef
*)fp
->buf
;
1891 fp
->stream
.avail_out
= sizeof(fp
->buf
);
1893 if (inflate(&(fp
->stream
), Z_NO_FLUSH
) == Z_STREAM_END
)
1896 * Read the CRC and length...
1899 unsigned char trailer
[8]; /* Trailer bytes */
1900 uLong tcrc
; /* Trailer CRC */
1903 if (read(fp
->fd
, trailer
, sizeof(trailer
)) < sizeof(trailer
))
1906 * Can't get it, so mark end-of-file...
1913 tcrc
= (((((trailer
[3] << 8) | trailer
[2]) << 8) | trailer
[1]) << 8) |
1916 if (tcrc
!= fp
->crc
)
1919 * Bad CRC, mark end-of-file...
1928 * Otherwise, reset the compressed flag so that we re-read the
1936 bytes
= sizeof(fp
->buf
) - fp
->stream
.avail_out
;
1939 * Return the decompressed data...
1943 fp
->end
= fp
->buf
+ bytes
;
1949 #endif /* HAVE_LIBZ */
1952 * Read a buffer's full of data...
1955 if ((bytes
= cups_read(fp
, fp
->buf
, sizeof(fp
->buf
))) <= 0)
1958 * Can't read from file!
1969 * Return the bytes we read...
1974 fp
->end
= fp
->buf
+ bytes
;
1981 * 'cups_read()' - Read from a file descriptor.
1984 static ssize_t
/* O - Number of bytes read or -1 */
1985 cups_read(cups_file_t
*fp
, /* I - CUPS file */
1986 char *buf
, /* I - Buffer */
1987 size_t bytes
) /* I - Number bytes */
1989 ssize_t total
; /* Total bytes read */
1993 * Loop until we read at least 0 bytes...
1998 if (fp
->mode
== 's')
1999 total
= recv(fp
->fd
, buf
, bytes
, 0);
2001 total
= read(fp
->fd
, buf
, bytes
);
2007 * Reads can be interrupted by signals and unavailable resources...
2010 if (errno
== EAGAIN
|| errno
== EINTR
)
2017 * Return the total number of bytes read...
2025 * 'cups_write()' - Write to a file descriptor.
2028 static ssize_t
/* O - Number of bytes written or -1 */
2029 cups_write(cups_file_t
*fp
, /* I - CUPS file */
2030 const char *buf
, /* I - Buffer */
2031 size_t bytes
) /* I - Number bytes */
2033 size_t total
, /* Total bytes written */
2034 count
; /* Count this time */
2037 DEBUG_printf(("cups_write(fp=%p, buf=%p, bytes=%ld)\n", fp
, buf
,
2041 * Loop until all bytes are written...
2047 if (fp
->mode
== 's')
2048 count
= send(fp
->fd
, buf
, bytes
, 0);
2050 count
= write(fp
->fd
, buf
, bytes
);
2055 * Writes can be interrupted by signals and unavailable resources...
2058 if (errno
== EAGAIN
|| errno
== EINTR
)
2064 DEBUG_printf((" count=%ld\n", (long)count
));
2067 * Update the counts for the last write call...
2076 * Return the total number of bytes written...
2084 * End of "$Id: file.c 5812 2006-08-09 22:42:51Z mike $".