Whamcloud - gitweb
LU-6210 tests: Change positional struct initializers to C99
[fs/lustre-release.git] / lustre / tests / mcreate.c
index 9056fa1..f3fa364 100644 (file)
@@ -1,6 +1,4 @@
-/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
- * vim:expandtab:shiftwidth=8:tabstop=8:
- *
+/*
  * GPL HEADER START
  *
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * You should have received a copy of the GNU General Public License
  * version 2 along with this program; If not, see
- * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
- *
- * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
- * CA 95054 USA or visit www.sun.com if you need additional information or
- * have any questions.
+ * http://www.gnu.org/licenses/gpl-2.0.html
  *
  * GPL HEADER END
  */
  */
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <errno.h>
 #include <string.h>
 #include <fcntl.h>
 #include <unistd.h>
+#include <getopt.h>
+
+void usage(const char *prog, int status)
+{
+        fprintf(status == 0 ? stdout : stderr,
+                "Usage: %s [OPTION]... FILE\n"
+                "  -d, --device=DEV  use device number DEV\n"
+                "  -h, --help        dispaly help\n"
+                "  -m, --mode=MODE   use mode MODE\n"
+                "  -M, --major=MAJOR use device major MAJOR\n"
+                "  -N, --minor=MINOR use device minor MINOR\n",
+                prog);
+
+        exit(status);
+}
 
 int main(int argc, char ** argv)
 {
+       struct option opts[] = {
+               { .name = "device", .has_arg = required_argument, .val = 'd' },
+               { .name = "help", .has_arg = no_argument, .val = 'h' },
+               { .name = "mode", .has_arg = required_argument, .val = 'm' },
+               { .name = "major", .has_arg = required_argument, .val = 'M' },
+               { .name = "minor", .has_arg = required_argument, .val = 'N' },
+               { .name = NULL }
+       };
+        const char *path;
+        mode_t mode = S_IFREG | 0644;
+        dev_t dev = 0;
         int rc;
 
-        if (argc < 2) {
-                printf("Usage %s filename\n", argv[0]);
-                return 1;
+        int c;
+        while ((c = getopt_long(argc, argv, "d:hm:M:N:", opts, NULL)) != -1) {
+                switch (c) {
+                case 'd':
+                        dev = strtoul(optarg, NULL, 0);
+                        break;
+                case 'h':
+                        usage(argv[0], 0);
+                case 'm':
+                        mode = strtoul(optarg, NULL, 0);
+                        break;
+                case 'M':
+                        dev = makedev(strtoul(optarg, NULL, 0), minor(dev));
+                        break;
+                case 'N':
+                        dev = makedev(major(dev), strtoul(optarg, NULL, 0));
+                        break;
+                case '?':
+                        usage(argv[0], 1);
+                }
         }
 
-        rc = mknod(argv[1], S_IFREG | 0644, 0);
-        if (rc) {
-                printf("mknod(%s) error: %s\n", argv[1], strerror(errno));
-        }
+        if (argc - optind != 1)
+                usage(argv[0], 1);
+
+        path = argv[optind];
+
+        if ((mode & S_IFMT) == S_IFDIR)
+                rc = mkdir(path, mode & ~S_IFMT);
+        else if ((mode & S_IFMT) == S_IFLNK)
+                rc = symlink("oldpath", path);
+        else
+                rc = mknod(path, mode, dev);
+
+        if (rc)
+                fprintf(stderr, "%s: cannot create `%s' with mode %#o: %s\n",
+                        argv[0], path, mode, strerror(errno));
+
         return rc;
 }