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