]> git.ipfire.org Git - thirdparty/util-linux.git/blame - disk-utils/mkfs.c
Merge branch 'meson-more-build-options' of https://github.com/jwillikers/util-linux
[thirdparty/util-linux.git] / disk-utils / mkfs.c
CommitLineData
6dbe3af9 1/*
9e95aa12
KZ
2 * SPDX-License-Identifier: GPL-2.0-or-later
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
dbb2fb39 9 * mkfs A simple generic frontend for the mkfs program
726f69e2 10 * under Linux. See the manual page for details.
6dbe3af9 11 *
9e95aa12
KZ
12 * Copyright (C) David Engel, <david@ods.com>
13 * Copyright (C) Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
14 * Copyright (C) Ron Sommeling, <sommel@sci.kun.nl>
fd6b7a7f
KZ
15 *
16 * Mon Jul 1 18:52:58 1996: janl@math.uio.no (Nicolai Langfeldt):
17 * Incorporated fix by Jonathan Kamens <jik@annex-1-slip-jik.cam.ov.com>
b50945d4 18 * 1999-02-22 Arkadiusz Miƛkiewicz <misiek@pld.ORG.PL>
7eda085c 19 * - added Native Language Support
2bb3aa36 20 *
6dbe3af9
KZ
21 */
22
7d2600e2
SK
23/*
24 * This command is deprecated. The utility is in maintenance mode,
25 * meaning we keep them in source tree for backward compatibility
26 * only. Do not waste time making this command better, unless the
27 * fix is about security or other very critical issue.
28 *
29 * See Documentation/deprecated.txt for more information.
30 */
31
316cad61 32#include <getopt.h>
d786727a 33#include <limits.h>
6dbe3af9 34#include <stdio.h>
d786727a 35#include <stdlib.h>
2b6fc908 36#include <string.h>
d786727a 37#include <unistd.h>
726f69e2 38
d786727a 39#include "c.h"
45ca68ec 40#include "closestream.h"
d786727a 41#include "nls.h"
eb811909
DB
42#include "xalloc.h"
43
6dbe3af9 44#ifndef DEFAULT_FSTYPE
02b1c216 45#define DEFAULT_FSTYPE "ext2"
6dbe3af9
KZ
46#endif
47
6e1eda6f 48static void __attribute__((__noreturn__)) usage(void)
316cad61 49{
6e1eda6f 50 FILE *out = stdout;
db433bf7 51 fputs(USAGE_HEADER, out);
4ec32f6a
BS
52 fprintf(out, _(" %s [options] [-t <type>] [fs-options] <device> [<size>]\n"),
53 program_invocation_short_name);
451dbcfa
BS
54
55 fputs(USAGE_SEPARATOR, out);
56 fputs(_("Make a Linux filesystem.\n"), out);
57
db433bf7 58 fputs(USAGE_OPTIONS, out);
4ec32f6a
BS
59 fprintf(out, _(" -t, --type=<type> filesystem type; when unspecified, ext2 is used\n"));
60 fprintf(out, _(" fs-options parameters for the real filesystem builder\n"));
61 fprintf(out, _(" <device> path to the device to be used\n"));
62 fprintf(out, _(" <size> number of blocks to be used on the device\n"));
63 fprintf(out, _(" -V, --verbose explain what is being done;\n"
64 " specifying -V more than once will cause a dry-run\n"));
bad4c729 65 fprintf(out, USAGE_HELP_OPTIONS(20));
316cad61 66
bad4c729 67 fprintf(out, USAGE_MAN_TAIL("mkfs(8)"));
6e1eda6f 68 exit(EXIT_SUCCESS);
316cad61
SK
69}
70
02b1c216 71int main(int argc, char **argv)
6dbe3af9 72{
02b1c216
SK
73 char *progname; /* name of executable to be called */
74 char *fstype = NULL;
75 int i, more = 0, verbose = 0;
02b1c216
SK
76
77 enum { VERSION_OPTION = CHAR_MAX + 1 };
78
79 static const struct option longopts[] = {
80 {"type", required_argument, NULL, 't'},
81 {"version", no_argument, NULL, VERSION_OPTION},
b5ab5bec 82 {"verbose", no_argument, NULL, 'V'},
02b1c216
SK
83 {"help", no_argument, NULL, 'h'},
84 {NULL, 0, NULL, 0}
85 };
86
87 setlocale(LC_ALL, "");
88 bindtextdomain(PACKAGE, LOCALEDIR);
89 textdomain(PACKAGE);
2c308875 90 close_stdout_atexit();
02b1c216
SK
91
92 if (argc == 2 && !strcmp(argv[1], "-V"))
68224d10 93 print_version(EXIT_SUCCESS);
02b1c216
SK
94
95 /* Check commandline options. */
96 opterr = 0;
97 while ((more == 0)
98 && ((i = getopt_long(argc, argv, "Vt:h", longopts, NULL))
99 != -1))
100 switch (i) {
101 case 'V':
102 verbose++;
103 break;
104 case 't':
105 fstype = optarg;
106 break;
107 case 'h':
6e1eda6f 108 usage();
02b1c216 109 case VERSION_OPTION:
68224d10 110 print_version(EXIT_SUCCESS);
02b1c216
SK
111 default:
112 optind--;
113 more = 1;
114 break; /* start of specific arguments */
115 }
6e1eda6f
RM
116 if (optind == argc) {
117 warnx(_("no device specified"));
118 errtryhelp(EXIT_FAILURE);
119 }
02b1c216
SK
120
121 /* If -t wasn't specified, use the default */
122 if (fstype == NULL)
123 fstype = DEFAULT_FSTYPE;
124
07b51567 125 xasprintf(&progname, "mkfs.%s", fstype);
02b1c216
SK
126 argv[--optind] = progname;
127
128 if (verbose) {
e421313d 129 printf(UTIL_LINUX_VERSION);
02b1c216
SK
130 i = optind;
131 while (argv[i])
132 printf("%s ", argv[i++]);
133 printf("\n");
134 if (verbose > 1)
135 return EXIT_SUCCESS;
136 }
137
138 /* Execute the program */
139 execvp(progname, argv + optind);
338a6bc5 140 err(EXIT_FAILURE, _("failed to execute %s"), progname);
6dbe3af9 141}