From 78944c815a5d8d1c93771ca1c343b9406bc262c4 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sun, 26 Sep 2021 15:23:26 -0400 Subject: Don't truncate files until we know they're not duplicates --- color.c | 39 ++------------------------------------- 1 file changed, 2 insertions(+), 37 deletions(-) (limited to 'color.c') diff --git a/color.c b/color.c index 09070f2..7064044 100644 --- a/color.c +++ b/color.c @@ -501,7 +501,7 @@ void free_colors(struct colors *colors) { } } -static CFILE *cfalloc(void) { +CFILE *cfwrap(FILE *file, const struct colors *colors, bool close) { CFILE *cfile = malloc(sizeof(*cfile)); if (!cfile) { return NULL; @@ -513,43 +513,8 @@ static CFILE *cfalloc(void) { return NULL; } - cfile->file = NULL; - cfile->colors = NULL; - cfile->close = false; - - return cfile; -} - -CFILE *cfopen(const char *path, const struct colors *colors) { - CFILE *cfile = cfalloc(); - if (!cfile) { - return NULL; - } - - cfile->file = xfopen(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC); - if (!cfile->file) { - cfclose(cfile); - return NULL; - } - cfile->close = true; - - if (isatty(fileno(cfile->file))) { - cfile->colors = colors; - } else { - cfile->colors = NULL; - } - - return cfile; -} - -CFILE *cfdup(FILE *file, const struct colors *colors) { - CFILE *cfile = cfalloc(); - if (!cfile) { - return NULL; - } - - cfile->close = false; cfile->file = file; + cfile->close = close; if (isatty(fileno(file))) { cfile->colors = colors; -- cgit v1.2.3