]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/file.c
d3726cf53448e4513f1e5dbff484dffb61537c63
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-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 */
130 static ssize_t
cups_compress(cups_file_t
*fp
, const char *buf
, size_t bytes
);
131 #endif /* HAVE_LIBZ */
132 static ssize_t
cups_fill(cups_file_t
*fp
);
133 static ssize_t
cups_read(cups_file_t
*fp
, char *buf
, size_t bytes
);
134 static ssize_t
cups_write(cups_file_t
*fp
, const char *buf
, size_t bytes
);
138 * 'cupsFileClose()' - Close a CUPS file.
143 int /* O - 0 on success, -1 on error */
144 cupsFileClose(cups_file_t
*fp
) /* I - CUPS file */
146 int fd
; /* File descriptor */
147 char mode
; /* Open mode */
148 int status
; /* Return status */
149 int is_stdio
; /* Is a stdio file? */
152 DEBUG_printf(("cupsFileClose(fp=%p)\n", fp
));
162 * Flush pending write data...
166 status
= cupsFileFlush(fp
);
171 if (fp
->compressed
&& status
>= 0)
176 * Free decompression data...
179 inflateEnd(&fp
->stream
);
184 * Flush any remaining compressed data...
187 unsigned char trailer
[8]; /* Trailer CRC and length */
188 int done
; /* Done writing... */
191 fp
->stream
.avail_in
= 0;
195 if (fp
->stream
.next_out
> fp
->cbuf
)
197 if (cups_write(fp
, (char *)fp
->cbuf
,
198 fp
->stream
.next_out
- fp
->cbuf
) < 0)
201 fp
->stream
.next_out
= fp
->cbuf
;
202 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
205 if (done
|| status
< 0)
208 done
= deflate(&fp
->stream
, Z_FINISH
) == Z_STREAM_END
&&
209 fp
->stream
.next_out
== fp
->cbuf
;
213 * Write the CRC and length...
216 trailer
[0] = fp
->crc
;
217 trailer
[1] = fp
->crc
>> 8;
218 trailer
[2] = fp
->crc
>> 16;
219 trailer
[3] = fp
->crc
>> 24;
220 trailer
[4] = fp
->pos
;
221 trailer
[5] = fp
->pos
>> 8;
222 trailer
[6] = fp
->pos
>> 16;
223 trailer
[7] = fp
->pos
>> 24;
225 if (cups_write(fp
, (char *)trailer
, 8) < 0)
229 * Free all memory used by the compression stream...
232 deflateEnd(&(fp
->stream
));
235 #endif /* HAVE_LIBZ */
238 * Save the file descriptor we used and free memory...
243 is_stdio
= fp
->is_stdio
;
248 * Close the file, returning the close status...
253 if (closesocket(fd
) < 0)
267 * 'cupsFileCompression()' - Return whether a file is compressed.
272 int /* O - @code CUPS_FILE_NONE@ or @code CUPS_FILE_GZIP@ */
273 cupsFileCompression(cups_file_t
*fp
) /* I - CUPS file */
275 return (fp
? fp
->compressed
: CUPS_FILE_NONE
);
280 * 'cupsFileEOF()' - Return the end-of-file status.
285 int /* O - 1 on end of file, 0 otherwise */
286 cupsFileEOF(cups_file_t
*fp
) /* I - CUPS file */
288 return (fp
? fp
->eof
: 1);
293 * 'cupsFileFind()' - Find a file using the specified path.
295 * This function allows the paths in the path string to be separated by
296 * colons (UNIX standard) or semicolons (Windows standard) and stores the
297 * result in the buffer supplied. If the file cannot be found in any of
298 * the supplied paths, @code NULL@ is returned. A @code NULL@ path only
299 * matches the current directory.
304 const char * /* O - Full path to file or @code NULL@ if not found */
305 cupsFileFind(const char *filename
, /* I - File to find */
306 const char *path
, /* I - Colon/semicolon-separated path */
307 int executable
, /* I - 1 = executable files, 0 = any file/dir */
308 char *buffer
, /* I - Filename buffer */
309 int bufsize
) /* I - Size of filename buffer */
311 char *bufptr
, /* Current position in buffer */
312 *bufend
; /* End of buffer */
316 * Range check input...
319 if (!filename
|| !buffer
|| bufsize
< 2)
325 * No path, so check current directory...
328 if (!access(filename
, 0))
330 strlcpy(buffer
, filename
, bufsize
);
338 * Now check each path and return the first match...
341 bufend
= buffer
+ bufsize
- 1;
347 if (*path
== ';' || (*path
== ':' && ((bufptr
- buffer
) > 1 || !isalpha(buffer
[0] & 255))))
349 if (*path
== ';' || *path
== ':')
352 if (bufptr
> buffer
&& bufptr
[-1] != '/' && bufptr
< bufend
)
355 strlcpy(bufptr
, filename
, bufend
- bufptr
);
358 if (!access(buffer
, 0))
360 if (!access(buffer
, executable
? X_OK
: 0))
363 DEBUG_printf(("cupsFileFind: Returning \"%s\"\n", buffer
));
369 else if (bufptr
< bufend
)
376 * Check the last path...
379 if (bufptr
> buffer
&& bufptr
[-1] != '/' && bufptr
< bufend
)
382 strlcpy(bufptr
, filename
, bufend
- bufptr
);
384 if (!access(buffer
, 0))
386 DEBUG_printf(("cupsFileFind: Returning \"%s\"\n", buffer
));
391 DEBUG_puts("cupsFileFind: Returning NULL");
398 * 'cupsFileFlush()' - Flush pending output.
403 int /* O - 0 on success, -1 on error */
404 cupsFileFlush(cups_file_t
*fp
) /* I - CUPS file */
406 ssize_t bytes
; /* Bytes to write */
409 DEBUG_printf(("cupsFileFlush(fp=%p)\n", fp
));
412 * Range check input...
415 if (!fp
|| fp
->mode
!= 'w')
417 DEBUG_puts("cupsFileFlush: Attempt to flush a read-only file...");
421 bytes
= (ssize_t
)(fp
->ptr
- fp
->buf
);
423 DEBUG_printf(("cupsFileFlush: Flushing " CUPS_LLFMT
" bytes...\n",
430 bytes
= cups_compress(fp
, fp
->buf
, bytes
);
432 #endif /* HAVE_LIBZ */
433 bytes
= cups_write(fp
, fp
->buf
, bytes
);
446 * 'cupsFileGetChar()' - Get a single character from a file.
451 int /* O - Character or -1 on end of file */
452 cupsFileGetChar(cups_file_t
*fp
) /* I - CUPS file */
455 * Range check input...
458 DEBUG_printf(("cupsFileGetChar(fp=%p)\n", fp
));
460 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
462 DEBUG_puts("cupsFileGetChar: Bad arguments!");
467 * If the input buffer is empty, try to read more data...
470 if (fp
->ptr
>= fp
->end
)
471 if (cups_fill(fp
) < 0)
473 DEBUG_puts("cupsFileGetChar: Unable to fill buffer!");
478 * Return the next character in the buffer...
481 DEBUG_printf(("cupsFileGetChar: Returning %d...\n", *(fp
->ptr
) & 255));
485 DEBUG_printf(("cupsFileGetChar: pos=" CUPS_LLFMT
"\n", CUPS_LLCAST fp
->pos
));
487 return (*(fp
->ptr
)++ & 255);
492 * 'cupsFileGetConf()' - Get a line from a configuration file...
497 char * /* O - Line read or @code NULL@ on end of file or error */
498 cupsFileGetConf(cups_file_t
*fp
, /* I - CUPS file */
499 char *buf
, /* O - String buffer */
500 size_t buflen
, /* I - Size of string buffer */
501 char **value
, /* O - Pointer to value */
502 int *linenum
) /* IO - Current line number */
504 char *ptr
; /* Pointer into line */
508 * Range check input...
511 DEBUG_printf(("cupsFileGetConf(fp=%p, buf=%p, buflen=" CUPS_LLFMT
512 ", value=%p, linenum=%p)\n", fp
, buf
, CUPS_LLCAST buflen
,
515 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') ||
516 !buf
|| buflen
< 2 || !value
)
525 * Read the next non-comment line...
530 while (cupsFileGets(fp
, buf
, buflen
))
535 * Strip any comments...
538 if ((ptr
= strchr(buf
, '#')) != NULL
)
540 if (ptr
> buf
&& ptr
[-1] == '\\')
543 _cups_strcpy(ptr
- 1, ptr
);
547 // Strip the comment and any trailing whitespace...
550 if (!isspace(ptr
[-1] & 255))
561 * Strip leading whitespace...
564 for (ptr
= buf
; isspace(*ptr
& 255); ptr
++);
567 _cups_strcpy(buf
, ptr
);
570 * See if there is anything left...
576 * Yes, grab any value and return...
579 for (ptr
= buf
; *ptr
; ptr
++)
580 if (isspace(*ptr
& 255))
586 * Have a value, skip any other spaces...
589 while (isspace(*ptr
& 255))
596 * Strip trailing whitespace and > for lines that begin with <...
599 ptr
+= strlen(ptr
) - 1;
601 if (buf
[0] == '<' && *ptr
== '>')
603 else if (buf
[0] == '<' && *ptr
!= '>')
613 while (ptr
> *value
&& isspace(*ptr
& 255))
630 * 'cupsFileGetLine()' - Get a CR and/or LF-terminated line that may
631 * contain binary data.
633 * This function differs from @link cupsFileGets@ in that the trailing CR
634 * and LF are preserved, as is any binary data on the line. The buffer is
635 * nul-terminated, however you should use the returned length to determine
636 * the number of bytes on the line.
641 size_t /* O - Number of bytes on line or 0 on end of file */
642 cupsFileGetLine(cups_file_t
*fp
, /* I - File to read from */
643 char *buf
, /* I - Buffer */
644 size_t buflen
) /* I - Size of buffer */
646 int ch
; /* Character from file */
647 char *ptr
, /* Current position in line buffer */
648 *end
; /* End of line buffer */
652 * Range check input...
655 DEBUG_printf(("cupsFileGetLine(fp=%p, buf=%p, buflen=" CUPS_LLFMT
")\n",
656 fp
, buf
, CUPS_LLCAST buflen
));
658 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') || !buf
|| buflen
< 3)
662 * Now loop until we have a valid line...
665 for (ptr
= buf
, end
= buf
+ buflen
- 2; ptr
< end
;)
667 if (fp
->ptr
>= fp
->end
)
668 if (cups_fill(fp
) <= 0)
671 *ptr
++ = ch
= *(fp
->ptr
)++;
680 if (fp
->ptr
>= fp
->end
)
681 if (cups_fill(fp
) <= 0)
684 if (*(fp
->ptr
) == '\n')
686 *ptr
++ = *(fp
->ptr
)++;
695 * Line feed ends a line...
704 DEBUG_printf(("cupsFileGetLine: pos=" CUPS_LLFMT
"\n", CUPS_LLCAST fp
->pos
));
711 * 'cupsFileGets()' - Get a CR and/or LF-terminated line.
716 char * /* O - Line read or @code NULL@ on end of file or error */
717 cupsFileGets(cups_file_t
*fp
, /* I - CUPS file */
718 char *buf
, /* O - String buffer */
719 size_t buflen
) /* I - Size of string buffer */
721 int ch
; /* Character from file */
722 char *ptr
, /* Current position in line buffer */
723 *end
; /* End of line buffer */
727 * Range check input...
730 DEBUG_printf(("cupsFileGets(fp=%p, buf=%p, buflen=" CUPS_LLFMT
")\n", fp
, buf
,
731 CUPS_LLCAST buflen
));
733 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') || !buf
|| buflen
< 2)
737 * Now loop until we have a valid line...
740 for (ptr
= buf
, end
= buf
+ buflen
- 1; ptr
< end
;)
742 if (fp
->ptr
>= fp
->end
)
743 if (cups_fill(fp
) <= 0)
760 if (fp
->ptr
>= fp
->end
)
761 if (cups_fill(fp
) <= 0)
764 if (*(fp
->ptr
) == '\n')
775 * Line feed ends a line...
786 DEBUG_printf(("cupsFileGets: pos=" CUPS_LLFMT
"\n", CUPS_LLCAST fp
->pos
));
793 * 'cupsFileLock()' - Temporarily lock access to a file.
798 int /* O - 0 on success, -1 on error */
799 cupsFileLock(cups_file_t
*fp
, /* I - CUPS file */
800 int block
) /* I - 1 to wait for the lock, 0 to fail right away */
806 if (!fp
|| fp
->mode
== 's')
814 return (locking(fp
->fd
, block
? _LK_LOCK
: _LK_NBLCK
, 0));
816 return (lockf(fp
->fd
, block
? F_LOCK
: F_TLOCK
, 0));
822 * 'cupsFileNumber()' - Return the file descriptor associated with a CUPS file.
827 int /* O - File descriptor */
828 cupsFileNumber(cups_file_t
*fp
) /* I - CUPS file */
838 * 'cupsFileOpen()' - Open a CUPS file.
840 * The "mode" parameter can be "r" to read, "w" to write, overwriting any
841 * existing file, "a" to append to an existing file or create a new file,
842 * or "s" to open a socket connection.
844 * When opening for writing ("w"), an optional number from 1 to 9 can be
845 * supplied which enables Flate compression of the file. Compression is
846 * not supported for the "a" (append) mode.
848 * When opening a socket connection, the filename is a string of the form
849 * "address:port" or "hostname:port". The socket will make an IPv4 or IPv6
850 * connection as needed, generally preferring IPv6 connections when there is
856 cups_file_t
* /* O - CUPS file or @code NULL@ if the file or socket cannot be opened */
857 cupsFileOpen(const char *filename
, /* I - Name of file */
858 const char *mode
) /* I - Open mode */
860 cups_file_t
*fp
; /* New CUPS file */
861 int fd
; /* File descriptor */
862 char hostname
[1024], /* Hostname */
863 *portname
; /* Port "name" (number or service) */
864 http_addrlist_t
*addrlist
; /* Host address list */
867 DEBUG_printf(("cupsFileOpen(filename=\"%s\", mode=\"%s\")\n", filename
,
871 * Range check input...
874 if (!filename
|| !mode
||
875 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's') ||
876 (*mode
== 'a' && isdigit(mode
[1] & 255)))
885 case 'a' : /* Append file */
886 fd
= open(filename
, O_RDWR
| O_CREAT
| O_APPEND
| O_LARGEFILE
| O_BINARY
, 0666);
889 case 'r' : /* Read file */
890 fd
= open(filename
, O_RDONLY
| O_LARGEFILE
| O_BINARY
, 0);
893 case 'w' : /* Write file */
894 fd
= open(filename
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_LARGEFILE
| O_BINARY
, 0666);
897 case 's' : /* Read/write socket */
898 strlcpy(hostname
, filename
, sizeof(hostname
));
899 if ((portname
= strrchr(hostname
, ':')) != NULL
)
905 * Lookup the hostname and service...
908 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
912 * Connect to the server...
915 if (!httpAddrConnect(addrlist
, &fd
))
917 httpAddrFreeList(addrlist
);
921 httpAddrFreeList(addrlist
);
924 default : /* Remove bogus compiler warning... */
932 * Create the CUPS file structure...
935 if ((fp
= cupsFileOpenFd(fd
, mode
)) == NULL
)
951 * 'cupsFileOpenFd()' - Open a CUPS file using a file descriptor.
953 * The "mode" parameter can be "r" to read, "w" to write, "a" to append,
954 * or "s" to treat the file descriptor as a bidirectional socket connection.
956 * When opening for writing ("w"), an optional number from 1 to 9 can be
957 * supplied which enables Flate compression of the file. Compression is
958 * not supported for the "a" (append) mode.
963 cups_file_t
* /* O - CUPS file or @code NULL@ if the file could not be opened */
964 cupsFileOpenFd(int fd
, /* I - File descriptor */
965 const char *mode
) /* I - Open mode */
967 cups_file_t
*fp
; /* New CUPS file */
970 DEBUG_printf(("cupsFileOpenFd(fd=%d, mode=\"%s\")\n", fd
, mode
));
973 * Range check input...
976 if (fd
< 0 || !mode
||
977 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's') ||
978 (*mode
== 'a' && isdigit(mode
[1] & 255)))
985 if ((fp
= calloc(1, sizeof(cups_file_t
))) == NULL
)
997 fp
->pos
= lseek(fd
, 0, SEEK_END
);
1002 fp
->end
= fp
->buf
+ sizeof(fp
->buf
);
1005 if (mode
[1] >= '1' && mode
[1] <= '9')
1008 * Open a compressed stream, so write the standard gzip file
1012 unsigned char header
[10]; /* gzip file header */
1013 time_t curtime
; /* Current time */
1016 curtime
= time(NULL
);
1019 header
[2] = Z_DEFLATED
;
1021 header
[4] = curtime
;
1022 header
[5] = curtime
>> 8;
1023 header
[6] = curtime
>> 16;
1024 header
[7] = curtime
>> 24;
1028 cups_write(fp
, (char *)header
, 10);
1031 * Initialize the compressor...
1034 deflateInit2(&(fp
->stream
), mode
[1] - '0', Z_DEFLATED
, -15, 8,
1035 Z_DEFAULT_STRATEGY
);
1037 fp
->stream
.next_out
= fp
->cbuf
;
1038 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
1040 fp
->crc
= crc32(0L, Z_NULL
, 0);
1042 #endif /* HAVE_LIBZ */
1053 default : /* Remove bogus compiler warning... */
1058 * Don't pass this file to child processes...
1062 fcntl(fp
->fd
, F_SETFD
, fcntl(fp
->fd
, F_GETFD
) | FD_CLOEXEC
);
1070 * 'cupsFilePeekChar()' - Peek at the next character from a file.
1075 int /* O - Character or -1 on end of file */
1076 cupsFilePeekChar(cups_file_t
*fp
) /* I - CUPS file */
1079 * Range check input...
1082 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
1086 * If the input buffer is empty, try to read more data...
1089 if (fp
->ptr
>= fp
->end
)
1090 if (cups_fill(fp
) < 0)
1094 * Return the next character in the buffer...
1097 return (*(fp
->ptr
) & 255);
1102 * 'cupsFilePrintf()' - Write a formatted string.
1107 int /* O - Number of bytes written or -1 on error */
1108 cupsFilePrintf(cups_file_t
*fp
, /* I - CUPS file */
1109 const char *format
, /* I - Printf-style format string */
1110 ...) /* I - Additional args as necessary */
1112 va_list ap
; /* Argument list */
1113 ssize_t bytes
; /* Formatted size */
1114 char buf
[8192]; /* Formatted text */
1117 DEBUG_printf(("cupsFilePrintf(fp=%p, format=\"%s\", ...)\n", fp
, format
));
1119 if (!fp
|| !format
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1122 va_start(ap
, format
);
1123 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
1126 if (bytes
>= sizeof(buf
))
1129 if (fp
->mode
== 's')
1131 if (cups_write(fp
, buf
, bytes
) < 0)
1136 DEBUG_printf(("cupsFilePrintf: pos=" CUPS_LLFMT
"\n", CUPS_LLCAST fp
->pos
));
1141 if ((fp
->ptr
+ bytes
) > fp
->end
)
1142 if (cupsFileFlush(fp
))
1147 DEBUG_printf(("cupsFilePrintf: pos=" CUPS_LLFMT
"\n", CUPS_LLCAST fp
->pos
));
1149 if (bytes
> sizeof(fp
->buf
))
1153 return (cups_compress(fp
, buf
, bytes
));
1155 #endif /* HAVE_LIBZ */
1156 return (cups_write(fp
, buf
, bytes
));
1160 memcpy(fp
->ptr
, buf
, bytes
);
1168 * 'cupsFilePutChar()' - Write a character.
1173 int /* O - 0 on success, -1 on error */
1174 cupsFilePutChar(cups_file_t
*fp
, /* I - CUPS file */
1175 int c
) /* I - Character to write */
1178 * Range check input...
1181 if (!fp
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1184 if (fp
->mode
== 's')
1187 * Send character immediately over socket...
1190 char ch
; /* Output character */
1195 if (send(fp
->fd
, &ch
, 1, 0) < 1)
1204 if (fp
->ptr
>= fp
->end
)
1205 if (cupsFileFlush(fp
))
1213 DEBUG_printf(("cupsFilePutChar: pos=" CUPS_LLFMT
"\n", CUPS_LLCAST fp
->pos
));
1220 * 'cupsFilePuts()' - Write a string.
1222 * Like the @code fputs@ function, no newline is appended to the string.
1227 int /* O - Number of bytes written or -1 on error */
1228 cupsFilePuts(cups_file_t
*fp
, /* I - CUPS file */
1229 const char *s
) /* I - String to write */
1231 ssize_t bytes
; /* Bytes to write */
1235 * Range check input...
1238 if (!fp
|| !s
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1242 * Write the string...
1245 bytes
= (int)strlen(s
);
1247 if (fp
->mode
== 's')
1249 if (cups_write(fp
, s
, bytes
) < 0)
1254 DEBUG_printf(("cupsFilePuts: pos=" CUPS_LLFMT
"\n", CUPS_LLCAST fp
->pos
));
1259 if ((fp
->ptr
+ bytes
) > fp
->end
)
1260 if (cupsFileFlush(fp
))
1265 DEBUG_printf(("cupsFilePuts: pos=" CUPS_LLFMT
"\n", CUPS_LLCAST fp
->pos
));
1267 if (bytes
> sizeof(fp
->buf
))
1271 return (cups_compress(fp
, s
, bytes
));
1273 #endif /* HAVE_LIBZ */
1274 return (cups_write(fp
, s
, bytes
));
1278 memcpy(fp
->ptr
, s
, bytes
);
1286 * 'cupsFileRead()' - Read from a file.
1291 ssize_t
/* O - Number of bytes read or -1 on error */
1292 cupsFileRead(cups_file_t
*fp
, /* I - CUPS file */
1293 char *buf
, /* O - Buffer */
1294 size_t bytes
) /* I - Number of bytes to read */
1296 size_t total
; /* Total bytes read */
1297 ssize_t count
; /* Bytes read */
1300 DEBUG_printf(("cupsFileRead(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")\n", fp
, buf
,
1301 CUPS_LLCAST bytes
));
1304 * Range check input...
1307 if (!fp
|| !buf
|| bytes
< 0 || (fp
->mode
!= 'r' && fp
->mode
!= 's'))
1314 * Loop until all bytes are read...
1320 if (fp
->ptr
>= fp
->end
)
1321 if (cups_fill(fp
) <= 0)
1323 DEBUG_printf(("cupsFileRead: cups_fill() returned -1, total=" CUPS_LLFMT
"\n",
1324 CUPS_LLCAST total
));
1327 return ((ssize_t
)total
);
1332 count
= (ssize_t
)(fp
->end
- fp
->ptr
);
1333 if (count
> (ssize_t
)bytes
)
1334 count
= (ssize_t
)bytes
;
1336 memcpy(buf
, fp
->ptr
, count
);
1340 DEBUG_printf(("cupsFileRead: pos=" CUPS_LLFMT
"\n", CUPS_LLCAST fp
->pos
));
1343 * Update the counts for the last read...
1352 * Return the total number of bytes read...
1355 DEBUG_printf(("cupsFileRead: total=%d\n", (int)total
));
1357 return ((ssize_t
)total
);
1362 * 'cupsFileRewind()' - Set the current file position to the beginning of the
1368 off_t
/* O - New file position or -1 on error */
1369 cupsFileRewind(cups_file_t
*fp
) /* I - CUPS file */
1372 * Range check input...
1375 DEBUG_printf(("cupsFileRewind(fp=%p)\n", fp
));
1376 DEBUG_printf(("cupsFileRewind: pos=" CUPS_LLFMT
"\n", CUPS_LLCAST fp
->pos
));
1378 if (!fp
|| fp
->mode
!= 'r')
1382 * Handle special cases...
1385 if (fp
->bufpos
== 0)
1388 * No seeking necessary...
1399 DEBUG_printf(("cupsFileRewind: pos=" CUPS_LLFMT
"\n", CUPS_LLCAST fp
->pos
));
1405 * Otherwise, seek in the file and cleanup any compression buffers...
1411 inflateEnd(&fp
->stream
);
1414 #endif /* HAVE_LIBZ */
1416 if (lseek(fp
->fd
, 0, SEEK_SET
))
1418 DEBUG_printf(("cupsFileRewind: lseek failed: %s\n", strerror(errno
)));
1428 DEBUG_printf(("cupsFileRewind: pos=" CUPS_LLFMT
"\n", CUPS_LLCAST fp
->pos
));
1435 * 'cupsFileSeek()' - Seek in a file.
1440 off_t
/* O - New file position or -1 on error */
1441 cupsFileSeek(cups_file_t
*fp
, /* I - CUPS file */
1442 off_t pos
) /* I - Position in file */
1444 ssize_t bytes
; /* Number bytes in buffer */
1447 DEBUG_printf(("cupsFileSeek(fp=%p, pos=" CUPS_LLFMT
")\n", fp
,
1449 DEBUG_printf(("cupsFileSeek: fp->pos=" CUPS_LLFMT
"\n", CUPS_LLCAST fp
->pos
));
1450 DEBUG_printf(("cupsFileSeek: fp->ptr=%p, fp->end=%p\n", fp
->ptr
, fp
->end
));
1453 * Range check input...
1456 if (!fp
|| pos
< 0 || fp
->mode
!= 'r')
1460 * Handle special cases...
1464 return (cupsFileRewind(fp
));
1468 bytes
= (ssize_t
)(fp
->end
- fp
->buf
);
1470 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1473 * No seeking necessary...
1477 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1485 if (!fp
->compressed
&& !fp
->ptr
)
1488 * Preload a buffer to determine whether the file is compressed...
1491 if (cups_fill(fp
) < 0)
1494 #endif /* HAVE_LIBZ */
1497 * Seek forwards or backwards...
1502 DEBUG_printf(("cupsFileSeek: bytes=" CUPS_LLFMT
"\n", CUPS_LLCAST bytes
));
1504 if (pos
< fp
->bufpos
)
1507 * Need to seek backwards...
1510 DEBUG_puts("cupsFileSeek: SEEK BACKWARDS");
1515 inflateEnd(&fp
->stream
);
1517 lseek(fp
->fd
, 0, SEEK_SET
);
1523 while ((bytes
= cups_fill(fp
)) > 0)
1524 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1530 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1534 #endif /* HAVE_LIBZ */
1536 fp
->bufpos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1537 fp
->pos
= fp
->bufpos
;
1541 DEBUG_printf(("cupsFileSeek: lseek() returned " CUPS_LLFMT
"...\n",
1542 CUPS_LLCAST fp
->pos
));
1548 * Need to seek forwards...
1551 DEBUG_puts("cupsFileSeek: SEEK FORWARDS");
1556 while ((bytes
= cups_fill(fp
)) > 0)
1558 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1565 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1569 #endif /* HAVE_LIBZ */
1571 fp
->bufpos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1572 fp
->pos
= fp
->bufpos
;
1576 DEBUG_printf(("cupsFileSeek: lseek() returned " CUPS_LLFMT
"...\n",
1577 CUPS_LLCAST fp
->pos
));
1581 DEBUG_printf(("cupsFileSeek: pos=" CUPS_LLFMT
"\n", CUPS_LLCAST fp
->pos
));
1588 * 'cupsFileStderr()' - Return a CUPS file associated with stderr.
1593 cups_file_t
* /* O - CUPS file */
1594 cupsFileStderr(void)
1596 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1600 * Open file descriptor 2 as needed...
1603 if (!cg
->stdio_files
[2])
1606 * Flush any pending output on the stdio file...
1612 * Open file descriptor 2...
1615 if ((cg
->stdio_files
[2] = cupsFileOpenFd(2, "w")) != NULL
)
1616 cg
->stdio_files
[2]->is_stdio
= 1;
1619 return (cg
->stdio_files
[2]);
1624 * 'cupsFileStdin()' - Return a CUPS file associated with stdin.
1629 cups_file_t
* /* O - CUPS file */
1632 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1636 * Open file descriptor 0 as needed...
1639 if (!cg
->stdio_files
[0])
1642 * Open file descriptor 0...
1645 if ((cg
->stdio_files
[0] = cupsFileOpenFd(0, "r")) != NULL
)
1646 cg
->stdio_files
[0]->is_stdio
= 1;
1649 return (cg
->stdio_files
[0]);
1654 * 'cupsFileStdout()' - Return a CUPS file associated with stdout.
1659 cups_file_t
* /* O - CUPS file */
1660 cupsFileStdout(void)
1662 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1666 * Open file descriptor 1 as needed...
1669 if (!cg
->stdio_files
[1])
1672 * Flush any pending output on the stdio file...
1678 * Open file descriptor 1...
1681 if ((cg
->stdio_files
[1] = cupsFileOpenFd(1, "w")) != NULL
)
1682 cg
->stdio_files
[1]->is_stdio
= 1;
1685 return (cg
->stdio_files
[1]);
1690 * 'cupsFileTell()' - Return the current file position.
1695 off_t
/* O - File position */
1696 cupsFileTell(cups_file_t
*fp
) /* I - CUPS file */
1698 DEBUG_printf(("cupsFileTell(fp=%p)\n", fp
));
1699 DEBUG_printf(("cupsFileTell: pos=" CUPS_LLFMT
"\n", CUPS_LLCAST (fp
? fp
->pos
: -1)));
1701 return (fp
? fp
->pos
: 0);
1706 * 'cupsFileUnlock()' - Unlock access to a file.
1711 int /* O - 0 on success, -1 on error */
1712 cupsFileUnlock(cups_file_t
*fp
) /* I - CUPS file */
1718 DEBUG_printf(("cupsFileUnlock(fp=%p)\n", fp
));
1720 if (!fp
|| fp
->mode
== 's')
1728 return (locking(fp
->fd
, _LK_UNLCK
, 0));
1730 return (lockf(fp
->fd
, F_ULOCK
, 0));
1736 * 'cupsFileWrite()' - Write to a file.
1741 ssize_t
/* O - Number of bytes written or -1 on error */
1742 cupsFileWrite(cups_file_t
*fp
, /* I - CUPS file */
1743 const char *buf
, /* I - Buffer */
1744 size_t bytes
) /* I - Number of bytes to write */
1747 * Range check input...
1750 DEBUG_printf(("cupsFileWrite(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")\n",
1751 fp
, buf
, CUPS_LLCAST bytes
));
1753 if (!fp
|| !buf
|| bytes
< 0 || (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1760 * Write the buffer...
1763 if (fp
->mode
== 's')
1765 if (cups_write(fp
, buf
, bytes
) < 0)
1768 fp
->pos
+= (off_t
)bytes
;
1770 DEBUG_printf(("cupsFileWrite: pos=" CUPS_LLFMT
"\n", CUPS_LLCAST fp
->pos
));
1772 return ((ssize_t
)bytes
);
1775 if ((fp
->ptr
+ bytes
) > fp
->end
)
1776 if (cupsFileFlush(fp
))
1779 fp
->pos
+= (off_t
)bytes
;
1781 DEBUG_printf(("cupsFileWrite: pos=" CUPS_LLFMT
"\n", CUPS_LLCAST fp
->pos
));
1783 if (bytes
> sizeof(fp
->buf
))
1787 return (cups_compress(fp
, buf
, bytes
));
1789 #endif /* HAVE_LIBZ */
1790 return (cups_write(fp
, buf
, bytes
));
1794 memcpy(fp
->ptr
, buf
, bytes
);
1796 return ((ssize_t
)bytes
);
1803 * 'cups_compress()' - Compress a buffer of data...
1806 static ssize_t
/* O - Number of bytes written or -1 */
1807 cups_compress(cups_file_t
*fp
, /* I - CUPS file */
1808 const char *buf
, /* I - Buffer */
1809 size_t bytes
) /* I - Number bytes */
1811 DEBUG_printf(("cups_compress(fp=%p, buf=%p, bytes=" CUPS_LLFMT
"\n", fp
, buf
,
1812 CUPS_LLCAST bytes
));
1818 fp
->crc
= crc32(fp
->crc
, (const Bytef
*)buf
, bytes
);
1821 * Deflate the bytes...
1824 fp
->stream
.next_in
= (Bytef
*)buf
;
1825 fp
->stream
.avail_in
= bytes
;
1827 while (fp
->stream
.avail_in
> 0)
1830 * Flush the current buffer...
1833 DEBUG_printf(("cups_compress: avail_in=%d, avail_out=%d\n",
1834 fp
->stream
.avail_in
, fp
->stream
.avail_out
));
1836 if (fp
->stream
.avail_out
< (int)(sizeof(fp
->cbuf
) / 8))
1838 if (cups_write(fp
, (char *)fp
->cbuf
, fp
->stream
.next_out
- fp
->cbuf
) < 0)
1841 fp
->stream
.next_out
= fp
->cbuf
;
1842 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
1845 deflate(&(fp
->stream
), Z_NO_FLUSH
);
1850 #endif /* HAVE_LIBZ */
1854 * 'cups_fill()' - Fill the input buffer...
1857 static ssize_t
/* O - Number of bytes or -1 */
1858 cups_fill(cups_file_t
*fp
) /* I - CUPS file */
1860 ssize_t bytes
; /* Number of bytes read */
1862 int status
; /* Decompression status */
1863 const unsigned char *ptr
, /* Pointer into buffer */
1864 *end
; /* End of buffer */
1865 #endif /* HAVE_LIBZ */
1868 DEBUG_printf(("cups_fill(fp=%p)\n", fp
));
1869 DEBUG_printf(("cups_fill: fp->ptr=%p, fp->end=%p, fp->buf=%p, "
1870 "fp->bufpos=" CUPS_LLFMT
", fp->eof=%d\n",
1871 fp
->ptr
, fp
->end
, fp
->buf
, CUPS_LLCAST fp
->bufpos
, fp
->eof
));
1873 if (fp
->ptr
&& fp
->end
)
1874 fp
->bufpos
+= fp
->end
- fp
->buf
;
1877 DEBUG_printf(("cups_fill: fp->compressed=%d\n", fp
->compressed
));
1879 while (!fp
->ptr
|| fp
->compressed
)
1882 * Check to see if we have read any data yet; if not, see if we have a
1883 * compressed file...
1889 * Reset the file position in case we are seeking...
1895 * Read the first bytes in the file to determine if we have a gzip'd
1899 if ((bytes
= cups_read(fp
, (char *)fp
->buf
, sizeof(fp
->buf
))) < 0)
1902 * Can't read from file!
1905 DEBUG_printf(("cups_fill: cups_read() returned " CUPS_LLFMT
"!\n",
1906 CUPS_LLCAST bytes
));
1911 if (bytes
< 10 || fp
->buf
[0] != 0x1f ||
1912 (fp
->buf
[1] & 255) != 0x8b ||
1913 fp
->buf
[2] != 8 || (fp
->buf
[3] & 0xe0) != 0)
1916 * Not a gzip'd file!
1920 fp
->end
= fp
->buf
+ bytes
;
1922 DEBUG_printf(("cups_fill: Returning " CUPS_LLFMT
"!\n",
1923 CUPS_LLCAST bytes
));
1929 * Parse header junk: extra data, original name, and comment...
1932 ptr
= (unsigned char *)fp
->buf
+ 10;
1933 end
= (unsigned char *)fp
->buf
+ bytes
;
1935 if (fp
->buf
[3] & 0x04)
1938 * Skip extra data...
1941 if ((ptr
+ 2) > end
)
1944 * Can't read from file!
1950 bytes
= ((unsigned char)ptr
[1] << 8) | (unsigned char)ptr
[0];
1956 * Can't read from file!
1963 if (fp
->buf
[3] & 0x08)
1966 * Skip original name data...
1969 while (ptr
< end
&& *ptr
)
1977 * Can't read from file!
1984 if (fp
->buf
[3] & 0x10)
1987 * Skip comment data...
1990 while (ptr
< end
&& *ptr
)
1998 * Can't read from file!
2005 if (fp
->buf
[3] & 0x02)
2008 * Skip header CRC data...
2016 * Can't read from file!
2024 * Copy the flate-compressed data to the compression buffer...
2027 if ((bytes
= end
- ptr
) > 0)
2028 memcpy(fp
->cbuf
, ptr
, bytes
);
2031 * Setup the decompressor data...
2034 fp
->stream
.zalloc
= (alloc_func
)0;
2035 fp
->stream
.zfree
= (free_func
)0;
2036 fp
->stream
.opaque
= (voidpf
)0;
2037 fp
->stream
.next_in
= (Bytef
*)fp
->cbuf
;
2038 fp
->stream
.next_out
= NULL
;
2039 fp
->stream
.avail_in
= bytes
;
2040 fp
->stream
.avail_out
= 0;
2041 fp
->crc
= crc32(0L, Z_NULL
, 0);
2043 if (inflateInit2(&(fp
->stream
), -15) != Z_OK
)
2052 * If we have reached end-of-file, return immediately...
2059 * Fill the decompression buffer as needed...
2062 if (fp
->stream
.avail_in
== 0)
2064 if ((bytes
= cups_read(fp
, (char *)fp
->cbuf
, sizeof(fp
->cbuf
))) <= 0)
2067 fp
->stream
.next_in
= fp
->cbuf
;
2068 fp
->stream
.avail_in
= bytes
;
2072 * Decompress data from the buffer...
2075 fp
->stream
.next_out
= (Bytef
*)fp
->buf
;
2076 fp
->stream
.avail_out
= sizeof(fp
->buf
);
2078 status
= inflate(&(fp
->stream
), Z_NO_FLUSH
);
2080 if (fp
->stream
.next_out
> (Bytef
*)fp
->buf
)
2081 fp
->crc
= crc32(fp
->crc
, (Bytef
*)fp
->buf
,
2082 fp
->stream
.next_out
- (Bytef
*)fp
->buf
);
2084 if (status
== Z_STREAM_END
)
2087 * Read the CRC and length...
2090 unsigned char trailer
[8]; /* Trailer bytes */
2091 uLong tcrc
; /* Trailer CRC */
2094 if (read(fp
->fd
, trailer
, sizeof(trailer
)) < sizeof(trailer
))
2097 * Can't get it, so mark end-of-file...
2104 tcrc
= (((((trailer
[3] << 8) | trailer
[2]) << 8) | trailer
[1]) << 8) |
2107 if (tcrc
!= fp
->crc
)
2110 * Bad CRC, mark end-of-file...
2113 DEBUG_printf(("cups_fill: tcrc=%08x, fp->crc=%08x\n",
2114 (unsigned int)tcrc
, (unsigned int)fp
->crc
));
2122 * Otherwise, reset the compressed flag so that we re-read the
2130 bytes
= sizeof(fp
->buf
) - fp
->stream
.avail_out
;
2133 * Return the decompressed data...
2137 fp
->end
= fp
->buf
+ bytes
;
2143 #endif /* HAVE_LIBZ */
2146 * Read a buffer's full of data...
2149 if ((bytes
= cups_read(fp
, fp
->buf
, sizeof(fp
->buf
))) <= 0)
2152 * Can't read from file!
2163 * Return the bytes we read...
2168 fp
->end
= fp
->buf
+ bytes
;
2175 * 'cups_read()' - Read from a file descriptor.
2178 static ssize_t
/* O - Number of bytes read or -1 */
2179 cups_read(cups_file_t
*fp
, /* I - CUPS file */
2180 char *buf
, /* I - Buffer */
2181 size_t bytes
) /* I - Number bytes */
2183 ssize_t total
; /* Total bytes read */
2186 DEBUG_printf(("cups_read(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")\n", fp
, buf
,
2187 CUPS_LLCAST bytes
));
2190 * Loop until we read at least 0 bytes...
2196 if (fp
->mode
== 's')
2197 total
= (ssize_t
)recv(fp
->fd
, buf
, (unsigned)bytes
, 0);
2199 total
= (ssize_t
)read(fp
->fd
, buf
, (unsigned)bytes
);
2201 if (fp
->mode
== 's')
2202 total
= recv(fp
->fd
, buf
, bytes
, 0);
2204 total
= read(fp
->fd
, buf
, bytes
);
2207 DEBUG_printf(("cups_read: total=" CUPS_LLFMT
"\n", CUPS_LLCAST total
));
2213 * Reads can be interrupted by signals and unavailable resources...
2216 if (errno
== EAGAIN
|| errno
== EINTR
)
2223 * Return the total number of bytes read...
2231 * 'cups_write()' - Write to a file descriptor.
2234 static ssize_t
/* O - Number of bytes written or -1 */
2235 cups_write(cups_file_t
*fp
, /* I - CUPS file */
2236 const char *buf
, /* I - Buffer */
2237 size_t bytes
) /* I - Number bytes */
2239 size_t total
; /* Total bytes written */
2240 ssize_t count
; /* Count this time */
2243 DEBUG_printf(("cups_write(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")\n", fp
, buf
,
2244 CUPS_LLCAST bytes
));
2247 * Loop until all bytes are written...
2254 if (fp
->mode
== 's')
2255 count
= (ssize_t
)send(fp
->fd
, buf
, (unsigned)bytes
, 0);
2257 count
= (ssize_t
)write(fp
->fd
, buf
, (unsigned)bytes
);
2259 if (fp
->mode
== 's')
2260 count
= send(fp
->fd
, buf
, bytes
, 0);
2262 count
= write(fp
->fd
, buf
, bytes
);
2265 DEBUG_printf(("cups_write: count=" CUPS_LLFMT
"\n", CUPS_LLCAST count
));
2270 * Writes can be interrupted by signals and unavailable resources...
2273 if (errno
== EAGAIN
|| errno
== EINTR
)
2280 * Update the counts for the last write call...
2289 * Return the total number of bytes written...
2292 return ((ssize_t
)total
);
2297 * End of "$Id: file.c 6962 2007-09-17 20:35:47Z mike $".