summaryrefslogtreecommitdiffstats
path: root/color.c
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2017-03-16 23:01:00 -0400
committerTavian Barnes <tavianator@tavianator.com>2017-03-16 23:02:33 -0400
commitc85f569daf3e0e99c9e8941d0234711afdb58cd7 (patch)
tree347db11ddb49f1f256d7d0f3f24dc15059318e8c /color.c
parenta7c51baacf0867b1089f683b05811744f01a70af (diff)
downloadbfs-c85f569daf3e0e99c9e8941d0234711afdb58cd7.tar.xz
Give struct expr a CFILE* instead of just a FILE*
This unifies the behaviour of -print and -fprint /dev/stdout.
Diffstat (limited to 'color.c')
-rw-r--r--color.c52
1 files changed, 43 insertions, 9 deletions
diff --git a/color.c b/color.c
index b8acc32..18a975f 100644
--- a/color.c
+++ b/color.c
@@ -213,22 +213,56 @@ void free_colors(struct colors *colors) {
}
}
+CFILE *cfopen(const char *path, const struct colors *colors) {
+ CFILE *cfile = malloc(sizeof(*cfile));
+ if (!cfile) {
+ return NULL;
+ }
+
+ cfile->close = false;
+ cfile->file = fopen(path, "wb");
+ 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 = malloc(sizeof(*cfile));
- if (cfile) {
- cfile->file = file;
- if (isatty(fileno(file))) {
- cfile->colors = colors;
- } else {
- cfile->colors = NULL;
- }
+ if (!cfile) {
+ return NULL;
+ }
+
+ cfile->close = false;
+ cfile->file = file;
+
+ if (isatty(fileno(file))) {
+ cfile->colors = colors;
+ } else {
+ cfile->colors = NULL;
}
+
return cfile;
}
int cfclose(CFILE *cfile) {
- free(cfile);
- return 0;
+ int ret = 0;
+ if (cfile) {
+ if (cfile->close) {
+ ret = fclose(cfile->file);
+ }
+ free(cfile);
+ }
+ return ret;
}
static const char *file_color(const struct colors *colors, const char *filename, const struct BFTW *ftwbuf) {