Build external/newfs_msdos. am: 5946c623c5
am: 105322515d

Change-Id: I6e7d87b4944e38bf3d4599f543f486ccad3e42cc
diff --git a/Android.bp b/Android.bp
new file mode 100755
index 0000000..fcaffbc
--- /dev/null
+++ b/Android.bp
@@ -0,0 +1,19 @@
+cc_binary {
+    name: "newfs_msdos",
+    cflags: [
+        "-Wall",
+        "-Werror",
+        "-Wno-unused-function",
+        "-Wno-unused-parameter",
+        "-Wno-unused-variable",
+        "-D_FILE_OFFSET_BITS=64",
+        "-D_GNU_SOURCE",
+        "-DSIGINFO=SIGUSR2",
+        "-Dnitems(x)=(sizeof((x))/sizeof((x)[0]))",
+    ],
+    srcs: [
+        "mkfs_msdos.c",
+        "newfs_msdos.c",
+    ],
+    host_supported: true,
+}
diff --git a/mkfs_msdos.c b/mkfs_msdos.c
index a10f769..350b782 100644
--- a/mkfs_msdos.c
+++ b/mkfs_msdos.c
@@ -31,10 +31,18 @@
 #endif /* not lint */
 
 #include <sys/param.h>
+#if defined(__linux__)
+#include <linux/fs.h>
+#include <linux/hdreg.h>
+#include <sys/ioctl.h>
+#elif defined(__APPLE__)
+/* Nothing. */
+#else
 #include <sys/fdcio.h>
 #include <sys/disk.h>
 #include <sys/disklabel.h>
 #include <sys/mount.h>
+#endif
 #include <sys/stat.h>
 #include <sys/time.h>
 
@@ -53,6 +61,13 @@
 
 #include "mkfs_msdos.h"
 
+#if !defined(__packed)
+#define __packed __attribute__((__packed__))
+#endif
+#if !defined(__unused)
+#define __unused __attribute__((__unused__))
+#endif
+
 #define	MAXU16	  0xffff	/* maximum unsigned 16-bit quantity */
 #define	BPN	  4		/* bits per nibble */
 #define	NPB	  2		/* nibbles per byte */
@@ -766,6 +781,7 @@
 static int
 check_mounted(const char *fname, mode_t mode)
 {
+#if 0
     struct statfs *mp;
     const char *s1, *s2;
     size_t len;
@@ -790,6 +806,7 @@
 	    return -1;
 	}
     }
+#endif
     return 0;
 }
 
@@ -814,6 +831,59 @@
 /*
  * Get disk slice, partition, and geometry information.
  */
+#if defined(__linux__)
+static int getdiskinfo(int fd, const char *fname, const char *dtype,
+                       __unused int oflag, struct bpb *bpb)
+{
+    if (ioctl(fd, BLKSSZGET, &bpb->bpbBytesPerSec)) {
+        err(1, "ioctl(BLKSSZGET) for bytes/sector failed");
+    }
+
+    if (ckgeom(fname, bpb->bpbBytesPerSec, "bytes/sector") == -1) return -1;
+
+    u_long block_size;
+    if (ioctl(fd, BLKGETSIZE, &block_size)) {
+        err(1, "ioctl(BLKGETSIZE) failed");
+    }
+
+    if (block_size > UINT32_MAX) {
+        errx(1, "block size too large: %lu", block_size);
+    }
+
+    bpb->bpbHugeSectors = (u_int)block_size;
+
+    struct hd_geometry geom;
+    if (ioctl(fd, HDIO_GETGEO, &geom)) {
+        warn("ioctl(HDIO_GETGEO) failed, but will use sane values");
+        geom.heads = 64;
+        geom.sectors = 63;
+    }
+
+    if (!geom.heads) {
+        warnx("Bogus heads from kernel - setting sane value");
+        geom.heads = 64;
+    }
+
+    if (!geom.sectors) {
+        warnx("Bogus sectors from kernel - setting sane value");
+        geom.sectors = 63;
+    }
+
+    bpb->bpbSecPerTrack = geom.sectors;
+    if (ckgeom(fname, bpb->bpbSecPerTrack, "sectors/track") == -1) return -1;
+
+    bpb->bpbHeads = geom.heads;
+    if (ckgeom(fname, bpb->bpbHeads, "drive heads") == -1) return -1;
+
+    return 0;
+}
+#elif defined(__APPLE__)
+static int
+getdiskinfo(int fd, const char *fname, const char *dtype, __unused int oflag,
+	    struct bpb *bpb) {
+    return 0;
+}
+#else
 static int
 getdiskinfo(int fd, const char *fname, const char *dtype, __unused int oflag,
 	    struct bpb *bpb)
@@ -903,6 +973,7 @@
 	bpb->bpbHiddenSecs = hs;
     return 0;
 }
+#endif
 
 /*
  * Print out BPB values.
diff --git a/mkfs_msdos.h b/mkfs_msdos.h
index acd5615..b49a577 100644
--- a/mkfs_msdos.h
+++ b/mkfs_msdos.h
@@ -32,6 +32,7 @@
 
 #include <sys/types.h>
 #include <stdbool.h>
+#include <stdint.h>
 #define ALLOPTS \
 AOPT('@', off_t, offset, 0, "Offset in device") \
 AOPT('A', bool, align, -2, "Attempt to cluster align root directory") \
@@ -70,3 +71,7 @@
 };
 
 int mkfs_msdos(const char *, const char *, const struct msdos_options *);
+
+#if defined(__GLIBC__)
+static inline char* getprogname() { return program_invocation_short_name; }
+#endif