tools/mtd-utils: prefer static linking
[openwrt.git] / tools / dosfstools / patches / 0014-Add-OSX-and-FreeBSD-support.patch
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
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
10 ---
11  Makefile       |  9 ++++++++-
12  src/boot.c     |  1 +
13  src/check.c    |  1 +
14  src/endian.h   | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
15  src/fat.c      |  1 +
16  src/fatlabel.c |  1 +
17  src/fsck.fat.h |  3 +--
18  src/io.h       |  2 +-
19  src/lfn.c      |  1 +
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
25
26 diff --git a/Makefile b/Makefile
27 index 1593f3d..7359a79 100644
28 --- a/Makefile
29 +++ b/Makefile
30 @@ -28,12 +28,19 @@ DOCDIR = $(PREFIX)/share/doc
31  MANDIR = $(PREFIX)/share/man
32  
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
38  DEBUGFLAGS = -g
39  CFLAGS += $(OPTFLAGS) $(WARNFLAGS) $(DEBUGFLAGS)
40  
41 +UNAME_S := $(shell uname -s)
42 +ifeq ($(UNAME_S),Darwin)
43 +  LDLIBS += -liconv
44 +else
45 +  OPTFLAGS += $(shell getconf LFS_CFLAGS)
46 +endif
47 +
48  VPATH = src
49  
50  all: build
51 diff --git a/src/boot.c b/src/boot.c
52 index 0c0918f..1da9889 100644
53 --- a/src/boot.c
54 +++ b/src/boot.c
55 @@ -31,6 +31,7 @@
56  #include <time.h>
57  
58  #include "common.h"
59 +#include "endian.h"
60  #include "fsck.fat.h"
61  #include "fat.h"
62  #include "io.h"
63 diff --git a/src/check.c b/src/check.c
64 index 488f715..17ff16a 100644
65 --- a/src/check.c
66 +++ b/src/check.c
67 @@ -31,6 +31,7 @@
68  #include <time.h>
69  
70  #include "common.h"
71 +#include "endian.h"
72  #include "fsck.fat.h"
73  #include "io.h"
74  #include "fat.h"
75 diff --git a/src/endian.h b/src/endian.h
76 new file mode 100644
77 index 0000000..6613e65
78 --- /dev/null
79 +++ b/src/endian.h
80 @@ -0,0 +1,57 @@
81 +/* endian.h - Endian functions
82 +
83 +   Copyright (C) 2015 Álvaro Fernández Rojas <noltari@gmail.com>
84 +
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.
89 +
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.
94 +
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/>.
97 +
98 +   The complete text of the GNU General Public License
99 +   can be found in /usr/share/common-licenses/GPL-3 file.
100 +*/
101 +
102 +#ifndef _ENDIAN_H
103 +#define _ENDIAN_H
104 +
105 +#if defined(__linux__)
106 +       #include <endian.h>
107 +#elif defined(__APPLE__)
108 +       #include <libkern/OSByteOrder.h>
109 +
110 +       #define htobe16(x) OSSwapHostToBigInt16(x)
111 +       #define htole16(x) OSSwapHostToLittleInt16(x)
112 +       #define be16toh(x) OSSwapBigToHostInt16(x)
113 +       #define le16toh(x) OSSwapLittleToHostInt16(x)
114 +
115 +       #define htobe32(x) OSSwapHostToBigInt32(x)
116 +       #define htole32(x) OSSwapHostToLittleInt32(x)
117 +       #define be32toh(x) OSSwapBigToHostInt32(x)
118 +       #define le32toh(x) OSSwapLittleToHostInt32(x)
119 +
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>
126 +
127 +       #define be16toh(x) betoh16(x)
128 +       #define le16toh(x) letoh16(x)
129 +
130 +       #define be32toh(x) betoh32(x)
131 +       #define le32toh(x) letoh32(x)
132 +
133 +       #define be64toh(x) betoh64(x)
134 +       #define le64toh(x) letoh64(x)
135 +#endif
136 +
137 +#endif /* _ENDIAN_H */
138 diff --git a/src/fat.c b/src/fat.c
139 index 5a92f56..481c08a 100644
140 --- a/src/fat.c
141 +++ b/src/fat.c
142 @@ -30,6 +30,7 @@
143  #include <unistd.h>
144  
145  #include "common.h"
146 +#include "endian.h"
147  #include "fsck.fat.h"
148  #include "io.h"
149  #include "check.h"
150 diff --git a/src/fatlabel.c b/src/fatlabel.c
151 index 1484ba5..6de831c 100644
152 --- a/src/fatlabel.c
153 +++ b/src/fatlabel.c
154 @@ -33,6 +33,7 @@
155  #include <ctype.h>
156  
157  #include "common.h"
158 +#include "types.h"
159  #include "fsck.fat.h"
160  #include "io.h"
161  #include "boot.h"
162 diff --git a/src/fsck.fat.h b/src/fsck.fat.h
163 index e5f6178..8b0ccb9 100644
164 --- a/src/fsck.fat.h
165 +++ b/src/fsck.fat.h
166 @@ -27,11 +27,10 @@
167  #ifndef _DOSFSCK_H
168  #define _DOSFSCK_H
169  
170 -#include <fcntl.h>
171  #include <stddef.h>
172  #include <stdint.h>
173 -#include <endian.h>
174  
175 +#include "types.h"
176  #include "msdos_fs.h"
177  
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
181 --- a/src/io.h
182 +++ b/src/io.h
183 @@ -27,7 +27,7 @@
184  #ifndef _IO_H
185  #define _IO_H
186  
187 -#include <fcntl.h>             /* for loff_t */
188 +#include "types.h"
189  
190  loff_t llseek(int fd, loff_t offset, int whence);
191  
192 diff --git a/src/lfn.c b/src/lfn.c
193 index 2601172..f679168 100644
194 --- a/src/lfn.c
195 +++ b/src/lfn.c
196 @@ -28,6 +28,7 @@
197  #include <time.h>
198  
199  #include "common.h"
200 +#include "endian.h"
201  #include "io.h"
202  #include "fsck.fat.h"
203  #include "lfn.h"
204 diff --git a/src/mkfs.fat.c b/src/mkfs.fat.c
205 index 02e0918..f2cee09 100644
206 --- a/src/mkfs.fat.c
207 +++ b/src/mkfs.fat.c
208 @@ -48,8 +48,6 @@
209  
210  #include <fcntl.h>
211  #include <sys/mount.h>
212 -#include <endian.h>
213 -#include <mntent.h>
214  #include <signal.h>
215  #include <string.h>
216  #include <stdio.h>
217 @@ -61,13 +59,14 @@
218  #include <errno.h>
219  #include <ctype.h>
220  #include <stdint.h>
221 -#include <endian.h>
222  
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>
231  
232      #define BLOCK_SIZE_BITS 10
233 @@ -97,7 +96,9 @@
234      };
235  #endif
236  
237 +#include "endian.h"
238  #include "msdos_fs.h"
239 +#include "types.h"
240  
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)
244  
245  static void check_mount(char *device_name)
246  {
247 +#if defined(__linux__)
248      FILE *f;
249      struct mntent *mnt;
250  
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.");
254      endmntent(f);
255 +#elif defined(__APPLE__) || defined(__FreeBSD__)
256 +    struct statfs* mounts;
257 +    int num_mounts = getmntinfo(&mounts, MNT_WAIT);
258 +    if (num_mounts < 0)
259 +        return;
260 +    for ( int i = 0; i < num_mounts; i++ )
261 +    {
262 +        if (strcmp(device_name, mounts[i].f_mntfromname) == 0)
263 +        die("%s contains a mounted filesystem.");
264 +    }
265 +#endif
266  }
267  
268  /* Establish the geometry and media parameters for the device */
269 diff --git a/src/types.h b/src/types.h
270 new file mode 100644
271 index 0000000..a3f1a47
272 --- /dev/null
273 +++ b/src/types.h
274 @@ -0,0 +1,57 @@
275 +/* types.h - Missing types
276 +
277 +   Copyright (C) 2015 Álvaro Fernández Rojas <noltari@gmail.com>
278 +
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.
283 +
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.
288 +
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/>.
291 +
292 +   The complete text of the GNU General Public License
293 +   can be found in /usr/share/common-licenses/GPL-3 file.
294 +*/
295 +
296 +#ifndef _TYPES_H
297 +#define _TYPES_H
298 +
299 +#if defined(__linux__)
300 +       #include <fcntl.h>
301 +#elif defined(__APPLE__)
302 +       #ifndef loff_t
303 +               typedef long long loff_t;
304 +       #endif /* loff_t */
305 +
306 +       #ifndef lseek64
307 +               #define lseek64 lseek
308 +       #endif /* lseek64 */
309 +
310 +       #ifndef off64_t
311 +               #ifdef _LP64
312 +                       typedef off_t off64_t;
313 +               #else
314 +                       typedef __longlong_t off64_t;
315 +               #endif /* _LP64 */
316 +       #endif /* off64_t */
317 +#elif defined(__FreeBSD__)
318 +       #ifndef loff_t
319 +               typedef long long loff_t;
320 +       #endif /* loff_t */
321 +
322 +       #ifndef lseek64
323 +               #define lseek64 lseek
324 +       #endif /* lseek64 */
325 +
326 +       #ifndef off64_t
327 +               typedef off_t off64_t;
328 +       #endif /* off64_t */
329 +#endif
330 +
331 +#endif /* _TYPES_H */
332 -- 
333 1.9.1
334