]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - disk-utils/mkfs.c
Make the ways of using output stream consistent in usage()
[thirdparty/util-linux.git] / disk-utils / mkfs.c
index c5aa7566ba6116db5edc4c3d14351c7416cd71e3..58856d87160ef112ac82e445e4282dc50823dfb9 100644 (file)
 /*
- * mkfs                A simple generic frontend for the for the mkfs program
+ * mkfs                A simple generic frontend for the mkfs program
  *             under Linux.  See the manual page for details.
  *
- * Usage:      mkfs [-V] [-t fstype] [fs-options] device [size]
- *
  * Authors:    David Engel, <david@ods.com>
  *             Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  *             Ron Sommeling, <sommel@sci.kun.nl>
  *
  * Mon Jul  1 18:52:58 1996: janl@math.uio.no (Nicolai Langfeldt):
  *     Incorporated fix by Jonathan Kamens <jik@annex-1-slip-jik.cam.ov.com>
- * 1999-02-22 Arkadiusz Mikiewicz <misiek@pld.ORG.PL>
+ * 1999-02-22 Arkadiusz Miśkiewicz <misiek@pld.ORG.PL>
  * - added Native Language Support
- *     
+ *
  */
 
+/*
+ * This command is deprecated.  The utility is in maintenance mode,
+ * meaning we keep them in source tree for backward compatibility
+ * only.  Do not waste time making this command better, unless the
+ * fix is about security or other very critical issue.
+ *
+ * See Documentation/deprecated.txt for more information.
+ */
 
+#include <getopt.h>
+#include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <unistd.h>
 #include <string.h>
-#include <getopt.h>
-#include <limits.h>
-#include "nls.h"
+#include <unistd.h>
 
-#define VERSION                UTIL_LINUX_VERSION
+#include "c.h"
+#include "closestream.h"
+#include "nls.h"
+#include "xalloc.h"
 
 #ifndef DEFAULT_FSTYPE
-# define DEFAULT_FSTYPE                "ext2"
+#define DEFAULT_FSTYPE "ext2"
 #endif
 
-#define SEARCH_PATH    "PATH=/sbin:/sbin/fs:/sbin/fs.d:/etc/fs:/etc"
-#define PROGNAME       "mkfs.%s"
+static void __attribute__((__noreturn__)) usage(void)
+{
+       FILE *out = stdout;
+       fputs(USAGE_HEADER, out);
+       fprintf(out, _(" %s [options] [-t <type>] [fs-options] <device> [<size>]\n"),
+                    program_invocation_short_name);
+
+       fputs(USAGE_SEPARATOR, out);
+       fputs(_("Make a Linux filesystem.\n"), out);
 
+       fputs(USAGE_OPTIONS, out);
+       fprintf(out, _(" -t, --type=<type>  filesystem type; when unspecified, ext2 is used\n"));
+       fprintf(out, _("     fs-options     parameters for the real filesystem builder\n"));
+       fprintf(out, _("     <device>       path to the device to be used\n"));
+       fprintf(out, _("     <size>         number of blocks to be used on the device\n"));
+       fprintf(out, _(" -V, --verbose      explain what is being done;\n"
+                      "                      specifying -V more than once will cause a dry-run\n"));
+       fprintf(out, USAGE_HELP_OPTIONS(20));
+
+       fprintf(out, USAGE_MAN_TAIL("mkfs(8)"));
+       exit(EXIT_SUCCESS);
+}
 
-int main(int argc, char *argv[])
+int main(int argc, char **argv)
 {
-  char progname[NAME_MAX];
-  char *fstype = NULL;
-  int i, more = 0, verbose = 0;
-  char *oldpath, *newpath;
-  char *program_name, *p;
-
-  program_name = argv[0];
-  if ((p = strrchr(program_name, '/')) != NULL)
-         program_name = p+1;
-
-  setlocale(LC_ALL, "");
-  bindtextdomain(PACKAGE, LOCALEDIR);
-  textdomain(PACKAGE);
-
-  if (argc == 2 &&
-      (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version"))) {
-         printf(_("%s from %s\n"), program_name, util_linux_version);
-         exit(0);
-  }
-
-  /* Check commandline options. */
-  opterr = 0;
-  while ((more == 0) && ((i = getopt(argc, argv, "Vt:")) != -1))
-    switch (i) {
-    case 'V':
-      verbose++;
-      break;
-    case 't':
-      fstype = optarg;
-      break;
-    default:
-      optind--;
-      more = 1;
-      break;           /* start of specific arguments */
-    }
-  if (optind == argc) {
-    fprintf(stderr,
-      _("Usage: mkfs [-V] [-t fstype] [fs-options] device [size]\n"));
-    return -1;
-  }
-  
-  /* If -t wasn't specified, use the default */
-  if (fstype == NULL)
-    fstype = DEFAULT_FSTYPE;
-
-  /* Set PATH and program name */
-  oldpath = getenv("PATH");
-  if (!oldpath)
-         oldpath = "/bin";
-  newpath = (char *) malloc(strlen(oldpath) + sizeof(SEARCH_PATH) + 2);
-  if (!newpath) {
-    fprintf(stderr, _("%s: Out of memory!\n"), "mkfs");
-    exit(1);
-  }
-  sprintf(newpath, "%s:%s\n", SEARCH_PATH, oldpath);
-  putenv(newpath);
-  snprintf(progname, sizeof(progname), PROGNAME, fstype);
-  argv[--optind] = progname;
-
-  if (verbose) {
-    printf(_("mkfs version %s (%s)\n"), VERSION, __DATE__);
-    i = optind;
-    while (argv[i])
-      printf("%s ", argv[i++]);
-    printf("\n");
-    if (verbose > 1)
-      return 0;
-  }
-
-  /* Execute the program */
-  execvp(progname, argv+optind);
-  perror(progname);
-  return 1;
+       char *progname;         /* name of executable to be called */
+       char *fstype = NULL;
+       int i, more = 0, verbose = 0;
+
+       enum { VERSION_OPTION = CHAR_MAX + 1 };
+
+       static const struct option longopts[] = {
+               {"type", required_argument, NULL, 't'},
+               {"version", no_argument, NULL, VERSION_OPTION},
+               {"verbose", no_argument, NULL, 'V'},
+               {"help", no_argument, NULL, 'h'},
+               {NULL, 0, NULL, 0}
+       };
+
+       setlocale(LC_ALL, "");
+       bindtextdomain(PACKAGE, LOCALEDIR);
+       textdomain(PACKAGE);
+       close_stdout_atexit();
+
+       if (argc == 2 && !strcmp(argv[1], "-V"))
+               print_version(EXIT_SUCCESS);
+
+       /* Check commandline options. */
+       opterr = 0;
+       while ((more == 0)
+              && ((i = getopt_long(argc, argv, "Vt:h", longopts, NULL))
+                  != -1))
+               switch (i) {
+               case 'V':
+                       verbose++;
+                       break;
+               case 't':
+                       fstype = optarg;
+                       break;
+               case 'h':
+                       usage();
+               case VERSION_OPTION:
+                       print_version(EXIT_SUCCESS);
+               default:
+                       optind--;
+                       more = 1;
+                       break;  /* start of specific arguments */
+               }
+       if (optind == argc) {
+               warnx(_("no device specified"));
+               errtryhelp(EXIT_FAILURE);
+       }
+
+       /* If -t wasn't specified, use the default */
+       if (fstype == NULL)
+               fstype = DEFAULT_FSTYPE;
+
+       xasprintf(&progname, "mkfs.%s", fstype);
+       argv[--optind] = progname;
+
+       if (verbose) {
+               printf(UTIL_LINUX_VERSION);
+               i = optind;
+               while (argv[i])
+                       printf("%s ", argv[i++]);
+               printf("\n");
+               if (verbose > 1)
+                       return EXIT_SUCCESS;
+       }
+
+       /* Execute the program */
+       execvp(progname, argv + optind);
+       err(EXIT_FAILURE, _("failed to execute %s"), progname);
 }