]> git.ipfire.org Git - thirdparty/util-linux.git/blob - disk-utils/mkfs.c
textual: improve error messages
[thirdparty/util-linux.git] / disk-utils / mkfs.c
1 /*
2 * mkfs A simple generic frontend for the for the mkfs program
3 * under Linux. See the manual page for details.
4 *
5 * Authors: David Engel, <david@ods.com>
6 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
7 * Ron Sommeling, <sommel@sci.kun.nl>
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>
11 * 1999-02-22 Arkadiusz Miƛkiewicz <misiek@pld.ORG.PL>
12 * - added Native Language Support
13 *
14 */
15
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
25 #include <getopt.h>
26 #include <limits.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31
32 #include "c.h"
33 #include "closestream.h"
34 #include "nls.h"
35 #include "xalloc.h"
36
37 #ifndef DEFAULT_FSTYPE
38 #define DEFAULT_FSTYPE "ext2"
39 #endif
40
41 #define SEARCH_PATH "PATH=" FS_SEARCH_PATH
42 #define PROGNAME "mkfs.%s"
43
44
45 static void __attribute__ ((__noreturn__)) usage(FILE * out)
46 {
47 fputs(USAGE_HEADER, out);
48 fprintf(out, _(" %s [options] [-t <type>] [fs-options] <device> [<size>]\n"),
49 program_invocation_short_name);
50 fputs(USAGE_OPTIONS, out);
51 fprintf(out, _(" -t, --type=<type> filesystem type; when unspecified, ext2 is used\n"));
52 fprintf(out, _(" fs-options parameters for the real filesystem builder\n"));
53 fprintf(out, _(" <device> path to the device to be used\n"));
54 fprintf(out, _(" <size> number of blocks to be used on the device\n"));
55 fprintf(out, _(" -V, --verbose explain what is being done;\n"
56 " specifying -V more than once will cause a dry-run\n"));
57 fprintf(out, _(" -V, --version display version information and exit;\n"
58 " -V as --version must be the only option\n"));
59 fprintf(out, _(" -h, --help display this help text and exit\n"));
60
61 fprintf(out, USAGE_MAN_TAIL("mkfs(8)"));
62
63 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
64 }
65
66 static void __attribute__ ((__noreturn__)) print_version(void)
67 {
68 printf(UTIL_LINUX_VERSION);
69 exit(EXIT_SUCCESS);
70 }
71
72 int main(int argc, char **argv)
73 {
74 char *progname; /* name of executable to be called */
75 char *fstype = NULL;
76 int i, more = 0, verbose = 0;
77 char *oldpath, *newpath;
78
79 enum { VERSION_OPTION = CHAR_MAX + 1 };
80
81 static const struct option longopts[] = {
82 {"type", required_argument, NULL, 't'},
83 {"version", no_argument, NULL, VERSION_OPTION},
84 {"verbose", no_argument, NULL, 'V'},
85 {"help", no_argument, NULL, 'h'},
86 {NULL, 0, NULL, 0}
87 };
88
89 setlocale(LC_ALL, "");
90 bindtextdomain(PACKAGE, LOCALEDIR);
91 textdomain(PACKAGE);
92 atexit(close_stdout);
93
94 if (argc == 2 && !strcmp(argv[1], "-V"))
95 print_version();
96
97 /* Check commandline options. */
98 opterr = 0;
99 while ((more == 0)
100 && ((i = getopt_long(argc, argv, "Vt:h", longopts, NULL))
101 != -1))
102 switch (i) {
103 case 'V':
104 verbose++;
105 break;
106 case 't':
107 fstype = optarg;
108 break;
109 case 'h':
110 usage(stdout);
111 case VERSION_OPTION:
112 print_version();
113 default:
114 optind--;
115 more = 1;
116 break; /* start of specific arguments */
117 }
118 if (optind == argc)
119 usage(stderr);
120
121 /* If -t wasn't specified, use the default */
122 if (fstype == NULL)
123 fstype = DEFAULT_FSTYPE;
124
125 /* Set PATH and program name */
126 oldpath = getenv("PATH");
127 if (!oldpath)
128 oldpath = "/bin";
129
130 newpath = xmalloc(strlen(oldpath) + sizeof(SEARCH_PATH) + 3);
131 sprintf(newpath, "%s:%s\n", SEARCH_PATH, oldpath);
132 putenv(newpath);
133
134 progname = xmalloc(sizeof(PROGNAME) + strlen(fstype) + 1);
135 sprintf(progname, PROGNAME, fstype);
136 argv[--optind] = progname;
137
138 if (verbose) {
139 printf(UTIL_LINUX_VERSION);
140 i = optind;
141 while (argv[i])
142 printf("%s ", argv[i++]);
143 printf("\n");
144 if (verbose > 1)
145 return EXIT_SUCCESS;
146 }
147
148 /* Execute the program */
149 execvp(progname, argv + optind);
150 err(EXIT_FAILURE, _("failed to execute %s"), progname);
151 }