From: Arran Cudbard-Bell Date: Thu, 7 Mar 2013 19:26:54 +0000 (-0500) Subject: Add function for checking whether files exist X-Git-Tag: release_3_0_0_beta1~805 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5f2979cb9516784379f8990cf0d4ad40bd145481;p=thirdparty%2Ffreeradius-server.git Add function for checking whether files exist --- diff --git a/src/include/radiusd.h b/src/include/radiusd.h index 1ab4b4a5717..0c3002a1186 100644 --- a/src/include/radiusd.h +++ b/src/include/radiusd.h @@ -610,6 +610,7 @@ void (*reset_signal(int signo, void (*func)(int)))(int); void request_free(REQUEST **request); int rad_mkdir(char *directory, mode_t mode); int rad_checkfilename(const char *filename); +int rad_file_exists(const char *filename); void *rad_malloc(size_t size); /* calls exit(1) on error! */ void *rad_calloc(size_t size); /* calls exit(1) on error! */ void rad_const_free(const void *ptr); diff --git a/src/main/util.c b/src/main/util.c index 13a8ca7f610..e8d3b57adba 100644 --- a/src/main/util.c +++ b/src/main/util.c @@ -283,6 +283,31 @@ int rad_checkfilename(const char *filename) return -1; } +/** Check if file exists + * + * @param filename to check. + * @return 0 if the file does not exist, 1 if the file exists, -1 if the file + * exists but there was an error opening it. errno value should be usable + * for error messages. + */ +int rad_file_exists(const char *filename) +{ + int des; + int ret = 1; + + if ((des = open(filename, O_RDONLY)) == -1) { + if (errno == ENOENT) { + ret = 0; + } else { + ret = -1; + } + } else { + close(des); + } + + return ret; +} + /* * Create possibly many directories. *