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