@@ -453,13 +453,25 @@ struct cfp
453
453
static int hasSuffix (const char * filename , const char * suffix );
454
454
#endif
455
455
456
+ /* free() without changing errno; useful in several places below */
457
+ static void
458
+ free_keep_errno (void * p )
459
+ {
460
+ int save_errno = errno ;
461
+
462
+ free (p );
463
+ errno = save_errno ;
464
+ }
465
+
456
466
/*
457
467
* Open a file for reading. 'path' is the file to open, and 'mode' should
458
468
* be either "r" or "rb".
459
469
*
460
470
* If the file at 'path' does not exist, we append the ".gz" suffix (if 'path'
461
471
* doesn't already have it) and try again. So if you pass "foo" as 'path',
462
472
* this will open either "foo" or "foo.gz".
473
+ *
474
+ * On failure, return NULL with an error code in errno.
463
475
*/
464
476
cfp *
465
477
cfopen_read (const char * path , const char * mode )
@@ -480,7 +492,7 @@ cfopen_read(const char *path, const char *mode)
480
492
481
493
fname = psprintf ("%s.gz" , path );
482
494
fp = cfopen (fname , mode , 1 );
483
- free (fname );
495
+ free_keep_errno (fname );
484
496
}
485
497
#endif
486
498
}
@@ -493,8 +505,10 @@ cfopen_read(const char *path, const char *mode)
493
505
* ("w", "wb", "a", or "ab").
494
506
*
495
507
* If 'compression' is non-zero, a gzip compressed stream is opened, and
496
- * and 'compression' indicates the compression level used. The ".gz" suffix
508
+ * 'compression' indicates the compression level used. The ".gz" suffix
497
509
* is automatically added to 'path' in that case.
510
+ *
511
+ * On failure, return NULL with an error code in errno.
498
512
*/
499
513
cfp *
500
514
cfopen_write (const char * path , const char * mode , int compression )
@@ -509,8 +523,8 @@ cfopen_write(const char *path, const char *mode, int compression)
509
523
char * fname ;
510
524
511
525
fname = psprintf ("%s.gz" , path );
512
- fp = cfopen (fname , mode , 1 );
513
- free (fname );
526
+ fp = cfopen (fname , mode , compression );
527
+ free_keep_errno (fname );
514
528
#else
515
529
exit_horribly (modulename , "not built with zlib support\n" );
516
530
fp = NULL ; /* keep compiler quiet */
@@ -521,7 +535,9 @@ cfopen_write(const char *path, const char *mode, int compression)
521
535
522
536
/*
523
537
* Opens file 'path' in 'mode'. If 'compression' is non-zero, the file
524
- * is opened with libz gzopen(), otherwise with plain fopen()
538
+ * is opened with libz gzopen(), otherwise with plain fopen().
539
+ *
540
+ * On failure, return NULL with an error code in errno.
525
541
*/
526
542
cfp *
527
543
cfopen (const char * path , const char * mode , int compression )
@@ -531,11 +547,15 @@ cfopen(const char *path, const char *mode, int compression)
531
547
if (compression != 0 )
532
548
{
533
549
#ifdef HAVE_LIBZ
534
- fp -> compressedfp = gzopen (path , mode );
550
+ char mode_compression [32 ];
551
+
552
+ snprintf (mode_compression , sizeof (mode_compression ), "%s%d" ,
553
+ mode , compression );
554
+ fp -> compressedfp = gzopen (path , mode_compression );
535
555
fp -> uncompressedfp = NULL ;
536
556
if (fp -> compressedfp == NULL )
537
557
{
538
- free (fp );
558
+ free_keep_errno (fp );
539
559
fp = NULL ;
540
560
}
541
561
#else
@@ -550,7 +570,7 @@ cfopen(const char *path, const char *mode, int compression)
550
570
fp -> uncompressedfp = fopen (path , mode );
551
571
if (fp -> uncompressedfp == NULL )
552
572
{
553
- free (fp );
573
+ free_keep_errno (fp );
554
574
fp = NULL ;
555
575
}
556
576
}
@@ -659,7 +679,7 @@ cfclose(cfp *fp)
659
679
result = fclose (fp -> uncompressedfp );
660
680
fp -> uncompressedfp = NULL ;
661
681
}
662
- free (fp );
682
+ free_keep_errno (fp );
663
683
664
684
return result ;
665
685
}
0 commit comments