| 342 | */ |
| 343 | |
| 344 | int /* O - 0 on success, -1 on error */ |
| 345 | cupsFileClose(cups_file_t *fp) /* I - CUPS file */ |
| 346 | { |
| 347 | int fd; /* File descriptor */ |
| 348 | char mode; /* Open mode */ |
| 349 | int status; /* Return status */ |
| 350 | |
| 351 | |
| 352 | DEBUG_printf(("cupsFileClose(fp=%p)", (void *)fp)); |
| 353 | |
| 354 | /* |
| 355 | * Range check... |
| 356 | */ |
| 357 | |
| 358 | if (!fp) |
| 359 | return (-1); |
| 360 | |
| 361 | /* |
| 362 | * Flush pending write data... |
| 363 | */ |
| 364 | |
| 365 | if (fp->mode == 'w') |
| 366 | status = cupsFileFlush(fp); |
| 367 | else |
| 368 | status = 0; |
| 369 | |
| 370 | #ifdef HAVE_LIBZ |
| 371 | if (fp->compressed && status >= 0) |
| 372 | { |
| 373 | if (fp->mode == 'r') |
| 374 | { |
| 375 | /* |
| 376 | * Free decompression data... |
| 377 | */ |
| 378 | |
| 379 | inflateEnd(&fp->stream); |
| 380 | } |
| 381 | else |
| 382 | { |
| 383 | /* |
| 384 | * Flush any remaining compressed data... |
| 385 | */ |
| 386 | |
| 387 | unsigned char trailer[8]; /* Trailer CRC and length */ |
| 388 | int done; /* Done writing... */ |
| 389 | |
| 390 | |
| 391 | fp->stream.avail_in = 0; |
| 392 | |
| 393 | for (done = 0;;) |
| 394 | { |
| 395 | if (fp->stream.next_out > fp->cbuf) |
| 396 | { |
| 397 | if (cups_write(fp, (char *)fp->cbuf, |
| 398 | (size_t)(fp->stream.next_out - fp->cbuf)) < 0) |
| 399 | status = -1; |
| 400 | |
| 401 | fp->stream.next_out = fp->cbuf; |