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