]> git.ipfire.org Git - thirdparty/util-linux.git/blob - disk-utils/mkfs.c
Merge branch 'mkfs' of git://github.com/kerolasa/lelux-utiliteetit
[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 #include <getopt.h>
17 #include <limits.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22
23 #include "c.h"
24 #include "nls.h"
25 #include "xalloc.h"
26
27 #ifndef DEFAULT_FSTYPE
28 #define DEFAULT_FSTYPE "ext2"
29 #endif
30
31 #define SEARCH_PATH "PATH=" FS_SEARCH_PATH
32 #define PROGNAME "mkfs.%s"
33
34
35 static void __attribute__ ((__noreturn__)) usage(FILE * out)
36 {
37 fprintf(out,
38 _("Usage: %s [options] [-t type fs-options] device [size]\n"),
39 program_invocation_short_name);
40
41 fprintf(out, _("\nOptions:\n"
42 " -t, --type=TYPE file system type, when undefined ext2 is used\n"
43 " fs-options parameters to real file system builder\n"
44 " device path to a device\n"
45 " size number of blocks on the device\n"
46 " -V, --verbose explain what is done\n"
47 " defining -V more than once will cause a dry-run\n"
48 " -V, --version output version information and exit\n"
49 " -V as version must be only option\n"
50 " -h, --help display this help and exit\n"));
51
52 fprintf(out, _("\nFor more information see mkfs(8).\n"));
53
54 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
55 }
56
57 static void __attribute__ ((__noreturn__)) print_version(void)
58 {
59 printf(_("%s (%s)\n"),
60 program_invocation_short_name, PACKAGE_STRING);
61 exit(EXIT_SUCCESS);
62 }
63
64 int main(int argc, char **argv)
65 {
66 char *progname; /* name of executable to be called */
67 char *fstype = NULL;
68 int i, more = 0, verbose = 0;
69 char *oldpath, *newpath;
70
71 enum { VERSION_OPTION = CHAR_MAX + 1 };
72
73 static const struct option longopts[] = {
74 {"type", required_argument, NULL, 't'},
75 {"version", no_argument, NULL, VERSION_OPTION},
76 {"help", no_argument, NULL, 'h'},
77 {NULL, 0, NULL, 0}
78 };
79
80 setlocale(LC_ALL, "");
81 bindtextdomain(PACKAGE, LOCALEDIR);
82 textdomain(PACKAGE);
83
84 if (argc == 2 && !strcmp(argv[1], "-V"))
85 print_version();
86
87 /* Check commandline options. */
88 opterr = 0;
89 while ((more == 0)
90 && ((i = getopt_long(argc, argv, "Vt:h", longopts, NULL))
91 != -1))
92 switch (i) {
93 case 'V':
94 verbose++;
95 break;
96 case 't':
97 fstype = optarg;
98 break;
99 case 'h':
100 usage(stdout);
101 case VERSION_OPTION:
102 print_version();
103 default:
104 optind--;
105 more = 1;
106 break; /* start of specific arguments */
107 }
108 if (optind == argc)
109 usage(stderr);
110
111 /* If -t wasn't specified, use the default */
112 if (fstype == NULL)
113 fstype = DEFAULT_FSTYPE;
114
115 /* Set PATH and program name */
116 oldpath = getenv("PATH");
117 if (!oldpath)
118 oldpath = "/bin";
119
120 newpath = xmalloc(strlen(oldpath) + sizeof(SEARCH_PATH) + 3);
121 sprintf(newpath, "%s:%s\n", SEARCH_PATH, oldpath);
122 putenv(newpath);
123
124 progname = xmalloc(sizeof(PROGNAME) + strlen(fstype) + 1);
125 sprintf(progname, PROGNAME, fstype);
126 argv[--optind] = progname;
127
128 if (verbose) {
129 printf(_("mkfs (%s)\n"), PACKAGE_STRING);
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);
140 perror(progname);
141 return EXIT_FAILURE;
142 }