]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
2abf387c | 2 | * "$Id: file.c 5993 2006-09-29 12:57:31Z 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 | { | |
2abf387c | 380 | ssize_t bytes; /* Bytes to write */ |
ef416fc2 | 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 */ | |
2abf387c | 994 | ssize_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 | { | |
2abf387c | 1093 | ssize_t bytes; /* Bytes to write */ |
ef416fc2 | 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 | { | |
2abf387c | 1152 | size_t total; /* Total bytes read */ |
1153 | ssize_t count; /* Bytes read */ | |
ef416fc2 | 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; | |
d09495fa | 1263 | fp->eof = 0; |
80ca4592 | 1264 | |
1265 | return (0); | |
ef416fc2 | 1266 | } |
1267 | ||
1268 | ||
1269 | /* | |
1270 | * 'cupsFileSeek()' - Seek in a file. | |
1271 | */ | |
1272 | ||
1273 | off_t /* O - New file position or -1 */ | |
1274 | cupsFileSeek(cups_file_t *fp, /* I - CUPS file */ | |
1275 | off_t pos) /* I - Position in file */ | |
1276 | { | |
2abf387c | 1277 | ssize_t bytes; /* Number bytes in buffer */ |
ef416fc2 | 1278 | |
1279 | ||
b423cd4c | 1280 | DEBUG_printf(("cupsFileSeek(fp=%p, pos=" CUPS_LLFMT ")\n", fp, pos)); |
1281 | DEBUG_printf((" fp->pos=" CUPS_LLFMT "\n", fp->pos)); | |
1282 | DEBUG_printf((" fp->ptr=%p, fp->end=%p\n", fp->ptr, fp->end)); | |
ef416fc2 | 1283 | |
1284 | /* | |
1285 | * Range check input... | |
1286 | */ | |
1287 | ||
1288 | if (!fp || pos < 0 || fp->mode != 'r') | |
1289 | return (-1); | |
1290 | ||
80ca4592 | 1291 | /* |
1292 | * Handle special cases... | |
1293 | */ | |
1294 | ||
1295 | if (pos == 0) | |
1296 | return (cupsFileRewind(fp)); | |
1297 | ||
b423cd4c | 1298 | if (fp->pos == pos) |
1299 | { | |
1300 | /* | |
1301 | * No seeking necessary... | |
1302 | */ | |
1303 | ||
1304 | if (fp->ptr) | |
1305 | { | |
1306 | fp->ptr = fp->buf; | |
1307 | fp->eof = 0; | |
1308 | } | |
1309 | ||
1310 | return (pos); | |
1311 | } | |
1312 | ||
80ca4592 | 1313 | #ifdef HAVE_LIBZ |
1314 | if (!fp->compressed && !fp->ptr) | |
1315 | { | |
1316 | /* | |
1317 | * Preload a buffer to determine whether the file is compressed... | |
1318 | */ | |
1319 | ||
1320 | if (cups_fill(fp) < 0) | |
1321 | return (-1); | |
1322 | } | |
1323 | #endif /* HAVE_LIBZ */ | |
1324 | ||
ef416fc2 | 1325 | /* |
1326 | * Figure out the number of bytes in the current buffer, and then | |
1327 | * see if we are outside of it... | |
1328 | */ | |
1329 | ||
80ca4592 | 1330 | if (fp->ptr) |
1331 | bytes = fp->end - fp->buf; | |
1332 | else | |
1333 | bytes = 0; | |
1334 | ||
ef416fc2 | 1335 | fp->eof = 0; |
1336 | ||
80ca4592 | 1337 | DEBUG_printf((" bytes=" CUPS_LLFMT "\n", CUPS_LLCAST bytes)); |
1338 | ||
ef416fc2 | 1339 | if (pos < fp->pos) |
1340 | { | |
1341 | /* | |
1342 | * Need to seek backwards... | |
1343 | */ | |
1344 | ||
80ca4592 | 1345 | DEBUG_puts(" SEEK BACKWARDS"); |
1346 | ||
ef416fc2 | 1347 | #ifdef HAVE_LIBZ |
1348 | if (fp->compressed) | |
1349 | { | |
1350 | inflateEnd(&fp->stream); | |
1351 | ||
1352 | lseek(fp->fd, 0, SEEK_SET); | |
1353 | fp->pos = 0; | |
1354 | fp->ptr = NULL; | |
1355 | fp->end = NULL; | |
1356 | ||
1357 | while ((bytes = cups_fill(fp)) > 0) | |
1358 | if (pos >= fp->pos && pos < (fp->pos + bytes)) | |
1359 | break; | |
1360 | ||
1361 | if (bytes <= 0) | |
1362 | return (-1); | |
80ca4592 | 1363 | |
1364 | fp->ptr = fp->buf + pos - fp->pos; | |
ef416fc2 | 1365 | } |
1366 | else | |
1367 | #endif /* HAVE_LIBZ */ | |
1368 | { | |
1369 | fp->pos = lseek(fp->fd, pos, SEEK_SET); | |
ef416fc2 | 1370 | fp->ptr = NULL; |
1371 | fp->end = NULL; | |
80ca4592 | 1372 | |
1373 | DEBUG_printf((" lseek() returned %ld...\n", (long)fp->pos)); | |
ef416fc2 | 1374 | } |
1375 | } | |
1376 | else if (pos >= (fp->pos + bytes)) | |
1377 | { | |
1378 | /* | |
1379 | * Need to seek forwards... | |
1380 | */ | |
1381 | ||
80ca4592 | 1382 | DEBUG_puts(" SEEK FORWARDS"); |
1383 | ||
ef416fc2 | 1384 | #ifdef HAVE_LIBZ |
80ca4592 | 1385 | if (fp->compressed) |
ef416fc2 | 1386 | { |
1387 | while ((bytes = cups_fill(fp)) > 0) | |
80ca4592 | 1388 | { |
ef416fc2 | 1389 | if (pos >= fp->pos && pos < (fp->pos + bytes)) |
1390 | break; | |
80ca4592 | 1391 | } |
ef416fc2 | 1392 | |
1393 | if (bytes <= 0) | |
1394 | return (-1); | |
80ca4592 | 1395 | |
1396 | fp->ptr = fp->buf + pos - fp->pos; | |
ef416fc2 | 1397 | } |
1398 | else | |
1399 | #endif /* HAVE_LIBZ */ | |
1400 | { | |
1401 | fp->pos = lseek(fp->fd, pos, SEEK_SET); | |
ef416fc2 | 1402 | fp->ptr = NULL; |
1403 | fp->end = NULL; | |
80ca4592 | 1404 | |
1405 | DEBUG_printf((" lseek() returned " CUPS_LLFMT "...\n", fp->pos)); | |
ef416fc2 | 1406 | } |
1407 | } | |
1408 | else | |
1409 | { | |
1410 | /* | |
1411 | * Just reposition the current pointer, since we have the right | |
1412 | * range... | |
1413 | */ | |
1414 | ||
80ca4592 | 1415 | DEBUG_puts(" SEEK INSIDE BUFFER"); |
1416 | ||
ef416fc2 | 1417 | fp->ptr = fp->buf + pos - fp->pos; |
ef416fc2 | 1418 | } |
1419 | ||
1420 | return (fp->pos); | |
1421 | } | |
1422 | ||
1423 | ||
80ca4592 | 1424 | /* |
1425 | * 'cupsFileStderr()' - Return a CUPS file associated with stderr. | |
1426 | */ | |
1427 | ||
1428 | cups_file_t * | |
1429 | cupsFileStderr(void) | |
1430 | { | |
1431 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals... */ | |
1432 | ||
1433 | ||
1434 | /* | |
1435 | * Open file descriptor 2 as needed... | |
1436 | */ | |
1437 | ||
1438 | if (!cg->stdio_files[2]) | |
1439 | { | |
1440 | /* | |
1441 | * Flush any pending output on the stdio file... | |
1442 | */ | |
1443 | ||
1444 | fflush(stderr); | |
1445 | ||
1446 | /* | |
1447 | * Open file descriptor 2... | |
1448 | */ | |
1449 | ||
1450 | if ((cg->stdio_files[2] = cupsFileOpenFd(2, "w")) != NULL) | |
1451 | cg->stdio_files[2]->is_stdio = 1; | |
1452 | } | |
1453 | ||
1454 | return (cg->stdio_files[2]); | |
1455 | } | |
1456 | ||
1457 | ||
1458 | /* | |
1459 | * 'cupsFileStdin()' - Return a CUPS file associated with stdin. | |
1460 | */ | |
1461 | ||
1462 | cups_file_t * | |
1463 | cupsFileStdin(void) | |
1464 | { | |
1465 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals... */ | |
1466 | ||
1467 | ||
1468 | /* | |
1469 | * Open file descriptor 0 as needed... | |
1470 | */ | |
1471 | ||
1472 | if (!cg->stdio_files[0]) | |
1473 | { | |
1474 | /* | |
1475 | * Open file descriptor 0... | |
1476 | */ | |
1477 | ||
1478 | if ((cg->stdio_files[0] = cupsFileOpenFd(0, "r")) != NULL) | |
1479 | cg->stdio_files[0]->is_stdio = 1; | |
1480 | } | |
1481 | ||
1482 | return (cg->stdio_files[0]); | |
1483 | } | |
1484 | ||
1485 | ||
1486 | /* | |
1487 | * 'cupsFileStdout()' - Return a CUPS file associated with stdout. | |
1488 | */ | |
1489 | ||
1490 | cups_file_t * | |
1491 | cupsFileStdout(void) | |
1492 | { | |
1493 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals... */ | |
1494 | ||
1495 | ||
1496 | /* | |
1497 | * Open file descriptor 1 as needed... | |
1498 | */ | |
1499 | ||
1500 | if (!cg->stdio_files[1]) | |
1501 | { | |
1502 | /* | |
1503 | * Flush any pending output on the stdio file... | |
1504 | */ | |
1505 | ||
1506 | fflush(stdout); | |
1507 | ||
1508 | /* | |
1509 | * Open file descriptor 1... | |
1510 | */ | |
1511 | ||
1512 | if ((cg->stdio_files[1] = cupsFileOpenFd(1, "w")) != NULL) | |
1513 | cg->stdio_files[1]->is_stdio = 1; | |
1514 | } | |
1515 | ||
1516 | return (cg->stdio_files[1]); | |
1517 | } | |
1518 | ||
1519 | ||
ef416fc2 | 1520 | /* |
1521 | * 'cupsFileTell()' - Return the current file position. | |
1522 | */ | |
1523 | ||
1524 | off_t /* O - File position */ | |
1525 | cupsFileTell(cups_file_t *fp) /* I - CUPS file */ | |
1526 | { | |
80ca4592 | 1527 | return (fp ? fp->pos : 0); |
ef416fc2 | 1528 | } |
1529 | ||
1530 | ||
1531 | /* | |
1532 | * 'cupsFileUnlock()' - Unlock access to a file. | |
1533 | */ | |
1534 | ||
1535 | int /* O - 0 on success, -1 on error */ | |
1536 | cupsFileUnlock(cups_file_t *fp) /* I - File to lock */ | |
1537 | { | |
1538 | /* | |
1539 | * Range check... | |
1540 | */ | |
1541 | ||
1542 | if (!fp || fp->mode == 's') | |
1543 | return (-1); | |
1544 | ||
1545 | /* | |
1546 | * Unlock... | |
1547 | */ | |
1548 | ||
1549 | #ifdef WIN32 | |
1550 | return (locking(fp->fd, _LK_UNLCK, 0)); | |
1551 | #else | |
1552 | return (lockf(fp->fd, F_ULOCK, 0)); | |
1553 | #endif /* WIN32 */ | |
1554 | } | |
1555 | ||
1556 | ||
1557 | /* | |
1558 | * 'cupsFileWrite()' - Write to a file. | |
1559 | */ | |
1560 | ||
1561 | ssize_t /* O - Number of bytes written */ | |
1562 | cupsFileWrite(cups_file_t *fp, /* I - CUPS file */ | |
1563 | const char *buf, /* I - Buffer */ | |
1564 | size_t bytes) /* I - Number of bytes to write */ | |
1565 | { | |
1566 | /* | |
1567 | * Range check input... | |
1568 | */ | |
1569 | ||
1570 | if (!fp || !buf || bytes < 0 || (fp->mode != 'w' && fp->mode != 's')) | |
1571 | return (-1); | |
1572 | ||
1573 | if (bytes == 0) | |
1574 | return (0); | |
1575 | ||
1576 | /* | |
1577 | * Write the buffer... | |
1578 | */ | |
1579 | ||
1580 | if (fp->mode == 's') | |
1581 | { | |
1582 | if (cups_write(fp, buf, bytes) < 0) | |
1583 | return (-1); | |
1584 | ||
1585 | fp->pos += bytes; | |
1586 | ||
1587 | return (bytes); | |
1588 | } | |
1589 | ||
1590 | if ((fp->ptr + bytes) > fp->end) | |
1591 | if (cupsFileFlush(fp)) | |
1592 | return (-1); | |
1593 | ||
1594 | fp->pos += bytes; | |
1595 | ||
1596 | if (bytes > sizeof(fp->buf)) | |
1597 | { | |
1598 | #ifdef HAVE_LIBZ | |
1599 | if (fp->compressed) | |
1600 | return (cups_compress(fp, buf, bytes)); | |
1601 | else | |
1602 | #endif /* HAVE_LIBZ */ | |
1603 | return (cups_write(fp, buf, bytes)); | |
1604 | } | |
1605 | else | |
1606 | { | |
1607 | memcpy(fp->ptr, buf, bytes); | |
1608 | fp->ptr += bytes; | |
1609 | return (bytes); | |
1610 | } | |
1611 | } | |
1612 | ||
1613 | ||
1614 | #ifdef HAVE_LIBZ | |
1615 | /* | |
1616 | * 'cups_compress()' - Compress a buffer of data... | |
1617 | */ | |
1618 | ||
1619 | static ssize_t /* O - Number of bytes written or -1 */ | |
1620 | cups_compress(cups_file_t *fp, /* I - CUPS file */ | |
1621 | const char *buf, /* I - Buffer */ | |
1622 | size_t bytes) /* I - Number bytes */ | |
1623 | { | |
ecdc0628 | 1624 | DEBUG_printf(("cups_compress(fp=%p, buf=%p, bytes=%ld)\n", fp, buf, |
1625 | (long)bytes)); | |
1626 | ||
ef416fc2 | 1627 | /* |
1628 | * Update the CRC... | |
1629 | */ | |
1630 | ||
1631 | fp->crc = crc32(fp->crc, (const Bytef *)buf, bytes); | |
1632 | ||
1633 | /* | |
1634 | * Deflate the bytes... | |
1635 | */ | |
1636 | ||
1637 | fp->stream.next_in = (Bytef *)buf; | |
1638 | fp->stream.avail_in = bytes; | |
1639 | ||
1640 | while (fp->stream.avail_in > 0) | |
1641 | { | |
1642 | /* | |
1643 | * Flush the current buffer... | |
1644 | */ | |
1645 | ||
ecdc0628 | 1646 | DEBUG_printf((" avail_in=%d, avail_out=%d\n", fp->stream.avail_in, |
1647 | fp->stream.avail_out)); | |
1648 | ||
ef416fc2 | 1649 | if (fp->stream.avail_out < (int)(sizeof(fp->cbuf) / 8)) |
1650 | { | |
1651 | if (cups_write(fp, (char *)fp->cbuf, fp->stream.next_out - fp->cbuf) < 0) | |
1652 | return (-1); | |
ecdc0628 | 1653 | |
1654 | fp->stream.next_out = fp->cbuf; | |
1655 | fp->stream.avail_out = sizeof(fp->cbuf); | |
ef416fc2 | 1656 | } |
1657 | ||
1658 | deflate(&(fp->stream), Z_NO_FLUSH); | |
1659 | } | |
1660 | ||
1661 | return (bytes); | |
1662 | } | |
1663 | #endif /* HAVE_LIBZ */ | |
1664 | ||
1665 | ||
1666 | /* | |
1667 | * 'cups_fill()' - Fill the input buffer... | |
1668 | */ | |
1669 | ||
1670 | static ssize_t /* O - Number of bytes or -1 */ | |
1671 | cups_fill(cups_file_t *fp) /* I - CUPS file */ | |
1672 | { | |
1673 | ssize_t bytes; /* Number of bytes read */ | |
1674 | #ifdef HAVE_LIBZ | |
1675 | const unsigned char *ptr, /* Pointer into buffer */ | |
1676 | *end; /* End of buffer */ | |
1677 | #endif /* HAVE_LIBZ */ | |
1678 | ||
1679 | ||
1680 | DEBUG_printf(("cups_fill(fp=%p)\n", fp)); | |
b423cd4c | 1681 | DEBUG_printf((" fp->ptr=%p, fp->end=%p, fp->buf=%p, " |
1682 | "fp->pos=" CUPS_LLFMT ", fp->eof=%d\n", | |
1683 | fp->ptr, fp->end, fp->buf, fp->pos, fp->eof)); | |
ef416fc2 | 1684 | |
1685 | /* | |
1686 | * Update the "pos" element as needed... | |
1687 | */ | |
1688 | ||
1689 | if (fp->ptr && fp->end) | |
1690 | fp->pos += fp->end - fp->buf; | |
1691 | ||
1692 | #ifdef HAVE_LIBZ | |
b423cd4c | 1693 | DEBUG_printf((" fp->compressed=%d\n", fp->compressed)); |
1694 | ||
ef416fc2 | 1695 | while (!fp->ptr || fp->compressed) |
1696 | { | |
1697 | /* | |
1698 | * Check to see if we have read any data yet; if not, see if we have a | |
1699 | * compressed file... | |
1700 | */ | |
1701 | ||
1702 | if (!fp->ptr) | |
1703 | { | |
1704 | /* | |
1705 | * Reset the file position in case we are seeking... | |
1706 | */ | |
1707 | ||
1708 | fp->compressed = 0; | |
ef416fc2 | 1709 | |
1710 | /* | |
1711 | * Read the first bytes in the file to determine if we have a gzip'd | |
1712 | * file... | |
1713 | */ | |
1714 | ||
fa73b229 | 1715 | if ((bytes = cups_read(fp, (char *)fp->buf, sizeof(fp->buf))) < 0) |
ef416fc2 | 1716 | { |
1717 | /* | |
1718 | * Can't read from file! | |
1719 | */ | |
1720 | ||
b423cd4c | 1721 | DEBUG_printf((" cups_read() returned " CUPS_LLFMT "!\n", |
1722 | CUPS_LLCAST bytes)); | |
1723 | ||
ef416fc2 | 1724 | return (-1); |
1725 | } | |
1726 | ||
fa73b229 | 1727 | if (bytes < 10 || fp->buf[0] != 0x1f || |
e00b005a | 1728 | (fp->buf[1] & 255) != 0x8b || |
fa73b229 | 1729 | fp->buf[2] != 8 || (fp->buf[3] & 0xe0) != 0) |
ef416fc2 | 1730 | { |
1731 | /* | |
1732 | * Not a gzip'd file! | |
1733 | */ | |
1734 | ||
ef416fc2 | 1735 | fp->ptr = fp->buf; |
1736 | fp->end = fp->buf + bytes; | |
1737 | ||
b423cd4c | 1738 | DEBUG_printf((" returning " CUPS_LLFMT "!\n", CUPS_LLCAST bytes)); |
1739 | ||
ef416fc2 | 1740 | return (bytes); |
1741 | } | |
1742 | ||
1743 | /* | |
1744 | * Parse header junk: extra data, original name, and comment... | |
1745 | */ | |
1746 | ||
fa73b229 | 1747 | ptr = (unsigned char *)fp->buf + 10; |
1748 | end = (unsigned char *)fp->buf + bytes; | |
ef416fc2 | 1749 | |
fa73b229 | 1750 | if (fp->buf[3] & 0x04) |
ef416fc2 | 1751 | { |
1752 | /* | |
1753 | * Skip extra data... | |
1754 | */ | |
1755 | ||
1756 | if ((ptr + 2) > end) | |
1757 | { | |
1758 | /* | |
1759 | * Can't read from file! | |
1760 | */ | |
1761 | ||
1762 | return (-1); | |
1763 | } | |
1764 | ||
1765 | bytes = ((unsigned char)ptr[1] << 8) | (unsigned char)ptr[0]; | |
1766 | ptr += 2 + bytes; | |
1767 | ||
1768 | if (ptr > end) | |
1769 | { | |
1770 | /* | |
1771 | * Can't read from file! | |
1772 | */ | |
1773 | ||
1774 | return (-1); | |
1775 | } | |
1776 | } | |
1777 | ||
fa73b229 | 1778 | if (fp->buf[3] & 0x08) |
ef416fc2 | 1779 | { |
1780 | /* | |
1781 | * Skip original name data... | |
1782 | */ | |
1783 | ||
1784 | while (ptr < end && *ptr) | |
1785 | ptr ++; | |
1786 | ||
1787 | if (ptr < end) | |
1788 | ptr ++; | |
1789 | else | |
1790 | { | |
1791 | /* | |
1792 | * Can't read from file! | |
1793 | */ | |
1794 | ||
1795 | return (-1); | |
1796 | } | |
1797 | } | |
1798 | ||
fa73b229 | 1799 | if (fp->buf[3] & 0x10) |
ef416fc2 | 1800 | { |
1801 | /* | |
1802 | * Skip comment data... | |
1803 | */ | |
1804 | ||
1805 | while (ptr < end && *ptr) | |
1806 | ptr ++; | |
1807 | ||
1808 | if (ptr < end) | |
1809 | ptr ++; | |
1810 | else | |
1811 | { | |
1812 | /* | |
1813 | * Can't read from file! | |
1814 | */ | |
1815 | ||
1816 | return (-1); | |
1817 | } | |
1818 | } | |
1819 | ||
fa73b229 | 1820 | if (fp->buf[3] & 0x02) |
ef416fc2 | 1821 | { |
1822 | /* | |
1823 | * Skip header CRC data... | |
1824 | */ | |
1825 | ||
1826 | ptr += 2; | |
1827 | ||
1828 | if (ptr > end) | |
1829 | { | |
1830 | /* | |
1831 | * Can't read from file! | |
1832 | */ | |
1833 | ||
1834 | return (-1); | |
1835 | } | |
1836 | } | |
1837 | ||
fa73b229 | 1838 | /* |
1839 | * Copy the flate-compressed data to the compression buffer... | |
1840 | */ | |
1841 | ||
1842 | if ((bytes = end - ptr) > 0) | |
1843 | memcpy(fp->cbuf, ptr, bytes); | |
1844 | ||
ef416fc2 | 1845 | /* |
1846 | * Setup the decompressor data... | |
1847 | */ | |
1848 | ||
1849 | fp->stream.zalloc = (alloc_func)0; | |
1850 | fp->stream.zfree = (free_func)0; | |
1851 | fp->stream.opaque = (voidpf)0; | |
fa73b229 | 1852 | fp->stream.next_in = (Bytef *)fp->cbuf; |
ef416fc2 | 1853 | fp->stream.next_out = NULL; |
fa73b229 | 1854 | fp->stream.avail_in = bytes; |
ef416fc2 | 1855 | fp->stream.avail_out = 0; |
1856 | fp->crc = crc32(0L, Z_NULL, 0); | |
1857 | ||
1858 | if (inflateInit2(&(fp->stream), -15) != Z_OK) | |
1859 | return (-1); | |
1860 | ||
1861 | fp->compressed = 1; | |
1862 | } | |
1863 | ||
1864 | if (fp->compressed) | |
1865 | { | |
1866 | /* | |
1867 | * If we have reached end-of-file, return immediately... | |
1868 | */ | |
1869 | ||
1870 | if (fp->eof) | |
1871 | return (-1); | |
1872 | ||
1873 | /* | |
1874 | * Fill the decompression buffer as needed... | |
1875 | */ | |
1876 | ||
1877 | if (fp->stream.avail_in == 0) | |
1878 | { | |
1879 | if ((bytes = cups_read(fp, (char *)fp->cbuf, sizeof(fp->cbuf))) <= 0) | |
1880 | return (-1); | |
1881 | ||
1882 | fp->stream.next_in = fp->cbuf; | |
1883 | fp->stream.avail_in = bytes; | |
1884 | } | |
1885 | ||
1886 | /* | |
1887 | * Decompress data from the buffer... | |
1888 | */ | |
1889 | ||
1890 | fp->stream.next_out = (Bytef *)fp->buf; | |
1891 | fp->stream.avail_out = sizeof(fp->buf); | |
1892 | ||
1893 | if (inflate(&(fp->stream), Z_NO_FLUSH) == Z_STREAM_END) | |
1894 | { | |
1895 | /* | |
1896 | * Read the CRC and length... | |
1897 | */ | |
1898 | ||
1899 | unsigned char trailer[8]; /* Trailer bytes */ | |
1900 | uLong tcrc; /* Trailer CRC */ | |
1901 | ||
1902 | ||
1903 | if (read(fp->fd, trailer, sizeof(trailer)) < sizeof(trailer)) | |
1904 | { | |
1905 | /* | |
1906 | * Can't get it, so mark end-of-file... | |
1907 | */ | |
1908 | ||
1909 | fp->eof = 1; | |
ef416fc2 | 1910 | } |
fa73b229 | 1911 | else |
1912 | { | |
1913 | tcrc = (((((trailer[3] << 8) | trailer[2]) << 8) | trailer[1]) << 8) | | |
1914 | trailer[0]; | |
ef416fc2 | 1915 | |
fa73b229 | 1916 | if (tcrc != fp->crc) |
1917 | { | |
1918 | /* | |
1919 | * Bad CRC, mark end-of-file... | |
1920 | */ | |
1921 | ||
1922 | fp->eof = 1; | |
1923 | ||
1924 | return (-1); | |
1925 | } | |
ef416fc2 | 1926 | |
ef416fc2 | 1927 | /* |
fa73b229 | 1928 | * Otherwise, reset the compressed flag so that we re-read the |
1929 | * file header... | |
ef416fc2 | 1930 | */ |
ef416fc2 | 1931 | |
fa73b229 | 1932 | fp->compressed = 0; |
ef416fc2 | 1933 | } |
ef416fc2 | 1934 | } |
1935 | ||
1936 | bytes = sizeof(fp->buf) - fp->stream.avail_out; | |
1937 | ||
1938 | /* | |
1939 | * Return the decompressed data... | |
1940 | */ | |
1941 | ||
1942 | fp->ptr = fp->buf; | |
1943 | fp->end = fp->buf + bytes; | |
1944 | ||
fa73b229 | 1945 | if (bytes) |
1946 | return (bytes); | |
ef416fc2 | 1947 | } |
1948 | } | |
1949 | #endif /* HAVE_LIBZ */ | |
1950 | ||
1951 | /* | |
1952 | * Read a buffer's full of data... | |
1953 | */ | |
1954 | ||
1955 | if ((bytes = cups_read(fp, fp->buf, sizeof(fp->buf))) <= 0) | |
1956 | { | |
1957 | /* | |
1958 | * Can't read from file! | |
1959 | */ | |
1960 | ||
1961 | fp->eof = 1; | |
1962 | fp->ptr = fp->buf; | |
1963 | fp->end = fp->buf; | |
1964 | ||
1965 | return (-1); | |
1966 | } | |
1967 | ||
1968 | /* | |
1969 | * Return the bytes we read... | |
1970 | */ | |
1971 | ||
1972 | fp->eof = 0; | |
1973 | fp->ptr = fp->buf; | |
1974 | fp->end = fp->buf + bytes; | |
1975 | ||
1976 | return (bytes); | |
1977 | } | |
1978 | ||
1979 | ||
1980 | /* | |
1981 | * 'cups_read()' - Read from a file descriptor. | |
1982 | */ | |
1983 | ||
1984 | static ssize_t /* O - Number of bytes read or -1 */ | |
1985 | cups_read(cups_file_t *fp, /* I - CUPS file */ | |
1986 | char *buf, /* I - Buffer */ | |
1987 | size_t bytes) /* I - Number bytes */ | |
1988 | { | |
1989 | ssize_t total; /* Total bytes read */ | |
1990 | ||
1991 | ||
1992 | /* | |
1993 | * Loop until we read at least 0 bytes... | |
1994 | */ | |
1995 | ||
1996 | for (;;) | |
1997 | { | |
1998 | if (fp->mode == 's') | |
1999 | total = recv(fp->fd, buf, bytes, 0); | |
2000 | else | |
2001 | total = read(fp->fd, buf, bytes); | |
2002 | ||
2003 | if (total >= 0) | |
2004 | break; | |
2005 | ||
2006 | /* | |
2007 | * Reads can be interrupted by signals and unavailable resources... | |
2008 | */ | |
2009 | ||
2010 | if (errno == EAGAIN || errno == EINTR) | |
2011 | continue; | |
2012 | else | |
2013 | return (-1); | |
2014 | } | |
2015 | ||
2016 | /* | |
2017 | * Return the total number of bytes read... | |
2018 | */ | |
2019 | ||
2020 | return (total); | |
2021 | } | |
2022 | ||
2023 | ||
2024 | /* | |
2025 | * 'cups_write()' - Write to a file descriptor. | |
2026 | */ | |
2027 | ||
2028 | static ssize_t /* O - Number of bytes written or -1 */ | |
2029 | cups_write(cups_file_t *fp, /* I - CUPS file */ | |
2030 | const char *buf, /* I - Buffer */ | |
2031 | size_t bytes) /* I - Number bytes */ | |
2032 | { | |
2abf387c | 2033 | size_t total; /* Total bytes written */ |
2034 | ssize_t count; /* Count this time */ | |
ef416fc2 | 2035 | |
2036 | ||
ecdc0628 | 2037 | DEBUG_printf(("cups_write(fp=%p, buf=%p, bytes=%ld)\n", fp, buf, |
2038 | (long)bytes)); | |
2039 | ||
ef416fc2 | 2040 | /* |
2041 | * Loop until all bytes are written... | |
2042 | */ | |
2043 | ||
2044 | total = 0; | |
2045 | while (bytes > 0) | |
2046 | { | |
2047 | if (fp->mode == 's') | |
2048 | count = send(fp->fd, buf, bytes, 0); | |
2049 | else | |
2050 | count = write(fp->fd, buf, bytes); | |
2051 | ||
2052 | if (count < 0) | |
2053 | { | |
2054 | /* | |
2055 | * Writes can be interrupted by signals and unavailable resources... | |
2056 | */ | |
2057 | ||
2058 | if (errno == EAGAIN || errno == EINTR) | |
2059 | continue; | |
2060 | else | |
2061 | return (-1); | |
2062 | } | |
2063 | ||
ecdc0628 | 2064 | DEBUG_printf((" count=%ld\n", (long)count)); |
2065 | ||
ef416fc2 | 2066 | /* |
2067 | * Update the counts for the last write call... | |
2068 | */ | |
2069 | ||
2070 | bytes -= count; | |
2071 | total += count; | |
2072 | buf += count; | |
2073 | } | |
2074 | ||
2075 | /* | |
2076 | * Return the total number of bytes written... | |
2077 | */ | |
2078 | ||
2079 | return (total); | |
2080 | } | |
2081 | ||
2082 | ||
2083 | /* | |
2abf387c | 2084 | * End of "$Id: file.c 5993 2006-09-29 12:57:31Z mike $". |
ef416fc2 | 2085 | */ |