@@ -467,13 +467,25 @@ struct cfp
467
467
static int hasSuffix (const char * filename ,const char * suffix );
468
468
#endif
469
469
470
+ /* free() without changing errno; useful in several places below */
471
+ static void
472
+ free_keep_errno (void * p )
473
+ {
474
+ int save_errno = errno ;
475
+
476
+ free (p );
477
+ errno = save_errno ;
478
+ }
479
+
470
480
/*
471
481
* Open a file for reading. 'path' is the file to open, and 'mode' should
472
482
* be either "r" or "rb".
473
483
*
474
484
* If the file at 'path' does not exist, we append the ".gz" suffix (if 'path'
475
485
* doesn't already have it) and try again. So if you pass "foo" as 'path',
476
486
* this will open either "foo" or "foo.gz".
487
+ *
488
+ * On failure, return NULL with an error code in errno.
477
489
*/
478
490
cfp *
479
491
cfopen_read (const char * path ,const char * mode )
@@ -498,7 +510,7 @@ cfopen_read(const char *path, const char *mode)
498
510
499
511
snprintf (fname ,fnamelen ,"%s%s" ,path ,".gz" );
500
512
fp = cfopen (fname ,mode ,1 );
501
- free (fname );
513
+ free_keep_errno (fname );
502
514
}
503
515
#endif
504
516
}
@@ -511,8 +523,10 @@ cfopen_read(const char *path, const char *mode)
511
523
* ("w", "wb", "a", or "ab").
512
524
*
513
525
* If 'compression' is non-zero, a gzip compressed stream is opened, and
514
- *and 'compression' indicates the compression level used. The ".gz" suffix
526
+ * 'compression' indicates the compression level used. The ".gz" suffix
515
527
* is automatically added to 'path' in that case.
528
+ *
529
+ * On failure, return NULL with an error code in errno.
516
530
*/
517
531
cfp *
518
532
cfopen_write (const char * path ,const char * mode ,int compression )
@@ -531,8 +545,8 @@ cfopen_write(const char *path, const char *mode, int compression)
531
545
die_horribly (NULL ,modulename ,"Out of memory\n" );
532
546
533
547
snprintf (fname ,fnamelen ,"%s%s" ,path ,".gz" );
534
- fp = cfopen (fname ,mode ,1 );
535
- free (fname );
548
+ fp = cfopen (fname ,mode ,compression );
549
+ free_keep_errno (fname );
536
550
#else
537
551
die_horribly (NULL ,modulename ,"not built with zlib support\n" );
538
552
fp = NULL ;/* keep compiler quiet */
@@ -543,7 +557,9 @@ cfopen_write(const char *path, const char *mode, int compression)
543
557
544
558
/*
545
559
* Opens file 'path' in 'mode'. If 'compression' is non-zero, the file
546
- * is opened with libz gzopen(), otherwise with plain fopen()
560
+ * is opened with libz gzopen(), otherwise with plain fopen().
561
+ *
562
+ * On failure, return NULL with an error code in errno.
547
563
*/
548
564
cfp *
549
565
cfopen (const char * path ,const char * mode ,int compression )
@@ -556,11 +572,15 @@ cfopen(const char *path, const char *mode, int compression)
556
572
if (compression != 0 )
557
573
{
558
574
#ifdef HAVE_LIBZ
559
- fp -> compressedfp = gzopen (path ,mode );
575
+ char mode_compression [32 ];
576
+
577
+ snprintf (mode_compression ,sizeof (mode_compression ),"%s%d" ,
578
+ mode ,compression );
579
+ fp -> compressedfp = gzopen (path ,mode_compression );
560
580
fp -> uncompressedfp = NULL ;
561
581
if (fp -> compressedfp == NULL )
562
582
{
563
- free (fp );
583
+ free_keep_errno (fp );
564
584
fp = NULL ;
565
585
}
566
586
#else
@@ -575,7 +595,7 @@ cfopen(const char *path, const char *mode, int compression)
575
595
fp -> uncompressedfp = fopen (path ,mode );
576
596
if (fp -> uncompressedfp == NULL )
577
597
{
578
- free (fp );
598
+ free_keep_errno (fp );
579
599
fp = NULL ;
580
600
}
581
601
}
@@ -650,7 +670,7 @@ cfclose(cfp *fp)
650
670
result = fclose (fp -> uncompressedfp );
651
671
fp -> uncompressedfp = NULL ;
652
672
}
653
- free (fp );
673
+ free_keep_errno (fp );
654
674
655
675
return result ;
656
676
}