~ubuntu-branches/ubuntu/trusty/util-linux/trusty-proposed

« back to all changes in this revision

Viewing changes to disk-utils/mkfs.c

  • Committer: Package Import Robot
  • Author(s): LaMont Jones
  • Date: 2011-11-03 15:38:23 UTC
  • mto: (4.5.5 sid) (1.6.4)
  • mto: This revision was merged to the branch mainline in revision 85.
  • Revision ID: package-import@ubuntu.com-20111103153823-10sx16jprzxlhkqf
ImportĀ upstreamĀ versionĀ 2.20.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 * mkfs         A simple generic frontend for the for the mkfs program
3
3
 *              under Linux.  See the manual page for details.
4
4
 *
5
 
 * Usage:       mkfs [-V] [-t fstype] [fs-options] device [size]
6
 
 *
7
5
 * Authors:     David Engel, <david@ods.com>
8
6
 *              Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
9
7
 *              Ron Sommeling, <sommel@sci.kun.nl>
15
13
 *      
16
14
 */
17
15
 
18
 
 
 
16
#include <getopt.h>
 
17
#include <limits.h>
19
18
#include <stdio.h>
 
19
#include <stdlib.h>
 
20
#include <string.h>
20
21
#include <unistd.h>
21
 
#include <string.h>
22
 
#include <nls.h>
23
22
 
 
23
#include "c.h"
 
24
#include "nls.h"
24
25
#include "xalloc.h"
25
26
 
26
27
#ifndef DEFAULT_FSTYPE
27
 
# define DEFAULT_FSTYPE         "ext2"
 
28
#define DEFAULT_FSTYPE  "ext2"
28
29
#endif
29
30
 
30
31
#define SEARCH_PATH     "PATH=" FS_SEARCH_PATH
31
32
#define PROGNAME        "mkfs.%s"
32
33
 
33
34
 
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(EXIT_SUCCESS);
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
 
          errx(EXIT_FAILURE, _("Usage: mkfs [-V] [-t fstype] [fs-options] device [size]"));
73
 
  
74
 
  /* If -t wasn't specified, use the default */
75
 
  if (fstype == NULL)
76
 
    fstype = DEFAULT_FSTYPE;
77
 
 
78
 
  /* Set PATH and program name */
79
 
  oldpath = getenv("PATH");
80
 
  if (!oldpath)
81
 
          oldpath = "/bin";
82
 
 
83
 
  newpath = xmalloc(strlen(oldpath) + sizeof(SEARCH_PATH) + 3);
84
 
  sprintf(newpath, "%s:%s\n", SEARCH_PATH, oldpath);
85
 
  putenv(newpath);
86
 
 
87
 
  progname = xmalloc(sizeof(PROGNAME) + strlen(fstype) + 1);
88
 
  sprintf(progname, PROGNAME, fstype);
89
 
  argv[--optind] = progname;
90
 
 
91
 
  if (verbose) {
92
 
    printf(_("mkfs (%s)\n"), PACKAGE_STRING);
93
 
    i = optind;
94
 
    while (argv[i])
95
 
      printf("%s ", argv[i++]);
96
 
    printf("\n");
97
 
    if (verbose > 1)
98
 
      return 0;
99
 
  }
100
 
 
101
 
  /* Execute the program */
102
 
  execvp(progname, argv+optind);
103
 
  perror(progname);
104
 
  return 1;
 
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;
105
142
}