]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
75bd9771 | 2 | * "$Id: file.c 7672 2008-06-18 22:03:02Z mike $" |
ef416fc2 | 3 | * |
4 | * File functions for the Common UNIX Printing System (CUPS). | |
5 | * | |
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. | |
10 | * | |
5a738aea | 11 | * Copyright 2007-2008 by Apple Inc. |
b86bc4cf | 12 | * Copyright 1997-2007 by Easy Software Products, all rights reserved. |
ef416fc2 | 13 | * |
14 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 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/". | |
ef416fc2 | 19 | * |
20 | * Contents: | |
21 | * | |
22 | * cupsFileClose() - Close a CUPS file. | |
23 | * cupsFileCompression() - Return whether a file is compressed. | |
24 | * cupsFileEOF() - Return the end-of-file status. | |
fa73b229 | 25 | * cupsFileFind() - Find a file using the specified path. |
ef416fc2 | 26 | * cupsFileFlush() - Flush pending output. |
27 | * cupsFileGetChar() - Get a single character from a file. | |
28 | * cupsFileGetConf() - Get a line from a configuration file... | |
5a738aea MS |
29 | * cupsFileGetLine() - Get a CR and/or LF-terminated line that may contain |
30 | * binary data. | |
ef416fc2 | 31 | * cupsFileGets() - Get a CR and/or LF-terminated line. |
32 | * cupsFileLock() - Temporarily lock access to a file. | |
80ca4592 | 33 | * cupsFileNumber() - Return the file descriptor associated with a CUPS |
34 | * file. | |
ef416fc2 | 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. | |
5a738aea MS |
42 | * cupsFileRewind() - Set the current file position to the beginning of |
43 | * the file. | |
ef416fc2 | 44 | * cupsFileSeek() - Seek in a file. |
80ca4592 | 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. | |
ef416fc2 | 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. | |
55 | */ | |
56 | ||
57 | /* | |
58 | * Include necessary headers... | |
59 | */ | |
60 | ||
61 | #include <stdio.h> | |
62 | #include <stdlib.h> | |
63 | #include <stdarg.h> | |
ef416fc2 | 64 | #include <errno.h> |
ef416fc2 | 65 | #include <sys/types.h> |
66 | #include <fcntl.h> | |
80ca4592 | 67 | #include "http-private.h" |
68 | #include "globals.h" | |
69 | #include "debug.h" | |
ef416fc2 | 70 | |
ef416fc2 | 71 | #ifdef HAVE_LIBZ |
72 | # include <zlib.h> | |
73 | #endif /* HAVE_LIBZ */ | |
74 | #ifdef WIN32 | |
75 | # include <io.h> | |
76 | # include <sys/locking.h> | |
77 | #endif /* WIN32 */ | |
78 | ||
79 | ||
80 | /* | |
81 | * Some operating systems support large files via open flag O_LARGEFILE... | |
82 | */ | |
83 | ||
84 | #ifndef O_LARGEFILE | |
85 | # define O_LARGEFILE 0 | |
86 | #endif /* !O_LARGEFILE */ | |
87 | ||
88 | ||
b86bc4cf | 89 | /* |
90 | * Some operating systems don't define O_BINARY, which is used by Microsoft | |
91 | * and IBM to flag binary files... | |
92 | */ | |
93 | ||
94 | #ifndef O_BINARY | |
95 | # define O_BINARY 0 | |
96 | #endif /* !O_BINARY */ | |
97 | ||
98 | ||
ef416fc2 | 99 | /* |
100 | * Types and structures... | |
101 | */ | |
102 | ||
103 | struct _cups_file_s /**** CUPS file structure... ****/ | |
104 | ||
105 | { | |
106 | int fd; /* File descriptor */ | |
107 | char mode, /* Mode ('r' or 'w') */ | |
108 | compressed, /* Compression used? */ | |
80ca4592 | 109 | is_stdio, /* stdin/out/err? */ |
ef416fc2 | 110 | eof, /* End of file? */ |
fa73b229 | 111 | buf[4096], /* Buffer */ |
ef416fc2 | 112 | *ptr, /* Pointer into buffer */ |
113 | *end; /* End of buffer data */ | |
634763e8 MS |
114 | off_t pos, /* Position in file */ |
115 | bufpos; /* File position for start of buffer */ | |
ef416fc2 | 116 | |
117 | #ifdef HAVE_LIBZ | |
118 | z_stream stream; /* (De)compression stream */ | |
fa73b229 | 119 | Bytef cbuf[4096]; /* (De)compression buffer */ |
ef416fc2 | 120 | uLong crc; /* (De)compression CRC */ |
121 | #endif /* HAVE_LIBZ */ | |
75bd9771 MS |
122 | |
123 | char *printf_buffer; /* cupsFilePrintf buffer */ | |
124 | size_t printf_size; /* Size of cupsFilePrintf buffer */ | |
ef416fc2 | 125 | }; |
126 | ||
127 | ||
128 | /* | |
129 | * Local functions... | |
130 | */ | |
131 | ||
132 | #ifdef HAVE_LIBZ | |
133 | static ssize_t cups_compress(cups_file_t *fp, const char *buf, size_t bytes); | |
134 | #endif /* HAVE_LIBZ */ | |
135 | static ssize_t cups_fill(cups_file_t *fp); | |
136 | static ssize_t cups_read(cups_file_t *fp, char *buf, size_t bytes); | |
137 | static ssize_t cups_write(cups_file_t *fp, const char *buf, size_t bytes); | |
138 | ||
139 | ||
140 | /* | |
141 | * 'cupsFileClose()' - Close a CUPS file. | |
5a738aea | 142 | * |
426c6a59 | 143 | * @since CUPS 1.2/Mac OS X 10.5@ |
ef416fc2 | 144 | */ |
145 | ||
146 | int /* O - 0 on success, -1 on error */ | |
147 | cupsFileClose(cups_file_t *fp) /* I - CUPS file */ | |
148 | { | |
149 | int fd; /* File descriptor */ | |
150 | char mode; /* Open mode */ | |
151 | int status; /* Return status */ | |
80ca4592 | 152 | int is_stdio; /* Is a stdio file? */ |
ef416fc2 | 153 | |
154 | ||
155 | DEBUG_printf(("cupsFileClose(fp=%p)\n", fp)); | |
156 | ||
157 | /* | |
158 | * Range check... | |
159 | */ | |
160 | ||
161 | if (!fp) | |
162 | return (-1); | |
163 | ||
164 | /* | |
165 | * Flush pending write data... | |
166 | */ | |
167 | ||
168 | if (fp->mode == 'w') | |
169 | status = cupsFileFlush(fp); | |
170 | else | |
171 | status = 0; | |
172 | ||
173 | #ifdef HAVE_LIBZ | |
174 | if (fp->compressed && status >= 0) | |
175 | { | |
176 | if (fp->mode == 'r') | |
177 | { | |
178 | /* | |
179 | * Free decompression data... | |
180 | */ | |
181 | ||
182 | inflateEnd(&fp->stream); | |
183 | } | |
184 | else | |
185 | { | |
186 | /* | |
187 | * Flush any remaining compressed data... | |
188 | */ | |
189 | ||
190 | unsigned char trailer[8]; /* Trailer CRC and length */ | |
191 | int done; /* Done writing... */ | |
192 | ||
193 | ||
194 | fp->stream.avail_in = 0; | |
195 | ||
196 | for (done = 0;;) | |
197 | { | |
198 | if (fp->stream.next_out > fp->cbuf) | |
199 | { | |
200 | if (cups_write(fp, (char *)fp->cbuf, | |
201 | fp->stream.next_out - fp->cbuf) < 0) | |
202 | status = -1; | |
203 | ||
204 | fp->stream.next_out = fp->cbuf; | |
205 | fp->stream.avail_out = sizeof(fp->cbuf); | |
206 | } | |
207 | ||
208 | if (done || status < 0) | |
209 | break; | |
210 | ||
211 | done = deflate(&fp->stream, Z_FINISH) == Z_STREAM_END && | |
212 | fp->stream.next_out == fp->cbuf; | |
213 | } | |
214 | ||
215 | /* | |
216 | * Write the CRC and length... | |
217 | */ | |
218 | ||
219 | trailer[0] = fp->crc; | |
220 | trailer[1] = fp->crc >> 8; | |
221 | trailer[2] = fp->crc >> 16; | |
222 | trailer[3] = fp->crc >> 24; | |
223 | trailer[4] = fp->pos; | |
224 | trailer[5] = fp->pos >> 8; | |
225 | trailer[6] = fp->pos >> 16; | |
226 | trailer[7] = fp->pos >> 24; | |
227 | ||
228 | if (cups_write(fp, (char *)trailer, 8) < 0) | |
229 | status = -1; | |
230 | ||
231 | /* | |
232 | * Free all memory used by the compression stream... | |
233 | */ | |
234 | ||
235 | deflateEnd(&(fp->stream)); | |
236 | } | |
237 | } | |
238 | #endif /* HAVE_LIBZ */ | |
239 | ||
240 | /* | |
241 | * Save the file descriptor we used and free memory... | |
242 | */ | |
243 | ||
80ca4592 | 244 | fd = fp->fd; |
245 | mode = fp->mode; | |
246 | is_stdio = fp->is_stdio; | |
ef416fc2 | 247 | |
75bd9771 MS |
248 | if (fp->printf_buffer) |
249 | free(fp->printf_buffer); | |
250 | ||
ef416fc2 | 251 | free(fp); |
252 | ||
253 | /* | |
254 | * Close the file, returning the close status... | |
255 | */ | |
256 | ||
257 | if (mode == 's') | |
258 | { | |
259 | if (closesocket(fd) < 0) | |
260 | status = -1; | |
261 | } | |
80ca4592 | 262 | else if (!is_stdio) |
ef416fc2 | 263 | { |
264 | if (close(fd) < 0) | |
265 | status = -1; | |
266 | } | |
267 | ||
268 | return (status); | |
269 | } | |
270 | ||
271 | ||
272 | /* | |
273 | * 'cupsFileCompression()' - Return whether a file is compressed. | |
5a738aea | 274 | * |
426c6a59 | 275 | * @since CUPS 1.2/Mac OS X 10.5@ |
ef416fc2 | 276 | */ |
277 | ||
5a738aea | 278 | int /* O - @code CUPS_FILE_NONE@ or @code CUPS_FILE_GZIP@ */ |
ef416fc2 | 279 | cupsFileCompression(cups_file_t *fp) /* I - CUPS file */ |
280 | { | |
80ca4592 | 281 | return (fp ? fp->compressed : CUPS_FILE_NONE); |
ef416fc2 | 282 | } |
283 | ||
284 | ||
285 | /* | |
286 | * 'cupsFileEOF()' - Return the end-of-file status. | |
5a738aea | 287 | * |
426c6a59 | 288 | * @since CUPS 1.2/Mac OS X 10.5@ |
ef416fc2 | 289 | */ |
290 | ||
5a738aea | 291 | int /* O - 1 on end of file, 0 otherwise */ |
ef416fc2 | 292 | cupsFileEOF(cups_file_t *fp) /* I - CUPS file */ |
293 | { | |
80ca4592 | 294 | return (fp ? fp->eof : 1); |
ef416fc2 | 295 | } |
296 | ||
297 | ||
fa73b229 | 298 | /* |
299 | * 'cupsFileFind()' - Find a file using the specified path. | |
300 | * | |
301 | * This function allows the paths in the path string to be separated by | |
302 | * colons (UNIX standard) or semicolons (Windows standard) and stores the | |
303 | * result in the buffer supplied. If the file cannot be found in any of | |
5a738aea MS |
304 | * the supplied paths, @code NULL@ is returned. A @code NULL@ path only |
305 | * matches the current directory. | |
306 | * | |
426c6a59 | 307 | * @since CUPS 1.2/Mac OS X 10.5@ |
fa73b229 | 308 | */ |
309 | ||
5a738aea | 310 | const char * /* O - Full path to file or @code NULL@ if not found */ |
fa73b229 | 311 | cupsFileFind(const char *filename, /* I - File to find */ |
312 | const char *path, /* I - Colon/semicolon-separated path */ | |
4400e98d | 313 | int executable, /* I - 1 = executable files, 0 = any file/dir */ |
314 | char *buffer, /* I - Filename buffer */ | |
fa73b229 | 315 | int bufsize) /* I - Size of filename buffer */ |
316 | { | |
317 | char *bufptr, /* Current position in buffer */ | |
318 | *bufend; /* End of buffer */ | |
319 | ||
320 | ||
321 | /* | |
322 | * Range check input... | |
323 | */ | |
324 | ||
325 | if (!filename || !buffer || bufsize < 2) | |
326 | return (NULL); | |
327 | ||
328 | if (!path) | |
329 | { | |
330 | /* | |
331 | * No path, so check current directory... | |
332 | */ | |
333 | ||
334 | if (!access(filename, 0)) | |
335 | { | |
336 | strlcpy(buffer, filename, bufsize); | |
337 | return (buffer); | |
338 | } | |
339 | else | |
340 | return (NULL); | |
341 | } | |
342 | ||
343 | /* | |
344 | * Now check each path and return the first match... | |
345 | */ | |
346 | ||
347 | bufend = buffer + bufsize - 1; | |
348 | bufptr = buffer; | |
349 | ||
350 | while (*path) | |
351 | { | |
b86bc4cf | 352 | #ifdef WIN32 |
353 | if (*path == ';' || (*path == ':' && ((bufptr - buffer) > 1 || !isalpha(buffer[0] & 255)))) | |
354 | #else | |
fa73b229 | 355 | if (*path == ';' || *path == ':') |
b86bc4cf | 356 | #endif /* WIN32 */ |
fa73b229 | 357 | { |
358 | if (bufptr > buffer && bufptr[-1] != '/' && bufptr < bufend) | |
359 | *bufptr++ = '/'; | |
360 | ||
361 | strlcpy(bufptr, filename, bufend - bufptr); | |
362 | ||
4400e98d | 363 | #ifdef WIN32 |
fa73b229 | 364 | if (!access(buffer, 0)) |
4400e98d | 365 | #else |
366 | if (!access(buffer, executable ? X_OK : 0)) | |
367 | #endif /* WIN32 */ | |
b86bc4cf | 368 | { |
369 | DEBUG_printf(("cupsFileFind: Returning \"%s\"\n", buffer)); | |
fa73b229 | 370 | return (buffer); |
b86bc4cf | 371 | } |
fa73b229 | 372 | |
373 | bufptr = buffer; | |
374 | } | |
375 | else if (bufptr < bufend) | |
376 | *bufptr++ = *path; | |
377 | ||
378 | path ++; | |
379 | } | |
380 | ||
381 | /* | |
382 | * Check the last path... | |
383 | */ | |
384 | ||
385 | if (bufptr > buffer && bufptr[-1] != '/' && bufptr < bufend) | |
386 | *bufptr++ = '/'; | |
387 | ||
388 | strlcpy(bufptr, filename, bufend - bufptr); | |
389 | ||
390 | if (!access(buffer, 0)) | |
b86bc4cf | 391 | { |
392 | DEBUG_printf(("cupsFileFind: Returning \"%s\"\n", buffer)); | |
fa73b229 | 393 | return (buffer); |
b86bc4cf | 394 | } |
fa73b229 | 395 | else |
b86bc4cf | 396 | { |
397 | DEBUG_puts("cupsFileFind: Returning NULL"); | |
fa73b229 | 398 | return (NULL); |
b86bc4cf | 399 | } |
fa73b229 | 400 | } |
401 | ||
402 | ||
ef416fc2 | 403 | /* |
404 | * 'cupsFileFlush()' - Flush pending output. | |
5a738aea | 405 | * |
426c6a59 | 406 | * @since CUPS 1.2/Mac OS X 10.5@ |
ef416fc2 | 407 | */ |
408 | ||
409 | int /* O - 0 on success, -1 on error */ | |
410 | cupsFileFlush(cups_file_t *fp) /* I - CUPS file */ | |
411 | { | |
2abf387c | 412 | ssize_t bytes; /* Bytes to write */ |
ef416fc2 | 413 | |
414 | ||
415 | DEBUG_printf(("cupsFileFlush(fp=%p)\n", fp)); | |
416 | ||
417 | /* | |
418 | * Range check input... | |
419 | */ | |
420 | ||
421 | if (!fp || fp->mode != 'w') | |
422 | { | |
634763e8 | 423 | DEBUG_puts("cupsFileFlush: Attempt to flush a read-only file..."); |
ef416fc2 | 424 | return (-1); |
425 | } | |
426 | ||
b86bc4cf | 427 | bytes = (ssize_t)(fp->ptr - fp->buf); |
ef416fc2 | 428 | |
634763e8 MS |
429 | DEBUG_printf(("cupsFileFlush: Flushing " CUPS_LLFMT " bytes...\n", |
430 | CUPS_LLCAST bytes)); | |
ecdc0628 | 431 | |
ef416fc2 | 432 | if (bytes > 0) |
433 | { | |
434 | #ifdef HAVE_LIBZ | |
435 | if (fp->compressed) | |
436 | bytes = cups_compress(fp, fp->buf, bytes); | |
437 | else | |
438 | #endif /* HAVE_LIBZ */ | |
439 | bytes = cups_write(fp, fp->buf, bytes); | |
440 | ||
441 | if (bytes < 0) | |
442 | return (-1); | |
443 | ||
444 | fp->ptr = fp->buf; | |
445 | } | |
446 | ||
447 | return (0); | |
448 | } | |
449 | ||
450 | ||
451 | /* | |
452 | * 'cupsFileGetChar()' - Get a single character from a file. | |
5a738aea | 453 | * |
426c6a59 | 454 | * @since CUPS 1.2/Mac OS X 10.5@ |
ef416fc2 | 455 | */ |
456 | ||
5a738aea | 457 | int /* O - Character or -1 on end of file */ |
ef416fc2 | 458 | cupsFileGetChar(cups_file_t *fp) /* I - CUPS file */ |
459 | { | |
460 | /* | |
461 | * Range check input... | |
462 | */ | |
463 | ||
634763e8 MS |
464 | DEBUG_printf(("cupsFileGetChar(fp=%p)\n", fp)); |
465 | ||
ef416fc2 | 466 | if (!fp || (fp->mode != 'r' && fp->mode != 's')) |
b86bc4cf | 467 | { |
468 | DEBUG_puts("cupsFileGetChar: Bad arguments!"); | |
ef416fc2 | 469 | return (-1); |
b86bc4cf | 470 | } |
ef416fc2 | 471 | |
472 | /* | |
473 | * If the input buffer is empty, try to read more data... | |
474 | */ | |
475 | ||
476 | if (fp->ptr >= fp->end) | |
477 | if (cups_fill(fp) < 0) | |
b86bc4cf | 478 | { |
479 | DEBUG_puts("cupsFileGetChar: Unable to fill buffer!"); | |
ef416fc2 | 480 | return (-1); |
b86bc4cf | 481 | } |
ef416fc2 | 482 | |
483 | /* | |
484 | * Return the next character in the buffer... | |
485 | */ | |
486 | ||
b86bc4cf | 487 | DEBUG_printf(("cupsFileGetChar: Returning %d...\n", *(fp->ptr) & 255)); |
488 | ||
634763e8 MS |
489 | fp->pos ++; |
490 | ||
491 | DEBUG_printf(("cupsFileGetChar: pos=" CUPS_LLFMT "\n", CUPS_LLCAST fp->pos)); | |
492 | ||
ef416fc2 | 493 | return (*(fp->ptr)++ & 255); |
494 | } | |
495 | ||
496 | ||
497 | /* | |
498 | * 'cupsFileGetConf()' - Get a line from a configuration file... | |
5a738aea | 499 | * |
426c6a59 | 500 | * @since CUPS 1.2/Mac OS X 10.5@ |
ef416fc2 | 501 | */ |
502 | ||
5a738aea | 503 | char * /* O - Line read or @code NULL@ on end of file or error */ |
ef416fc2 | 504 | cupsFileGetConf(cups_file_t *fp, /* I - CUPS file */ |
505 | char *buf, /* O - String buffer */ | |
506 | size_t buflen, /* I - Size of string buffer */ | |
507 | char **value, /* O - Pointer to value */ | |
508 | int *linenum) /* IO - Current line number */ | |
509 | { | |
510 | char *ptr; /* Pointer into line */ | |
511 | ||
512 | ||
513 | /* | |
514 | * Range check input... | |
515 | */ | |
516 | ||
634763e8 MS |
517 | DEBUG_printf(("cupsFileGetConf(fp=%p, buf=%p, buflen=" CUPS_LLFMT |
518 | ", value=%p, linenum=%p)\n", fp, buf, CUPS_LLCAST buflen, | |
519 | value, linenum)); | |
520 | ||
ef416fc2 | 521 | if (!fp || (fp->mode != 'r' && fp->mode != 's') || |
522 | !buf || buflen < 2 || !value) | |
523 | { | |
524 | if (value) | |
525 | *value = NULL; | |
526 | ||
527 | return (NULL); | |
528 | } | |
529 | ||
530 | /* | |
531 | * Read the next non-comment line... | |
532 | */ | |
533 | ||
534 | *value = NULL; | |
f7deaa1a | 535 | |
ef416fc2 | 536 | while (cupsFileGets(fp, buf, buflen)) |
537 | { | |
538 | (*linenum) ++; | |
539 | ||
540 | /* | |
541 | * Strip any comments... | |
542 | */ | |
543 | ||
544 | if ((ptr = strchr(buf, '#')) != NULL) | |
545 | { | |
f7deaa1a | 546 | if (ptr > buf && ptr[-1] == '\\') |
ef416fc2 | 547 | { |
f7deaa1a | 548 | // Unquote the #... |
549 | _cups_strcpy(ptr - 1, ptr); | |
ef416fc2 | 550 | } |
f7deaa1a | 551 | else |
552 | { | |
553 | // Strip the comment and any trailing whitespace... | |
554 | while (ptr > buf) | |
555 | { | |
556 | if (!isspace(ptr[-1] & 255)) | |
557 | break; | |
558 | ||
559 | ptr --; | |
560 | } | |
ef416fc2 | 561 | |
f7deaa1a | 562 | *ptr = '\0'; |
563 | } | |
ef416fc2 | 564 | } |
565 | ||
566 | /* | |
567 | * Strip leading whitespace... | |
568 | */ | |
569 | ||
570 | for (ptr = buf; isspace(*ptr & 255); ptr ++); | |
571 | ||
572 | if (ptr > buf) | |
573 | _cups_strcpy(buf, ptr); | |
574 | ||
575 | /* | |
576 | * See if there is anything left... | |
577 | */ | |
578 | ||
579 | if (buf[0]) | |
580 | { | |
581 | /* | |
582 | * Yes, grab any value and return... | |
583 | */ | |
584 | ||
585 | for (ptr = buf; *ptr; ptr ++) | |
586 | if (isspace(*ptr & 255)) | |
587 | break; | |
588 | ||
589 | if (*ptr) | |
590 | { | |
591 | /* | |
592 | * Have a value, skip any other spaces... | |
593 | */ | |
594 | ||
595 | while (isspace(*ptr & 255)) | |
596 | *ptr++ = '\0'; | |
597 | ||
598 | if (*ptr) | |
599 | *value = ptr; | |
600 | ||
601 | /* | |
602 | * Strip trailing whitespace and > for lines that begin with <... | |
603 | */ | |
604 | ||
605 | ptr += strlen(ptr) - 1; | |
606 | ||
607 | if (buf[0] == '<' && *ptr == '>') | |
608 | *ptr-- = '\0'; | |
609 | else if (buf[0] == '<' && *ptr != '>') | |
610 | { | |
611 | /* | |
612 | * Syntax error... | |
613 | */ | |
614 | ||
615 | *value = NULL; | |
616 | return (buf); | |
617 | } | |
618 | ||
619 | while (ptr > *value && isspace(*ptr & 255)) | |
620 | *ptr-- = '\0'; | |
621 | } | |
622 | ||
623 | /* | |
624 | * Return the line... | |
625 | */ | |
626 | ||
627 | return (buf); | |
628 | } | |
629 | } | |
630 | ||
631 | return (NULL); | |
632 | } | |
633 | ||
634 | ||
80ca4592 | 635 | /* |
636 | * 'cupsFileGetLine()' - Get a CR and/or LF-terminated line that may | |
637 | * contain binary data. | |
638 | * | |
5a738aea MS |
639 | * This function differs from @link cupsFileGets@ in that the trailing CR |
640 | * and LF are preserved, as is any binary data on the line. The buffer is | |
641 | * nul-terminated, however you should use the returned length to determine | |
80ca4592 | 642 | * the number of bytes on the line. |
5a738aea | 643 | * |
426c6a59 | 644 | * @since CUPS 1.2/Mac OS X 10.5@ |
80ca4592 | 645 | */ |
646 | ||
5a738aea | 647 | size_t /* O - Number of bytes on line or 0 on end of file */ |
80ca4592 | 648 | cupsFileGetLine(cups_file_t *fp, /* I - File to read from */ |
649 | char *buf, /* I - Buffer */ | |
650 | size_t buflen) /* I - Size of buffer */ | |
651 | { | |
652 | int ch; /* Character from file */ | |
653 | char *ptr, /* Current position in line buffer */ | |
654 | *end; /* End of line buffer */ | |
655 | ||
656 | ||
657 | /* | |
658 | * Range check input... | |
659 | */ | |
660 | ||
634763e8 MS |
661 | DEBUG_printf(("cupsFileGetLine(fp=%p, buf=%p, buflen=" CUPS_LLFMT ")\n", |
662 | fp, buf, CUPS_LLCAST buflen)); | |
663 | ||
80ca4592 | 664 | if (!fp || (fp->mode != 'r' && fp->mode != 's') || !buf || buflen < 3) |
665 | return (0); | |
666 | ||
667 | /* | |
668 | * Now loop until we have a valid line... | |
669 | */ | |
670 | ||
671 | for (ptr = buf, end = buf + buflen - 2; ptr < end ;) | |
672 | { | |
673 | if (fp->ptr >= fp->end) | |
674 | if (cups_fill(fp) <= 0) | |
675 | break; | |
676 | ||
677 | *ptr++ = ch = *(fp->ptr)++; | |
634763e8 | 678 | fp->pos ++; |
80ca4592 | 679 | |
680 | if (ch == '\r') | |
681 | { | |
682 | /* | |
683 | * Check for CR LF... | |
684 | */ | |
685 | ||
686 | if (fp->ptr >= fp->end) | |
687 | if (cups_fill(fp) <= 0) | |
688 | break; | |
689 | ||
690 | if (*(fp->ptr) == '\n') | |
634763e8 | 691 | { |
80ca4592 | 692 | *ptr++ = *(fp->ptr)++; |
634763e8 MS |
693 | fp->pos ++; |
694 | } | |
80ca4592 | 695 | |
696 | break; | |
697 | } | |
698 | else if (ch == '\n') | |
699 | { | |
700 | /* | |
701 | * Line feed ends a line... | |
702 | */ | |
703 | ||
704 | break; | |
705 | } | |
706 | } | |
707 | ||
708 | *ptr = '\0'; | |
709 | ||
634763e8 MS |
710 | DEBUG_printf(("cupsFileGetLine: pos=" CUPS_LLFMT "\n", CUPS_LLCAST fp->pos)); |
711 | ||
80ca4592 | 712 | return (ptr - buf); |
713 | } | |
714 | ||
715 | ||
ef416fc2 | 716 | /* |
717 | * 'cupsFileGets()' - Get a CR and/or LF-terminated line. | |
5a738aea | 718 | * |
426c6a59 | 719 | * @since CUPS 1.2/Mac OS X 10.5@ |
ef416fc2 | 720 | */ |
721 | ||
5a738aea | 722 | char * /* O - Line read or @code NULL@ on end of file or error */ |
ef416fc2 | 723 | cupsFileGets(cups_file_t *fp, /* I - CUPS file */ |
724 | char *buf, /* O - String buffer */ | |
725 | size_t buflen) /* I - Size of string buffer */ | |
726 | { | |
727 | int ch; /* Character from file */ | |
728 | char *ptr, /* Current position in line buffer */ | |
729 | *end; /* End of line buffer */ | |
730 | ||
731 | ||
732 | /* | |
733 | * Range check input... | |
734 | */ | |
735 | ||
634763e8 MS |
736 | DEBUG_printf(("cupsFileGets(fp=%p, buf=%p, buflen=" CUPS_LLFMT ")\n", fp, buf, |
737 | CUPS_LLCAST buflen)); | |
738 | ||
ef416fc2 | 739 | if (!fp || (fp->mode != 'r' && fp->mode != 's') || !buf || buflen < 2) |
740 | return (NULL); | |
741 | ||
742 | /* | |
743 | * Now loop until we have a valid line... | |
744 | */ | |
745 | ||
746 | for (ptr = buf, end = buf + buflen - 1; ptr < end ;) | |
747 | { | |
748 | if (fp->ptr >= fp->end) | |
749 | if (cups_fill(fp) <= 0) | |
750 | { | |
751 | if (ptr == buf) | |
752 | return (NULL); | |
753 | else | |
754 | break; | |
755 | } | |
756 | ||
757 | ch = *(fp->ptr)++; | |
634763e8 | 758 | fp->pos ++; |
ef416fc2 | 759 | |
760 | if (ch == '\r') | |
761 | { | |
762 | /* | |
763 | * Check for CR LF... | |
764 | */ | |
765 | ||
766 | if (fp->ptr >= fp->end) | |
767 | if (cups_fill(fp) <= 0) | |
768 | break; | |
769 | ||
770 | if (*(fp->ptr) == '\n') | |
634763e8 MS |
771 | { |
772 | fp->ptr ++; | |
773 | fp->pos ++; | |
774 | } | |
ef416fc2 | 775 | |
776 | break; | |
777 | } | |
778 | else if (ch == '\n') | |
779 | { | |
780 | /* | |
781 | * Line feed ends a line... | |
782 | */ | |
783 | ||
784 | break; | |
785 | } | |
786 | else | |
787 | *ptr++ = ch; | |
788 | } | |
789 | ||
790 | *ptr = '\0'; | |
791 | ||
634763e8 MS |
792 | DEBUG_printf(("cupsFileGets: pos=" CUPS_LLFMT "\n", CUPS_LLCAST fp->pos)); |
793 | ||
ef416fc2 | 794 | return (buf); |
795 | } | |
796 | ||
797 | ||
798 | /* | |
799 | * 'cupsFileLock()' - Temporarily lock access to a file. | |
5a738aea | 800 | * |
426c6a59 | 801 | * @since CUPS 1.2/Mac OS X 10.5@ |
ef416fc2 | 802 | */ |
803 | ||
804 | int /* O - 0 on success, -1 on error */ | |
5a738aea | 805 | cupsFileLock(cups_file_t *fp, /* I - CUPS file */ |
ef416fc2 | 806 | int block) /* I - 1 to wait for the lock, 0 to fail right away */ |
807 | { | |
808 | /* | |
809 | * Range check... | |
810 | */ | |
811 | ||
812 | if (!fp || fp->mode == 's') | |
813 | return (-1); | |
814 | ||
815 | /* | |
816 | * Try the lock... | |
817 | */ | |
818 | ||
819 | #ifdef WIN32 | |
820 | return (locking(fp->fd, block ? _LK_LOCK : _LK_NBLCK, 0)); | |
821 | #else | |
822 | return (lockf(fp->fd, block ? F_LOCK : F_TLOCK, 0)); | |
823 | #endif /* WIN32 */ | |
824 | } | |
825 | ||
826 | ||
827 | /* | |
828 | * 'cupsFileNumber()' - Return the file descriptor associated with a CUPS file. | |
5a738aea | 829 | * |
426c6a59 | 830 | * @since CUPS 1.2/Mac OS X 10.5@ |
ef416fc2 | 831 | */ |
832 | ||
833 | int /* O - File descriptor */ | |
834 | cupsFileNumber(cups_file_t *fp) /* I - CUPS file */ | |
835 | { | |
5a738aea MS |
836 | if (fp) |
837 | return (fp->fd); | |
838 | else | |
839 | return (-1); | |
ef416fc2 | 840 | } |
841 | ||
842 | ||
843 | /* | |
844 | * 'cupsFileOpen()' - Open a CUPS file. | |
5a738aea MS |
845 | * |
846 | * The "mode" parameter can be "r" to read, "w" to write, overwriting any | |
847 | * existing file, "a" to append to an existing file or create a new file, | |
848 | * or "s" to open a socket connection. | |
849 | * | |
634763e8 MS |
850 | * When opening for writing ("w"), an optional number from 1 to 9 can be |
851 | * supplied which enables Flate compression of the file. Compression is | |
852 | * not supported for the "a" (append) mode. | |
5a738aea MS |
853 | * |
854 | * When opening a socket connection, the filename is a string of the form | |
855 | * "address:port" or "hostname:port". The socket will make an IPv4 or IPv6 | |
856 | * connection as needed, generally preferring IPv6 connections when there is | |
857 | * a choice. | |
858 | * | |
426c6a59 | 859 | * @since CUPS 1.2/Mac OS X 10.5@ |
ef416fc2 | 860 | */ |
861 | ||
5a738aea | 862 | cups_file_t * /* O - CUPS file or @code NULL@ if the file or socket cannot be opened */ |
ef416fc2 | 863 | cupsFileOpen(const char *filename, /* I - Name of file */ |
864 | const char *mode) /* I - Open mode */ | |
865 | { | |
866 | cups_file_t *fp; /* New CUPS file */ | |
867 | int fd; /* File descriptor */ | |
868 | char hostname[1024], /* Hostname */ | |
869 | *portname; /* Port "name" (number or service) */ | |
870 | http_addrlist_t *addrlist; /* Host address list */ | |
871 | ||
872 | ||
b423cd4c | 873 | DEBUG_printf(("cupsFileOpen(filename=\"%s\", mode=\"%s\")\n", filename, |
874 | mode)); | |
875 | ||
ef416fc2 | 876 | /* |
877 | * Range check input... | |
878 | */ | |
879 | ||
880 | if (!filename || !mode || | |
634763e8 MS |
881 | (*mode != 'r' && *mode != 'w' && *mode != 'a' && *mode != 's') || |
882 | (*mode == 'a' && isdigit(mode[1] & 255))) | |
ef416fc2 | 883 | return (NULL); |
884 | ||
885 | /* | |
886 | * Open the file... | |
887 | */ | |
888 | ||
889 | switch (*mode) | |
890 | { | |
891 | case 'a' : /* Append file */ | |
b86bc4cf | 892 | fd = open(filename, O_RDWR | O_CREAT | O_APPEND | O_LARGEFILE | O_BINARY, 0666); |
ef416fc2 | 893 | break; |
894 | ||
895 | case 'r' : /* Read file */ | |
b86bc4cf | 896 | fd = open(filename, O_RDONLY | O_LARGEFILE | O_BINARY, 0); |
ef416fc2 | 897 | break; |
898 | ||
899 | case 'w' : /* Write file */ | |
b86bc4cf | 900 | fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT | O_LARGEFILE | O_BINARY, 0666); |
ef416fc2 | 901 | break; |
902 | ||
903 | case 's' : /* Read/write socket */ | |
904 | strlcpy(hostname, filename, sizeof(hostname)); | |
905 | if ((portname = strrchr(hostname, ':')) != NULL) | |
906 | *portname++ = '\0'; | |
907 | else | |
908 | return (NULL); | |
909 | ||
910 | /* | |
911 | * Lookup the hostname and service... | |
912 | */ | |
913 | ||
914 | if ((addrlist = httpAddrGetList(hostname, AF_UNSPEC, portname)) == NULL) | |
915 | return (NULL); | |
916 | ||
917 | /* | |
918 | * Connect to the server... | |
919 | */ | |
920 | ||
921 | if (!httpAddrConnect(addrlist, &fd)) | |
922 | { | |
923 | httpAddrFreeList(addrlist); | |
924 | return (NULL); | |
925 | } | |
926 | ||
927 | httpAddrFreeList(addrlist); | |
928 | break; | |
929 | ||
930 | default : /* Remove bogus compiler warning... */ | |
931 | return (NULL); | |
932 | } | |
933 | ||
934 | if (fd < 0) | |
935 | return (NULL); | |
936 | ||
937 | /* | |
938 | * Create the CUPS file structure... | |
939 | */ | |
940 | ||
941 | if ((fp = cupsFileOpenFd(fd, mode)) == NULL) | |
942 | { | |
943 | if (*mode == 's') | |
944 | closesocket(fd); | |
945 | else | |
946 | close(fd); | |
947 | } | |
948 | ||
949 | /* | |
950 | * Return it... | |
951 | */ | |
952 | ||
953 | return (fp); | |
954 | } | |
955 | ||
956 | /* | |
957 | * 'cupsFileOpenFd()' - Open a CUPS file using a file descriptor. | |
5a738aea | 958 | * |
634763e8 MS |
959 | * The "mode" parameter can be "r" to read, "w" to write, "a" to append, |
960 | * or "s" to treat the file descriptor as a bidirectional socket connection. | |
5a738aea | 961 | * |
634763e8 MS |
962 | * When opening for writing ("w"), an optional number from 1 to 9 can be |
963 | * supplied which enables Flate compression of the file. Compression is | |
964 | * not supported for the "a" (append) mode. | |
5a738aea | 965 | * |
426c6a59 | 966 | * @since CUPS 1.2/Mac OS X 10.5@ |
ef416fc2 | 967 | */ |
968 | ||
5a738aea | 969 | cups_file_t * /* O - CUPS file or @code NULL@ if the file could not be opened */ |
ef416fc2 | 970 | cupsFileOpenFd(int fd, /* I - File descriptor */ |
971 | const char *mode) /* I - Open mode */ | |
972 | { | |
973 | cups_file_t *fp; /* New CUPS file */ | |
974 | ||
975 | ||
b423cd4c | 976 | DEBUG_printf(("cupsFileOpenFd(fd=%d, mode=\"%s\")\n", fd, mode)); |
977 | ||
ef416fc2 | 978 | /* |
979 | * Range check input... | |
980 | */ | |
981 | ||
982 | if (fd < 0 || !mode || | |
634763e8 MS |
983 | (*mode != 'r' && *mode != 'w' && *mode != 'a' && *mode != 's') || |
984 | (*mode == 'a' && isdigit(mode[1] & 255))) | |
ef416fc2 | 985 | return (NULL); |
986 | ||
987 | /* | |
988 | * Allocate memory... | |
989 | */ | |
990 | ||
991 | if ((fp = calloc(1, sizeof(cups_file_t))) == NULL) | |
992 | return (NULL); | |
993 | ||
994 | /* | |
995 | * Open the file... | |
996 | */ | |
997 | ||
998 | fp->fd = fd; | |
999 | ||
1000 | switch (*mode) | |
1001 | { | |
ef416fc2 | 1002 | case 'a' : |
634763e8 MS |
1003 | fp->pos = lseek(fd, 0, SEEK_END); |
1004 | ||
1005 | case 'w' : | |
ef416fc2 | 1006 | fp->mode = 'w'; |
1007 | fp->ptr = fp->buf; | |
1008 | fp->end = fp->buf + sizeof(fp->buf); | |
1009 | ||
1010 | #ifdef HAVE_LIBZ | |
1011 | if (mode[1] >= '1' && mode[1] <= '9') | |
1012 | { | |
1013 | /* | |
1014 | * Open a compressed stream, so write the standard gzip file | |
1015 | * header... | |
1016 | */ | |
1017 | ||
1018 | unsigned char header[10]; /* gzip file header */ | |
1019 | time_t curtime; /* Current time */ | |
1020 | ||
1021 | ||
1022 | curtime = time(NULL); | |
1023 | header[0] = 0x1f; | |
1024 | header[1] = 0x8b; | |
1025 | header[2] = Z_DEFLATED; | |
1026 | header[3] = 0; | |
1027 | header[4] = curtime; | |
1028 | header[5] = curtime >> 8; | |
1029 | header[6] = curtime >> 16; | |
1030 | header[7] = curtime >> 24; | |
1031 | header[8] = 0; | |
1032 | header[9] = 0x03; | |
1033 | ||
1034 | cups_write(fp, (char *)header, 10); | |
1035 | ||
1036 | /* | |
1037 | * Initialize the compressor... | |
1038 | */ | |
1039 | ||
1040 | deflateInit2(&(fp->stream), mode[1] - '0', Z_DEFLATED, -15, 8, | |
1041 | Z_DEFAULT_STRATEGY); | |
1042 | ||
1043 | fp->stream.next_out = fp->cbuf; | |
1044 | fp->stream.avail_out = sizeof(fp->cbuf); | |
1045 | fp->compressed = 1; | |
1046 | fp->crc = crc32(0L, Z_NULL, 0); | |
1047 | } | |
1048 | #endif /* HAVE_LIBZ */ | |
1049 | break; | |
1050 | ||
1051 | case 'r' : | |
1052 | fp->mode = 'r'; | |
1053 | break; | |
1054 | ||
1055 | case 's' : | |
1056 | fp->mode = 's'; | |
1057 | break; | |
1058 | ||
1059 | default : /* Remove bogus compiler warning... */ | |
1060 | return (NULL); | |
1061 | } | |
1062 | ||
1063 | /* | |
1064 | * Don't pass this file to child processes... | |
1065 | */ | |
1066 | ||
1067 | #ifndef WIN32 | |
1068 | fcntl(fp->fd, F_SETFD, fcntl(fp->fd, F_GETFD) | FD_CLOEXEC); | |
1069 | #endif /* !WIN32 */ | |
1070 | ||
1071 | return (fp); | |
1072 | } | |
1073 | ||
1074 | ||
1075 | /* | |
1076 | * 'cupsFilePeekChar()' - Peek at the next character from a file. | |
5a738aea | 1077 | * |
426c6a59 | 1078 | * @since CUPS 1.2/Mac OS X 10.5@ |
ef416fc2 | 1079 | */ |
1080 | ||
5a738aea | 1081 | int /* O - Character or -1 on end of file */ |
ef416fc2 | 1082 | cupsFilePeekChar(cups_file_t *fp) /* I - CUPS file */ |
1083 | { | |
1084 | /* | |
1085 | * Range check input... | |
1086 | */ | |
1087 | ||
1088 | if (!fp || (fp->mode != 'r' && fp->mode != 's')) | |
1089 | return (-1); | |
1090 | ||
1091 | /* | |
1092 | * If the input buffer is empty, try to read more data... | |
1093 | */ | |
1094 | ||
1095 | if (fp->ptr >= fp->end) | |
1096 | if (cups_fill(fp) < 0) | |
1097 | return (-1); | |
1098 | ||
1099 | /* | |
1100 | * Return the next character in the buffer... | |
1101 | */ | |
1102 | ||
1103 | return (*(fp->ptr) & 255); | |
1104 | } | |
1105 | ||
1106 | ||
1107 | /* | |
1108 | * 'cupsFilePrintf()' - Write a formatted string. | |
5a738aea | 1109 | * |
426c6a59 | 1110 | * @since CUPS 1.2/Mac OS X 10.5@ |
ef416fc2 | 1111 | */ |
1112 | ||
5a738aea | 1113 | int /* O - Number of bytes written or -1 on error */ |
ef416fc2 | 1114 | cupsFilePrintf(cups_file_t *fp, /* I - CUPS file */ |
1115 | const char *format, /* I - Printf-style format string */ | |
1116 | ...) /* I - Additional args as necessary */ | |
1117 | { | |
1118 | va_list ap; /* Argument list */ | |
2abf387c | 1119 | ssize_t bytes; /* Formatted size */ |
ecdc0628 | 1120 | |
ef416fc2 | 1121 | |
ecdc0628 | 1122 | DEBUG_printf(("cupsFilePrintf(fp=%p, format=\"%s\", ...)\n", fp, format)); |
ef416fc2 | 1123 | |
1124 | if (!fp || !format || (fp->mode != 'w' && fp->mode != 's')) | |
1125 | return (-1); | |
1126 | ||
75bd9771 MS |
1127 | if (!fp->printf_buffer) |
1128 | { | |
1129 | /* | |
1130 | * Start with an 1k printf buffer... | |
1131 | */ | |
1132 | ||
1133 | if ((fp->printf_buffer = malloc(1024)) == NULL) | |
1134 | return (-1); | |
1135 | ||
1136 | fp->printf_size = 1024; | |
1137 | } | |
1138 | ||
ef416fc2 | 1139 | va_start(ap, format); |
75bd9771 | 1140 | bytes = vsnprintf(fp->printf_buffer, fp->printf_size, format, ap); |
ef416fc2 | 1141 | va_end(ap); |
1142 | ||
75bd9771 MS |
1143 | if (bytes >= fp->printf_size) |
1144 | { | |
1145 | /* | |
1146 | * Expand the printf buffer... | |
1147 | */ | |
1148 | ||
1149 | char *temp; /* Temporary buffer pointer */ | |
1150 | ||
1151 | ||
1152 | if (bytes > 65535) | |
1153 | return (-1); | |
1154 | ||
1155 | if ((temp = realloc(fp->printf_buffer, bytes + 1)) == NULL) | |
1156 | return (-1); | |
1157 | ||
1158 | fp->printf_buffer = temp; | |
1159 | fp->printf_size = bytes + 1; | |
1160 | ||
1161 | va_start(ap, format); | |
1162 | bytes = vsnprintf(fp->printf_buffer, fp->printf_size, format, ap); | |
1163 | va_end(ap); | |
1164 | } | |
ecdc0628 | 1165 | |
ef416fc2 | 1166 | if (fp->mode == 's') |
634763e8 | 1167 | { |
75bd9771 | 1168 | if (cups_write(fp, fp->printf_buffer, bytes) < 0) |
634763e8 MS |
1169 | return (-1); |
1170 | ||
1171 | fp->pos += bytes; | |
1172 | ||
1173 | DEBUG_printf(("cupsFilePrintf: pos=" CUPS_LLFMT "\n", CUPS_LLCAST fp->pos)); | |
1174 | ||
1175 | return (bytes); | |
1176 | } | |
ef416fc2 | 1177 | |
1178 | if ((fp->ptr + bytes) > fp->end) | |
1179 | if (cupsFileFlush(fp)) | |
1180 | return (-1); | |
1181 | ||
1182 | fp->pos += bytes; | |
1183 | ||
634763e8 MS |
1184 | DEBUG_printf(("cupsFilePrintf: pos=" CUPS_LLFMT "\n", CUPS_LLCAST fp->pos)); |
1185 | ||
ef416fc2 | 1186 | if (bytes > sizeof(fp->buf)) |
1187 | { | |
1188 | #ifdef HAVE_LIBZ | |
1189 | if (fp->compressed) | |
75bd9771 | 1190 | return (cups_compress(fp, fp->printf_buffer, bytes)); |
ef416fc2 | 1191 | else |
1192 | #endif /* HAVE_LIBZ */ | |
75bd9771 | 1193 | return (cups_write(fp, fp->printf_buffer, bytes)); |
ef416fc2 | 1194 | } |
1195 | else | |
1196 | { | |
75bd9771 | 1197 | memcpy(fp->ptr, fp->printf_buffer, bytes); |
ef416fc2 | 1198 | fp->ptr += bytes; |
1199 | return (bytes); | |
1200 | } | |
1201 | } | |
1202 | ||
1203 | ||
1204 | /* | |
1205 | * 'cupsFilePutChar()' - Write a character. | |
5a738aea | 1206 | * |
426c6a59 | 1207 | * @since CUPS 1.2/Mac OS X 10.5@ |
ef416fc2 | 1208 | */ |
1209 | ||
1210 | int /* O - 0 on success, -1 on error */ | |
1211 | cupsFilePutChar(cups_file_t *fp, /* I - CUPS file */ | |
1212 | int c) /* I - Character to write */ | |
1213 | { | |
1214 | /* | |
1215 | * Range check input... | |
1216 | */ | |
1217 | ||
1218 | if (!fp || (fp->mode != 'w' && fp->mode != 's')) | |
1219 | return (-1); | |
1220 | ||
1221 | if (fp->mode == 's') | |
1222 | { | |
1223 | /* | |
1224 | * Send character immediately over socket... | |
1225 | */ | |
1226 | ||
1227 | char ch; /* Output character */ | |
1228 | ||
1229 | ||
1230 | ch = c; | |
1231 | ||
1232 | if (send(fp->fd, &ch, 1, 0) < 1) | |
1233 | return (-1); | |
1234 | } | |
1235 | else | |
1236 | { | |
1237 | /* | |
1238 | * Buffer it up... | |
1239 | */ | |
1240 | ||
1241 | if (fp->ptr >= fp->end) | |
1242 | if (cupsFileFlush(fp)) | |
1243 | return (-1); | |
1244 | ||
1245 | *(fp->ptr) ++ = c; | |
1246 | } | |
1247 | ||
1248 | fp->pos ++; | |
1249 | ||
634763e8 MS |
1250 | DEBUG_printf(("cupsFilePutChar: pos=" CUPS_LLFMT "\n", CUPS_LLCAST fp->pos)); |
1251 | ||
ef416fc2 | 1252 | return (0); |
1253 | } | |
1254 | ||
1255 | ||
58dc1933 MS |
1256 | /* |
1257 | * 'cupsFilePutConf()' - Write a configuration line. | |
1258 | * | |
1259 | * This function handles any comment escaping of the value. | |
1260 | * | |
1261 | * @since CUPS 1.4@ | |
1262 | */ | |
1263 | ||
1264 | ssize_t /* O - Number of bytes written or -1 on error */ | |
1265 | cupsFilePutConf(cups_file_t *fp, /* I - CUPS file */ | |
1266 | const char *directive, /* I - Directive */ | |
1267 | const char *value) /* I - Value */ | |
1268 | { | |
1269 | ssize_t bytes, /* Number of bytes written */ | |
1270 | temp; /* Temporary byte count */ | |
1271 | const char *ptr; /* Pointer into value */ | |
1272 | ||
1273 | ||
1274 | if (!fp || !directive || !*directive) | |
1275 | return (-1); | |
1276 | ||
1277 | if ((bytes = cupsFilePuts(fp, directive)) < 0) | |
1278 | return (-1); | |
1279 | ||
1280 | if (cupsFilePutChar(fp, ' ') < 0) | |
1281 | return (-1); | |
1282 | bytes ++; | |
1283 | ||
1284 | if (value && *value) | |
1285 | { | |
1286 | if ((ptr = strchr(value, '#')) != NULL) | |
1287 | { | |
1288 | /* | |
1289 | * Need to quote the first # in the info string... | |
1290 | */ | |
1291 | ||
1292 | if ((temp = cupsFileWrite(fp, value, ptr - value)) < 0) | |
1293 | return (-1); | |
1294 | bytes += temp; | |
1295 | ||
1296 | if (cupsFilePutChar(fp, '\\') < 0) | |
1297 | return (-1); | |
1298 | bytes ++; | |
1299 | ||
1300 | if ((temp = cupsFilePuts(fp, ptr)) < 0) | |
1301 | return (-1); | |
1302 | bytes += temp; | |
1303 | } | |
1304 | else if ((temp = cupsFilePuts(fp, value)) < 0) | |
1305 | return (-1); | |
1306 | else | |
1307 | bytes += temp; | |
1308 | } | |
1309 | ||
1310 | if (cupsFilePutChar(fp, '\n') < 0) | |
1311 | return (-1); | |
1312 | else | |
1313 | return (bytes + 1); | |
1314 | } | |
1315 | ||
1316 | ||
ef416fc2 | 1317 | /* |
1318 | * 'cupsFilePuts()' - Write a string. | |
5a738aea MS |
1319 | * |
1320 | * Like the @code fputs@ function, no newline is appended to the string. | |
1321 | * | |
426c6a59 | 1322 | * @since CUPS 1.2/Mac OS X 10.5@ |
ef416fc2 | 1323 | */ |
1324 | ||
5a738aea | 1325 | int /* O - Number of bytes written or -1 on error */ |
ef416fc2 | 1326 | cupsFilePuts(cups_file_t *fp, /* I - CUPS file */ |
1327 | const char *s) /* I - String to write */ | |
1328 | { | |
2abf387c | 1329 | ssize_t bytes; /* Bytes to write */ |
ef416fc2 | 1330 | |
1331 | ||
1332 | /* | |
1333 | * Range check input... | |
1334 | */ | |
1335 | ||
1336 | if (!fp || !s || (fp->mode != 'w' && fp->mode != 's')) | |
1337 | return (-1); | |
1338 | ||
1339 | /* | |
1340 | * Write the string... | |
1341 | */ | |
1342 | ||
b86bc4cf | 1343 | bytes = (int)strlen(s); |
ef416fc2 | 1344 | |
1345 | if (fp->mode == 's') | |
1346 | { | |
1347 | if (cups_write(fp, s, bytes) < 0) | |
1348 | return (-1); | |
1349 | ||
1350 | fp->pos += bytes; | |
1351 | ||
634763e8 MS |
1352 | DEBUG_printf(("cupsFilePuts: pos=" CUPS_LLFMT "\n", CUPS_LLCAST fp->pos)); |
1353 | ||
ef416fc2 | 1354 | return (bytes); |
1355 | } | |
1356 | ||
1357 | if ((fp->ptr + bytes) > fp->end) | |
1358 | if (cupsFileFlush(fp)) | |
1359 | return (-1); | |
1360 | ||
1361 | fp->pos += bytes; | |
1362 | ||
634763e8 MS |
1363 | DEBUG_printf(("cupsFilePuts: pos=" CUPS_LLFMT "\n", CUPS_LLCAST fp->pos)); |
1364 | ||
ef416fc2 | 1365 | if (bytes > sizeof(fp->buf)) |
1366 | { | |
1367 | #ifdef HAVE_LIBZ | |
1368 | if (fp->compressed) | |
1369 | return (cups_compress(fp, s, bytes)); | |
1370 | else | |
1371 | #endif /* HAVE_LIBZ */ | |
1372 | return (cups_write(fp, s, bytes)); | |
1373 | } | |
1374 | else | |
1375 | { | |
1376 | memcpy(fp->ptr, s, bytes); | |
1377 | fp->ptr += bytes; | |
1378 | return (bytes); | |
1379 | } | |
1380 | } | |
1381 | ||
1382 | ||
1383 | /* | |
1384 | * 'cupsFileRead()' - Read from a file. | |
5a738aea | 1385 | * |
426c6a59 | 1386 | * @since CUPS 1.2/Mac OS X 10.5@ |
ef416fc2 | 1387 | */ |
1388 | ||
5a738aea | 1389 | ssize_t /* O - Number of bytes read or -1 on error */ |
ef416fc2 | 1390 | cupsFileRead(cups_file_t *fp, /* I - CUPS file */ |
1391 | char *buf, /* O - Buffer */ | |
1392 | size_t bytes) /* I - Number of bytes to read */ | |
1393 | { | |
2abf387c | 1394 | size_t total; /* Total bytes read */ |
1395 | ssize_t count; /* Bytes read */ | |
ef416fc2 | 1396 | |
1397 | ||
634763e8 MS |
1398 | DEBUG_printf(("cupsFileRead(fp=%p, buf=%p, bytes=" CUPS_LLFMT ")\n", fp, buf, |
1399 | CUPS_LLCAST bytes)); | |
ef416fc2 | 1400 | |
1401 | /* | |
1402 | * Range check input... | |
1403 | */ | |
1404 | ||
1405 | if (!fp || !buf || bytes < 0 || (fp->mode != 'r' && fp->mode != 's')) | |
1406 | return (-1); | |
1407 | ||
1408 | if (bytes == 0) | |
1409 | return (0); | |
1410 | ||
1411 | /* | |
1412 | * Loop until all bytes are read... | |
1413 | */ | |
1414 | ||
1415 | total = 0; | |
1416 | while (bytes > 0) | |
1417 | { | |
1418 | if (fp->ptr >= fp->end) | |
1419 | if (cups_fill(fp) <= 0) | |
1420 | { | |
634763e8 MS |
1421 | DEBUG_printf(("cupsFileRead: cups_fill() returned -1, total=" CUPS_LLFMT "\n", |
1422 | CUPS_LLCAST total)); | |
ef416fc2 | 1423 | |
1424 | if (total > 0) | |
b86bc4cf | 1425 | return ((ssize_t)total); |
ef416fc2 | 1426 | else |
1427 | return (-1); | |
1428 | } | |
1429 | ||
b86bc4cf | 1430 | count = (ssize_t)(fp->end - fp->ptr); |
1431 | if (count > (ssize_t)bytes) | |
1432 | count = (ssize_t)bytes; | |
ef416fc2 | 1433 | |
1434 | memcpy(buf, fp->ptr, count); | |
1435 | fp->ptr += count; | |
634763e8 MS |
1436 | fp->pos += count; |
1437 | ||
1438 | DEBUG_printf(("cupsFileRead: pos=" CUPS_LLFMT "\n", CUPS_LLCAST fp->pos)); | |
ef416fc2 | 1439 | |
1440 | /* | |
1441 | * Update the counts for the last read... | |
1442 | */ | |
1443 | ||
1444 | bytes -= count; | |
1445 | total += count; | |
1446 | buf += count; | |
1447 | } | |
1448 | ||
1449 | /* | |
1450 | * Return the total number of bytes read... | |
1451 | */ | |
1452 | ||
634763e8 | 1453 | DEBUG_printf(("cupsFileRead: total=%d\n", (int)total)); |
ef416fc2 | 1454 | |
b86bc4cf | 1455 | return ((ssize_t)total); |
ef416fc2 | 1456 | } |
1457 | ||
1458 | ||
1459 | /* | |
5a738aea MS |
1460 | * 'cupsFileRewind()' - Set the current file position to the beginning of the |
1461 | * file. | |
1462 | * | |
426c6a59 | 1463 | * @since CUPS 1.2/Mac OS X 10.5@ |
ef416fc2 | 1464 | */ |
1465 | ||
5a738aea | 1466 | off_t /* O - New file position or -1 on error */ |
ef416fc2 | 1467 | cupsFileRewind(cups_file_t *fp) /* I - CUPS file */ |
1468 | { | |
80ca4592 | 1469 | /* |
1470 | * Range check input... | |
1471 | */ | |
1472 | ||
634763e8 MS |
1473 | DEBUG_printf(("cupsFileRewind(fp=%p)\n", fp)); |
1474 | DEBUG_printf(("cupsFileRewind: pos=" CUPS_LLFMT "\n", CUPS_LLCAST fp->pos)); | |
1475 | ||
80ca4592 | 1476 | if (!fp || fp->mode != 'r') |
1477 | return (-1); | |
1478 | ||
1479 | /* | |
1480 | * Handle special cases... | |
1481 | */ | |
1482 | ||
634763e8 | 1483 | if (fp->bufpos == 0) |
80ca4592 | 1484 | { |
1485 | /* | |
1486 | * No seeking necessary... | |
1487 | */ | |
1488 | ||
634763e8 MS |
1489 | fp->pos = 0; |
1490 | ||
80ca4592 | 1491 | if (fp->ptr) |
1492 | { | |
1493 | fp->ptr = fp->buf; | |
1494 | fp->eof = 0; | |
1495 | } | |
1496 | ||
634763e8 MS |
1497 | DEBUG_printf(("cupsFileRewind: pos=" CUPS_LLFMT "\n", CUPS_LLCAST fp->pos)); |
1498 | ||
80ca4592 | 1499 | return (0); |
1500 | } | |
1501 | ||
1502 | /* | |
1503 | * Otherwise, seek in the file and cleanup any compression buffers... | |
1504 | */ | |
1505 | ||
1506 | #ifdef HAVE_LIBZ | |
1507 | if (fp->compressed) | |
1508 | { | |
1509 | inflateEnd(&fp->stream); | |
1510 | fp->compressed = 0; | |
1511 | } | |
1512 | #endif /* HAVE_LIBZ */ | |
1513 | ||
c9fc04c6 MS |
1514 | if (lseek(fp->fd, 0, SEEK_SET)) |
1515 | { | |
1516 | DEBUG_printf(("cupsFileRewind: lseek failed: %s\n", strerror(errno))); | |
1517 | return (-1); | |
1518 | } | |
80ca4592 | 1519 | |
634763e8 MS |
1520 | fp->bufpos = 0; |
1521 | fp->pos = 0; | |
1522 | fp->ptr = NULL; | |
1523 | fp->end = NULL; | |
1524 | fp->eof = 0; | |
1525 | ||
1526 | DEBUG_printf(("cupsFileRewind: pos=" CUPS_LLFMT "\n", CUPS_LLCAST fp->pos)); | |
80ca4592 | 1527 | |
1528 | return (0); | |
ef416fc2 | 1529 | } |
1530 | ||
1531 | ||
1532 | /* | |
1533 | * 'cupsFileSeek()' - Seek in a file. | |
5a738aea | 1534 | * |
426c6a59 | 1535 | * @since CUPS 1.2/Mac OS X 10.5@ |
ef416fc2 | 1536 | */ |
1537 | ||
5a738aea | 1538 | off_t /* O - New file position or -1 on error */ |
ef416fc2 | 1539 | cupsFileSeek(cups_file_t *fp, /* I - CUPS file */ |
1540 | off_t pos) /* I - Position in file */ | |
1541 | { | |
2abf387c | 1542 | ssize_t bytes; /* Number bytes in buffer */ |
ef416fc2 | 1543 | |
1544 | ||
634763e8 MS |
1545 | DEBUG_printf(("cupsFileSeek(fp=%p, pos=" CUPS_LLFMT ")\n", fp, |
1546 | CUPS_LLCAST pos)); | |
1547 | DEBUG_printf(("cupsFileSeek: fp->pos=" CUPS_LLFMT "\n", CUPS_LLCAST fp->pos)); | |
1548 | DEBUG_printf(("cupsFileSeek: fp->ptr=%p, fp->end=%p\n", fp->ptr, fp->end)); | |
ef416fc2 | 1549 | |
1550 | /* | |
1551 | * Range check input... | |
1552 | */ | |
1553 | ||
1554 | if (!fp || pos < 0 || fp->mode != 'r') | |
1555 | return (-1); | |
1556 | ||
80ca4592 | 1557 | /* |
1558 | * Handle special cases... | |
1559 | */ | |
1560 | ||
1561 | if (pos == 0) | |
1562 | return (cupsFileRewind(fp)); | |
1563 | ||
634763e8 | 1564 | if (fp->ptr) |
b423cd4c | 1565 | { |
634763e8 | 1566 | bytes = (ssize_t)(fp->end - fp->buf); |
b423cd4c | 1567 | |
634763e8 | 1568 | if (pos >= fp->bufpos && pos < (fp->bufpos + bytes)) |
b423cd4c | 1569 | { |
634763e8 MS |
1570 | /* |
1571 | * No seeking necessary... | |
1572 | */ | |
1573 | ||
1574 | fp->pos = pos; | |
1575 | fp->ptr = fp->buf + pos - fp->bufpos; | |
b423cd4c | 1576 | fp->eof = 0; |
b423cd4c | 1577 | |
634763e8 MS |
1578 | return (pos); |
1579 | } | |
b423cd4c | 1580 | } |
1581 | ||
80ca4592 | 1582 | #ifdef HAVE_LIBZ |
1583 | if (!fp->compressed && !fp->ptr) | |
1584 | { | |
1585 | /* | |
1586 | * Preload a buffer to determine whether the file is compressed... | |
1587 | */ | |
1588 | ||
1589 | if (cups_fill(fp) < 0) | |
1590 | return (-1); | |
1591 | } | |
1592 | #endif /* HAVE_LIBZ */ | |
1593 | ||
ef416fc2 | 1594 | /* |
634763e8 | 1595 | * Seek forwards or backwards... |
ef416fc2 | 1596 | */ |
1597 | ||
ef416fc2 | 1598 | fp->eof = 0; |
1599 | ||
634763e8 | 1600 | DEBUG_printf(("cupsFileSeek: bytes=" CUPS_LLFMT "\n", CUPS_LLCAST bytes)); |
80ca4592 | 1601 | |
634763e8 | 1602 | if (pos < fp->bufpos) |
ef416fc2 | 1603 | { |
1604 | /* | |
1605 | * Need to seek backwards... | |
1606 | */ | |
1607 | ||
634763e8 | 1608 | DEBUG_puts("cupsFileSeek: SEEK BACKWARDS"); |
80ca4592 | 1609 | |
ef416fc2 | 1610 | #ifdef HAVE_LIBZ |
1611 | if (fp->compressed) | |
1612 | { | |
1613 | inflateEnd(&fp->stream); | |
1614 | ||
1615 | lseek(fp->fd, 0, SEEK_SET); | |
634763e8 MS |
1616 | fp->bufpos = 0; |
1617 | fp->pos = 0; | |
1618 | fp->ptr = NULL; | |
1619 | fp->end = NULL; | |
ef416fc2 | 1620 | |
1621 | while ((bytes = cups_fill(fp)) > 0) | |
634763e8 | 1622 | if (pos >= fp->bufpos && pos < (fp->bufpos + bytes)) |
ef416fc2 | 1623 | break; |
1624 | ||
1625 | if (bytes <= 0) | |
1626 | return (-1); | |
80ca4592 | 1627 | |
634763e8 MS |
1628 | fp->ptr = fp->buf + pos - fp->bufpos; |
1629 | fp->pos = pos; | |
ef416fc2 | 1630 | } |
1631 | else | |
1632 | #endif /* HAVE_LIBZ */ | |
1633 | { | |
634763e8 MS |
1634 | fp->bufpos = lseek(fp->fd, pos, SEEK_SET); |
1635 | fp->pos = fp->bufpos; | |
1636 | fp->ptr = NULL; | |
1637 | fp->end = NULL; | |
80ca4592 | 1638 | |
634763e8 MS |
1639 | DEBUG_printf(("cupsFileSeek: lseek() returned " CUPS_LLFMT "...\n", |
1640 | CUPS_LLCAST fp->pos)); | |
ef416fc2 | 1641 | } |
1642 | } | |
634763e8 | 1643 | else |
ef416fc2 | 1644 | { |
1645 | /* | |
1646 | * Need to seek forwards... | |
1647 | */ | |
1648 | ||
634763e8 | 1649 | DEBUG_puts("cupsFileSeek: SEEK FORWARDS"); |
80ca4592 | 1650 | |
ef416fc2 | 1651 | #ifdef HAVE_LIBZ |
80ca4592 | 1652 | if (fp->compressed) |
ef416fc2 | 1653 | { |
1654 | while ((bytes = cups_fill(fp)) > 0) | |
80ca4592 | 1655 | { |
634763e8 | 1656 | if (pos >= fp->bufpos && pos < (fp->bufpos + bytes)) |
ef416fc2 | 1657 | break; |
80ca4592 | 1658 | } |
ef416fc2 | 1659 | |
1660 | if (bytes <= 0) | |
1661 | return (-1); | |
80ca4592 | 1662 | |
634763e8 MS |
1663 | fp->ptr = fp->buf + pos - fp->bufpos; |
1664 | fp->pos = pos; | |
ef416fc2 | 1665 | } |
1666 | else | |
1667 | #endif /* HAVE_LIBZ */ | |
1668 | { | |
634763e8 MS |
1669 | fp->bufpos = lseek(fp->fd, pos, SEEK_SET); |
1670 | fp->pos = fp->bufpos; | |
1671 | fp->ptr = NULL; | |
1672 | fp->end = NULL; | |
80ca4592 | 1673 | |
634763e8 MS |
1674 | DEBUG_printf(("cupsFileSeek: lseek() returned " CUPS_LLFMT "...\n", |
1675 | CUPS_LLCAST fp->pos)); | |
ef416fc2 | 1676 | } |
1677 | } | |
ef416fc2 | 1678 | |
634763e8 | 1679 | DEBUG_printf(("cupsFileSeek: pos=" CUPS_LLFMT "\n", CUPS_LLCAST fp->pos)); |
ef416fc2 | 1680 | |
1681 | return (fp->pos); | |
1682 | } | |
1683 | ||
1684 | ||
80ca4592 | 1685 | /* |
1686 | * 'cupsFileStderr()' - Return a CUPS file associated with stderr. | |
5a738aea | 1687 | * |
426c6a59 | 1688 | * @since CUPS 1.2/Mac OS X 10.5@ |
80ca4592 | 1689 | */ |
1690 | ||
5a738aea | 1691 | cups_file_t * /* O - CUPS file */ |
80ca4592 | 1692 | cupsFileStderr(void) |
1693 | { | |
1694 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals... */ | |
1695 | ||
1696 | ||
1697 | /* | |
1698 | * Open file descriptor 2 as needed... | |
1699 | */ | |
1700 | ||
1701 | if (!cg->stdio_files[2]) | |
1702 | { | |
1703 | /* | |
1704 | * Flush any pending output on the stdio file... | |
1705 | */ | |
1706 | ||
1707 | fflush(stderr); | |
1708 | ||
1709 | /* | |
1710 | * Open file descriptor 2... | |
1711 | */ | |
1712 | ||
1713 | if ((cg->stdio_files[2] = cupsFileOpenFd(2, "w")) != NULL) | |
1714 | cg->stdio_files[2]->is_stdio = 1; | |
1715 | } | |
1716 | ||
1717 | return (cg->stdio_files[2]); | |
1718 | } | |
1719 | ||
1720 | ||
1721 | /* | |
1722 | * 'cupsFileStdin()' - Return a CUPS file associated with stdin. | |
5a738aea | 1723 | * |
426c6a59 | 1724 | * @since CUPS 1.2/Mac OS X 10.5@ |
80ca4592 | 1725 | */ |
1726 | ||
5a738aea | 1727 | cups_file_t * /* O - CUPS file */ |
80ca4592 | 1728 | cupsFileStdin(void) |
1729 | { | |
1730 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals... */ | |
1731 | ||
1732 | ||
1733 | /* | |
1734 | * Open file descriptor 0 as needed... | |
1735 | */ | |
1736 | ||
1737 | if (!cg->stdio_files[0]) | |
1738 | { | |
1739 | /* | |
1740 | * Open file descriptor 0... | |
1741 | */ | |
1742 | ||
1743 | if ((cg->stdio_files[0] = cupsFileOpenFd(0, "r")) != NULL) | |
1744 | cg->stdio_files[0]->is_stdio = 1; | |
1745 | } | |
1746 | ||
1747 | return (cg->stdio_files[0]); | |
1748 | } | |
1749 | ||
1750 | ||
1751 | /* | |
1752 | * 'cupsFileStdout()' - Return a CUPS file associated with stdout. | |
5a738aea | 1753 | * |
426c6a59 | 1754 | * @since CUPS 1.2/Mac OS X 10.5@ |
80ca4592 | 1755 | */ |
1756 | ||
5a738aea | 1757 | cups_file_t * /* O - CUPS file */ |
80ca4592 | 1758 | cupsFileStdout(void) |
1759 | { | |
1760 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals... */ | |
1761 | ||
1762 | ||
1763 | /* | |
1764 | * Open file descriptor 1 as needed... | |
1765 | */ | |
1766 | ||
1767 | if (!cg->stdio_files[1]) | |
1768 | { | |
1769 | /* | |
1770 | * Flush any pending output on the stdio file... | |
1771 | */ | |
1772 | ||
1773 | fflush(stdout); | |
1774 | ||
1775 | /* | |
1776 | * Open file descriptor 1... | |
1777 | */ | |
1778 | ||
1779 | if ((cg->stdio_files[1] = cupsFileOpenFd(1, "w")) != NULL) | |
1780 | cg->stdio_files[1]->is_stdio = 1; | |
1781 | } | |
1782 | ||
1783 | return (cg->stdio_files[1]); | |
1784 | } | |
1785 | ||
1786 | ||
ef416fc2 | 1787 | /* |
1788 | * 'cupsFileTell()' - Return the current file position. | |
5a738aea | 1789 | * |
426c6a59 | 1790 | * @since CUPS 1.2/Mac OS X 10.5@ |
ef416fc2 | 1791 | */ |
1792 | ||
1793 | off_t /* O - File position */ | |
1794 | cupsFileTell(cups_file_t *fp) /* I - CUPS file */ | |
1795 | { | |
634763e8 | 1796 | DEBUG_printf(("cupsFileTell(fp=%p)\n", fp)); |
c9fc04c6 | 1797 | DEBUG_printf(("cupsFileTell: pos=" CUPS_LLFMT "\n", CUPS_LLCAST (fp ? fp->pos : -1))); |
634763e8 | 1798 | |
80ca4592 | 1799 | return (fp ? fp->pos : 0); |
ef416fc2 | 1800 | } |
1801 | ||
1802 | ||
1803 | /* | |
1804 | * 'cupsFileUnlock()' - Unlock access to a file. | |
5a738aea | 1805 | * |
426c6a59 | 1806 | * @since CUPS 1.2/Mac OS X 10.5@ |
ef416fc2 | 1807 | */ |
1808 | ||
1809 | int /* O - 0 on success, -1 on error */ | |
5a738aea | 1810 | cupsFileUnlock(cups_file_t *fp) /* I - CUPS file */ |
ef416fc2 | 1811 | { |
1812 | /* | |
1813 | * Range check... | |
1814 | */ | |
1815 | ||
634763e8 MS |
1816 | DEBUG_printf(("cupsFileUnlock(fp=%p)\n", fp)); |
1817 | ||
ef416fc2 | 1818 | if (!fp || fp->mode == 's') |
1819 | return (-1); | |
1820 | ||
1821 | /* | |
1822 | * Unlock... | |
1823 | */ | |
1824 | ||
1825 | #ifdef WIN32 | |
1826 | return (locking(fp->fd, _LK_UNLCK, 0)); | |
1827 | #else | |
1828 | return (lockf(fp->fd, F_ULOCK, 0)); | |
1829 | #endif /* WIN32 */ | |
1830 | } | |
1831 | ||
1832 | ||
1833 | /* | |
1834 | * 'cupsFileWrite()' - Write to a file. | |
5a738aea | 1835 | * |
426c6a59 | 1836 | * @since CUPS 1.2/Mac OS X 10.5@ |
ef416fc2 | 1837 | */ |
1838 | ||
5a738aea | 1839 | ssize_t /* O - Number of bytes written or -1 on error */ |
ef416fc2 | 1840 | cupsFileWrite(cups_file_t *fp, /* I - CUPS file */ |
1841 | const char *buf, /* I - Buffer */ | |
1842 | size_t bytes) /* I - Number of bytes to write */ | |
1843 | { | |
1844 | /* | |
1845 | * Range check input... | |
1846 | */ | |
1847 | ||
634763e8 MS |
1848 | DEBUG_printf(("cupsFileWrite(fp=%p, buf=%p, bytes=" CUPS_LLFMT ")\n", |
1849 | fp, buf, CUPS_LLCAST bytes)); | |
1850 | ||
ef416fc2 | 1851 | if (!fp || !buf || bytes < 0 || (fp->mode != 'w' && fp->mode != 's')) |
1852 | return (-1); | |
1853 | ||
1854 | if (bytes == 0) | |
1855 | return (0); | |
1856 | ||
1857 | /* | |
1858 | * Write the buffer... | |
1859 | */ | |
1860 | ||
1861 | if (fp->mode == 's') | |
1862 | { | |
1863 | if (cups_write(fp, buf, bytes) < 0) | |
1864 | return (-1); | |
1865 | ||
b86bc4cf | 1866 | fp->pos += (off_t)bytes; |
ef416fc2 | 1867 | |
634763e8 MS |
1868 | DEBUG_printf(("cupsFileWrite: pos=" CUPS_LLFMT "\n", CUPS_LLCAST fp->pos)); |
1869 | ||
b86bc4cf | 1870 | return ((ssize_t)bytes); |
ef416fc2 | 1871 | } |
1872 | ||
1873 | if ((fp->ptr + bytes) > fp->end) | |
1874 | if (cupsFileFlush(fp)) | |
1875 | return (-1); | |
1876 | ||
b86bc4cf | 1877 | fp->pos += (off_t)bytes; |
ef416fc2 | 1878 | |
634763e8 MS |
1879 | DEBUG_printf(("cupsFileWrite: pos=" CUPS_LLFMT "\n", CUPS_LLCAST fp->pos)); |
1880 | ||
ef416fc2 | 1881 | if (bytes > sizeof(fp->buf)) |
1882 | { | |
1883 | #ifdef HAVE_LIBZ | |
1884 | if (fp->compressed) | |
1885 | return (cups_compress(fp, buf, bytes)); | |
1886 | else | |
1887 | #endif /* HAVE_LIBZ */ | |
1888 | return (cups_write(fp, buf, bytes)); | |
1889 | } | |
1890 | else | |
1891 | { | |
1892 | memcpy(fp->ptr, buf, bytes); | |
1893 | fp->ptr += bytes; | |
b86bc4cf | 1894 | return ((ssize_t)bytes); |
ef416fc2 | 1895 | } |
1896 | } | |
1897 | ||
1898 | ||
1899 | #ifdef HAVE_LIBZ | |
1900 | /* | |
1901 | * 'cups_compress()' - Compress a buffer of data... | |
1902 | */ | |
1903 | ||
1904 | static ssize_t /* O - Number of bytes written or -1 */ | |
1905 | cups_compress(cups_file_t *fp, /* I - CUPS file */ | |
1906 | const char *buf, /* I - Buffer */ | |
1907 | size_t bytes) /* I - Number bytes */ | |
1908 | { | |
634763e8 MS |
1909 | DEBUG_printf(("cups_compress(fp=%p, buf=%p, bytes=" CUPS_LLFMT "\n", fp, buf, |
1910 | CUPS_LLCAST bytes)); | |
ecdc0628 | 1911 | |
ef416fc2 | 1912 | /* |
1913 | * Update the CRC... | |
1914 | */ | |
1915 | ||
1916 | fp->crc = crc32(fp->crc, (const Bytef *)buf, bytes); | |
1917 | ||
1918 | /* | |
1919 | * Deflate the bytes... | |
1920 | */ | |
1921 | ||
1922 | fp->stream.next_in = (Bytef *)buf; | |
1923 | fp->stream.avail_in = bytes; | |
1924 | ||
1925 | while (fp->stream.avail_in > 0) | |
1926 | { | |
1927 | /* | |
1928 | * Flush the current buffer... | |
1929 | */ | |
1930 | ||
634763e8 MS |
1931 | DEBUG_printf(("cups_compress: avail_in=%d, avail_out=%d\n", |
1932 | fp->stream.avail_in, fp->stream.avail_out)); | |
ecdc0628 | 1933 | |
ef416fc2 | 1934 | if (fp->stream.avail_out < (int)(sizeof(fp->cbuf) / 8)) |
1935 | { | |
1936 | if (cups_write(fp, (char *)fp->cbuf, fp->stream.next_out - fp->cbuf) < 0) | |
1937 | return (-1); | |
ecdc0628 | 1938 | |
1939 | fp->stream.next_out = fp->cbuf; | |
1940 | fp->stream.avail_out = sizeof(fp->cbuf); | |
ef416fc2 | 1941 | } |
1942 | ||
1943 | deflate(&(fp->stream), Z_NO_FLUSH); | |
1944 | } | |
1945 | ||
1946 | return (bytes); | |
1947 | } | |
1948 | #endif /* HAVE_LIBZ */ | |
1949 | ||
1950 | ||
1951 | /* | |
1952 | * 'cups_fill()' - Fill the input buffer... | |
1953 | */ | |
1954 | ||
1955 | static ssize_t /* O - Number of bytes or -1 */ | |
1956 | cups_fill(cups_file_t *fp) /* I - CUPS file */ | |
1957 | { | |
1958 | ssize_t bytes; /* Number of bytes read */ | |
1959 | #ifdef HAVE_LIBZ | |
c277e2f8 | 1960 | int status; /* Decompression status */ |
ef416fc2 | 1961 | const unsigned char *ptr, /* Pointer into buffer */ |
1962 | *end; /* End of buffer */ | |
1963 | #endif /* HAVE_LIBZ */ | |
1964 | ||
1965 | ||
1966 | DEBUG_printf(("cups_fill(fp=%p)\n", fp)); | |
634763e8 MS |
1967 | DEBUG_printf(("cups_fill: fp->ptr=%p, fp->end=%p, fp->buf=%p, " |
1968 | "fp->bufpos=" CUPS_LLFMT ", fp->eof=%d\n", | |
1969 | fp->ptr, fp->end, fp->buf, CUPS_LLCAST fp->bufpos, fp->eof)); | |
ef416fc2 | 1970 | |
1971 | if (fp->ptr && fp->end) | |
c9fc04c6 | 1972 | fp->bufpos += fp->end - fp->buf; |
ef416fc2 | 1973 | |
1974 | #ifdef HAVE_LIBZ | |
634763e8 | 1975 | DEBUG_printf(("cups_fill: fp->compressed=%d\n", fp->compressed)); |
b423cd4c | 1976 | |
ef416fc2 | 1977 | while (!fp->ptr || fp->compressed) |
1978 | { | |
1979 | /* | |
1980 | * Check to see if we have read any data yet; if not, see if we have a | |
1981 | * compressed file... | |
1982 | */ | |
1983 | ||
1984 | if (!fp->ptr) | |
1985 | { | |
1986 | /* | |
1987 | * Reset the file position in case we are seeking... | |
1988 | */ | |
1989 | ||
1990 | fp->compressed = 0; | |
ef416fc2 | 1991 | |
1992 | /* | |
1993 | * Read the first bytes in the file to determine if we have a gzip'd | |
1994 | * file... | |
1995 | */ | |
1996 | ||
fa73b229 | 1997 | if ((bytes = cups_read(fp, (char *)fp->buf, sizeof(fp->buf))) < 0) |
ef416fc2 | 1998 | { |
1999 | /* | |
2000 | * Can't read from file! | |
2001 | */ | |
2002 | ||
634763e8 | 2003 | DEBUG_printf(("cups_fill: cups_read() returned " CUPS_LLFMT "!\n", |
b423cd4c | 2004 | CUPS_LLCAST bytes)); |
2005 | ||
ef416fc2 | 2006 | return (-1); |
2007 | } | |
2008 | ||
fa73b229 | 2009 | if (bytes < 10 || fp->buf[0] != 0x1f || |
e00b005a | 2010 | (fp->buf[1] & 255) != 0x8b || |
fa73b229 | 2011 | fp->buf[2] != 8 || (fp->buf[3] & 0xe0) != 0) |
ef416fc2 | 2012 | { |
2013 | /* | |
2014 | * Not a gzip'd file! | |
2015 | */ | |
2016 | ||
ef416fc2 | 2017 | fp->ptr = fp->buf; |
2018 | fp->end = fp->buf + bytes; | |
2019 | ||
c9fc04c6 MS |
2020 | DEBUG_printf(("cups_fill: Returning " CUPS_LLFMT "!\n", |
2021 | CUPS_LLCAST bytes)); | |
b423cd4c | 2022 | |
ef416fc2 | 2023 | return (bytes); |
2024 | } | |
2025 | ||
2026 | /* | |
2027 | * Parse header junk: extra data, original name, and comment... | |
2028 | */ | |
2029 | ||
fa73b229 | 2030 | ptr = (unsigned char *)fp->buf + 10; |
2031 | end = (unsigned char *)fp->buf + bytes; | |
ef416fc2 | 2032 | |
fa73b229 | 2033 | if (fp->buf[3] & 0x04) |
ef416fc2 | 2034 | { |
2035 | /* | |
2036 | * Skip extra data... | |
2037 | */ | |
2038 | ||
2039 | if ((ptr + 2) > end) | |
2040 | { | |
2041 | /* | |
2042 | * Can't read from file! | |
2043 | */ | |
2044 | ||
2045 | return (-1); | |
2046 | } | |
2047 | ||
2048 | bytes = ((unsigned char)ptr[1] << 8) | (unsigned char)ptr[0]; | |
2049 | ptr += 2 + bytes; | |
2050 | ||
2051 | if (ptr > end) | |
2052 | { | |
2053 | /* | |
2054 | * Can't read from file! | |
2055 | */ | |
2056 | ||
2057 | return (-1); | |
2058 | } | |
2059 | } | |
2060 | ||
fa73b229 | 2061 | if (fp->buf[3] & 0x08) |
ef416fc2 | 2062 | { |
2063 | /* | |
2064 | * Skip original name data... | |
2065 | */ | |
2066 | ||
2067 | while (ptr < end && *ptr) | |
2068 | ptr ++; | |
2069 | ||
2070 | if (ptr < end) | |
2071 | ptr ++; | |
2072 | else | |
2073 | { | |
2074 | /* | |
2075 | * Can't read from file! | |
2076 | */ | |
2077 | ||
2078 | return (-1); | |
2079 | } | |
2080 | } | |
2081 | ||
fa73b229 | 2082 | if (fp->buf[3] & 0x10) |
ef416fc2 | 2083 | { |
2084 | /* | |
2085 | * Skip comment data... | |
2086 | */ | |
2087 | ||
2088 | while (ptr < end && *ptr) | |
2089 | ptr ++; | |
2090 | ||
2091 | if (ptr < end) | |
2092 | ptr ++; | |
2093 | else | |
2094 | { | |
2095 | /* | |
2096 | * Can't read from file! | |
2097 | */ | |
2098 | ||
2099 | return (-1); | |
2100 | } | |
2101 | } | |
2102 | ||
fa73b229 | 2103 | if (fp->buf[3] & 0x02) |
ef416fc2 | 2104 | { |
2105 | /* | |
2106 | * Skip header CRC data... | |
2107 | */ | |
2108 | ||
2109 | ptr += 2; | |
2110 | ||
2111 | if (ptr > end) | |
2112 | { | |
2113 | /* | |
2114 | * Can't read from file! | |
2115 | */ | |
2116 | ||
2117 | return (-1); | |
2118 | } | |
2119 | } | |
2120 | ||
fa73b229 | 2121 | /* |
2122 | * Copy the flate-compressed data to the compression buffer... | |
2123 | */ | |
2124 | ||
2125 | if ((bytes = end - ptr) > 0) | |
2126 | memcpy(fp->cbuf, ptr, bytes); | |
2127 | ||
ef416fc2 | 2128 | /* |
2129 | * Setup the decompressor data... | |
2130 | */ | |
2131 | ||
2132 | fp->stream.zalloc = (alloc_func)0; | |
2133 | fp->stream.zfree = (free_func)0; | |
2134 | fp->stream.opaque = (voidpf)0; | |
fa73b229 | 2135 | fp->stream.next_in = (Bytef *)fp->cbuf; |
ef416fc2 | 2136 | fp->stream.next_out = NULL; |
fa73b229 | 2137 | fp->stream.avail_in = bytes; |
ef416fc2 | 2138 | fp->stream.avail_out = 0; |
2139 | fp->crc = crc32(0L, Z_NULL, 0); | |
2140 | ||
2141 | if (inflateInit2(&(fp->stream), -15) != Z_OK) | |
2142 | return (-1); | |
2143 | ||
2144 | fp->compressed = 1; | |
2145 | } | |
2146 | ||
2147 | if (fp->compressed) | |
2148 | { | |
2149 | /* | |
2150 | * If we have reached end-of-file, return immediately... | |
2151 | */ | |
2152 | ||
2153 | if (fp->eof) | |
2154 | return (-1); | |
2155 | ||
2156 | /* | |
2157 | * Fill the decompression buffer as needed... | |
2158 | */ | |
2159 | ||
2160 | if (fp->stream.avail_in == 0) | |
2161 | { | |
2162 | if ((bytes = cups_read(fp, (char *)fp->cbuf, sizeof(fp->cbuf))) <= 0) | |
2163 | return (-1); | |
2164 | ||
2165 | fp->stream.next_in = fp->cbuf; | |
2166 | fp->stream.avail_in = bytes; | |
2167 | } | |
2168 | ||
2169 | /* | |
2170 | * Decompress data from the buffer... | |
2171 | */ | |
2172 | ||
2173 | fp->stream.next_out = (Bytef *)fp->buf; | |
2174 | fp->stream.avail_out = sizeof(fp->buf); | |
2175 | ||
c277e2f8 MS |
2176 | status = inflate(&(fp->stream), Z_NO_FLUSH); |
2177 | ||
2178 | if (fp->stream.next_out > (Bytef *)fp->buf) | |
2179 | fp->crc = crc32(fp->crc, (Bytef *)fp->buf, | |
2180 | fp->stream.next_out - (Bytef *)fp->buf); | |
2181 | ||
2182 | if (status == Z_STREAM_END) | |
ef416fc2 | 2183 | { |
2184 | /* | |
2185 | * Read the CRC and length... | |
2186 | */ | |
2187 | ||
2188 | unsigned char trailer[8]; /* Trailer bytes */ | |
2189 | uLong tcrc; /* Trailer CRC */ | |
2190 | ||
2191 | ||
2192 | if (read(fp->fd, trailer, sizeof(trailer)) < sizeof(trailer)) | |
2193 | { | |
2194 | /* | |
2195 | * Can't get it, so mark end-of-file... | |
2196 | */ | |
2197 | ||
2198 | fp->eof = 1; | |
ef416fc2 | 2199 | } |
fa73b229 | 2200 | else |
2201 | { | |
2202 | tcrc = (((((trailer[3] << 8) | trailer[2]) << 8) | trailer[1]) << 8) | | |
2203 | trailer[0]; | |
ef416fc2 | 2204 | |
fa73b229 | 2205 | if (tcrc != fp->crc) |
2206 | { | |
2207 | /* | |
2208 | * Bad CRC, mark end-of-file... | |
2209 | */ | |
2210 | ||
c277e2f8 MS |
2211 | DEBUG_printf(("cups_fill: tcrc=%08x, fp->crc=%08x\n", |
2212 | (unsigned int)tcrc, (unsigned int)fp->crc)); | |
2213 | ||
fa73b229 | 2214 | fp->eof = 1; |
2215 | ||
2216 | return (-1); | |
2217 | } | |
ef416fc2 | 2218 | |
ef416fc2 | 2219 | /* |
fa73b229 | 2220 | * Otherwise, reset the compressed flag so that we re-read the |
2221 | * file header... | |
ef416fc2 | 2222 | */ |
ef416fc2 | 2223 | |
fa73b229 | 2224 | fp->compressed = 0; |
ef416fc2 | 2225 | } |
ef416fc2 | 2226 | } |
2227 | ||
2228 | bytes = sizeof(fp->buf) - fp->stream.avail_out; | |
2229 | ||
2230 | /* | |
2231 | * Return the decompressed data... | |
2232 | */ | |
2233 | ||
2234 | fp->ptr = fp->buf; | |
2235 | fp->end = fp->buf + bytes; | |
2236 | ||
fa73b229 | 2237 | if (bytes) |
2238 | return (bytes); | |
ef416fc2 | 2239 | } |
2240 | } | |
2241 | #endif /* HAVE_LIBZ */ | |
2242 | ||
2243 | /* | |
2244 | * Read a buffer's full of data... | |
2245 | */ | |
2246 | ||
2247 | if ((bytes = cups_read(fp, fp->buf, sizeof(fp->buf))) <= 0) | |
2248 | { | |
2249 | /* | |
2250 | * Can't read from file! | |
2251 | */ | |
2252 | ||
2253 | fp->eof = 1; | |
2254 | fp->ptr = fp->buf; | |
2255 | fp->end = fp->buf; | |
2256 | ||
2257 | return (-1); | |
2258 | } | |
2259 | ||
2260 | /* | |
2261 | * Return the bytes we read... | |
2262 | */ | |
2263 | ||
2264 | fp->eof = 0; | |
2265 | fp->ptr = fp->buf; | |
2266 | fp->end = fp->buf + bytes; | |
2267 | ||
2268 | return (bytes); | |
2269 | } | |
2270 | ||
2271 | ||
2272 | /* | |
2273 | * 'cups_read()' - Read from a file descriptor. | |
2274 | */ | |
2275 | ||
2276 | static ssize_t /* O - Number of bytes read or -1 */ | |
2277 | cups_read(cups_file_t *fp, /* I - CUPS file */ | |
2278 | char *buf, /* I - Buffer */ | |
2279 | size_t bytes) /* I - Number bytes */ | |
2280 | { | |
2281 | ssize_t total; /* Total bytes read */ | |
2282 | ||
2283 | ||
634763e8 MS |
2284 | DEBUG_printf(("cups_read(fp=%p, buf=%p, bytes=" CUPS_LLFMT ")\n", fp, buf, |
2285 | CUPS_LLCAST bytes)); | |
2286 | ||
ef416fc2 | 2287 | /* |
2288 | * Loop until we read at least 0 bytes... | |
2289 | */ | |
2290 | ||
2291 | for (;;) | |
2292 | { | |
b86bc4cf | 2293 | #ifdef WIN32 |
2294 | if (fp->mode == 's') | |
2295 | total = (ssize_t)recv(fp->fd, buf, (unsigned)bytes, 0); | |
2296 | else | |
2297 | total = (ssize_t)read(fp->fd, buf, (unsigned)bytes); | |
2298 | #else | |
ef416fc2 | 2299 | if (fp->mode == 's') |
2300 | total = recv(fp->fd, buf, bytes, 0); | |
2301 | else | |
2302 | total = read(fp->fd, buf, bytes); | |
b86bc4cf | 2303 | #endif /* WIN32 */ |
ef416fc2 | 2304 | |
634763e8 MS |
2305 | DEBUG_printf(("cups_read: total=" CUPS_LLFMT "\n", CUPS_LLCAST total)); |
2306 | ||
ef416fc2 | 2307 | if (total >= 0) |
2308 | break; | |
2309 | ||
2310 | /* | |
2311 | * Reads can be interrupted by signals and unavailable resources... | |
2312 | */ | |
2313 | ||
2314 | if (errno == EAGAIN || errno == EINTR) | |
2315 | continue; | |
2316 | else | |
2317 | return (-1); | |
2318 | } | |
2319 | ||
2320 | /* | |
2321 | * Return the total number of bytes read... | |
2322 | */ | |
2323 | ||
2324 | return (total); | |
2325 | } | |
2326 | ||
2327 | ||
2328 | /* | |
2329 | * 'cups_write()' - Write to a file descriptor. | |
2330 | */ | |
2331 | ||
2332 | static ssize_t /* O - Number of bytes written or -1 */ | |
2333 | cups_write(cups_file_t *fp, /* I - CUPS file */ | |
2334 | const char *buf, /* I - Buffer */ | |
2335 | size_t bytes) /* I - Number bytes */ | |
2336 | { | |
2abf387c | 2337 | size_t total; /* Total bytes written */ |
2338 | ssize_t count; /* Count this time */ | |
ef416fc2 | 2339 | |
2340 | ||
634763e8 MS |
2341 | DEBUG_printf(("cups_write(fp=%p, buf=%p, bytes=" CUPS_LLFMT ")\n", fp, buf, |
2342 | CUPS_LLCAST bytes)); | |
ecdc0628 | 2343 | |
ef416fc2 | 2344 | /* |
2345 | * Loop until all bytes are written... | |
2346 | */ | |
2347 | ||
2348 | total = 0; | |
2349 | while (bytes > 0) | |
2350 | { | |
b86bc4cf | 2351 | #ifdef WIN32 |
2352 | if (fp->mode == 's') | |
2353 | count = (ssize_t)send(fp->fd, buf, (unsigned)bytes, 0); | |
2354 | else | |
2355 | count = (ssize_t)write(fp->fd, buf, (unsigned)bytes); | |
2356 | #else | |
ef416fc2 | 2357 | if (fp->mode == 's') |
2358 | count = send(fp->fd, buf, bytes, 0); | |
2359 | else | |
2360 | count = write(fp->fd, buf, bytes); | |
b86bc4cf | 2361 | #endif /* WIN32 */ |
ef416fc2 | 2362 | |
634763e8 MS |
2363 | DEBUG_printf(("cups_write: count=" CUPS_LLFMT "\n", CUPS_LLCAST count)); |
2364 | ||
ef416fc2 | 2365 | if (count < 0) |
2366 | { | |
2367 | /* | |
2368 | * Writes can be interrupted by signals and unavailable resources... | |
2369 | */ | |
2370 | ||
2371 | if (errno == EAGAIN || errno == EINTR) | |
2372 | continue; | |
2373 | else | |
2374 | return (-1); | |
2375 | } | |
2376 | ||
2377 | /* | |
2378 | * Update the counts for the last write call... | |
2379 | */ | |
2380 | ||
2381 | bytes -= count; | |
2382 | total += count; | |
2383 | buf += count; | |
2384 | } | |
2385 | ||
2386 | /* | |
2387 | * Return the total number of bytes written... | |
2388 | */ | |
2389 | ||
b86bc4cf | 2390 | return ((ssize_t)total); |
ef416fc2 | 2391 | } |
2392 | ||
2393 | ||
2394 | /* | |
75bd9771 | 2395 | * End of "$Id: file.c 7672 2008-06-18 22:03:02Z mike $". |
ef416fc2 | 2396 | */ |