]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/file.c
2 * "$Id: file.c 177 2006-06-21 00:20:03Z jlovell $"
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
);
1269 * 'cupsFileSeek()' - Seek in a file.
1272 off_t
/* O - New file position or -1 */
1273 cupsFileSeek(cups_file_t
*fp
, /* I - CUPS file */
1274 off_t pos
) /* I - Position in file */
1276 size_t bytes
; /* Number bytes in buffer */
1279 DEBUG_printf(("cupsFileSeek(fp=%p, pos=" CUPS_LLFMT
")\n", fp
, pos
));
1280 DEBUG_printf((" fp->pos=" CUPS_LLFMT
"\n", fp
->pos
));
1281 DEBUG_printf((" fp->ptr=%p, fp->end=%p\n", fp
->ptr
, fp
->end
));
1284 * Range check input...
1287 if (!fp
|| pos
< 0 || fp
->mode
!= 'r')
1291 * Handle special cases...
1295 return (cupsFileRewind(fp
));
1300 * No seeking necessary...
1313 if (!fp
->compressed
&& !fp
->ptr
)
1316 * Preload a buffer to determine whether the file is compressed...
1319 if (cups_fill(fp
) < 0)
1322 #endif /* HAVE_LIBZ */
1325 * Figure out the number of bytes in the current buffer, and then
1326 * see if we are outside of it...
1330 bytes
= fp
->end
- fp
->buf
;
1336 DEBUG_printf((" bytes=" CUPS_LLFMT
"\n", CUPS_LLCAST bytes
));
1341 * Need to seek backwards...
1344 DEBUG_puts(" SEEK BACKWARDS");
1349 inflateEnd(&fp
->stream
);
1351 lseek(fp
->fd
, 0, SEEK_SET
);
1356 while ((bytes
= cups_fill(fp
)) > 0)
1357 if (pos
>= fp
->pos
&& pos
< (fp
->pos
+ bytes
))
1363 fp
->ptr
= fp
->buf
+ pos
- fp
->pos
;
1366 #endif /* HAVE_LIBZ */
1368 fp
->pos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1372 DEBUG_printf((" lseek() returned %ld...\n", (long)fp
->pos
));
1375 else if (pos
>= (fp
->pos
+ bytes
))
1378 * Need to seek forwards...
1381 DEBUG_puts(" SEEK FORWARDS");
1386 while ((bytes
= cups_fill(fp
)) > 0)
1388 if (pos
>= fp
->pos
&& pos
< (fp
->pos
+ bytes
))
1395 fp
->ptr
= fp
->buf
+ pos
- fp
->pos
;
1398 #endif /* HAVE_LIBZ */
1400 fp
->pos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1404 DEBUG_printf((" lseek() returned " CUPS_LLFMT
"...\n", fp
->pos
));
1410 * Just reposition the current pointer, since we have the right
1414 DEBUG_puts(" SEEK INSIDE BUFFER");
1416 fp
->ptr
= fp
->buf
+ pos
- fp
->pos
;
1424 * 'cupsFileStderr()' - Return a CUPS file associated with stderr.
1428 cupsFileStderr(void)
1430 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1434 * Open file descriptor 2 as needed...
1437 if (!cg
->stdio_files
[2])
1440 * Flush any pending output on the stdio file...
1446 * Open file descriptor 2...
1449 if ((cg
->stdio_files
[2] = cupsFileOpenFd(2, "w")) != NULL
)
1450 cg
->stdio_files
[2]->is_stdio
= 1;
1453 return (cg
->stdio_files
[2]);
1458 * 'cupsFileStdin()' - Return a CUPS file associated with stdin.
1464 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1468 * Open file descriptor 0 as needed...
1471 if (!cg
->stdio_files
[0])
1474 * Open file descriptor 0...
1477 if ((cg
->stdio_files
[0] = cupsFileOpenFd(0, "r")) != NULL
)
1478 cg
->stdio_files
[0]->is_stdio
= 1;
1481 return (cg
->stdio_files
[0]);
1486 * 'cupsFileStdout()' - Return a CUPS file associated with stdout.
1490 cupsFileStdout(void)
1492 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1496 * Open file descriptor 1 as needed...
1499 if (!cg
->stdio_files
[1])
1502 * Flush any pending output on the stdio file...
1508 * Open file descriptor 1...
1511 if ((cg
->stdio_files
[1] = cupsFileOpenFd(1, "w")) != NULL
)
1512 cg
->stdio_files
[1]->is_stdio
= 1;
1515 return (cg
->stdio_files
[1]);
1520 * 'cupsFileTell()' - Return the current file position.
1523 off_t
/* O - File position */
1524 cupsFileTell(cups_file_t
*fp
) /* I - CUPS file */
1526 return (fp
? fp
->pos
: 0);
1531 * 'cupsFileUnlock()' - Unlock access to a file.
1534 int /* O - 0 on success, -1 on error */
1535 cupsFileUnlock(cups_file_t
*fp
) /* I - File to lock */
1541 if (!fp
|| fp
->mode
== 's')
1549 return (locking(fp
->fd
, _LK_UNLCK
, 0));
1551 return (lockf(fp
->fd
, F_ULOCK
, 0));
1557 * 'cupsFileWrite()' - Write to a file.
1560 ssize_t
/* O - Number of bytes written */
1561 cupsFileWrite(cups_file_t
*fp
, /* I - CUPS file */
1562 const char *buf
, /* I - Buffer */
1563 size_t bytes
) /* I - Number of bytes to write */
1566 * Range check input...
1569 if (!fp
|| !buf
|| bytes
< 0 || (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1576 * Write the buffer...
1579 if (fp
->mode
== 's')
1581 if (cups_write(fp
, buf
, bytes
) < 0)
1589 if ((fp
->ptr
+ bytes
) > fp
->end
)
1590 if (cupsFileFlush(fp
))
1595 if (bytes
> sizeof(fp
->buf
))
1599 return (cups_compress(fp
, buf
, bytes
));
1601 #endif /* HAVE_LIBZ */
1602 return (cups_write(fp
, buf
, bytes
));
1606 memcpy(fp
->ptr
, buf
, bytes
);
1615 * 'cups_compress()' - Compress a buffer of data...
1618 static ssize_t
/* O - Number of bytes written or -1 */
1619 cups_compress(cups_file_t
*fp
, /* I - CUPS file */
1620 const char *buf
, /* I - Buffer */
1621 size_t bytes
) /* I - Number bytes */
1623 DEBUG_printf(("cups_compress(fp=%p, buf=%p, bytes=%ld)\n", fp
, buf
,
1630 fp
->crc
= crc32(fp
->crc
, (const Bytef
*)buf
, bytes
);
1633 * Deflate the bytes...
1636 fp
->stream
.next_in
= (Bytef
*)buf
;
1637 fp
->stream
.avail_in
= bytes
;
1639 while (fp
->stream
.avail_in
> 0)
1642 * Flush the current buffer...
1645 DEBUG_printf((" avail_in=%d, avail_out=%d\n", fp
->stream
.avail_in
,
1646 fp
->stream
.avail_out
));
1648 if (fp
->stream
.avail_out
< (int)(sizeof(fp
->cbuf
) / 8))
1650 if (cups_write(fp
, (char *)fp
->cbuf
, fp
->stream
.next_out
- fp
->cbuf
) < 0)
1653 fp
->stream
.next_out
= fp
->cbuf
;
1654 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
1657 deflate(&(fp
->stream
), Z_NO_FLUSH
);
1662 #endif /* HAVE_LIBZ */
1666 * 'cups_fill()' - Fill the input buffer...
1669 static ssize_t
/* O - Number of bytes or -1 */
1670 cups_fill(cups_file_t
*fp
) /* I - CUPS file */
1672 ssize_t bytes
; /* Number of bytes read */
1674 const unsigned char *ptr
, /* Pointer into buffer */
1675 *end
; /* End of buffer */
1676 #endif /* HAVE_LIBZ */
1679 DEBUG_printf(("cups_fill(fp=%p)\n", fp
));
1680 DEBUG_printf((" fp->ptr=%p, fp->end=%p, fp->buf=%p, "
1681 "fp->pos=" CUPS_LLFMT
", fp->eof=%d\n",
1682 fp
->ptr
, fp
->end
, fp
->buf
, fp
->pos
, fp
->eof
));
1685 * Update the "pos" element as needed...
1688 if (fp
->ptr
&& fp
->end
)
1689 fp
->pos
+= fp
->end
- fp
->buf
;
1692 DEBUG_printf((" fp->compressed=%d\n", fp
->compressed
));
1694 while (!fp
->ptr
|| fp
->compressed
)
1697 * Check to see if we have read any data yet; if not, see if we have a
1698 * compressed file...
1704 * Reset the file position in case we are seeking...
1710 * Read the first bytes in the file to determine if we have a gzip'd
1714 if ((bytes
= cups_read(fp
, (char *)fp
->buf
, sizeof(fp
->buf
))) < 0)
1717 * Can't read from file!
1720 DEBUG_printf((" cups_read() returned " CUPS_LLFMT
"!\n",
1721 CUPS_LLCAST bytes
));
1726 if (bytes
< 10 || fp
->buf
[0] != 0x1f ||
1727 (fp
->buf
[1] & 255) != 0x8b ||
1728 fp
->buf
[2] != 8 || (fp
->buf
[3] & 0xe0) != 0)
1731 * Not a gzip'd file!
1735 fp
->end
= fp
->buf
+ bytes
;
1737 DEBUG_printf((" returning " CUPS_LLFMT
"!\n", CUPS_LLCAST bytes
));
1743 * Parse header junk: extra data, original name, and comment...
1746 ptr
= (unsigned char *)fp
->buf
+ 10;
1747 end
= (unsigned char *)fp
->buf
+ bytes
;
1749 if (fp
->buf
[3] & 0x04)
1752 * Skip extra data...
1755 if ((ptr
+ 2) > end
)
1758 * Can't read from file!
1764 bytes
= ((unsigned char)ptr
[1] << 8) | (unsigned char)ptr
[0];
1770 * Can't read from file!
1777 if (fp
->buf
[3] & 0x08)
1780 * Skip original name data...
1783 while (ptr
< end
&& *ptr
)
1791 * Can't read from file!
1798 if (fp
->buf
[3] & 0x10)
1801 * Skip comment data...
1804 while (ptr
< end
&& *ptr
)
1812 * Can't read from file!
1819 if (fp
->buf
[3] & 0x02)
1822 * Skip header CRC data...
1830 * Can't read from file!
1838 * Copy the flate-compressed data to the compression buffer...
1841 if ((bytes
= end
- ptr
) > 0)
1842 memcpy(fp
->cbuf
, ptr
, bytes
);
1845 * Setup the decompressor data...
1848 fp
->stream
.zalloc
= (alloc_func
)0;
1849 fp
->stream
.zfree
= (free_func
)0;
1850 fp
->stream
.opaque
= (voidpf
)0;
1851 fp
->stream
.next_in
= (Bytef
*)fp
->cbuf
;
1852 fp
->stream
.next_out
= NULL
;
1853 fp
->stream
.avail_in
= bytes
;
1854 fp
->stream
.avail_out
= 0;
1855 fp
->crc
= crc32(0L, Z_NULL
, 0);
1857 if (inflateInit2(&(fp
->stream
), -15) != Z_OK
)
1866 * If we have reached end-of-file, return immediately...
1873 * Fill the decompression buffer as needed...
1876 if (fp
->stream
.avail_in
== 0)
1878 if ((bytes
= cups_read(fp
, (char *)fp
->cbuf
, sizeof(fp
->cbuf
))) <= 0)
1881 fp
->stream
.next_in
= fp
->cbuf
;
1882 fp
->stream
.avail_in
= bytes
;
1886 * Decompress data from the buffer...
1889 fp
->stream
.next_out
= (Bytef
*)fp
->buf
;
1890 fp
->stream
.avail_out
= sizeof(fp
->buf
);
1892 if (inflate(&(fp
->stream
), Z_NO_FLUSH
) == Z_STREAM_END
)
1895 * Read the CRC and length...
1898 unsigned char trailer
[8]; /* Trailer bytes */
1899 uLong tcrc
; /* Trailer CRC */
1902 if (read(fp
->fd
, trailer
, sizeof(trailer
)) < sizeof(trailer
))
1905 * Can't get it, so mark end-of-file...
1912 tcrc
= (((((trailer
[3] << 8) | trailer
[2]) << 8) | trailer
[1]) << 8) |
1915 if (tcrc
!= fp
->crc
)
1918 * Bad CRC, mark end-of-file...
1927 * Otherwise, reset the compressed flag so that we re-read the
1935 bytes
= sizeof(fp
->buf
) - fp
->stream
.avail_out
;
1938 * Return the decompressed data...
1942 fp
->end
= fp
->buf
+ bytes
;
1948 #endif /* HAVE_LIBZ */
1951 * Read a buffer's full of data...
1954 if ((bytes
= cups_read(fp
, fp
->buf
, sizeof(fp
->buf
))) <= 0)
1957 * Can't read from file!
1968 * Return the bytes we read...
1973 fp
->end
= fp
->buf
+ bytes
;
1980 * 'cups_read()' - Read from a file descriptor.
1983 static ssize_t
/* O - Number of bytes read or -1 */
1984 cups_read(cups_file_t
*fp
, /* I - CUPS file */
1985 char *buf
, /* I - Buffer */
1986 size_t bytes
) /* I - Number bytes */
1988 ssize_t total
; /* Total bytes read */
1992 * Loop until we read at least 0 bytes...
1997 if (fp
->mode
== 's')
1998 total
= recv(fp
->fd
, buf
, bytes
, 0);
2000 total
= read(fp
->fd
, buf
, bytes
);
2006 * Reads can be interrupted by signals and unavailable resources...
2009 if (errno
== EAGAIN
|| errno
== EINTR
)
2016 * Return the total number of bytes read...
2024 * 'cups_write()' - Write to a file descriptor.
2027 static ssize_t
/* O - Number of bytes written or -1 */
2028 cups_write(cups_file_t
*fp
, /* I - CUPS file */
2029 const char *buf
, /* I - Buffer */
2030 size_t bytes
) /* I - Number bytes */
2032 size_t total
, /* Total bytes written */
2033 count
; /* Count this time */
2036 DEBUG_printf(("cups_write(fp=%p, buf=%p, bytes=%ld)\n", fp
, buf
,
2040 * Loop until all bytes are written...
2046 if (fp
->mode
== 's')
2047 count
= send(fp
->fd
, buf
, bytes
, 0);
2049 count
= write(fp
->fd
, buf
, bytes
);
2054 * Writes can be interrupted by signals and unavailable resources...
2057 if (errno
== EAGAIN
|| errno
== EINTR
)
2063 DEBUG_printf((" count=%ld\n", (long)count
));
2066 * Update the counts for the last write call...
2075 * Return the total number of bytes written...
2083 * End of "$Id: file.c 177 2006-06-21 00:20:03Z jlovell $".