]> git.ipfire.org Git - thirdparty/util-linux.git/blob - disk-utils/mkfs.c
fsck/mkfs/mount: unify default search paths for helpers
[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 * Usage: mkfs [-V] [-t fstype] [fs-options] device [size]
6 *
7 * Authors: David Engel, <david@ods.com>
8 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
9 * Ron Sommeling, <sommel@sci.kun.nl>
10 *
11 * Mon Jul 1 18:52:58 1996: janl@math.uio.no (Nicolai Langfeldt):
12 * Incorporated fix by Jonathan Kamens <jik@annex-1-slip-jik.cam.ov.com>
13 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
14 * - added Native Language Support
15 *
16 */
17
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <getopt.h>
24 #include <nls.h>
25
26 #ifndef DEFAULT_FSTYPE
27 # define DEFAULT_FSTYPE "ext2"
28 #endif
29
30 #define SEARCH_PATH "PATH=" FS_SEARCH_PATH
31 #define PROGNAME "mkfs.%s"
32
33
34 int main(int argc, char *argv[])
35 {
36 char *progname; /* name of executable to be called */
37 char *fstype = NULL;
38 int i, more = 0, verbose = 0;
39 char *oldpath, *newpath;
40 char *program_name, *p;
41
42 program_name = argv[0];
43 if ((p = strrchr(program_name, '/')) != NULL)
44 program_name = p+1;
45
46 setlocale(LC_ALL, "");
47 bindtextdomain(PACKAGE, LOCALEDIR);
48 textdomain(PACKAGE);
49
50 if (argc == 2 &&
51 (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version"))) {
52 printf(_("%s (%s)\n"), program_name, PACKAGE_STRING);
53 exit(0);
54 }
55
56 /* Check commandline options. */
57 opterr = 0;
58 while ((more == 0) && ((i = getopt(argc, argv, "Vt:")) != -1))
59 switch (i) {
60 case 'V':
61 verbose++;
62 break;
63 case 't':
64 fstype = optarg;
65 break;
66 default:
67 optind--;
68 more = 1;
69 break; /* start of specific arguments */
70 }
71 if (optind == argc) {
72 fprintf(stderr,
73 _("Usage: mkfs [-V] [-t fstype] [fs-options] device [size]\n"));
74 return -1;
75 }
76
77 /* If -t wasn't specified, use the default */
78 if (fstype == NULL)
79 fstype = DEFAULT_FSTYPE;
80
81 /* Set PATH and program name */
82 oldpath = getenv("PATH");
83 if (!oldpath)
84 oldpath = "/bin";
85
86 newpath = (char *) malloc(strlen(oldpath) + sizeof(SEARCH_PATH) + 3);
87 if (!newpath) {
88 fprintf(stderr, _("%s: Out of memory!\n"), "mkfs");
89 exit(1);
90 }
91 sprintf(newpath, "%s:%s\n", SEARCH_PATH, oldpath);
92 putenv(newpath);
93
94 progname = (char *) malloc(sizeof(PROGNAME) + strlen(fstype) + 1);
95 if (!progname) {
96 fprintf(stderr, _("%s: Out of memory!\n"), "mkfs");
97 exit(1);
98 }
99 sprintf(progname, PROGNAME, fstype);
100 argv[--optind] = progname;
101
102 if (verbose) {
103 printf(_("mkfs (%s)\n"), PACKAGE_STRING);
104 i = optind;
105 while (argv[i])
106 printf("%s ", argv[i++]);
107 printf("\n");
108 if (verbose > 1)
109 return 0;
110 }
111
112 /* Execute the program */
113 execvp(progname, argv+optind);
114 perror(progname);
115 return 1;
116 }