New helper function, fexistf(), made for the Infix project

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg
2023-06-11 12:18:45 +02:00
parent 1f6ff90d9e
commit 784a667584
2 changed files with 39 additions and 0 deletions
+38
View File
@@ -29,8 +29,13 @@
*/
#include <errno.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include "lite.h"
/**
* Check if a file exists in the file system.
* @param file File to look for, with full path.
@@ -50,6 +55,39 @@ int fexist(const char *file)
return 1;
}
/**
* Like fexist() but with formatted string support
* @param fmt Formatted string to be composed into the file to look for.
*
* This is a wrapper for the fexist() function in lite.h, lessening the
* burden of having to compose the filename from parts in a seprate
* buffer.
*
* @returns @c TRUE(1) if the @a file exists, otherwise @c FALSE(0).
*/
int fexistf(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 = ENOMEM;
return -1;
}
va_start(ap, fmt);
vsnprintf(file, len + 1, fmt, ap);
va_end(ap);
return fexist(file);
}
/**
* Local Variables:
* indent-tabs-mode: t
+1
View File
@@ -77,6 +77,7 @@ int erasef (const char *fmt, ...) __attribute__ ((format (printf, 1, 2))
char *fparseln (FILE *, size_t *, size_t *, const char[3], int);
int fexist (const char *file);
int fexistf (const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));;
int fisdir (const char *path);
FILE *vfopenf (const char *mode, const char *fmt, va_list ap);