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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
37 #include <sys/types.h>
44 #define GOTO(label, rc) do { rc; goto label; } while (0)
46 int main (int argc, char **argv) {
48 unsigned long bytes, lbytes;
50 char *str, *str2, *readbuf;
53 fprintf(stderr, "usage: %s <filename> <bytes>\n", argv[0]);
57 bytes = strtoul(argv[2], NULL, 10);
59 printf("No bytes!\n");
63 printf("Need an even number of bytes!\n");
68 str = malloc(bytes+1);
70 printf("No enough memory for %lu bytes.\n", bytes);
73 str2 = malloc(lbytes+1);
75 printf("No enough memory for %lu bytes.\n", lbytes);
76 GOTO(out_str, rc = 5);
78 readbuf = malloc(bytes*2);
80 printf("No enough memory for %lu bytes.\n", bytes*2);
81 GOTO(out_str2, rc = 6);
84 for(i=0; i < bytes; i++)
85 str[i] = 'a' + (i % 26);
88 memcpy(str2, str, bytes);
89 memcpy(str2+(bytes/2), str, bytes);
93 printf("First String: %s\nSecond String: %s\n", str, str2);
95 fd = open(argv[1], O_CREAT|O_RDWR|O_TRUNC, 0700);
97 printf("Could not open file %s.\n", argv[1]);
98 GOTO(out_readbuf, rc = 7);
101 rc = write(fd, str, bytes);
103 printf("Write failed!\n");
104 GOTO(out_fd, rc = 8);
109 if (rc < 0 || st.st_size != bytes) {
110 printf("bad file %lu size first write %lu != %lu: rc %d\n",
111 (unsigned long)st.st_ino, (unsigned long)st.st_size,
113 GOTO(out_fd, rc = 9);
116 rc = lseek(fd, bytes / 2, SEEK_SET);
117 if (rc != bytes / 2) {
118 printf("Seek failed!\n");
119 GOTO(out_fd, rc = 10);
122 rc = write(fd, str, bytes);
124 printf("Write failed!\n");
125 GOTO(out_fd, rc = 11);
129 if (rc < 0 || st.st_size != bytes + bytes / 2) {
130 printf("bad file %lu size second write %lu != %lu: rc %d\n",
131 (unsigned long)st.st_ino, (unsigned long)st.st_size,
133 GOTO(out_fd, rc = 12);
136 rc = lseek(fd, 0, SEEK_SET);
138 printf("Seek failed!\n");
139 GOTO(out_fd, rc = 13);
142 rc = read(fd, readbuf, bytes * 2);
144 printf("Read %d bytes instead of %lu.\n", rc, lbytes);
148 printf("%s\n%s\n", readbuf, str2);
150 if (rc < 0 || st.st_size != bytes + bytes / 2) {
151 printf("bad file size after read %lu != %lu: rc %d\n",
152 (unsigned long)st.st_size, bytes + bytes / 2,
154 GOTO(out_fd, rc = 14);
157 GOTO(out_fd, rc = 15);
162 printf("%s\n%s\n", readbuf, str2);
163 if (strcmp(readbuf, str2)) {
164 printf("No match!\n");
165 GOTO(out_fd, rc = 16);