1 From 514985ae786dcde9842e46899ef5b6218662a119 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?=C3=81lvaro=20Fern=C3=A1ndez=20Rojas?= <noltari@gmail.com>
3 Date: Sat, 7 Mar 2015 16:32:51 +0100
4 Subject: [PATCH 14/14] Add OSX and FreeBSD support
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
9 Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
11 Makefile | 9 ++++++++-
14 src/endian.h | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
17 src/fsck.fat.h | 3 +--
20 src/mkfs.fat.c | 19 ++++++++++++++++---
21 src/types.h | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
22 11 files changed, 145 insertions(+), 7 deletions(-)
23 create mode 100644 src/endian.h
24 create mode 100644 src/types.h
26 diff --git a/Makefile b/Makefile
27 index 1593f3d..7359a79 100644
30 @@ -28,12 +28,19 @@ DOCDIR = $(PREFIX)/share/doc
31 MANDIR = $(PREFIX)/share/man
33 #OPTFLAGS = -O2 -fomit-frame-pointer -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64
34 -OPTFLAGS = -O2 -fomit-frame-pointer -D_GNU_SOURCE $(shell getconf LFS_CFLAGS)
35 +OPTFLAGS = -O2 -fomit-frame-pointer -D_GNU_SOURCE
36 #WARNFLAGS = -Wall -pedantic -std=c99
37 WARNFLAGS = -Wall -Wextra -Wno-sign-compare -Wno-missing-field-initializers -Wmissing-prototypes -Wstrict-prototypes -Wwrite-strings
39 CFLAGS += $(OPTFLAGS) $(WARNFLAGS) $(DEBUGFLAGS)
41 +UNAME_S := $(shell uname -s)
42 +ifeq ($(UNAME_S),Darwin)
45 + OPTFLAGS += $(shell getconf LFS_CFLAGS)
51 diff --git a/src/boot.c b/src/boot.c
52 index 0c0918f..1da9889 100644
63 diff --git a/src/check.c b/src/check.c
64 index 488f715..17ff16a 100644
75 diff --git a/src/endian.h b/src/endian.h
77 index 0000000..6613e65
81 +/* endian.h - Endian functions
83 + Copyright (C) 2015 Álvaro Fernández Rojas <noltari@gmail.com>
85 + This program is free software: you can redistribute it and/or modify
86 + it under the terms of the GNU General Public License as published by
87 + the Free Software Foundation, either version 3 of the License, or
88 + (at your option) any later version.
90 + This program is distributed in the hope that it will be useful,
91 + but WITHOUT ANY WARRANTY; without even the implied warranty of
92 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
93 + GNU General Public License for more details.
95 + You should have received a copy of the GNU General Public License
96 + along with this program. If not, see <http://www.gnu.org/licenses/>.
98 + The complete text of the GNU General Public License
99 + can be found in /usr/share/common-licenses/GPL-3 file.
105 +#if defined(__linux__)
106 + #include <endian.h>
107 +#elif defined(__APPLE__)
108 + #include <libkern/OSByteOrder.h>
110 + #define htobe16(x) OSSwapHostToBigInt16(x)
111 + #define htole16(x) OSSwapHostToLittleInt16(x)
112 + #define be16toh(x) OSSwapBigToHostInt16(x)
113 + #define le16toh(x) OSSwapLittleToHostInt16(x)
115 + #define htobe32(x) OSSwapHostToBigInt32(x)
116 + #define htole32(x) OSSwapHostToLittleInt32(x)
117 + #define be32toh(x) OSSwapBigToHostInt32(x)
118 + #define le32toh(x) OSSwapLittleToHostInt32(x)
120 + #define htobe64(x) OSSwapHostToBigInt64(x)
121 + #define htole64(x) OSSwapHostToLittleInt64(x)
122 + #define be64toh(x) OSSwapBigToHostInt64(x)
123 + #define le64toh(x) OSSwapLittleToHostInt64(x)
124 +#elif defined(__FreeBSD__)
125 + #include <sys/endian.h>
127 + #define be16toh(x) betoh16(x)
128 + #define le16toh(x) letoh16(x)
130 + #define be32toh(x) betoh32(x)
131 + #define le32toh(x) letoh32(x)
133 + #define be64toh(x) betoh64(x)
134 + #define le64toh(x) letoh64(x)
137 +#endif /* _ENDIAN_H */
138 diff --git a/src/fat.c b/src/fat.c
139 index 5a92f56..481c08a 100644
147 #include "fsck.fat.h"
150 diff --git a/src/fatlabel.c b/src/fatlabel.c
151 index 1484ba5..6de831c 100644
159 #include "fsck.fat.h"
162 diff --git a/src/fsck.fat.h b/src/fsck.fat.h
163 index e5f6178..8b0ccb9 100644
176 #include "msdos_fs.h"
178 #define VFAT_LN_ATTR (ATTR_RO | ATTR_HIDDEN | ATTR_SYS | ATTR_VOLUME)
179 diff --git a/src/io.h b/src/io.h
180 index d23d07e..eecfdc5 100644
187 -#include <fcntl.h> /* for loff_t */
190 loff_t llseek(int fd, loff_t offset, int whence);
192 diff --git a/src/lfn.c b/src/lfn.c
193 index 2601172..f679168 100644
202 #include "fsck.fat.h"
204 diff --git a/src/mkfs.fat.c b/src/mkfs.fat.c
205 index 02e0918..f2cee09 100644
211 #include <sys/mount.h>
223 #if defined(__linux__)
224 + #include <mntent.h>
225 #include <linux/hdreg.h>
226 #include <linux/fs.h>
227 #include <linux/fd.h>
228 #elif defined(__FreeBSD__) || defined(__APPLE__)
229 + #include <sys/mount.h>
230 #include <sys/disk.h>
232 #define BLOCK_SIZE_BITS 10
238 #include "msdos_fs.h"
241 /* In earlier versions, an own llseek() was used, but glibc lseek() is
242 * sufficient (or even better :) for 64 bit offsets in the meantime */
243 @@ -525,6 +526,7 @@ static uint64_t count_blocks(char *filename, int *remainder)
245 static void check_mount(char *device_name)
247 +#if defined(__linux__)
251 @@ -534,6 +536,17 @@ static void check_mount(char *device_name)
252 if (strcmp(device_name, mnt->mnt_fsname) == 0)
253 die("%s contains a mounted filesystem.");
255 +#elif defined(__APPLE__) || defined(__FreeBSD__)
256 + struct statfs* mounts;
257 + int num_mounts = getmntinfo(&mounts, MNT_WAIT);
258 + if (num_mounts < 0)
260 + for ( int i = 0; i < num_mounts; i++ )
262 + if (strcmp(device_name, mounts[i].f_mntfromname) == 0)
263 + die("%s contains a mounted filesystem.");
268 /* Establish the geometry and media parameters for the device */
269 diff --git a/src/types.h b/src/types.h
271 index 0000000..a3f1a47
275 +/* types.h - Missing types
277 + Copyright (C) 2015 Álvaro Fernández Rojas <noltari@gmail.com>
279 + This program is free software: you can redistribute it and/or modify
280 + it under the terms of the GNU General Public License as published by
281 + the Free Software Foundation, either version 3 of the License, or
282 + (at your option) any later version.
284 + This program is distributed in the hope that it will be useful,
285 + but WITHOUT ANY WARRANTY; without even the implied warranty of
286 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
287 + GNU General Public License for more details.
289 + You should have received a copy of the GNU General Public License
290 + along with this program. If not, see <http://www.gnu.org/licenses/>.
292 + The complete text of the GNU General Public License
293 + can be found in /usr/share/common-licenses/GPL-3 file.
299 +#if defined(__linux__)
301 +#elif defined(__APPLE__)
303 + typedef long long loff_t;
304 + #endif /* loff_t */
307 + #define lseek64 lseek
308 + #endif /* lseek64 */
312 + typedef off_t off64_t;
314 + typedef __longlong_t off64_t;
316 + #endif /* off64_t */
317 +#elif defined(__FreeBSD__)
319 + typedef long long loff_t;
320 + #endif /* loff_t */
323 + #define lseek64 lseek
324 + #endif /* lseek64 */
327 + typedef off_t off64_t;
328 + #endif /* off64_t */
331 +#endif /* _TYPES_H */