Fix the get_dirattrs dmapi test tool
[xfstests-dev.git] / dmapi / src / suite1 / cmd / rwt.c
1 /*
2  * Copyright (c) 2000-2001 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 #include <string.h>
44
45 /*---------------------------------------------------------------------------
46
47 Test program used to test DMAPI by issuing read, write, and trunc calls to a
48 file.  The command line is:
49
50         rwt [-r|-w|-t] [-o offset] [-l length] pathname
51
52 where:
53 -r
54         indicates that a read should be done (the default if none specified)
55 -w
56         indiates that a write should be done
57 -t
58         indicates that a truncate should be done, in which case the -l
59         parameter is ignored.
60 -o offset
61         offset at which to begin the read, write or truncate (default is 0).
62 -l length
63         the length in bytes to read or write (default is 1).
64 pathname
65         the file to be used by the test.
66
67 ----------------------------------------------------------------------------*/
68
69 #ifndef linux
70 extern  char    *sys_errlist[];
71 #endif
72 extern  int     optind;
73 extern  char    *optarg;
74
75
76 char    *Progname;
77
78
79 static void
80 usage(void)
81 {
82         fprintf(stderr, "usage:\t%s [-r|-w|-t] [-o offset] [-l length] "
83                 "pathname\n", Progname);
84         exit(1);
85 }
86
87
88 int
89 main(
90         int     argc, 
91         char    **argv)
92 {
93         char            *pathname = NULL;
94         off_t           offset = 0;
95         size_t          length = 1;
96         u_char          ch = 'X';
97         void            *bufp = NULL;
98         off_t           seek_off;
99         int             rflag = 0;
100         int             wflag = 0;
101         int             tflag = 0;
102         int             fd;
103         ssize_t         rc;
104         int             opt;
105
106         if (Progname = strrchr(argv[0], '/')) {
107                 Progname++;
108         } else {
109                 Progname = argv[0];
110         }
111
112         /* Crack and validate the command line options. */
113
114         while ((opt = getopt(argc, argv, "rwto:l:")) != EOF) {
115                 switch (opt) {
116                 case 'r':
117                         rflag++;
118                         break;
119                 case 'w':
120                         wflag++;
121                         break;
122                 case 't':
123                         tflag++;
124                         break;
125                 case 'o':
126                         offset = atol(optarg);
127                         break;
128                 case 'l':
129                         length = atol(optarg);
130                         break;
131                 case '?':
132                         usage();
133                 }
134         }
135         if (optind + 1 != argc)
136                 usage();
137         if (rflag + wflag + tflag > 1)
138                 usage();
139         pathname = argv[optind];
140
141         if ((fd = open(pathname, O_RDWR)) < 0) {
142                 fprintf(stderr, "open of %s failed, %s\n", pathname,
143                         strerror(errno));
144                 exit(1);
145         }
146         if (length > 0) {
147                 if ((bufp = malloc(length)) == NULL) {
148                         fprintf(stderr, "malloc of %d bytes failed\n", length);
149                         exit(1);
150                 }
151                 if (wflag)
152                         memset(bufp, ch, length);
153         }
154
155         if (!tflag) {
156                 if ((seek_off = lseek(fd, offset, SEEK_SET)) < 0) {
157                         fprintf(stderr, "seek failed, %s\n", strerror(errno));
158                         exit(1);
159                 }
160                 if (seek_off != offset) {
161                         fprintf(stderr,
162                                 "seeked to offset %lld, actually "
163                                 "arrived at %lld\n",
164                                 (int64_t)offset, (int64_t)seek_off);
165                         exit(1);
166                 }
167         }
168
169         if (wflag) {
170                 if ((rc = write(fd, bufp, length)) < 0) {
171                         fprintf(stderr, "write failed, %s\n", strerror(errno));
172                         exit(1);
173                 }
174                 if (rc != length) {
175                         fprintf(stderr, "expected to write %d bytes, actually "
176                                 "wrote %d bytes\n", length, rc);
177                         exit(1);
178                 }
179         } else if (tflag) {
180                 if (ftruncate(fd, offset) != 0) {
181                         fprintf(stderr, "truncate failed, %s\n",
182                                 strerror(errno));
183                         exit(1);
184                 }
185         } else {
186                 if ((rc = read(fd, bufp, length)) < 0) {
187                         fprintf(stderr, "read failed, %s\n", strerror(errno));
188                         exit(1);
189                 }
190                 if (rc != length) {
191                         fprintf(stderr, "expected to read %d bytes, actually "
192                                 "read %d bytes\n", length, rc);
193                         exit(1);
194                 }
195         }
196         exit(0);
197 }