From 59bed6ca87c1297b0c0363c418dfd6f947977474 Mon Sep 17 00:00:00 2001 From: Richard Nyberg Date: Sun, 22 Jan 2006 21:51:13 +0000 Subject: [PATCH] Added vfsync and vfopen. --- misc/subr.c | 52 +++++++++++++++++++++++++++++++++++++++++++++------- misc/subr.h | 6 ++++++ 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/misc/subr.c b/misc/subr.c index a6200f5..729d588 100644 --- a/misc/subr.c +++ b/misc/subr.c @@ -74,18 +74,13 @@ mkdirs(char *path) } int -vopen(int *res, int flags, const char *fmt, ...) +vaopen(int *res, int flags, const char *fmt, va_list ap) { int fd, didmkdirs; char path[PATH_MAX + 1]; - va_list ap; - va_start(ap, fmt); - if (vsnprintf(path, PATH_MAX, fmt, ap) >= PATH_MAX) { - va_end(ap); + if (vsnprintf(path, PATH_MAX, fmt, ap) >= PATH_MAX) return ENAMETOOLONG; - } - va_end(ap); didmkdirs = 0; again: @@ -105,6 +100,49 @@ again: return errno; } +int +vopen(int *res, int flags, const char *fmt, ...) +{ + int err; + va_list ap; + va_start(ap, fmt); + err = vaopen(res, flags, fmt, ap); + va_end(ap); + return err; +} + +int +vfsync(const char *fmt, ...) +{ + int err, fd; + va_list ap; + va_start(ap, fmt); + err = vaopen(&fd, O_RDONLY, fmt, ap); + va_end(ap); + if (err != 0) + return err; + if (fsync(fd) < 0) + err = errno; + close(fd); + return err; +} + +int +vfopen(FILE **ret, const char *mode, const char *fmt, ...) +{ + int err = 0; + char path[PATH_MAX + 1]; + va_list ap; + va_start(ap, fmt); + if (vsnprintf(path, PATH_MAX, fmt, ap) >= PATH_MAX) + err = ENAMETOOLONG; + va_end(ap); + if (err == 0) + if ((*ret = fopen(path, mode)) == NULL) + err = errno; + return err; +} + int canon_path(const char *path, char **res) { diff --git a/misc/subr.h b/misc/subr.h index 56448cc..8ad42c2 100644 --- a/misc/subr.h +++ b/misc/subr.h @@ -1,6 +1,9 @@ #ifndef BTPD_SUBR_H #define BTPD_SUBR_H +#include +#include + #define min(x, y) ((x) <= (y) ? (x) : (y)) int set_nonblocking(int fd); @@ -8,7 +11,10 @@ int set_blocking(int fd); int mkdirs(char *path); +int vaopen(int *resfd, int flags, const char *fmt, va_list ap); int vopen(int *resfd, int flags, const char *fmt, ...); +int vfopen(FILE **ret, const char *mode, const char *fmt, ...); +int vfsync(const char *fmt, ...); void set_bit(uint8_t *bits, unsigned long index); int has_bit(const uint8_t *bits, unsigned long index);