mirror of
https://github.com/troglobit/libite.git
synced 2026-10-10 16:52:25 +07:00
New helper function, fexistf(), made for the Infix project
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user