Another take on fixing building issue on Macs

This patch is based on artosx's feedback on GitHub:

https://github.com/AltraMayor/f3/issues/6#issuecomment-67075953
This commit is contained in:
Michel Machado 2014-12-16 07:05:48 -05:00
parent 62f3c1d861
commit e8509a05e9
2 changed files with 40 additions and 29 deletions

34
utils.c
View File

@ -1,8 +1,19 @@
#define _GNU_SOURCE
#if __APPLE__ && __MACH__
#define _DARWIN_C_SOURCE
#include <fcntl.h> /* For function fcntl. */
#endif /* Apple Macintosh */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include <limits.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
@ -215,3 +226,26 @@ void print_header(FILE *f, const char *name)
"This is free software; see the source for copying conditions.\n"
"\n", name);
}
#if __APPLE__ && __MACH__
/* This function is a _rough_ approximation of fdatasync(2). */
static inline int fdatasync(int fd)
{
return fcntl(fd, F_FULLFSYNC);
}
/* This function is a _rough_ approximation of posix_fadvise(2). */
int posix_fadvise(int fd, off_t offset, off_t len, int advice)
{
switch (advice) {
case POSIX_FADV_SEQUENTIAL:
return fcntl(fd, F_RDAHEAD, 1);
case POSIX_FADV_DONTNEED:
return fcntl(fd, F_NOCACHE, 1);
default:
assert(0);
}
}
#endif /* Apple Macintosh */

35
utils.h
View File

@ -1,11 +1,9 @@
#ifndef HEADER_UTILS_H
#define HEADER_UTILS_H
#include <ctype.h>
#include <assert.h>
#include <sys/time.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h> /* For type FILE. */
#include <sys/time.h> /* For struct timeval. */
#include <stdint.h> /* For type uint64_t. */
#define SECTOR_SIZE (512)
#define GIGABYTES (1024 * 1024 * 1024)
@ -38,34 +36,13 @@ static inline uint64_t random_number(uint64_t prv_number)
#if __APPLE__ && __MACH__
#define _DARWIN_C_SOURCE
/* For function fcntl. */
#include <fcntl.h>
/* For type off_t. */
#include <unistd.h>
/* This function is a _rough_ approximation of fdatasync(2). */
static inline int fdatasync(int fd)
{
return fcntl(fd, F_FULLFSYNC);
}
#include <unistd.h> /* For type off_t. */
#define POSIX_FADV_SEQUENTIAL 2 /* Expect sequential page references. */
#define POSIX_FADV_DONTNEED 4 /* Don't need these pages. */
/* This function is a _rough_ approximation of posix_fadvise(2). */
static inline int posix_fadvise(int fd, off_t offset, off_t len, int advice)
{
switch (advice) {
case POSIX_FADV_SEQUENTIAL:
return fcntl(fd, F_RDAHEAD, 1);
case POSIX_FADV_DONTNEED:
return fcntl(fd, F_NOCACHE, 1);
default:
assert(0);
}
}
int fdatasync(int fd);
int posix_fadvise(int fd, off_t offset, off_t len, int advice);
#endif /* Apple Macintosh */