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