(root)/
util-linux-2.39/
disk-utils/
mkfs.c
       1  /*
       2   * mkfs		A simple generic frontend 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  static void __attribute__((__noreturn__)) usage(void)
      42  {
      43  	FILE *out = stdout;
      44  	fputs(USAGE_HEADER, out);
      45  	fprintf(out, _(" %s [options] [-t <type>] [fs-options] <device> [<size>]\n"),
      46  		     program_invocation_short_name);
      47  
      48  	fputs(USAGE_SEPARATOR, out);
      49  	fputs(_("Make a Linux filesystem.\n"), out);
      50  
      51  	fputs(USAGE_OPTIONS, out);
      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"));
      58  	printf(USAGE_HELP_OPTIONS(20));
      59  
      60  	printf(USAGE_MAN_TAIL("mkfs(8)"));
      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  
      70  	enum { VERSION_OPTION = CHAR_MAX + 1 };
      71  
      72  	static const struct option longopts[] = {
      73  		{"type", required_argument, NULL, 't'},
      74  		{"version", no_argument, NULL, VERSION_OPTION},
      75  		{"verbose", no_argument, NULL, 'V'},
      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  	close_stdout_atexit();
      84  
      85  	if (argc == 2 && !strcmp(argv[1], "-V"))
      86  		print_version(EXIT_SUCCESS);
      87  
      88  	/* Check commandline options. */
      89  	opterr = 0;
      90  	while ((more == 0)
      91  	       && ((i = getopt_long(argc, argv, "Vt:h", longopts, NULL))
      92  		   != -1))
      93  		switch (i) {
      94  		case 'V':
      95  			verbose++;
      96  			break;
      97  		case 't':
      98  			fstype = optarg;
      99  			break;
     100  		case 'h':
     101  			usage();
     102  		case VERSION_OPTION:
     103  			print_version(EXIT_SUCCESS);
     104  		default:
     105  			optind--;
     106  			more = 1;
     107  			break;	/* start of specific arguments */
     108  		}
     109  	if (optind == argc) {
     110  		warnx(_("no device specified"));
     111  		errtryhelp(EXIT_FAILURE);
     112  	}
     113  
     114  	/* If -t wasn't specified, use the default */
     115  	if (fstype == NULL)
     116  		fstype = DEFAULT_FSTYPE;
     117  
     118  	xasprintf(&progname, "mkfs.%s", fstype);
     119  	argv[--optind] = progname;
     120  
     121  	if (verbose) {
     122  		printf(UTIL_LINUX_VERSION);
     123  		i = optind;
     124  		while (argv[i])
     125  			printf("%s ", argv[i++]);
     126  		printf("\n");
     127  		if (verbose > 1)
     128  			return EXIT_SUCCESS;
     129  	}
     130  
     131  	/* Execute the program */
     132  	execvp(progname, argv + optind);
     133  	err(EXIT_FAILURE, _("failed to execute %s"), progname);
     134  }