Whamcloud - gitweb
LU-6142 tests: Fix style issues for mkdirmany.c
[fs/lustre-release.git] / lustre / tests / mcreate.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  */
26 /*
27  * This file is part of Lustre, http://www.lustre.org/
28  * Lustre is a trademark of Sun Microsystems, Inc.
29  */
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <sys/sysmacros.h>
34 #include <sys/stat.h>
35 #include <errno.h>
36 #include <string.h>
37 #include <fcntl.h>
38 #include <unistd.h>
39 #include <getopt.h>
40
41 void usage(const char *prog, int status)
42 {
43         fprintf(status == 0 ? stdout : stderr,
44                 "Usage: %s [OPTION]... FILE\n"
45                 "  -d, --device=DEV  use device number DEV\n"
46                 "  -h, --help        dispaly help\n"
47                 "  -m, --mode=MODE   use mode MODE\n"
48                 "  -M, --major=MAJOR use device major MAJOR\n"
49                 "  -N, --minor=MINOR use device minor MINOR\n",
50                 prog);
51
52         exit(status);
53 }
54
55 int main(int argc, char **argv)
56 {
57         struct option opts[] = {
58                 { .name = "device", .has_arg = required_argument, .val = 'd' },
59                 { .name = "help", .has_arg = no_argument, .val = 'h' },
60                 { .name = "mode", .has_arg = required_argument, .val = 'm' },
61                 { .name = "major", .has_arg = required_argument, .val = 'M' },
62                 { .name = "minor", .has_arg = required_argument, .val = 'N' },
63                 { .name = NULL }
64         };
65         const char *path;
66         mode_t mode = S_IFREG | 0644;
67         dev_t dev = 0;
68         int rc;
69         int c;
70
71         while ((c = getopt_long(argc, argv, "d:hm:M:N:", opts, NULL)) != -1) {
72                 switch (c) {
73                 case 'd':
74                         dev = strtoul(optarg, NULL, 0);
75                         break;
76                 case 'h':
77                         usage(argv[0], 0);
78                 case 'm':
79                         mode = strtoul(optarg, NULL, 0);
80                         break;
81                 case 'M':
82                         dev = makedev(strtoul(optarg, NULL, 0), minor(dev));
83                         break;
84                 case 'N':
85                         dev = makedev(major(dev), strtoul(optarg, NULL, 0));
86                         break;
87                 case '?':
88                         usage(argv[0], 1);
89                 }
90         }
91
92         if (argc - optind != 1)
93                 usage(argv[0], 1);
94
95         path = argv[optind];
96
97         if ((mode & S_IFMT) == S_IFDIR)
98                 rc = mkdir(path, mode & ~S_IFMT);
99         else if ((mode & S_IFMT) == S_IFLNK)
100                 rc = symlink("oldpath", path);
101         else
102                 rc = mknod(path, mode, dev);
103
104         if (rc)
105                 fprintf(stderr, "%s: cannot create `%s' with mode %#o: %s\n",
106                         argv[0], path, mode, strerror(errno));
107
108         return rc;
109 }