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/
28 * Lustre is a trademark of Sun Microsystems, Inc.
33 #include <sys/types.h>
40 #define GOTO(label, rc) do { rc; goto label; } while (0)
42 int main (int argc, char **argv) {
44 unsigned long bytes, lbytes;
46 char *str, *str2, *readbuf;
49 fprintf(stderr, "usage: %s <filename> <bytes>\n", argv[0]);
53 bytes = strtoul(argv[2], NULL, 10);
55 printf("No bytes!\n");
59 printf("Need an even number of bytes!\n");
64 str = malloc(bytes+1);
66 printf("No enough memory for %lu bytes.\n", bytes);
69 str2 = malloc(lbytes+1);
71 printf("No enough memory for %lu bytes.\n", lbytes);
72 GOTO(out_str, rc = 5);
74 readbuf = malloc(bytes*2);
76 printf("No enough memory for %lu bytes.\n", bytes*2);
77 GOTO(out_str2, rc = 6);
80 for(i=0; i < bytes; i++)
81 str[i] = 'a' + (i % 26);
84 memcpy(str2, str, bytes);
85 memcpy(str2+(bytes/2), str, bytes);
89 printf("First String: %s\nSecond String: %s\n", str, str2);
91 fd = open(argv[1], O_CREAT|O_RDWR|O_TRUNC, 0700);
93 printf("Could not open file %s.\n", argv[1]);
94 GOTO(out_readbuf, rc = 7);
97 rc = write(fd, str, bytes);
99 printf("Write failed!\n");
100 GOTO(out_fd, rc = 8);
105 if (rc < 0 || st.st_size != bytes) {
106 printf("bad file %lu size first write %lu != %lu: rc %d\n",
107 (unsigned long)st.st_ino, (unsigned long)st.st_size,
109 GOTO(out_fd, rc = 9);
112 rc = lseek(fd, bytes / 2, SEEK_SET);
113 if (rc != bytes / 2) {
114 printf("Seek failed!\n");
115 GOTO(out_fd, rc = 10);
118 rc = write(fd, str, bytes);
120 printf("Write failed!\n");
121 GOTO(out_fd, rc = 11);
125 if (rc < 0 || st.st_size != bytes + bytes / 2) {
126 printf("bad file %lu size second write %lu != %lu: rc %d\n",
127 (unsigned long)st.st_ino, (unsigned long)st.st_size,
129 GOTO(out_fd, rc = 12);
132 rc = lseek(fd, 0, SEEK_SET);
134 printf("Seek failed!\n");
135 GOTO(out_fd, rc = 13);
138 rc = read(fd, readbuf, bytes * 2);
140 printf("Read %d bytes instead of %lu.\n", rc, lbytes);
144 printf("%s\n%s\n", readbuf, str2);
146 if (rc < 0 || st.st_size != bytes + bytes / 2) {
147 printf("bad file size after read %lu != %lu: rc %d\n",
148 (unsigned long)st.st_size, bytes + bytes / 2,
150 GOTO(out_fd, rc = 14);
153 GOTO(out_fd, rc = 15);
158 printf("%s\n%s\n", readbuf, str2);
159 if (strcmp(readbuf, str2)) {
160 printf("No match!\n");
161 GOTO(out_fd, rc = 16);