Whamcloud - gitweb
941dea467c01969213603bae0f95b92a788c1fe2
[fs/lustre-release.git] / libsysio / tests / test_stddir.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-2003 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 #include <stdlib.h>
45 #include <stdio.h>
46 #include <errno.h>
47 #include <string.h>
48 #include <getopt.h>
49 #include <dirent.h>
50 #include <sys/types.h>
51
52 #include "xtio.h"
53 #include "test.h"
54
55 /*
56  * Test {open, read, close}dir functions
57  * 
58  * Usage: test_stddir [path, ...]
59  */
60 static int testit(const char *);
61 static void usage(void);
62
63 int 
64 main (int argc, char** argv)
65 {
66         int     err;
67         int     i;
68         int     n;
69         const char *path;
70
71         /*
72          * Parse command line arguments.
73          */
74         while ((i = getopt(argc, argv, "")) != -1)
75                 switch (i) {
76
77                 default:
78                         usage();
79                 }
80
81         /*
82          * Init sysio lib.
83          */
84         err = _test_sysio_startup();
85         if (err) {
86                 errno = -err;
87                 perror("sysio startup");
88                 exit(1);
89         }
90
91         /*
92          * If no command-line arguments, read from stdin until EOF.
93          */
94         n = argc - optind;
95         if (!n) {
96                 int     doflush;
97                 static char buf[4096];
98                 size_t  len;
99                 char    *cp;
100                 char    c;
101
102                 doflush = 0;
103                 while (fgets(buf, sizeof(buf), stdin) != NULL) {
104                         len = strlen(buf);
105                         cp = buf + len - 1;
106                         c = *cp;
107                         *cp = '\0';
108                         if (!doflush)
109                                 err = testit(buf);
110                         if (err)
111                                 break;
112                         doflush = c == '\n' ? 0 : 1;
113                 }
114         }
115
116         /*
117          * Try path(s) listed on command-line.
118          */
119         while (optind < argc) {
120                 path = argv[optind++];
121                 err = testit(path);
122                 if (err)
123                         break;
124         }
125
126         /*
127          * Clean up.
128          */
129         _test_sysio_shutdown();
130
131         return err;
132 }
133
134 int
135 testit(const char *path)
136 {
137         DIR     *d;
138         struct dirent *de;
139
140         printf("testing directory functions on %s\n", path);
141
142         if ((d = opendir(path)) == NULL) {
143                 perror(path);   
144                 return errno;
145         }
146
147         while ((de = readdir(d)) != NULL)
148                 printf("\t %s: ino %lu off %lu type %u\n",
149                         de->d_name, (unsigned long )de->d_ino, 
150                         (unsigned long )de->d_off, (int )de->d_type);
151
152         if (closedir(d)) {
153                 perror("closedir");
154                 return errno;
155         }
156
157         return 0;
158 }
159
160 static void
161 usage()
162 {
163
164         (void )fprintf(stderr,
165                        "Usage: test_stddir [<path> ...]\n");
166
167         exit(1);
168 }