]> git.ipfire.org Git - thirdparty/util-linux.git/blob - disk-utils/mkfs.c
Imported from util-linux-2.8 tarball.
[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 */
14
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <string.h>
20 #include <getopt.h>
21 #include <limits.h>
22
23
24 #define VERSION "1.10"
25
26 #ifndef DEFAULT_FSTYPE
27 # define DEFAULT_FSTYPE "ext2"
28 #endif
29
30 #define SEARCH_PATH "PATH=/sbin:/sbin/fs:/sbin/fs.d:/etc/fs:/etc"
31 #define PROGNAME "mkfs.%s"
32
33
34 int main(int argc, char *argv[])
35 {
36 char progname[NAME_MAX];
37 char *fstype = NULL;
38 int i, more = 0, verbose = 0;
39 char *oldpath, *newpath;
40
41 /* Check commandline options. */
42 opterr = 0;
43 while ((more == 0) && ((i = getopt(argc, argv, "Vt:")) != EOF))
44 switch (i) {
45 case 'V':
46 verbose++;
47 break;
48 case 't':
49 fstype = optarg;
50 break;
51 default:
52 optind--;
53 more = 1;
54 break; /* start of specific arguments */
55 }
56 if (optind == argc) {
57 fprintf(stderr,
58 "Usage: mkfs [-V] [-t fstype] [fs-options] device [size]\n");
59 return -1;
60 }
61
62 /* If -t wasn't specified, use the default */
63 if (fstype == NULL)
64 fstype = DEFAULT_FSTYPE;
65
66 /* Set PATH and program name */
67 oldpath = getenv("PATH");
68 if (!oldpath)
69 oldpath = "/bin";
70 newpath = (char *) malloc(strlen(oldpath) + sizeof(SEARCH_PATH) + 2);
71 if (!newpath) {
72 fputs("mkfs: out of memory\n", stderr);
73 exit(1);
74 }
75 sprintf(newpath, "%s:%s\n", SEARCH_PATH, oldpath);
76 putenv(newpath);
77 sprintf(progname, PROGNAME, fstype);
78 argv[--optind] = progname;
79
80 if (verbose) {
81 puts("mkfs version " VERSION " (" __DATE__ ")");
82 i = optind;
83 while (argv[i])
84 printf("%s ", argv[i++]);
85 printf("\n");
86 if (verbose > 1)
87 return 0;
88 }
89
90 /* Execute the program */
91 execvp(progname, argv+optind);
92 perror(progname);
93 return 1;
94 }