Whamcloud - gitweb
Branch b1_4
[fs/lustre-release.git] / libsysio / tests / test_mknod.c
1 /*
2  *    This Cplant(TM) source code is the property of Sandia National
3  *    Laboratories.
4  *
5  *    This Cplant(TM) source code is copyrighted by Sandia National
6  *    Laboratories.
7  *
8  *    The redistribution of this Cplant(TM) source code is subject to the
9  *    terms of the GNU Lesser General Public License
10  *    (see cit/LGPL or http://www.gnu.org/licenses/lgpl.html)
11  *
12  *    Cplant(TM) Copyright 1998-2006 Sandia Corporation. 
13  *    Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
14  *    license for use of this work by or on behalf of the US Government.
15  *    Export of this program may require a license from the United States
16  *    Government.
17  */
18
19 /*
20  * This library is free software; you can redistribute it and/or
21  * modify it under the terms of the GNU Lesser General Public
22  * License as published by the Free Software Foundation; either
23  * version 2.1 of the License, or (at your option) any later version.
24  * 
25  * This library is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28  * Lesser General Public License for more details.
29  * 
30  * You should have received a copy of the GNU Lesser General Public
31  * License along with this library; if not, write to the Free Software
32  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33  *
34  * Questions or comments about this library should be sent to:
35  *
36  * Lee Ward
37  * Sandia National Laboratories, New Mexico
38  * P.O. Box 5800
39  * Albuquerque, NM 87185-1110
40  *
41  * lee@sandia.gov
42  */
43
44 /* 
45  * Can't provoke a definition of the S_IFMT macros without a little extra work.
46  */
47 #define _BSD_SOURCE
48
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
53 #include <errno.h>
54 #include <sys/types.h>
55 #include <sys/stat.h>
56 #include <fcntl.h>
57 #include <getopt.h>
58
59 #if defined(SYSIO_LABEL_NAMES)
60 #include "sysio.h"
61 #endif
62 #include "test.h"
63
64 /*
65  * Create a node.
66  *
67  * Usage: mknod path {f|b|c} [dev]
68  *
69  * The dev argument should not be present for regular file and FIFO object
70  * creation.
71  */
72
73 static int do_mknod(const char *path, mode_t mode, dev_t dev);
74 static void usage(void);
75
76 int
77 main(int argc, char *const argv[])
78 {
79         int     i;
80         int     err;
81         mode_t  mode;
82         dev_t   dev;
83         extern int _test_sysio_startup(void);
84
85         /*
86          * Parse command line arguments.
87          */
88         while ((i = getopt(argc, argv, "")) != -1)
89                 switch (i) {
90
91                 default:
92                         usage();
93                 }
94
95         /*
96          * Init sysio lib.
97          */
98         err = _test_sysio_startup();
99         if (err) {
100                 errno = -err;
101                 perror("sysio startup");
102                 exit(1);
103         }
104
105         if (argc - optind < 2)
106                 usage();
107         if (strlen(argv[optind + 1]) != 1)
108                 usage();
109         mode = 0666;
110         switch (*argv[optind + 1]) {
111         
112         case 'f':
113                 mode |= S_IFREG;
114                 break;
115         case 'b':
116                 mode |= S_IFBLK;
117                 break;
118         case 'c':
119                 mode |= S_IFCHR;
120                 break;
121         case 'p':
122                 mode |= S_IFIFO;
123                 break;
124         default:
125                 usage();
126         }
127         dev = 0;
128         if (!(S_ISREG(mode) || S_ISFIFO(mode)))
129                 dev = atoi(argv[optind + 2]);
130         else if (argc - optind != 2) {
131                 (void )fprintf(stderr, "Too many arguments\n");
132                 usage();
133         }
134         (void )do_mknod(argv[optind + 0], mode, dev);
135
136         /*
137          * Clean up.
138          */
139         _test_sysio_shutdown();
140
141         return 0;
142 }
143
144 static int
145 do_mknod(const char *path, mode_t mode, dev_t dev)
146 {
147
148         if (SYSIO_INTERFACE_NAME(mknod)(path, mode, dev) != 0) {
149                 perror(path);
150                 return -1;
151         }
152
153         return 0;
154 }
155
156 static void
157 usage()
158 {
159
160         (void )fprintf(stderr, "Usage: mknod path {f|b|c|p} dev\n");
161         exit(1);
162 }