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