Browse Source

Added vfsync and vfopen.

master
Richard Nyberg 19 years ago
parent
commit
59bed6ca87
2 changed files with 51 additions and 7 deletions
  1. +45
    -7
      misc/subr.c
  2. +6
    -0
      misc/subr.h

+ 45
- 7
misc/subr.c View File

@@ -74,18 +74,13 @@ mkdirs(char *path)
} }


int int
vopen(int *res, int flags, const char *fmt, ...)
vaopen(int *res, int flags, const char *fmt, va_list ap)
{ {
int fd, didmkdirs; int fd, didmkdirs;
char path[PATH_MAX + 1]; 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; return ENAMETOOLONG;
}
va_end(ap);


didmkdirs = 0; didmkdirs = 0;
again: again:
@@ -105,6 +100,49 @@ again:
return errno; 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 int
canon_path(const char *path, char **res) canon_path(const char *path, char **res)
{ {


+ 6
- 0
misc/subr.h View File

@@ -1,6 +1,9 @@
#ifndef BTPD_SUBR_H #ifndef BTPD_SUBR_H
#define BTPD_SUBR_H #define BTPD_SUBR_H


#include <stdio.h>
#include <stdarg.h>

#define min(x, y) ((x) <= (y) ? (x) : (y)) #define min(x, y) ((x) <= (y) ? (x) : (y))


int set_nonblocking(int fd); int set_nonblocking(int fd);
@@ -8,7 +11,10 @@ int set_blocking(int fd);


int mkdirs(char *path); 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 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); void set_bit(uint8_t *bits, unsigned long index);
int has_bit(const uint8_t *bits, unsigned long index); int has_bit(const uint8_t *bits, unsigned long index);


Loading…
Cancel
Save