From 02a42a1cadb9b18545df9d768f790dba84491b1c Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Tue, 10 Mar 2020 09:07:56 +0100 Subject: [PATCH] Add support for fopenf(), formatted string fopen() Signed-off-by: Joachim Nilsson --- README.md | 9 +++++++ src/Makefile.am | 2 +- src/fopenf.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++ src/lite.h | 1 + tests/.gitignore | 2 ++ tests/Makefile.am | 1 + tests/fopenf.c | 18 ++++++++++++++ 7 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 src/fopenf.c create mode 100644 tests/fopenf.c diff --git a/README.md b/README.md index 75e4055..f41b6b3 100644 --- a/README.md +++ b/README.md @@ -181,6 +181,15 @@ Generic Functions Check for the existence of a directory, returns True(1) or False(0). +- `fopenf(mode, fmt, ...)` + + Like `fopen()`, but takes a formatted string as argument. This + greatly simplifies operations that usually consist of composing a + filename from parts into a dynamic buffer before actually opening + the file. + + > **Notice:** the swapped order of `pathname` and `mode`! + - `fsendfile(src, dst, len)` Copy data between file streams, very similar to `fcopyfile()`, but diff --git a/src/Makefile.am b/src/Makefile.am index 8f3bde6..580ccc8 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -5,7 +5,7 @@ libite_la_CFLAGS = -W -Wall -Wextra libite_la_LDFLAGS = $(AM_LDFLAGS) -version-info 5:1:0 libite_la_SOURCES = chomp.c copyfile.c \ conio.c conio.h \ - dir.c \ + dir.c fopenf.c \ fexist.c fisdir.c \ fparseln.c fsendfile.c \ ifconfig.c lfile.c \ diff --git a/src/fopenf.c b/src/fopenf.c new file mode 100644 index 0000000..a56a55b --- /dev/null +++ b/src/fopenf.c @@ -0,0 +1,61 @@ +/* Formatted fopen() + * + * Copyright (c) 2020 Joachim Nilsson + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include + +/** + * fopenf - Open a file based on the formatted string and optional arguments + * @mode: Last argument in optional list, if omitted EINVAL + * @fmt: Formatted string to be composed into a pathname + * + * This function is an extension to the fopen() family, lessening the burden + * of first having to compose the filename from parts in a seprate buffer. + * + * Returns: + * Upon successful completion fopenf() a FILE pointer. Otherwise, NULL + * is returned and errno is set to indicate the error. + */ +FILE *fopenf(const char *mode, const char *fmt, ...) +{ + va_list ap; + FILE *fp = NULL; + char *file; + int len; + + va_start(ap, fmt); + len = vsnprintf(NULL, 0, fmt, ap); + va_end(ap); + + va_start(ap, fmt); + file = alloca(len + 1); + if (file) { + vsnprintf(file, len + 1, fmt, ap); + fp = fopen(file, mode); + } + va_end(ap); + + return fp; +} + +/** + * Local Variables: + * indent-tabs-mode: t + * c-file-style: "linux" + * End: + */ diff --git a/src/lite.h b/src/lite.h index 0458480..0ab057a 100644 --- a/src/lite.h +++ b/src/lite.h @@ -65,6 +65,7 @@ char *fparseln (FILE *, size_t *, size_t *, const char[3], int); int fexist (const char *file); int fisdir (const char *path); +FILE *fopenf (const char *mode, const char *fmt, ...); FILE *tempfile (void); ssize_t copyfile (const char *src, const char *dst, int len, int opt); int movefile (const char *src, const char *dst); diff --git a/tests/.gitignore b/tests/.gitignore index db647d7..b6e830e 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -1,11 +1,13 @@ *.o *.log *.trs +.deps/ chomp copyfile dir fexist fisdir +fopenf fsendfile lfile makepath diff --git a/tests/Makefile.am b/tests/Makefile.am index 2f7f3e2..dbd9d72 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -7,6 +7,7 @@ TESTS += copyfile TESTS += dir TESTS += fexist TESTS += fisdir +TESTS += fopenf TESTS += fsendfile TESTS += lfile TESTS += makepath diff --git a/tests/fopenf.c b/tests/fopenf.c new file mode 100644 index 0000000..7b89b02 --- /dev/null +++ b/tests/fopenf.c @@ -0,0 +1,18 @@ +#include "check.h" + +int main(void) +{ + FILE *fp; + char *dir = "/tmp"; + char *file = "__foo__"; + + fp = fopenf("w", "%s/%s", dir, file); + if (fp) { + fputs("foo", fp); + fclose(fp); + } + + fail_unless(fexist("/tmp/__foo__")); + + return 0; +}