cmd/xfsprogs/libdm/dmapi_tests/README 1.1 Renamed to cmd/xfstests/dmapi/README
[xfstests-dev.git] / dmapi / src / suite1 / cmd / rwt.c
1 /*
2  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
3  * 
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  * 
8  * This program is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * 
12  * Further, this software is distributed without any warranty that it is
13  * free of the rightful claim of any third person regarding infringement
14  * or the like.  Any license provided herein, whether implied or
15  * otherwise, applies only to this software file.  Patent licenses, if
16  * any, provided herein do not apply to combinations of this program with
17  * other software, or any other product whatsoever.
18  * 
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write the Free Software Foundation, Inc., 59
21  * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22  * 
23  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24  * Mountain View, CA  94043, or:
25  * 
26  * http://www.sgi.com 
27  * 
28  * For further information regarding this notice, see: 
29  * 
30  * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31  */
32
33 #include <sys/types.h>
34 #include <sys/stat.h>
35
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <strings.h>
41 #include <unistd.h>
42
43 #ifdef linux
44 #include <string.h>
45 #endif
46
47 /*---------------------------------------------------------------------------
48
49 Test program used to test DMAPI by issuing read, write, and trunc calls to a
50 file.  The command line is:
51
52         rwt [-r|-w|-t] [-o offset] [-l length] pathname
53
54 where:
55 -r
56         indicates that a read should be done (the default if none specified)
57 -w
58         indiates that a write should be done
59 -t
60         indicates that a truncate should be done, in which case the -l
61         parameter is ignored.
62 -o offset
63         offset at which to begin the read, write or truncate (default is 0).
64 -l length
65         the length in bytes to read or write (default is 1).
66 pathname
67         the file to be used by the test.
68
69 ----------------------------------------------------------------------------*/
70
71 #ifndef linux
72 extern  char    *sys_errlist[];
73 #endif
74 extern  int     optind;
75 extern  char    *optarg;
76
77
78 char    *Progname;
79
80
81 static void
82 usage(void)
83 {
84         int     i;
85
86         fprintf(stderr, "usage:\t%s [-r|-w|-t] [-o offset] [-l length] "
87                 "pathname\n", Progname);
88         exit(1);
89 }
90
91
92 int
93 main(
94         int     argc, 
95         char    **argv)
96 {
97         char            *pathname = NULL;
98         off_t           offset = 0;
99         size_t          length = 1;
100         u_char          ch = 'X';
101         void            *bufp = NULL;
102         off_t           seek_off;
103         int             rflag = 0;
104         int             wflag = 0;
105         int             tflag = 0;
106         int             fd;
107         ssize_t         rc;
108         int             opt;
109         int             i;
110
111         if (Progname = strrchr(argv[0], '/')) {
112                 Progname++;
113         } else {
114                 Progname = argv[0];
115         }
116
117         /* Crack and validate the command line options. */
118
119         while ((opt = getopt(argc, argv, "rwto:l:")) != EOF) {
120                 switch (opt) {
121                 case 'r':
122                         rflag++;
123                         break;
124                 case 'w':
125                         wflag++;
126                         break;
127                 case 't':
128                         tflag++;
129                         break;
130                 case 'o':
131                         offset = atol(optarg);
132                         break;
133                 case 'l':
134                         length = atol(optarg);
135                         break;
136                 case '?':
137                         usage();
138                 }
139         }
140         if (optind + 1 != argc)
141                 usage();
142         if (rflag + wflag + tflag > 1)
143                 usage();
144         pathname = argv[optind];
145
146         if ((fd = open(pathname, O_RDWR)) < 0) {
147                 fprintf(stderr, "open of %s failed, %s\n", pathname,
148                         strerror(errno));
149                 exit(1);
150         }
151         if (length > 0) {
152                 if ((bufp = malloc(length)) == NULL) {
153                         fprintf(stderr, "malloc of %d bytes failed\n", length);
154                         exit(1);
155                 }
156                 if (wflag)
157                         memset(bufp, ch, length);
158         }
159
160         if (!tflag) {
161                 if ((seek_off = lseek(fd, offset, SEEK_SET)) < 0) {
162                         fprintf(stderr, "seek failed, %s\n", strerror(errno));
163                         exit(1);
164                 }
165                 if (seek_off != offset) {
166                         fprintf(stderr, "seeked to offset %d, actually "
167                                 "arrived at %d\n", offset, seek_off);
168                         exit(1);
169                 }
170         }
171
172         if (wflag) {
173                 if ((rc = write(fd, bufp, length)) < 0) {
174                         fprintf(stderr, "write failed, %s\n", strerror(errno));
175                         exit(1);
176                 }
177                 if (rc != length) {
178                         fprintf(stderr, "expected to write %d bytes, actually "
179                                 "wrote %d bytes\n", length, rc);
180                         exit(1);
181                 }
182         } else if (tflag) {
183                 if (ftruncate(fd, offset) != 0) {
184                         fprintf(stderr, "truncate failed, %s\n",
185                                 strerror(errno));
186                         exit(1);
187                 }
188         } else {
189                 if ((rc = read(fd, bufp, length)) < 0) {
190                         fprintf(stderr, "read failed, %s\n", strerror(errno));
191                         exit(1);
192                 }
193                 if (rc != length) {
194                         fprintf(stderr, "expected to read %d bytes, actually "
195                                 "read %d bytes\n", length, rc);
196                         exit(1);
197                 }
198         }
199         exit(0);
200 }