]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - disk-utils/mkfs.bfs.c
hwclock: free temporary variable before return
[thirdparty/util-linux.git] / disk-utils / mkfs.bfs.c
index dcdb09850e88d10e1f6dcaebb4733dacb4889d66..895a1f27b7a5c242b60a1571fca8129f3c308d98 100644 (file)
@@ -1,9 +1,17 @@
 /*
- *  mkfs.bfs - Create SCO BFS filesystem - aeb, 1999-09-07
+ * SPDX-License-Identifier: GPL-2.0-or-later
  *
- *     Usage: mkfs.bfs [-N nr-of-inodes] [-V volume-name] [-F fsname] device
+ * 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 2 of the License, or
+ * (at your option) any later version.
+ *
+ * mkfs.bfs - Create SCO BFS filesystem - aeb, 1999-09-07
+ *
+ * Copyright (C) 1999 Andries E. Brouwe
+ *
+ * Usage: mkfs.bfs [-N nr-of-inodes] [-V volume-name] [-F fsname] device
  */
-
 #include <errno.h>
 #include <fcntl.h>
 #include <getopt.h>
@@ -22,6 +30,7 @@
 #include "strutils.h"
 #include "xalloc.h"
 #include "bitops.h"
+#include "exitcodes.h"
 
 #define BFS_ROOT_INO           2
 #define BFS_NAMELEN            14
@@ -67,11 +76,16 @@ struct bfsde {
        char d_name[BFS_NAMELEN];
 };
 
-static void __attribute__ ((__noreturn__)) usage(FILE * out)
+static void __attribute__((__noreturn__)) usage(void)
 {
+       FILE *out = stdout;
        fprintf(out,
                _("Usage: %s [options] device [block-count]\n"),
                program_invocation_short_name);
+
+       fputs(USAGE_SEPARATOR, out);
+       fputs(_("Make an SCO bfs filesystem.\n"), out);
+
        fprintf(out, _("\nOptions:\n"
                       " -N, --inodes=NUM    specify desired number of inodes\n"
                       " -V, --vname=NAME    specify volume name\n"
@@ -79,22 +93,18 @@ static void __attribute__ ((__noreturn__)) usage(FILE * out)
                       " -v, --verbose       explain what is being done\n"
                       " -c                  this option is silently ignored\n"
                       " -l                  this option is silently ignored\n"
-                      " -V, --version       output version information and exit\n"
-                      "                     -V as version must be only option\n"
-                      " -h, --help          display this help and exit\n\n"));
-
-       exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
-}
+                      " --lock[=<mode>]     use exclusive device lock (yes, no or nonblock)\n"
+                      ));
+       fprintf(out, USAGE_HELP_OPTIONS(21));
 
-static void __attribute__ ((__noreturn__)) print_version(void)
-{
-       printf(UTIL_LINUX_VERSION);
+       fprintf(out, USAGE_MAN_TAIL("mkfs.bfs(8)"));
        exit(EXIT_SUCCESS);
 }
 
 int main(int argc, char **argv)
 {
        char *device, *volume, *fsname;
+       char *lockmode = 0;
        long inodes;
        unsigned long long total_blocks, ino_bytes, ino_blocks, data_blocks;
        unsigned long long user_specified_total_blocks = 0;
@@ -108,7 +118,10 @@ int main(int argc, char **argv)
        time_t now;
        int c, i, len;
 
-       enum { VERSION_OPTION = CHAR_MAX + 1 };
+       enum {
+           VERSION_OPTION = CHAR_MAX + 1,
+           OPT_LOCK
+       };
        static const struct option longopts[] = {
                {"inodes", required_argument, NULL, 'N'},
                {"vname", required_argument, NULL, 'V'},
@@ -116,19 +129,21 @@ int main(int argc, char **argv)
                {"verbose", no_argument, NULL, 'v'},
                {"version", no_argument, NULL, VERSION_OPTION},
                {"help", no_argument, NULL, 'h'},
+               {"lock", optional_argument, NULL, OPT_LOCK},
                {NULL, 0, NULL, 0}
        };
 
        setlocale(LC_ALL, "");
        bindtextdomain(PACKAGE, LOCALEDIR);
        textdomain(PACKAGE);
-       atexit(close_stdout);
-
-       if (argc < 2)
-               usage(stderr);
+       close_stdout_atexit();
 
+       if (argc < 2) {
+               warnx(_("not enough arguments"));
+               errtryhelp(EXIT_FAILURE);
+       }
        if (argc == 2 && !strcmp(argv[1], "-V"))
-               print_version();
+               print_version(EXIT_SUCCESS);
 
        volume = fsname = "      ";     /* is there a default? */
        inodes = 0;
@@ -162,35 +177,48 @@ int main(int argc, char **argv)
                        /* when called via mkfs we may get options c,l,v */
                        break;
 
+                case OPT_LOCK:
+                       lockmode = "1";
+                       if (optarg) {
+                               if (*optarg == '=')
+                                       optarg++;
+                               lockmode = optarg;
+                       }
+                       break;
+
                case VERSION_OPTION:
-                       print_version();
+                       print_version(EXIT_SUCCESS);
                case 'h':
-                       usage(stdout);
+                       usage();
                default:
-                       usage(stderr);
+                       errtryhelp(EXIT_FAILURE);
                }
        }
 
-       if (optind == argc)
-               usage(stderr);
+       if (optind == argc) {
+               warnx(_("no device specified"));
+               errtryhelp(EXIT_FAILURE);
+       }
 
        device = argv[optind++];
 
        if (stat(device, &statbuf) < 0)
-               err(EXIT_FAILURE, _("stat failed %s"), device);
-
-       if (!S_ISBLK(statbuf.st_mode))
-               errx(EXIT_FAILURE, _("%s is not a block special device"), device);
+               err(EXIT_FAILURE, _("stat of %s failed"), device);
 
-       fd = open(device, O_RDWR | O_EXCL);
+       fd = open_blkdev_or_file(&statbuf, device, O_RDWR);
        if (fd < 0)
                err(EXIT_FAILURE, _("cannot open %s"), device);
 
+       if (blkdev_lock(fd, device, lockmode) != 0)
+               exit(MKFS_EX_ERROR);
+
        if (optind == argc - 1)
                user_specified_total_blocks =
                        strtou64_or_err(argv[optind], _("invalid block-count"));
-       else if (optind != argc)
-               usage(stderr);
+       else if (optind != argc) {
+               warnx(_("bad usage"));
+               errtryhelp(EXIT_FAILURE);
+       }
 
        if (blkdev_get_sectors(fd, &total_blocks) == -1) {
                if (!user_specified_total_blocks)
@@ -221,7 +249,7 @@ int main(int argc, char **argv)
        ino_blocks = (ino_bytes + BFS_BLOCKSIZE - 1) / BFS_BLOCKSIZE;
        data_blocks = total_blocks - ino_blocks - 1;
 
-       /* mimic the behaviour of SCO's mkfs - maybe this limit is needed */
+       /* mimic the behavior of SCO's mkfs - maybe this limit is needed */
        if (data_blocks < 32)
                errx(EXIT_FAILURE,
                     _("not enough space, need at least %llu blocks"),
@@ -241,12 +269,12 @@ int main(int argc, char **argv)
                fprintf(stderr, _("FSname: <%-6s>\n"), fsname);
                fprintf(stderr, _("BlockSize: %d\n"), BFS_BLOCKSIZE);
                if (ino_blocks == 1)
-                       fprintf(stderr, _("Inodes: %lu (in 1 block)\n"),
+                       fprintf(stderr, _("Inodes: %ld (in 1 block)\n"),
                                inodes);
                else
-                       fprintf(stderr, _("Inodes: %lu (in %llu blocks)\n"),
+                       fprintf(stderr, _("Inodes: %ld (in %llu blocks)\n"),
                                inodes, ino_blocks);
-               fprintf(stderr, _("Blocks: %lld\n"), total_blocks);
+               fprintf(stderr, _("Blocks: %llu\n"), total_blocks);
                fprintf(stderr, _("Inode end: %d, Data end: %d\n"),
                        le32_to_cpu(sb.s_start) - 1, le32_to_cpu(sb.s_end));
        }