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