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