summaryrefslogtreecommitdiffstats
path: root/disk-utils/mkfs.c
blob: 120da0636b6317a5706c672db9743d69894dd1f4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 * mkfs		A simple generic frontend for the for the mkfs program
 *		under Linux.  See the manual page for details.
 *
 * Usage:	mkfs [-V] [-t fstype] [fs-options] device [size]
 *
 * Authors:	David Engel, <david@ods.com>
 *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
 *		Ron Sommeling, <sommel@sci.kun.nl>
 *
 * Mon Jul  1 18:52:58 1996: janl@math.uio.no (Nicolai Langfeldt):
 *	Incorporated fix by Jonathan Kamens <jik@annex-1-slip-jik.cam.ov.com>
 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
 * - added Native Language Support
 *	
 */


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <getopt.h>
#include <nls.h>

#ifndef DEFAULT_FSTYPE
# define DEFAULT_FSTYPE		"ext2"
#endif

#define SEARCH_PATH	"PATH=/sbin:/sbin/fs:/sbin/fs.d:/etc/fs:/etc"
#define PROGNAME	"mkfs.%s"


int main(int argc, char *argv[])
{
  char *progname;	/* name of executable to be called */
  char *fstype = NULL;
  int i, more = 0, verbose = 0;
  char *oldpath, *newpath;
  char *program_name, *p;

  program_name = argv[0];
  if ((p = strrchr(program_name, '/')) != NULL)
	  program_name = p+1;

  setlocale(LC_ALL, "");
  bindtextdomain(PACKAGE, LOCALEDIR);
  textdomain(PACKAGE);

  if (argc == 2 &&
      (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version"))) {
	  printf(_("%s (%s)\n"), program_name, PACKAGE_STRING);
	  exit(0);
  }

  /* Check commandline options. */
  opterr = 0;
  while ((more == 0) && ((i = getopt(argc, argv, "Vt:")) != -1))
    switch (i) {
    case 'V':
      verbose++;
      break;
    case 't':
      fstype = optarg;
      break;
    default:
      optind--;
      more = 1;
      break;		/* start of specific arguments */
    }
  if (optind == argc) {
    fprintf(stderr,
      _("Usage: mkfs [-V] [-t fstype] [fs-options] device [size]\n"));
    return -1;
  }
  
  /* If -t wasn't specified, use the default */
  if (fstype == NULL)
    fstype = DEFAULT_FSTYPE;

  /* Set PATH and program name */
  oldpath = getenv("PATH");
  if (!oldpath)
	  oldpath = "/bin";

  newpath = (char *) malloc(strlen(oldpath) + sizeof(SEARCH_PATH) + 3);
  if (!newpath) {
    fprintf(stderr, _("%s: Out of memory!\n"), "mkfs");
    exit(1);
  }
  sprintf(newpath, "%s:%s\n", SEARCH_PATH, oldpath);
  putenv(newpath);

  progname = (char *) malloc(sizeof(PROGNAME) + strlen(fstype) + 1);
  if (!progname) {
    fprintf(stderr, _("%s: Out of memory!\n"), "mkfs");
    exit(1);
  }
  sprintf(progname, PROGNAME, fstype);
  argv[--optind] = progname;

  if (verbose) {
    printf(_("mkfs (%s)\n"), PACKAGE_STRING);
    i = optind;
    while (argv[i])
      printf("%s ", argv[i++]);
    printf("\n");
    if (verbose > 1)
      return 0;
  }

  /* Execute the program */
  execvp(progname, argv+optind);
  perror(progname);
  return 1;
}