diff --git a/README.md b/README.md index abd5a3a..9173b31 100644 --- a/README.md +++ b/README.md @@ -189,6 +189,13 @@ Generic Functions > **Notice:** the swapped order of `pathname` and `mode`! +- `fremove(fmt, ...)` + + Like `remove()`, 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 removing + the file. + - `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 b04554f..ccb1566 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 6:0:1 libite_la_SOURCES = chomp.c copyfile.c \ conio.c conio.h \ - dir.c fopenf.c \ + dir.c fopenf.c fremove.c \ fexist.c fisdir.c \ fparseln.c fsendfile.c \ ifconfig.c lfile.c \ diff --git a/src/fremove.c b/src/fremove.c new file mode 100644 index 0000000..8911c64 --- /dev/null +++ b/src/fremove.c @@ -0,0 +1,61 @@ +/* Formatted fremove() + * + * 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 +#include + +/** + * fremove - Remove a file based on the formatted string and optional arguments + * @fmt: Formatted string to be composed into a pathname + * + * This function is an extension to remove(), lessening the burden of + * first having to compose the filename from parts in a seprate buffer. + * + * Returns: + * Return value for remove(3). + */ +int fremove(const char *fmt, ...) +{ + va_list ap; + char *file; + int len; + + va_start(ap, fmt); + len = vsnprintf(NULL, 0, fmt, ap); + va_end(ap); + + file = alloca(len + 1); + if (!file) { + errno = ENOBUFS; + return -1; + } + + va_start(ap, fmt); + vsnprintf(file, len + 1, fmt, ap); + va_end(ap); + + return remove(file); +} + +/** + * Local Variables: + * indent-tabs-mode: t + * c-file-style: "linux" + * End: + */ diff --git a/src/lite.h b/src/lite.h index 555bdae..19d4257 100644 --- a/src/lite.h +++ b/src/lite.h @@ -66,6 +66,7 @@ int fexist (const char *file); int fisdir (const char *path); FILE *fopenf (const char *mode, const char *fmt, ...); +int fremove (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);