Whamcloud - gitweb
LU-4961 lustre: remove liblustre.h and obd.h from userspace
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  */
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <errno.h>
40 #include <string.h>
41 #include <fcntl.h>
42 #include <unistd.h>
43 #include <getopt.h>
44
45 void usage(const char *prog, int status)
46 {
47         fprintf(status == 0 ? stdout : stderr,
48                 "Usage: %s [OPTION]... FILE\n"
49                 "  -d, --device=DEV  use device number DEV\n"
50                 "  -h, --help        dispaly help\n"
51                 "  -m, --mode=MODE   use mode MODE\n"
52                 "  -M, --major=MAJOR use device major MAJOR\n"
53                 "  -N, --minor=MINOR use device minor MINOR\n",
54                 prog);
55
56         exit(status);
57 }
58
59 int main(int argc, char ** argv)
60 {
61         struct option opts[] = {
62                 { "device", 1, NULL, 'd' },
63                 { "help",   0, NULL, 'h' },
64                 { "mode",   1, NULL, 'm' },
65                 { "major",  1, NULL, 'M' },
66                 { "minor",  1, NULL, 'N' },
67                 { NULL },
68         };
69         const char *path;
70         mode_t mode = S_IFREG | 0644;
71         dev_t dev = 0;
72         int rc;
73
74         int c;
75         while ((c = getopt_long(argc, argv, "d:hm:M:N:", opts, NULL)) != -1) {
76                 switch (c) {
77                 case 'd':
78                         dev = strtoul(optarg, NULL, 0);
79                         break;
80                 case 'h':
81                         usage(argv[0], 0);
82                 case 'm':
83                         mode = strtoul(optarg, NULL, 0);
84                         break;
85                 case 'M':
86                         dev = makedev(strtoul(optarg, NULL, 0), minor(dev));
87                         break;
88                 case 'N':
89                         dev = makedev(major(dev), strtoul(optarg, NULL, 0));
90                         break;
91                 case '?':
92                         usage(argv[0], 1);
93                 }
94         }
95
96         if (argc - optind != 1)
97                 usage(argv[0], 1);
98
99         path = argv[optind];
100
101         if ((mode & S_IFMT) == S_IFDIR)
102                 rc = mkdir(path, mode & ~S_IFMT);
103         else if ((mode & S_IFMT) == S_IFLNK)
104                 rc = symlink("oldpath", path);
105         else
106                 rc = mknod(path, mode, dev);
107
108         if (rc)
109                 fprintf(stderr, "%s: cannot create `%s' with mode %#o: %s\n",
110                         argv[0], path, mode, strerror(errno));
111
112         return rc;
113 }