_scratch_mkfs_geom(): Filter out 'k' suffix from fs block size
[xfstests-dev.git] / dmapi / src / suite1 / cmd / rwt.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2001 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6
7 #include <sys/types.h>
8 #include <sys/stat.h>
9
10 #include <errno.h>
11 #include <fcntl.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <strings.h>
15 #include <unistd.h>
16
17 #include <string.h>
18
19 /*---------------------------------------------------------------------------
20
21 Test program used to test DMAPI by issuing read, write, and trunc calls to a
22 file.  The command line is:
23
24         rwt [-r|-w|-t] [-o offset] [-l length] pathname
25
26 where:
27 -r
28         indicates that a read should be done (the default if none specified)
29 -w
30         indiates that a write should be done
31 -t
32         indicates that a truncate should be done, in which case the -l
33         parameter is ignored.
34 -o offset
35         offset at which to begin the read, write or truncate (default is 0).
36 -l length
37         the length in bytes to read or write (default is 1).
38 pathname
39         the file to be used by the test.
40
41 ----------------------------------------------------------------------------*/
42
43 #ifndef linux
44 extern  char    *sys_errlist[];
45 #endif
46 extern  int     optind;
47 extern  char    *optarg;
48
49
50 char    *Progname;
51
52
53 static void
54 usage(void)
55 {
56         fprintf(stderr, "usage:\t%s [-r|-w|-t] [-o offset] [-l length] "
57                 "pathname\n", Progname);
58         exit(1);
59 }
60
61
62 int
63 main(
64         int     argc, 
65         char    **argv)
66 {
67         char            *pathname = NULL;
68         off_t           offset = 0;
69         size_t          length = 1;
70         u_char          ch = 'X';
71         void            *bufp = NULL;
72         off_t           seek_off;
73         int             rflag = 0;
74         int             wflag = 0;
75         int             tflag = 0;
76         int             fd;
77         ssize_t         rc;
78         int             opt;
79
80         Progname = strrchr(argv[0], '/');
81         if (Progname) {
82                 Progname++;
83         } else {
84                 Progname = argv[0];
85         }
86
87         /* Crack and validate the command line options. */
88
89         while ((opt = getopt(argc, argv, "rwto:l:")) != EOF) {
90                 switch (opt) {
91                 case 'r':
92                         rflag++;
93                         break;
94                 case 'w':
95                         wflag++;
96                         break;
97                 case 't':
98                         tflag++;
99                         break;
100                 case 'o':
101                         offset = atol(optarg);
102                         break;
103                 case 'l':
104                         length = atol(optarg);
105                         break;
106                 case '?':
107                         usage();
108                 }
109         }
110         if (optind + 1 != argc)
111                 usage();
112         if (rflag + wflag + tflag > 1)
113                 usage();
114         pathname = argv[optind];
115
116         if ((fd = open(pathname, O_RDWR)) < 0) {
117                 fprintf(stderr, "open of %s failed, %s\n", pathname,
118                         strerror(errno));
119                 exit(1);
120         }
121         if (length > 0) {
122                 if ((bufp = malloc(length)) == NULL) {
123                         fprintf(stderr, "malloc of %zd bytes failed\n", length);
124                         exit(1);
125                 }
126                 if (wflag)
127                         memset(bufp, ch, length);
128         }
129
130         if (!tflag) {
131                 if ((seek_off = lseek(fd, offset, SEEK_SET)) < 0) {
132                         fprintf(stderr, "seek failed, %s\n", strerror(errno));
133                         exit(1);
134                 }
135                 if (seek_off != offset) {
136                         fprintf(stderr,
137                                 "seeked to offset %lld, actually "
138                                 "arrived at %lld\n",
139                                 (long long) offset, (long long) seek_off);
140                         exit(1);
141                 }
142         }
143
144         if (wflag) {
145                 if ((rc = write(fd, bufp, length)) < 0) {
146                         fprintf(stderr, "write failed, %s\n", strerror(errno));
147                         exit(1);
148                 }
149                 if (rc != length) {
150                         fprintf(stderr, "expected to write %zd bytes, actually "
151                                 "wrote %zd bytes\n", length, rc);
152                         exit(1);
153                 }
154         } else if (tflag) {
155                 if (ftruncate(fd, offset) != 0) {
156                         fprintf(stderr, "truncate failed, %s\n",
157                                 strerror(errno));
158                         exit(1);
159                 }
160         } else {
161                 if ((rc = read(fd, bufp, length)) < 0) {
162                         fprintf(stderr, "read failed, %s\n", strerror(errno));
163                         exit(1);
164                 }
165                 if (rc != length) {
166                         fprintf(stderr, "expected to read %zd bytes, actually "
167                                 "read %zd bytes\n", length, rc);
168                         exit(1);
169                 }
170         }
171         exit(0);
172 }