Add support for fopenf(), formatted string fopen()

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson
2020-03-10 09:07:56 +01:00
parent 28c11f010a
commit 02a42a1cad
7 changed files with 93 additions and 1 deletions
+9
View File
@@ -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
+1 -1
View File
@@ -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 \
+61
View File
@@ -0,0 +1,61 @@
/* Formatted fopen()
*
* Copyright (c) 2020 Joachim Nilsson <troglobit@gmail.com>
*
* 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 <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
/**
* 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:
*/
+1
View File
@@ -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);
+2
View File
@@ -1,11 +1,13 @@
*.o
*.log
*.trs
.deps/
chomp
copyfile
dir
fexist
fisdir
fopenf
fsendfile
lfile
makepath
+1
View File
@@ -7,6 +7,7 @@ TESTS += copyfile
TESTS += dir
TESTS += fexist
TESTS += fisdir
TESTS += fopenf
TESTS += fsendfile
TESTS += lfile
TESTS += makepath
+18
View File
@@ -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;
}