1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2001 Silicon Graphics, Inc.
19 /*---------------------------------------------------------------------------
21 Test program used to test DMAPI by issuing read, write, and trunc calls to a
22 file. The command line is:
24 rwt [-r|-w|-t] [-o offset] [-l length] pathname
28 indicates that a read should be done (the default if none specified)
30 indiates that a write should be done
32 indicates that a truncate should be done, in which case the -l
35 offset at which to begin the read, write or truncate (default is 0).
37 the length in bytes to read or write (default is 1).
39 the file to be used by the test.
41 ----------------------------------------------------------------------------*/
44 extern char *sys_errlist[];
56 fprintf(stderr, "usage:\t%s [-r|-w|-t] [-o offset] [-l length] "
57 "pathname\n", Progname);
67 char *pathname = NULL;
80 Progname = strrchr(argv[0], '/');
87 /* Crack and validate the command line options. */
89 while ((opt = getopt(argc, argv, "rwto:l:")) != EOF) {
101 offset = atol(optarg);
104 length = atol(optarg);
110 if (optind + 1 != argc)
112 if (rflag + wflag + tflag > 1)
114 pathname = argv[optind];
116 if ((fd = open(pathname, O_RDWR)) < 0) {
117 fprintf(stderr, "open of %s failed, %s\n", pathname,
122 if ((bufp = malloc(length)) == NULL) {
123 fprintf(stderr, "malloc of %zd bytes failed\n", length);
127 memset(bufp, ch, length);
131 if ((seek_off = lseek(fd, offset, SEEK_SET)) < 0) {
132 fprintf(stderr, "seek failed, %s\n", strerror(errno));
135 if (seek_off != offset) {
137 "seeked to offset %lld, actually "
139 (long long) offset, (long long) seek_off);
145 if ((rc = write(fd, bufp, length)) < 0) {
146 fprintf(stderr, "write failed, %s\n", strerror(errno));
150 fprintf(stderr, "expected to write %zd bytes, actually "
151 "wrote %zd bytes\n", length, rc);
155 if (ftruncate(fd, offset) != 0) {
156 fprintf(stderr, "truncate failed, %s\n",
161 if ((rc = read(fd, bufp, length)) < 0) {
162 fprintf(stderr, "read failed, %s\n", strerror(errno));
166 fprintf(stderr, "expected to read %zd bytes, actually "
167 "read %zd bytes\n", length, rc);