]> git.sven.stormbind.net Git - sven/exfat-utils.git/blobdiff - mkfs/main.c
releasing package exfat-utils version 1.3.0-2
[sven/exfat-utils.git] / mkfs / main.c
index 4d44fa1004e43e31420ba690072b2790aa31ed1f..473b476fd02b0e3557375db2725f156d8910703f 100644 (file)
@@ -2,11 +2,12 @@
        main.c (15.08.10)
        Creates exFAT file system.
 
        main.c (15.08.10)
        Creates exFAT file system.
 
-       Copyright (C) 2011, 2012  Andrew Nayenko
+       Free exFAT implementation.
+       Copyright (C) 2011-2018  Andrew Nayenko
 
 
-       This program is free software: you can redistribute it and/or modify
+       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
        it under the terms of the GNU General Public License as published by
-       the Free Software Foundation, either version 3 of the License, or
+       the Free Software Foundation, either version 2 of the License, or
        (at your option) any later version.
 
        This program is distributed in the hope that it will be useful,
        (at your option) any later version.
 
        This program is distributed in the hope that it will be useful,
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.
 
        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/>.
+       You should have received a copy of the GNU General Public License along
+       with this program; if not, write to the Free Software Foundation, Inc.,
+       51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
 */
 
+#include "mkexfat.h"
+#include "vbr.h"
+#include "fat.h"
+#include "cbm.h"
+#include "uct.h"
+#include "rootdir.h"
+#include <exfat.h>
 #include <sys/types.h>
 #include <sys/time.h>
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/time.h>
 #include <unistd.h>
 #include <stdio.h>
 #include <string.h>
 #include <limits.h>
 #include <stdio.h>
 #include <string.h>
 #include <limits.h>
-#include <exfat.h>
-#include "mkexfat.h"
-#include "vbr.h"
-#include "fat.h"
-#include "cbm.h"
-#include "uct.h"
-#include "rootdir.h"
 
 const struct fs_object* objects[] =
 {
 
 const struct fs_object* objects[] =
 {
@@ -96,14 +98,6 @@ int get_cluster_size(void)
        return get_sector_size() << get_spc_bits();
 }
 
        return get_sector_size() << get_spc_bits();
 }
 
-static off_t setup_volume_size(struct exfat_dev* dev)
-{
-       off_t size = exfat_seek(dev, 0, SEEK_END);
-       if (size == (off_t) -1)
-               exfat_error("failed to get volume size");
-       return size;
-}
-
 static int setup_spc_bits(int sector_bits, int user_defined, off_t volume_size)
 {
        int i;
 static int setup_spc_bits(int sector_bits, int user_defined, off_t volume_size)
 {
        int i;
@@ -142,7 +136,7 @@ static int setup_volume_label(le16_t label[EXFAT_ENAME_MAX + 1], const char* s)
        memset(label, 0, (EXFAT_ENAME_MAX + 1) * sizeof(le16_t));
        if (s == NULL)
                return 0;
        memset(label, 0, (EXFAT_ENAME_MAX + 1) * sizeof(le16_t));
        if (s == NULL)
                return 0;
-       return utf8_to_utf16(label, s, EXFAT_ENAME_MAX, strlen(s));
+       return utf8_to_utf16(label, s, EXFAT_ENAME_MAX + 1, strlen(s));
 }
 
 static uint32_t setup_volume_serial(uint32_t user_defined)
 }
 
 static uint32_t setup_volume_serial(uint32_t user_defined)
@@ -166,10 +160,7 @@ static int setup(struct exfat_dev* dev, int sector_bits, int spc_bits,
 {
        param.sector_bits = sector_bits;
        param.first_sector = first_sector;
 {
        param.sector_bits = sector_bits;
        param.first_sector = first_sector;
-
-       param.volume_size = setup_volume_size(dev);
-       if (param.volume_size == (off_t) -1)
-               return 1;
+       param.volume_size = exfat_get_size(dev);
 
        param.spc_bits = setup_spc_bits(sector_bits, spc_bits, param.volume_size);
        if (param.spc_bits == -1)
 
        param.spc_bits = setup_spc_bits(sector_bits, spc_bits, param.volume_size);
        if (param.spc_bits == -1)
@@ -199,72 +190,56 @@ static void usage(const char* prog)
 {
        fprintf(stderr, "Usage: %s [-i volume-id] [-n label] "
                        "[-p partition-first-sector] "
 {
        fprintf(stderr, "Usage: %s [-i volume-id] [-n label] "
                        "[-p partition-first-sector] "
-                       "[-s sectors-per-cluster] [-v] <device>\n", prog);
+                       "[-s sectors-per-cluster] [-V] <device>\n", prog);
        exit(1);
 }
 
 int main(int argc, char* argv[])
 {
        const char* spec = NULL;
        exit(1);
 }
 
 int main(int argc, char* argv[])
 {
        const char* spec = NULL;
-       char** pp;
+       int opt;
        int spc_bits = -1;
        const char* volume_label = NULL;
        uint32_t volume_serial = 0;
        uint64_t first_sector = 0;
        struct exfat_dev* dev;
 
        int spc_bits = -1;
        const char* volume_label = NULL;
        uint32_t volume_serial = 0;
        uint64_t first_sector = 0;
        struct exfat_dev* dev;
 
-       printf("mkexfatfs %u.%u.%u\n",
-                       EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH);
+       printf("mkexfatfs %s\n", VERSION);
 
 
-       for (pp = argv + 1; *pp; pp++)
+       while ((opt = getopt(argc, argv, "i:n:p:s:V")) != -1)
        {
        {
-               if (strcmp(*pp, "-s") == 0)
+               switch (opt)
                {
                {
-                       pp++;
-                       if (*pp == NULL)
-                               usage(argv[0]);
-                       spc_bits = logarithm2(atoi(*pp));
+               case 'i':
+                       volume_serial = strtol(optarg, NULL, 16);
+                       break;
+               case 'n':
+                       volume_label = optarg;
+                       break;
+               case 'p':
+                       first_sector = strtoll(optarg, NULL, 10);
+                       break;
+               case 's':
+                       spc_bits = logarithm2(atoi(optarg));
                        if (spc_bits < 0)
                        {
                        if (spc_bits < 0)
                        {
-                               exfat_error("invalid option value: `%s'", *pp);
+                               exfat_error("invalid option value: '%s'", optarg);
                                return 1;
                        }
                                return 1;
                        }
-               }
-               else if (strcmp(*pp, "-n") == 0)
-               {
-                       pp++;
-                       if (*pp == NULL)
-                               usage(argv[0]);
-                       volume_label = *pp;
-               }
-               else if (strcmp(*pp, "-i") == 0)
-               {
-                       pp++;
-                       if (*pp == NULL)
-                               usage(argv[0]);
-                       volume_serial = strtol(*pp, NULL, 16);
-               }
-               else if (strcmp(*pp, "-p") == 0)
-               {
-                       pp++;
-                       if (*pp == NULL)
-                               usage(argv[0]);
-                       first_sector = strtoll(*pp, NULL, 10);
-               }
-               else if (strcmp(*pp, "-v") == 0)
-               {
-                       puts("Copyright (C) 2011, 2012  Andrew Nayenko");
+                       break;
+               case 'V':
+                       puts("Copyright (C) 2011-2018  Andrew Nayenko");
                        return 0;
                        return 0;
-               }
-               else if (spec == NULL)
-                       spec = *pp;
-               else
+               default:
                        usage(argv[0]);
                        usage(argv[0]);
+                       break;
+               }
        }
        }
-       if (spec == NULL)
+       if (argc - optind != 1)
                usage(argv[0]);
                usage(argv[0]);
+       spec = argv[optind];
 
 
-       dev = exfat_open(spec, 0);
+       dev = exfat_open(spec, EXFAT_MODE_RW);
        if (dev == NULL)
                return 1;
        if (setup(dev, 9, spc_bits, volume_label, volume_serial,
        if (dev == NULL)
                return 1;
        if (setup(dev, 9, spc_bits, volume_label, volume_serial,