Whamcloud - gitweb
Ignore generated files.
[fs/lustre-release.git] / libsysio / tests / test_regions.c
1 /*
2  *    This Cplant(TM) source code is the property of Sandia National
3  *    Laboratories.
4  *
5  *    This Cplant(TM) source code is regionsrighted 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-2004 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 regions 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 #define _BSD_SOURCE
45
46 #if (_LARGEFILE64_SOURCE && \
47      ((defined(__STDC_VERSION__) && __STDC_VERSION__ == 199901L)))
48 #define GO64
49 #else
50 #warning Cannot prompt the 64-bit interface
51 #endif
52
53 #if defined(GO64) && defined(__GLIBC__)
54 #define  _ISOC99_SOURCE 1
55 #endif
56
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <unistd.h>
61 #include <limits.h>
62 #include <errno.h>
63 #include <sys/types.h>
64 #include <sys/stat.h>
65 #include <fcntl.h>
66 #include <sys/uio.h>
67
68 #include "xtio.h"
69 #include "test.h"
70
71 /*
72  * Copy one file to another.
73  *
74  * Usage: test_regions [-x] \
75  *              {r,w} <off> <count> <path>
76  *
77  * Destination will not be overwritten if it already exist.
78  */
79
80 #if (_LARGEFILE64_SOURCE && \
81      ((defined(__STDC_VERSION__) && __STDC_VERSION__ == 199901L) || \
82       (defined(_ISOC99_SOURCE) && _ISOC99_SOURCE)))
83 #define GO64
84 #else
85 #warning Cannot prompt the 64-bit interface
86 #endif
87
88 char    which;
89 #ifdef GO64
90 int     use64 = 0;                                      /* 64-bit interface? */
91 #endif
92
93 void    usage(void);
94
95 int
96 main(int argc, char * const argv[])
97 {
98         int     i;
99         int     err;
100         long    l;
101         off_t   off;
102 #ifdef GO64
103         long long ll;
104         off64_t off64;
105 #endif
106         char    *cp;
107         unsigned long nbytes;
108         const char *path;
109         char    *buf;
110         int     flags;
111         int     fd;
112         ssize_t cc;
113         extern int _test_sysio_startup(void);
114
115         /*
116          * Parse command-line args.
117          */
118         while ((i = getopt(argc,
119                            argv,
120 #ifdef __GLIBC__
121                            "+"
122 #endif
123 #ifdef GO64
124                            "x"
125 #endif
126                            "")) != -1)
127                 switch (i) {
128
129 #ifdef GO64
130                 case 'x':
131                         use64 = 1;
132                         break;
133 #endif
134                 default:
135                         usage();
136                 }
137
138         if (argc - optind != 4)
139                 usage();
140
141         which = *argv[optind];
142         if (strlen(argv[optind]) != 1 || !(which == 'r' || which == 'w')) {
143                 (void )fprintf(stderr, "Which op?\n");
144                 exit(1);
145         }
146         optind++;
147         off = l =
148 #ifdef GO64
149             ll = strtoll(argv[optind++], &cp, 0);
150 #else
151             strtol(argv[optind++], &cp, 0);
152 #endif
153 #ifdef GO64
154         off64 = ll;
155 #endif
156         if (*cp != '\0' ||
157 #ifdef GO64
158             ((ll == LLONG_MIN || ll == LLONG_MAX) && errno == ERANGE) ||
159             off64 != ll || (!use64 && off != ll)
160 #else
161             ((l == LONG_MIN || l == LONG_MAX) && errno == ERANGE) ||
162             off != l
163 #endif
164            ) {
165                 (void )fprintf(stderr, "Offset out of range\n");
166                 exit(1);
167         }
168         nbytes = strtoul(argv[optind++], &cp, 0);
169         if (*cp != '\0' || (nbytes == ULONG_MAX && errno == ERANGE)) {
170                 (void )fprintf(stderr, "Transfer count out of range\n");
171                 exit(1);
172         }
173         if (!(argc - optind))
174                 usage();
175         path = argv[optind++];
176
177         err = _test_sysio_startup();
178         if (err) {
179                 errno = -err;
180                 perror("sysio startup");
181                 exit(1);
182         }       
183
184         (void )umask(022);
185
186         buf = malloc(nbytes);
187         if (!buf) {
188                 perror("malloc");
189                 err = 1;
190                 goto out;
191         }
192         (void )memset(buf, 0, nbytes);
193
194         err = 0;
195         flags = which == 'r' ? O_RDONLY : (O_WRONLY|O_CREAT|O_EXCL);
196 #ifdef GO64
197         if (use64)
198                 flags |= O_LARGEFILE;
199 #endif
200         fd = open(path, flags, 0666);
201         if (fd < 0) {
202                 perror(path);
203                 err = 1;
204                 goto error;
205         }
206 #ifdef GO64
207         if (use64)
208                 off64 = lseek64(fd, off64, SEEK_SET);
209         else
210                 off64 =
211 #endif
212                   off = lseek(fd, off, SEEK_SET);
213 #ifdef GO64
214         if ((use64 && off64 < 0) || (!use64 && off < 0)) {
215                 perror(use64 ? "lseek64" : "lseek");
216                 err = 1;
217                 goto error;
218         }
219 #else
220         if (off < 0) {
221                 perror("lseek");
222                 err = 1;
223                 goto error;
224         }
225 #endif
226         if (which == 'r')
227                 cc = read(fd, buf, nbytes);
228         else
229                 cc = write(fd, buf, nbytes);
230         if (cc < 0) {
231                 perror(path);
232                 err = 1;
233                 goto error;
234         }
235 #ifdef GO64
236         if (use64) {
237                 off64 = lseek64(fd, 0, SEEK_CUR);
238         } else
239                 off64 =
240 #endif
241                   off = lseek(fd, 0, SEEK_CUR);
242         (void )printf(("%s%s@"
243 #ifdef GO64
244                        "%lld"
245 #else
246                        "%ld"
247 #endif
248                        ": %ld, off "
249 #ifdef GO64
250                        "%lld"
251 #else
252                        "%ld"
253 #endif
254                        "\n"),
255                       which == 'r' ? "read" : "write",
256 #ifdef GO64
257                       use64 ? "64" : "",
258                       ll,
259 #else
260                       "",
261                       l,
262 #endif
263                       (long )cc,
264 #ifdef GO64
265                       (long long int)off64
266 #else
267                       off
268 #endif
269                       );
270
271 error:
272         if (fd > 0 && close(fd) != 0)
273                 perror(path);
274         free(buf);
275 out:
276         _test_sysio_shutdown();
277
278         return err;
279 }
280
281 void
282 usage()
283 {
284
285         (void )fprintf(stderr,
286                        "Usage: test_regions "
287 #ifdef GO64
288                        "[-x] "
289 #endif
290                        " {r,w} <offset> <nbytes> <path>\n");
291         exit(1);
292 }