Pārlūkot izejas kodu

Added vfsync and vfopen.

master
Richard Nyberg pirms 19 gadiem
vecāks
revīzija
59bed6ca87
2 mainītis faili ar 51 papildinājumiem un 7 dzēšanām
  1. +45
    -7
      misc/subr.c
  2. +6
    -0
      misc/subr.h

+ 45
- 7
misc/subr.c Parādīt failu

@@ -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)
{


+ 6
- 0
misc/subr.h Parādīt failu

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

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

#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);


Notiek ielāde…
Atcelt
Saglabāt