]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/file.c
2 * "$Id: file.c 6962 2007-09-17 20:35:47Z 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 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
30 * contain binary data.
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() - Rewind a file.
43 * cupsFileSeek() - Seek in a file.
44 * cupsFileStderr() - Return a CUPS file associated with stderr.
45 * cupsFileStdin() - Return a CUPS file associated with stdin.
46 * cupsFileStdout() - Return a CUPS file associated with stdout.
47 * cupsFileTell() - Return the current file position.
48 * cupsFileUnlock() - Unlock access to a file.
49 * cupsFileWrite() - Write to a file.
50 * cups_compress() - Compress a buffer of data...
51 * cups_fill() - Fill the input buffer...
52 * cups_read() - Read from a file descriptor.
53 * cups_write() - Write to a file descriptor.
57 * Include necessary headers...
64 #include <sys/types.h>
66 #include "http-private.h"
72 #endif /* HAVE_LIBZ */
75 # include <sys/locking.h>
80 * Some operating systems support large files via open flag O_LARGEFILE...
84 # define O_LARGEFILE 0
85 #endif /* !O_LARGEFILE */
89 * Some operating systems don't define O_BINARY, which is used by Microsoft
90 * and IBM to flag binary files...
95 #endif /* !O_BINARY */
99 * Types and structures...
102 struct _cups_file_s
/**** CUPS file structure... ****/
105 int fd
; /* File descriptor */
106 char mode
, /* Mode ('r' or 'w') */
107 compressed
, /* Compression used? */
108 is_stdio
, /* stdin/out/err? */
109 eof
, /* End of file? */
110 buf
[4096], /* Buffer */
111 *ptr
, /* Pointer into buffer */
112 *end
; /* End of buffer data */
113 off_t pos
; /* File position for start of buffer */
116 z_stream stream
; /* (De)compression stream */
117 Bytef cbuf
[4096]; /* (De)compression buffer */
118 uLong crc
; /* (De)compression CRC */
119 #endif /* HAVE_LIBZ */
128 static ssize_t
cups_compress(cups_file_t
*fp
, const char *buf
, size_t bytes
);
129 #endif /* HAVE_LIBZ */
130 static ssize_t
cups_fill(cups_file_t
*fp
);
131 static ssize_t
cups_read(cups_file_t
*fp
, char *buf
, size_t bytes
);
132 static ssize_t
cups_write(cups_file_t
*fp
, const char *buf
, size_t bytes
);
136 * 'cupsFileClose()' - Close a CUPS file.
139 int /* O - 0 on success, -1 on error */
140 cupsFileClose(cups_file_t
*fp
) /* I - CUPS file */
142 int fd
; /* File descriptor */
143 char mode
; /* Open mode */
144 int status
; /* Return status */
145 int is_stdio
; /* Is a stdio file? */
148 DEBUG_printf(("cupsFileClose(fp=%p)\n", fp
));
158 * Flush pending write data...
162 status
= cupsFileFlush(fp
);
167 if (fp
->compressed
&& status
>= 0)
172 * Free decompression data...
175 inflateEnd(&fp
->stream
);
180 * Flush any remaining compressed data...
183 unsigned char trailer
[8]; /* Trailer CRC and length */
184 int done
; /* Done writing... */
187 fp
->stream
.avail_in
= 0;
191 if (fp
->stream
.next_out
> fp
->cbuf
)
193 if (cups_write(fp
, (char *)fp
->cbuf
,
194 fp
->stream
.next_out
- fp
->cbuf
) < 0)
197 fp
->stream
.next_out
= fp
->cbuf
;
198 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
201 if (done
|| status
< 0)
204 done
= deflate(&fp
->stream
, Z_FINISH
) == Z_STREAM_END
&&
205 fp
->stream
.next_out
== fp
->cbuf
;
209 * Write the CRC and length...
212 trailer
[0] = fp
->crc
;
213 trailer
[1] = fp
->crc
>> 8;
214 trailer
[2] = fp
->crc
>> 16;
215 trailer
[3] = fp
->crc
>> 24;
216 trailer
[4] = fp
->pos
;
217 trailer
[5] = fp
->pos
>> 8;
218 trailer
[6] = fp
->pos
>> 16;
219 trailer
[7] = fp
->pos
>> 24;
221 if (cups_write(fp
, (char *)trailer
, 8) < 0)
225 * Free all memory used by the compression stream...
228 deflateEnd(&(fp
->stream
));
231 #endif /* HAVE_LIBZ */
234 * Save the file descriptor we used and free memory...
239 is_stdio
= fp
->is_stdio
;
244 * Close the file, returning the close status...
249 if (closesocket(fd
) < 0)
263 * 'cupsFileCompression()' - Return whether a file is compressed.
266 int /* O - CUPS_FILE_NONE or CUPS_FILE_GZIP */
267 cupsFileCompression(cups_file_t
*fp
) /* I - CUPS file */
269 return (fp
? fp
->compressed
: CUPS_FILE_NONE
);
274 * 'cupsFileEOF()' - Return the end-of-file status.
277 int /* O - 1 on EOF, 0 otherwise */
278 cupsFileEOF(cups_file_t
*fp
) /* I - CUPS file */
280 return (fp
? fp
->eof
: 1);
285 * 'cupsFileFind()' - Find a file using the specified path.
287 * This function allows the paths in the path string to be separated by
288 * colons (UNIX standard) or semicolons (Windows standard) and stores the
289 * result in the buffer supplied. If the file cannot be found in any of
290 * the supplied paths, NULL is returned. A NULL path only matches the
294 const char * /* O - Full path to file or NULL */
295 cupsFileFind(const char *filename
, /* I - File to find */
296 const char *path
, /* I - Colon/semicolon-separated path */
297 int executable
, /* I - 1 = executable files, 0 = any file/dir */
298 char *buffer
, /* I - Filename buffer */
299 int bufsize
) /* I - Size of filename buffer */
301 char *bufptr
, /* Current position in buffer */
302 *bufend
; /* End of buffer */
306 * Range check input...
309 if (!filename
|| !buffer
|| bufsize
< 2)
315 * No path, so check current directory...
318 if (!access(filename
, 0))
320 strlcpy(buffer
, filename
, bufsize
);
328 * Now check each path and return the first match...
331 bufend
= buffer
+ bufsize
- 1;
337 if (*path
== ';' || (*path
== ':' && ((bufptr
- buffer
) > 1 || !isalpha(buffer
[0] & 255))))
339 if (*path
== ';' || *path
== ':')
342 if (bufptr
> buffer
&& bufptr
[-1] != '/' && bufptr
< bufend
)
345 strlcpy(bufptr
, filename
, bufend
- bufptr
);
348 if (!access(buffer
, 0))
350 if (!access(buffer
, executable
? X_OK
: 0))
353 DEBUG_printf(("cupsFileFind: Returning \"%s\"\n", buffer
));
359 else if (bufptr
< bufend
)
366 * Check the last path...
369 if (bufptr
> buffer
&& bufptr
[-1] != '/' && bufptr
< bufend
)
372 strlcpy(bufptr
, filename
, bufend
- bufptr
);
374 if (!access(buffer
, 0))
376 DEBUG_printf(("cupsFileFind: Returning \"%s\"\n", buffer
));
381 DEBUG_puts("cupsFileFind: Returning NULL");
388 * 'cupsFileFlush()' - Flush pending output.
391 int /* O - 0 on success, -1 on error */
392 cupsFileFlush(cups_file_t
*fp
) /* I - CUPS file */
394 ssize_t bytes
; /* Bytes to write */
397 DEBUG_printf(("cupsFileFlush(fp=%p)\n", fp
));
400 * Range check input...
403 if (!fp
|| fp
->mode
!= 'w')
405 DEBUG_puts(" Attempt to flush a read-only file...");
409 bytes
= (ssize_t
)(fp
->ptr
- fp
->buf
);
411 DEBUG_printf((" Flushing %ld bytes...\n", (long)bytes
));
417 bytes
= cups_compress(fp
, fp
->buf
, bytes
);
419 #endif /* HAVE_LIBZ */
420 bytes
= cups_write(fp
, fp
->buf
, bytes
);
433 * 'cupsFileGetChar()' - Get a single character from a file.
436 int /* O - Character or -1 on EOF */
437 cupsFileGetChar(cups_file_t
*fp
) /* I - CUPS file */
440 * Range check input...
443 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
445 DEBUG_puts("cupsFileGetChar: Bad arguments!");
450 * If the input buffer is empty, try to read more data...
453 if (fp
->ptr
>= fp
->end
)
454 if (cups_fill(fp
) < 0)
456 DEBUG_puts("cupsFileGetChar: Unable to fill buffer!");
461 * Return the next character in the buffer...
464 DEBUG_printf(("cupsFileGetChar: Returning %d...\n", *(fp
->ptr
) & 255));
466 return (*(fp
->ptr
)++ & 255);
471 * 'cupsFileGetConf()' - Get a line from a configuration file...
474 char * /* O - Line read or NULL on eof/error */
475 cupsFileGetConf(cups_file_t
*fp
, /* I - CUPS file */
476 char *buf
, /* O - String buffer */
477 size_t buflen
, /* I - Size of string buffer */
478 char **value
, /* O - Pointer to value */
479 int *linenum
) /* IO - Current line number */
481 char *ptr
; /* Pointer into line */
485 * Range check input...
488 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') ||
489 !buf
|| buflen
< 2 || !value
)
498 * Read the next non-comment line...
503 while (cupsFileGets(fp
, buf
, buflen
))
508 * Strip any comments...
511 if ((ptr
= strchr(buf
, '#')) != NULL
)
513 if (ptr
> buf
&& ptr
[-1] == '\\')
516 _cups_strcpy(ptr
- 1, ptr
);
520 // Strip the comment and any trailing whitespace...
523 if (!isspace(ptr
[-1] & 255))
534 * Strip leading whitespace...
537 for (ptr
= buf
; isspace(*ptr
& 255); ptr
++);
540 _cups_strcpy(buf
, ptr
);
543 * See if there is anything left...
549 * Yes, grab any value and return...
552 for (ptr
= buf
; *ptr
; ptr
++)
553 if (isspace(*ptr
& 255))
559 * Have a value, skip any other spaces...
562 while (isspace(*ptr
& 255))
569 * Strip trailing whitespace and > for lines that begin with <...
572 ptr
+= strlen(ptr
) - 1;
574 if (buf
[0] == '<' && *ptr
== '>')
576 else if (buf
[0] == '<' && *ptr
!= '>')
586 while (ptr
> *value
&& isspace(*ptr
& 255))
603 * 'cupsFileGetLine()' - Get a CR and/or LF-terminated line that may
604 * contain binary data.
606 * This function differs from cupsFileGets() in that the trailing CR and LF
607 * are preserved, as is any binary data on the line. The buffer is nul-
608 * terminated, however you should use the returned length to determine
609 * the number of bytes on the line.
612 size_t /* O - Number of bytes on line or 0 on EOF */
613 cupsFileGetLine(cups_file_t
*fp
, /* I - File to read from */
614 char *buf
, /* I - Buffer */
615 size_t buflen
) /* I - Size of buffer */
617 int ch
; /* Character from file */
618 char *ptr
, /* Current position in line buffer */
619 *end
; /* End of line buffer */
623 * Range check input...
626 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') || !buf
|| buflen
< 3)
630 * Now loop until we have a valid line...
633 for (ptr
= buf
, end
= buf
+ buflen
- 2; ptr
< end
;)
635 if (fp
->ptr
>= fp
->end
)
636 if (cups_fill(fp
) <= 0)
639 *ptr
++ = ch
= *(fp
->ptr
)++;
647 if (fp
->ptr
>= fp
->end
)
648 if (cups_fill(fp
) <= 0)
651 if (*(fp
->ptr
) == '\n')
652 *ptr
++ = *(fp
->ptr
)++;
659 * Line feed ends a line...
673 * 'cupsFileGets()' - Get a CR and/or LF-terminated line.
676 char * /* O - Line read or NULL on eof/error */
677 cupsFileGets(cups_file_t
*fp
, /* I - CUPS file */
678 char *buf
, /* O - String buffer */
679 size_t buflen
) /* I - Size of string buffer */
681 int ch
; /* Character from file */
682 char *ptr
, /* Current position in line buffer */
683 *end
; /* End of line buffer */
687 * Range check input...
690 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') || !buf
|| buflen
< 2)
694 * Now loop until we have a valid line...
697 for (ptr
= buf
, end
= buf
+ buflen
- 1; ptr
< end
;)
699 if (fp
->ptr
>= fp
->end
)
700 if (cups_fill(fp
) <= 0)
716 if (fp
->ptr
>= fp
->end
)
717 if (cups_fill(fp
) <= 0)
720 if (*(fp
->ptr
) == '\n')
728 * Line feed ends a line...
744 * 'cupsFileLock()' - Temporarily lock access to a file.
747 int /* O - 0 on success, -1 on error */
748 cupsFileLock(cups_file_t
*fp
, /* I - File to lock */
749 int block
) /* I - 1 to wait for the lock, 0 to fail right away */
755 if (!fp
|| fp
->mode
== 's')
763 return (locking(fp
->fd
, block
? _LK_LOCK
: _LK_NBLCK
, 0));
765 return (lockf(fp
->fd
, block
? F_LOCK
: F_TLOCK
, 0));
771 * 'cupsFileNumber()' - Return the file descriptor associated with a CUPS file.
774 int /* O - File descriptor */
775 cupsFileNumber(cups_file_t
*fp
) /* I - CUPS file */
782 * 'cupsFileOpen()' - Open a CUPS file.
785 cups_file_t
* /* O - CUPS file or NULL */
786 cupsFileOpen(const char *filename
, /* I - Name of file */
787 const char *mode
) /* I - Open mode */
789 cups_file_t
*fp
; /* New CUPS file */
790 int fd
; /* File descriptor */
791 char hostname
[1024], /* Hostname */
792 *portname
; /* Port "name" (number or service) */
793 http_addrlist_t
*addrlist
; /* Host address list */
796 DEBUG_printf(("cupsFileOpen(filename=\"%s\", mode=\"%s\")\n", filename
,
800 * Range check input...
803 if (!filename
|| !mode
||
804 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's'))
813 case 'a' : /* Append file */
814 fd
= open(filename
, O_RDWR
| O_CREAT
| O_APPEND
| O_LARGEFILE
| O_BINARY
, 0666);
817 case 'r' : /* Read file */
818 fd
= open(filename
, O_RDONLY
| O_LARGEFILE
| O_BINARY
, 0);
821 case 'w' : /* Write file */
822 fd
= open(filename
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_LARGEFILE
| O_BINARY
, 0666);
825 case 's' : /* Read/write socket */
826 strlcpy(hostname
, filename
, sizeof(hostname
));
827 if ((portname
= strrchr(hostname
, ':')) != NULL
)
833 * Lookup the hostname and service...
836 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
840 * Connect to the server...
843 if (!httpAddrConnect(addrlist
, &fd
))
845 httpAddrFreeList(addrlist
);
849 httpAddrFreeList(addrlist
);
852 default : /* Remove bogus compiler warning... */
860 * Create the CUPS file structure...
863 if ((fp
= cupsFileOpenFd(fd
, mode
)) == NULL
)
879 * 'cupsFileOpenFd()' - Open a CUPS file using a file descriptor.
882 cups_file_t
* /* O - CUPS file or NULL */
883 cupsFileOpenFd(int fd
, /* I - File descriptor */
884 const char *mode
) /* I - Open mode */
886 cups_file_t
*fp
; /* New CUPS file */
889 DEBUG_printf(("cupsFileOpenFd(fd=%d, mode=\"%s\")\n", fd
, mode
));
892 * Range check input...
895 if (fd
< 0 || !mode
||
896 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's'))
903 if ((fp
= calloc(1, sizeof(cups_file_t
))) == NULL
)
918 fp
->end
= fp
->buf
+ sizeof(fp
->buf
);
921 if (mode
[1] >= '1' && mode
[1] <= '9')
924 * Open a compressed stream, so write the standard gzip file
928 unsigned char header
[10]; /* gzip file header */
929 time_t curtime
; /* Current time */
932 curtime
= time(NULL
);
935 header
[2] = Z_DEFLATED
;
938 header
[5] = curtime
>> 8;
939 header
[6] = curtime
>> 16;
940 header
[7] = curtime
>> 24;
944 cups_write(fp
, (char *)header
, 10);
947 * Initialize the compressor...
950 deflateInit2(&(fp
->stream
), mode
[1] - '0', Z_DEFLATED
, -15, 8,
953 fp
->stream
.next_out
= fp
->cbuf
;
954 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
956 fp
->crc
= crc32(0L, Z_NULL
, 0);
958 #endif /* HAVE_LIBZ */
969 default : /* Remove bogus compiler warning... */
974 * Don't pass this file to child processes...
978 fcntl(fp
->fd
, F_SETFD
, fcntl(fp
->fd
, F_GETFD
) | FD_CLOEXEC
);
986 * 'cupsFilePeekChar()' - Peek at the next character from a file.
989 int /* O - Character or -1 on EOF */
990 cupsFilePeekChar(cups_file_t
*fp
) /* I - CUPS file */
993 * Range check input...
996 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
1000 * If the input buffer is empty, try to read more data...
1003 if (fp
->ptr
>= fp
->end
)
1004 if (cups_fill(fp
) < 0)
1008 * Return the next character in the buffer...
1011 return (*(fp
->ptr
) & 255);
1016 * 'cupsFilePrintf()' - Write a formatted string.
1019 int /* O - Number of bytes written or -1 */
1020 cupsFilePrintf(cups_file_t
*fp
, /* I - CUPS file */
1021 const char *format
, /* I - Printf-style format string */
1022 ...) /* I - Additional args as necessary */
1024 va_list ap
; /* Argument list */
1025 ssize_t bytes
; /* Formatted size */
1026 char buf
[8192]; /* Formatted text */
1029 DEBUG_printf(("cupsFilePrintf(fp=%p, format=\"%s\", ...)\n", fp
, format
));
1031 if (!fp
|| !format
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1034 va_start(ap
, format
);
1035 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
1038 if (bytes
>= sizeof(buf
))
1041 if (fp
->mode
== 's')
1042 return (cups_write(fp
, buf
, bytes
));
1044 if ((fp
->ptr
+ bytes
) > fp
->end
)
1045 if (cupsFileFlush(fp
))
1050 if (bytes
> sizeof(fp
->buf
))
1054 return (cups_compress(fp
, buf
, bytes
));
1056 #endif /* HAVE_LIBZ */
1057 return (cups_write(fp
, buf
, bytes
));
1061 memcpy(fp
->ptr
, buf
, bytes
);
1069 * 'cupsFilePutChar()' - Write a character.
1072 int /* O - 0 on success, -1 on error */
1073 cupsFilePutChar(cups_file_t
*fp
, /* I - CUPS file */
1074 int c
) /* I - Character to write */
1077 * Range check input...
1080 if (!fp
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1083 if (fp
->mode
== 's')
1086 * Send character immediately over socket...
1089 char ch
; /* Output character */
1094 if (send(fp
->fd
, &ch
, 1, 0) < 1)
1103 if (fp
->ptr
>= fp
->end
)
1104 if (cupsFileFlush(fp
))
1117 * 'cupsFilePuts()' - Write a string.
1120 int /* O - Number of bytes written or -1 */
1121 cupsFilePuts(cups_file_t
*fp
, /* I - CUPS file */
1122 const char *s
) /* I - String to write */
1124 ssize_t bytes
; /* Bytes to write */
1128 * Range check input...
1131 if (!fp
|| !s
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1135 * Write the string...
1138 bytes
= (int)strlen(s
);
1140 if (fp
->mode
== 's')
1142 if (cups_write(fp
, s
, bytes
) < 0)
1150 if ((fp
->ptr
+ bytes
) > fp
->end
)
1151 if (cupsFileFlush(fp
))
1156 if (bytes
> sizeof(fp
->buf
))
1160 return (cups_compress(fp
, s
, bytes
));
1162 #endif /* HAVE_LIBZ */
1163 return (cups_write(fp
, s
, bytes
));
1167 memcpy(fp
->ptr
, s
, bytes
);
1175 * 'cupsFileRead()' - Read from a file.
1178 ssize_t
/* O - Number of bytes read or -1 */
1179 cupsFileRead(cups_file_t
*fp
, /* I - CUPS file */
1180 char *buf
, /* O - Buffer */
1181 size_t bytes
) /* I - Number of bytes to read */
1183 size_t total
; /* Total bytes read */
1184 ssize_t count
; /* Bytes read */
1187 DEBUG_printf(("cupsFileRead(fp=%p, buf=%p, bytes=%ld)\n", fp
, buf
,
1191 * Range check input...
1194 if (!fp
|| !buf
|| bytes
< 0 || (fp
->mode
!= 'r' && fp
->mode
!= 's'))
1201 * Loop until all bytes are read...
1207 if (fp
->ptr
>= fp
->end
)
1208 if (cups_fill(fp
) <= 0)
1210 DEBUG_printf((" cups_fill() returned -1, total=%d\n", (int)total
));
1213 return ((ssize_t
)total
);
1218 count
= (ssize_t
)(fp
->end
- fp
->ptr
);
1219 if (count
> (ssize_t
)bytes
)
1220 count
= (ssize_t
)bytes
;
1222 memcpy(buf
, fp
->ptr
, count
);
1226 * Update the counts for the last read...
1235 * Return the total number of bytes read...
1238 DEBUG_printf((" total=%d\n", (int)total
));
1240 return ((ssize_t
)total
);
1245 * 'cupsFileRewind()' - Rewind a file.
1248 off_t
/* O - New file position or -1 */
1249 cupsFileRewind(cups_file_t
*fp
) /* I - CUPS file */
1252 * Range check input...
1255 if (!fp
|| fp
->mode
!= 'r')
1259 * Handle special cases...
1265 * No seeking necessary...
1278 * Otherwise, seek in the file and cleanup any compression buffers...
1284 inflateEnd(&fp
->stream
);
1287 #endif /* HAVE_LIBZ */
1289 lseek(fp
->fd
, 0, SEEK_SET
);
1301 * 'cupsFileSeek()' - Seek in a file.
1304 off_t
/* O - New file position or -1 */
1305 cupsFileSeek(cups_file_t
*fp
, /* I - CUPS file */
1306 off_t pos
) /* I - Position in file */
1308 ssize_t bytes
; /* Number bytes in buffer */
1311 DEBUG_printf(("cupsFileSeek(fp=%p, pos=" CUPS_LLFMT
")\n", fp
, pos
));
1312 DEBUG_printf((" fp->pos=" CUPS_LLFMT
"\n", fp
->pos
));
1313 DEBUG_printf((" fp->ptr=%p, fp->end=%p\n", fp
->ptr
, fp
->end
));
1316 * Range check input...
1319 if (!fp
|| pos
< 0 || fp
->mode
!= 'r')
1323 * Handle special cases...
1327 return (cupsFileRewind(fp
));
1332 * No seeking necessary...
1345 if (!fp
->compressed
&& !fp
->ptr
)
1348 * Preload a buffer to determine whether the file is compressed...
1351 if (cups_fill(fp
) < 0)
1354 #endif /* HAVE_LIBZ */
1357 * Figure out the number of bytes in the current buffer, and then
1358 * see if we are outside of it...
1362 bytes
= (ssize_t
)(fp
->end
- fp
->buf
);
1368 DEBUG_printf((" bytes=" CUPS_LLFMT
"\n", CUPS_LLCAST bytes
));
1373 * Need to seek backwards...
1376 DEBUG_puts(" SEEK BACKWARDS");
1381 inflateEnd(&fp
->stream
);
1383 lseek(fp
->fd
, 0, SEEK_SET
);
1388 while ((bytes
= cups_fill(fp
)) > 0)
1389 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 %ld...\n", (long)fp
->pos
));
1407 else if (pos
>= (fp
->pos
+ bytes
))
1410 * Need to seek forwards...
1413 DEBUG_puts(" SEEK FORWARDS");
1418 while ((bytes
= cups_fill(fp
)) > 0)
1420 if (pos
>= fp
->pos
&& pos
< (fp
->pos
+ bytes
))
1427 fp
->ptr
= fp
->buf
+ pos
- fp
->pos
;
1430 #endif /* HAVE_LIBZ */
1432 fp
->pos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1436 DEBUG_printf((" lseek() returned " CUPS_LLFMT
"...\n", fp
->pos
));
1442 * Just reposition the current pointer, since we have the right
1446 DEBUG_puts(" SEEK INSIDE BUFFER");
1448 fp
->ptr
= fp
->buf
+ pos
- fp
->pos
;
1456 * 'cupsFileStderr()' - Return a CUPS file associated with stderr.
1460 cupsFileStderr(void)
1462 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1466 * Open file descriptor 2 as needed...
1469 if (!cg
->stdio_files
[2])
1472 * Flush any pending output on the stdio file...
1478 * Open file descriptor 2...
1481 if ((cg
->stdio_files
[2] = cupsFileOpenFd(2, "w")) != NULL
)
1482 cg
->stdio_files
[2]->is_stdio
= 1;
1485 return (cg
->stdio_files
[2]);
1490 * 'cupsFileStdin()' - Return a CUPS file associated with stdin.
1496 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1500 * Open file descriptor 0 as needed...
1503 if (!cg
->stdio_files
[0])
1506 * Open file descriptor 0...
1509 if ((cg
->stdio_files
[0] = cupsFileOpenFd(0, "r")) != NULL
)
1510 cg
->stdio_files
[0]->is_stdio
= 1;
1513 return (cg
->stdio_files
[0]);
1518 * 'cupsFileStdout()' - Return a CUPS file associated with stdout.
1522 cupsFileStdout(void)
1524 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1528 * Open file descriptor 1 as needed...
1531 if (!cg
->stdio_files
[1])
1534 * Flush any pending output on the stdio file...
1540 * Open file descriptor 1...
1543 if ((cg
->stdio_files
[1] = cupsFileOpenFd(1, "w")) != NULL
)
1544 cg
->stdio_files
[1]->is_stdio
= 1;
1547 return (cg
->stdio_files
[1]);
1552 * 'cupsFileTell()' - Return the current file position.
1555 off_t
/* O - File position */
1556 cupsFileTell(cups_file_t
*fp
) /* I - CUPS file */
1558 return (fp
? fp
->pos
: 0);
1563 * 'cupsFileUnlock()' - Unlock access to a file.
1566 int /* O - 0 on success, -1 on error */
1567 cupsFileUnlock(cups_file_t
*fp
) /* I - File to lock */
1573 if (!fp
|| fp
->mode
== 's')
1581 return (locking(fp
->fd
, _LK_UNLCK
, 0));
1583 return (lockf(fp
->fd
, F_ULOCK
, 0));
1589 * 'cupsFileWrite()' - Write to a file.
1592 ssize_t
/* O - Number of bytes written */
1593 cupsFileWrite(cups_file_t
*fp
, /* I - CUPS file */
1594 const char *buf
, /* I - Buffer */
1595 size_t bytes
) /* I - Number of bytes to write */
1598 * Range check input...
1601 if (!fp
|| !buf
|| bytes
< 0 || (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1608 * Write the buffer...
1611 if (fp
->mode
== 's')
1613 if (cups_write(fp
, buf
, bytes
) < 0)
1616 fp
->pos
+= (off_t
)bytes
;
1618 return ((ssize_t
)bytes
);
1621 if ((fp
->ptr
+ bytes
) > fp
->end
)
1622 if (cupsFileFlush(fp
))
1625 fp
->pos
+= (off_t
)bytes
;
1627 if (bytes
> sizeof(fp
->buf
))
1631 return (cups_compress(fp
, buf
, bytes
));
1633 #endif /* HAVE_LIBZ */
1634 return (cups_write(fp
, buf
, bytes
));
1638 memcpy(fp
->ptr
, buf
, bytes
);
1640 return ((ssize_t
)bytes
);
1647 * 'cups_compress()' - Compress a buffer of data...
1650 static ssize_t
/* O - Number of bytes written or -1 */
1651 cups_compress(cups_file_t
*fp
, /* I - CUPS file */
1652 const char *buf
, /* I - Buffer */
1653 size_t bytes
) /* I - Number bytes */
1655 DEBUG_printf(("cups_compress(fp=%p, buf=%p, bytes=%ld)\n", fp
, buf
,
1662 fp
->crc
= crc32(fp
->crc
, (const Bytef
*)buf
, bytes
);
1665 * Deflate the bytes...
1668 fp
->stream
.next_in
= (Bytef
*)buf
;
1669 fp
->stream
.avail_in
= bytes
;
1671 while (fp
->stream
.avail_in
> 0)
1674 * Flush the current buffer...
1677 DEBUG_printf((" avail_in=%d, avail_out=%d\n", fp
->stream
.avail_in
,
1678 fp
->stream
.avail_out
));
1680 if (fp
->stream
.avail_out
< (int)(sizeof(fp
->cbuf
) / 8))
1682 if (cups_write(fp
, (char *)fp
->cbuf
, fp
->stream
.next_out
- fp
->cbuf
) < 0)
1685 fp
->stream
.next_out
= fp
->cbuf
;
1686 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
1689 deflate(&(fp
->stream
), Z_NO_FLUSH
);
1694 #endif /* HAVE_LIBZ */
1698 * 'cups_fill()' - Fill the input buffer...
1701 static ssize_t
/* O - Number of bytes or -1 */
1702 cups_fill(cups_file_t
*fp
) /* I - CUPS file */
1704 ssize_t bytes
; /* Number of bytes read */
1706 int status
; /* Decompression status */
1707 const unsigned char *ptr
, /* Pointer into buffer */
1708 *end
; /* End of buffer */
1709 #endif /* HAVE_LIBZ */
1712 DEBUG_printf(("cups_fill(fp=%p)\n", fp
));
1713 DEBUG_printf((" fp->ptr=%p, fp->end=%p, fp->buf=%p, "
1714 "fp->pos=" CUPS_LLFMT
", fp->eof=%d\n",
1715 fp
->ptr
, fp
->end
, fp
->buf
, fp
->pos
, fp
->eof
));
1718 * Update the "pos" element as needed...
1721 if (fp
->ptr
&& fp
->end
)
1722 fp
->pos
+= (off_t
)(fp
->end
- fp
->buf
);
1725 DEBUG_printf((" fp->compressed=%d\n", fp
->compressed
));
1727 while (!fp
->ptr
|| fp
->compressed
)
1730 * Check to see if we have read any data yet; if not, see if we have a
1731 * compressed file...
1737 * Reset the file position in case we are seeking...
1743 * Read the first bytes in the file to determine if we have a gzip'd
1747 if ((bytes
= cups_read(fp
, (char *)fp
->buf
, sizeof(fp
->buf
))) < 0)
1750 * Can't read from file!
1753 DEBUG_printf((" cups_read() returned " CUPS_LLFMT
"!\n",
1754 CUPS_LLCAST bytes
));
1759 if (bytes
< 10 || fp
->buf
[0] != 0x1f ||
1760 (fp
->buf
[1] & 255) != 0x8b ||
1761 fp
->buf
[2] != 8 || (fp
->buf
[3] & 0xe0) != 0)
1764 * Not a gzip'd file!
1768 fp
->end
= fp
->buf
+ bytes
;
1770 DEBUG_printf((" returning " CUPS_LLFMT
"!\n", CUPS_LLCAST bytes
));
1776 * Parse header junk: extra data, original name, and comment...
1779 ptr
= (unsigned char *)fp
->buf
+ 10;
1780 end
= (unsigned char *)fp
->buf
+ bytes
;
1782 if (fp
->buf
[3] & 0x04)
1785 * Skip extra data...
1788 if ((ptr
+ 2) > end
)
1791 * Can't read from file!
1797 bytes
= ((unsigned char)ptr
[1] << 8) | (unsigned char)ptr
[0];
1803 * Can't read from file!
1810 if (fp
->buf
[3] & 0x08)
1813 * Skip original name data...
1816 while (ptr
< end
&& *ptr
)
1824 * Can't read from file!
1831 if (fp
->buf
[3] & 0x10)
1834 * Skip comment data...
1837 while (ptr
< end
&& *ptr
)
1845 * Can't read from file!
1852 if (fp
->buf
[3] & 0x02)
1855 * Skip header CRC data...
1863 * Can't read from file!
1871 * Copy the flate-compressed data to the compression buffer...
1874 if ((bytes
= end
- ptr
) > 0)
1875 memcpy(fp
->cbuf
, ptr
, bytes
);
1878 * Setup the decompressor data...
1881 fp
->stream
.zalloc
= (alloc_func
)0;
1882 fp
->stream
.zfree
= (free_func
)0;
1883 fp
->stream
.opaque
= (voidpf
)0;
1884 fp
->stream
.next_in
= (Bytef
*)fp
->cbuf
;
1885 fp
->stream
.next_out
= NULL
;
1886 fp
->stream
.avail_in
= bytes
;
1887 fp
->stream
.avail_out
= 0;
1888 fp
->crc
= crc32(0L, Z_NULL
, 0);
1890 if (inflateInit2(&(fp
->stream
), -15) != Z_OK
)
1899 * If we have reached end-of-file, return immediately...
1906 * Fill the decompression buffer as needed...
1909 if (fp
->stream
.avail_in
== 0)
1911 if ((bytes
= cups_read(fp
, (char *)fp
->cbuf
, sizeof(fp
->cbuf
))) <= 0)
1914 fp
->stream
.next_in
= fp
->cbuf
;
1915 fp
->stream
.avail_in
= bytes
;
1919 * Decompress data from the buffer...
1922 fp
->stream
.next_out
= (Bytef
*)fp
->buf
;
1923 fp
->stream
.avail_out
= sizeof(fp
->buf
);
1925 status
= inflate(&(fp
->stream
), Z_NO_FLUSH
);
1927 if (fp
->stream
.next_out
> (Bytef
*)fp
->buf
)
1928 fp
->crc
= crc32(fp
->crc
, (Bytef
*)fp
->buf
,
1929 fp
->stream
.next_out
- (Bytef
*)fp
->buf
);
1931 if (status
== Z_STREAM_END
)
1934 * Read the CRC and length...
1937 unsigned char trailer
[8]; /* Trailer bytes */
1938 uLong tcrc
; /* Trailer CRC */
1941 if (read(fp
->fd
, trailer
, sizeof(trailer
)) < sizeof(trailer
))
1944 * Can't get it, so mark end-of-file...
1951 tcrc
= (((((trailer
[3] << 8) | trailer
[2]) << 8) | trailer
[1]) << 8) |
1954 if (tcrc
!= fp
->crc
)
1957 * Bad CRC, mark end-of-file...
1960 DEBUG_printf(("cups_fill: tcrc=%08x, fp->crc=%08x\n",
1961 (unsigned int)tcrc
, (unsigned int)fp
->crc
));
1969 * Otherwise, reset the compressed flag so that we re-read the
1977 bytes
= sizeof(fp
->buf
) - fp
->stream
.avail_out
;
1980 * Return the decompressed data...
1984 fp
->end
= fp
->buf
+ bytes
;
1990 #endif /* HAVE_LIBZ */
1993 * Read a buffer's full of data...
1996 if ((bytes
= cups_read(fp
, fp
->buf
, sizeof(fp
->buf
))) <= 0)
1999 * Can't read from file!
2010 * Return the bytes we read...
2015 fp
->end
= fp
->buf
+ bytes
;
2022 * 'cups_read()' - Read from a file descriptor.
2025 static ssize_t
/* O - Number of bytes read or -1 */
2026 cups_read(cups_file_t
*fp
, /* I - CUPS file */
2027 char *buf
, /* I - Buffer */
2028 size_t bytes
) /* I - Number bytes */
2030 ssize_t total
; /* Total bytes read */
2034 * Loop until we read at least 0 bytes...
2040 if (fp
->mode
== 's')
2041 total
= (ssize_t
)recv(fp
->fd
, buf
, (unsigned)bytes
, 0);
2043 total
= (ssize_t
)read(fp
->fd
, buf
, (unsigned)bytes
);
2045 if (fp
->mode
== 's')
2046 total
= recv(fp
->fd
, buf
, bytes
, 0);
2048 total
= read(fp
->fd
, buf
, bytes
);
2055 * Reads can be interrupted by signals and unavailable resources...
2058 if (errno
== EAGAIN
|| errno
== EINTR
)
2065 * Return the total number of bytes read...
2073 * 'cups_write()' - Write to a file descriptor.
2076 static ssize_t
/* O - Number of bytes written or -1 */
2077 cups_write(cups_file_t
*fp
, /* I - CUPS file */
2078 const char *buf
, /* I - Buffer */
2079 size_t bytes
) /* I - Number bytes */
2081 size_t total
; /* Total bytes written */
2082 ssize_t count
; /* Count this time */
2085 DEBUG_printf(("cups_write(fp=%p, buf=%p, bytes=%ld)\n", fp
, buf
,
2089 * Loop until all bytes are written...
2096 if (fp
->mode
== 's')
2097 count
= (ssize_t
)send(fp
->fd
, buf
, (unsigned)bytes
, 0);
2099 count
= (ssize_t
)write(fp
->fd
, buf
, (unsigned)bytes
);
2101 if (fp
->mode
== 's')
2102 count
= send(fp
->fd
, buf
, bytes
, 0);
2104 count
= write(fp
->fd
, buf
, bytes
);
2110 * Writes can be interrupted by signals and unavailable resources...
2113 if (errno
== EAGAIN
|| errno
== EINTR
)
2119 DEBUG_printf((" count=%ld\n", (long)count
));
2122 * Update the counts for the last write call...
2131 * Return the total number of bytes written...
2134 return ((ssize_t
)total
);
2139 * End of "$Id: file.c 6962 2007-09-17 20:35:47Z mike $".