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