4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
27 * This file is part of Lustre, http://www.lustre.org/
32 #include <sys/types.h>
39 #define GOTO(label, rc) do { rc; goto label; } while (0)
41 int main (int argc, char **argv) {
43 unsigned long bytes, lbytes;
45 char *str, *str2, *readbuf;
48 fprintf(stderr, "usage: %s <filename> <bytes>\n", argv[0]);
52 bytes = strtoul(argv[2], NULL, 10);
54 printf("No bytes!\n");
58 printf("Need an even number of bytes!\n");
63 str = malloc(bytes+1);
65 printf("No enough memory for %lu bytes.\n", bytes);
68 str2 = malloc(lbytes+1);
70 printf("No enough memory for %lu bytes.\n", lbytes);
71 GOTO(out_str, rc = 5);
73 readbuf = malloc(bytes*2);
75 printf("No enough memory for %lu bytes.\n", bytes*2);
76 GOTO(out_str2, rc = 6);
79 for(i=0; i < bytes; i++)
80 str[i] = 'a' + (i % 26);
83 memcpy(str2, str, bytes);
84 memcpy(str2+(bytes/2), str, bytes);
88 printf("First String: %s\nSecond String: %s\n", str, str2);
90 fd = open(argv[1], O_CREAT|O_RDWR|O_TRUNC, 0700);
92 printf("Could not open file %s.\n", argv[1]);
93 GOTO(out_readbuf, rc = 7);
96 rc = write(fd, str, bytes);
98 printf("Write failed!\n");
104 if (rc < 0 || st.st_size != bytes) {
105 printf("bad file %lu size first write %lu != %lu: rc %d\n",
106 (unsigned long)st.st_ino, (unsigned long)st.st_size,
108 GOTO(out_fd, rc = 9);
111 rc = lseek(fd, bytes / 2, SEEK_SET);
112 if (rc != bytes / 2) {
113 printf("Seek failed!\n");
114 GOTO(out_fd, rc = 10);
117 rc = write(fd, str, bytes);
119 printf("Write failed!\n");
120 GOTO(out_fd, rc = 11);
124 if (rc < 0 || st.st_size != bytes + bytes / 2) {
125 printf("bad file %lu size second write %lu != %lu: rc %d\n",
126 (unsigned long)st.st_ino, (unsigned long)st.st_size,
128 GOTO(out_fd, rc = 12);
131 rc = lseek(fd, 0, SEEK_SET);
133 printf("Seek failed!\n");
134 GOTO(out_fd, rc = 13);
137 rc = read(fd, readbuf, bytes * 2);
139 printf("Read %d bytes instead of %lu.\n", rc, lbytes);
143 printf("%s\n%s\n", readbuf, str2);
145 if (rc < 0 || st.st_size != bytes + bytes / 2) {
146 printf("bad file size after read %lu != %lu: rc %d\n",
147 (unsigned long)st.st_size, bytes + bytes / 2,
149 GOTO(out_fd, rc = 14);
152 GOTO(out_fd, rc = 15);
157 printf("%s\n%s\n", readbuf, str2);
158 if (strcmp(readbuf, str2)) {
159 printf("No match!\n");
160 GOTO(out_fd, rc = 16);