aboutsummaryrefslogtreecommitdiffstats
path: root/tools/dosfstools/patches/0014-Add-OSX-and-FreeBSD-support.patch
blob: cc3c2c3d6ee994a4856b41cbfe7d82ee51d9c093 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
From 514985ae786dcde9842e46899ef5b6218662a119 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C3=81lvaro=20Fern=C3=A1ndez=20Rojas?= <noltari@gmail.com>
Date: Sat, 7 Mar 2015 16:32:51 +0100
Subject: [PATCH 14/14] Add OSX and FreeBSD support
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
---
 Makefile       |  9 ++++++++-
 src/boot.c     |  1 +
 src/check.c    |  1 +
 src/endian.h   | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/fat.c      |  1 +
 src/fatlabel.c |  1 +
 src/fsck.fat.h |  3 +--
 src/io.h       |  2 +-
 src/lfn.c      |  1 +
 src/mkfs.fat.c | 19 ++++++++++++++++---
 src/types.h    | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 11 files changed, 145 insertions(+), 7 deletions(-)
 create mode 100644 src/endian.h
 create mode 100644 src/types.h

diff --git a/Makefile b/Makefile
index 1593f3d..7359a79 100644
--- a/Makefile
+++ b/Makefile
@@ -28,12 +28,19 @@ DOCDIR = $(PREFIX)/share/doc
 MANDIR = $(PREFIX)/share/man
 
 #OPTFLAGS = -O2 -fomit-frame-pointer -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64
-OPTFLAGS = -O2 -fomit-frame-pointer -D_GNU_SOURCE $(shell getconf LFS_CFLAGS)
+OPTFLAGS = -O2 -fomit-frame-pointer -D_GNU_SOURCE
 #WARNFLAGS = -Wall -pedantic -std=c99
 WARNFLAGS = -Wall -Wextra -Wno-sign-compare -Wno-missing-field-initializers -Wmissing-prototypes -Wstrict-prototypes -Wwrite-strings
 DEBUGFLAGS = -g
 CFLAGS += $(OPTFLAGS) $(WARNFLAGS) $(DEBUGFLAGS)
 
+UNAME_S := $(shell uname -s)
+ifeq ($(UNAME_S),Darwin)
+  LDLIBS += -liconv
+else
+  OPTFLAGS += $(shell getconf LFS_CFLAGS)
+endif
+
 VPATH = src
 
 all: build
diff --git a/src/boot.c b/src/boot.c
index 0c0918f..1da9889 100644
--- a/src/boot.c
+++ b/src/boot.c
@@ -31,6 +31,7 @@
 #include <time.h>
 
 #include "common.h"
+#include "endian.h"
 #include "fsck.fat.h"
 #include "fat.h"
 #include "io.h"
diff --git a/src/check.c b/src/check.c
index 488f715..17ff16a 100644
--- a/src/check.c
+++ b/src/check.c
@@ -31,6 +31,7 @@
 #include <time.h>
 
 #include "common.h"
+#include "endian.h"
 #include "fsck.fat.h"
 #include "io.h"
 #include "fat.h"
diff --git a/src/endian.h b/src/endian.h
new file mode 100644
index 0000000..6613e65
--- /dev/null
+++ b/src/endian.h
@@ -0,0 +1,57 @@
+/* endian.h - Endian functions
+
+   Copyright (C) 2015 Álvaro Fernández Rojas <noltari@gmail.com>
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+   The complete text of the GNU General Public License
+   can be found in /usr/share/common-licenses/GPL-3 file.
+*/
+
+#ifndef _ENDIAN_H
+#define _ENDIAN_H
+
+#if defined(__linux__)
+	#include <endian.h>
+#elif defined(__APPLE__)
+	#include <libkern/OSByteOrder.h>
+
+	#define htobe16(x) OSSwapHostToBigInt16(x)
+	#define htole16(x) OSSwapHostToLittleInt16(x)
+	#define be16toh(x) OSSwapBigToHostInt16(x)
+	#define le16toh(x) OSSwapLittleToHostInt16(x)
+
+	#define htobe32(x) OSSwapHostToBigInt32(x)
+	#define htole32(x) OSSwapHostToLittleInt32(x)
+	#define be32toh(x) OSSwapBigToHostInt32(x)
+	#define le32toh(x) OSSwapLittleToHostInt32(x)
+
+	#define htobe64(x) OSSwapHostToBigInt64(x)
+	#define htole64(x) OSSwapHostToLittleInt64(x)
+	#define be64toh(x) OSSwapBigToHostInt64(x)
+	#define le64toh(x) OSSwapLittleToHostInt64(x)
+#elif defined(__FreeBSD__)
+	#include <sys/endian.h>
+
+	#define be16toh(x) betoh16(x)
+	#define le16toh(x) letoh16(x)
+
+	#define be32toh(x) betoh32(x)
+	#define le32toh(x) letoh32(x)
+
+	#define be64toh(x) betoh64(x)
+	#define le64toh(x) letoh64(x)
+#endif
+
+#endif /* _ENDIAN_H */
diff --git a/src/fat.c b/src/fat.c
index 5a92f56..481c08a 100644
--- a/src/fat.c
+++ b/src/fat.c
@@ -30,6 +30,7 @@
 #include <unistd.h>
 
 #include "common.h"
+#include "endian.h"
 #include "fsck.fat.h"
 #include "io.h"
 #include "check.h"
diff --git a/src/fatlabel.c b/src/fatlabel.c
index 1484ba5..6de831c 100644
--- a/src/fatlabel.c
+++ b/src/fatlabel.c
@@ -33,6 +33,7 @@
 #include <ctype.h>
 
 #include "common.h"
+#include "types.h"
 #include "fsck.fat.h"
 #include "io.h"
 #include "boot.h"
diff --git a/src/fsck.fat.h b/src/fsck.fat.h
index e5f6178..8b0ccb9 100644
--- a/src/fsck.fat.h
+++ b/src/fsck.fat.h
@@ -27,11 +27,10 @@
 #ifndef _DOSFSCK_H
 #define _DOSFSCK_H
 
-#include <fcntl.h>
 #include <stddef.h>
 #include <stdint.h>
-#include <endian.h>
 
+#include "types.h"
 #include "msdos_fs.h"
 
 #define VFAT_LN_ATTR (ATTR_RO | ATTR_HIDDEN | ATTR_SYS | ATTR_VOLUME)
diff --git a/src/io.h b/src/io.h
index d23d07e..eecfdc5 100644
--- a/src/io.h
+++ b/src/io.h
@@ -27,7 +27,7 @@
 #ifndef _IO_H
 #define _IO_H
 
-#include <fcntl.h>		/* for loff_t */
+#include "types.h"
 
 loff_t llseek(int fd, loff_t offset, int whence);
 
diff --git a/src/lfn.c b/src/lfn.c
index 2601172..f679168 100644
--- a/src/lfn.c
+++ b/src/lfn.c
@@ -28,6 +28,7 @@
 #include <time.h>
 
 #include "common.h"
+#include "endian.h"
 #include "io.h"
 #include "fsck.fat.h"
 #include "lfn.h"
diff --git a/src/mkfs.fat.c b/src/mkfs.fat.c
index 02e0918..f2cee09 100644
--- a/src/mkfs.fat.c
+++ b/src/mkfs.fat.c
@@ -48,8 +48,6 @@
 
 #include <fcntl.h>
 #include <sys/mount.h>
-#include <endian.h>
-#include <mntent.h>
 #include <signal.h>
 #include <string.h>
 #include <stdio.h>
@@ -61,13 +59,14 @@
 #include <errno.h>
 #include <ctype.h>
 #include <stdint.h>
-#include <endian.h>
 
 #if defined(__linux__)
+    #include <mntent.h>
     #include <linux/hdreg.h>
     #include <linux/fs.h>
     #include <linux/fd.h>
 #elif defined(__FreeBSD__) || defined(__APPLE__)
+    #include <sys/mount.h>
     #include <sys/disk.h>
 
     #define BLOCK_SIZE_BITS 10
@@ -97,7 +96,9 @@
     };
 #endif
 
+#include "endian.h"
 #include "msdos_fs.h"
+#include "types.h"
 
 /* In earlier versions, an own llseek() was used, but glibc lseek() is
  * sufficient (or even better :) for 64 bit offsets in the meantime */
@@ -525,6 +526,7 @@ static uint64_t count_blocks(char *filename, int *remainder)
 
 static void check_mount(char *device_name)
 {
+#if defined(__linux__)
     FILE *f;
     struct mntent *mnt;
 
@@ -534,6 +536,17 @@ static void check_mount(char *device_name)
 	if (strcmp(device_name, mnt->mnt_fsname) == 0)
 	    die("%s contains a mounted filesystem.");
     endmntent(f);
+#elif defined(__APPLE__) || defined(__FreeBSD__)
+    struct statfs* mounts;
+    int num_mounts = getmntinfo(&mounts, MNT_WAIT);
+    if (num_mounts < 0)
+        return;
+    for ( int i = 0; i < num_mounts; i++ )
+    {
+        if (strcmp(device_name, mounts[i].f_mntfromname) == 0)
+        die("%s contains a mounted filesystem.");
+    }
+#endif
 }
 
 /* Establish the geometry and media parameters for the device */
diff --git a/src/types.h b/src/types.h
new file mode 100644
index 0000000..a3f1a47
--- /dev/null
+++ b/src/types.h
@@ -0,0 +1,57 @@
+/* types.h - Missing types
+
+   Copyright (C) 2015 Álvaro Fernández Rojas <noltari@gmail.com>
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+   The complete text of the GNU General Public License
+   can be found in /usr/share/common-licenses/GPL-3 file.
+*/
+
+#ifndef _TYPES_H
+#define _TYPES_H
+
+#if defined(__linux__)
+	#include <fcntl.h>
+#elif defined(__APPLE__)
+	#ifndef loff_t
+		typedef long long loff_t;
+	#endif /* loff_t */
+
+	#ifndef lseek64
+		#define lseek64 lseek
+	#endif /* lseek64 */
+
+	#ifndef off64_t
+		#ifdef _LP64
+			typedef off_t off64_t;
+		#else
+			typedef __longlong_t off64_t;
+		#endif /* _LP64 */
+	#endif /* off64_t */
+#elif defined(__FreeBSD__)
+	#ifndef loff_t
+		typedef long long loff_t;
+	#endif /* loff_t */
+
+	#ifndef lseek64
+		#define lseek64 lseek
+	#endif /* lseek64 */
+
+	#ifndef off64_t
+		typedef off_t off64_t;
+	#endif /* off64_t */
+#endif
+
+#endif /* _TYPES_H */
-- 
1.9.1