1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
39 #include <sys/types.h>
46 #define GOTO(label, rc) do { rc; goto label; } while (0)
48 int main (int argc, char **argv) {
50 unsigned long bytes, lbytes;
52 char *str, *str2, *readbuf;
55 fprintf(stderr, "usage: %s <filename> <bytes>\n", argv[0]);
59 bytes = strtoul(argv[2], NULL, 10);
61 printf("No bytes!\n");
65 printf("Need an even number of bytes!\n");
70 str = malloc(bytes+1);
72 printf("No enough memory for %lu bytes.\n", bytes);
75 str2 = malloc(lbytes+1);
77 printf("No enough memory for %lu bytes.\n", lbytes);
78 GOTO(out_str, rc = 5);
80 readbuf = malloc(bytes*2);
82 printf("No enough memory for %lu bytes.\n", bytes*2);
83 GOTO(out_str2, rc = 6);
86 for(i=0; i < bytes; i++)
87 str[i] = 'a' + (i % 26);
90 memcpy(str2, str, bytes);
91 memcpy(str2+(bytes/2), str, bytes);
95 printf("First String: %s\nSecond String: %s\n", str, str2);
97 fd = open(argv[1], O_CREAT|O_RDWR|O_TRUNC, 0700);
99 printf("Could not open file %s.\n", argv[1]);
100 GOTO(out_readbuf, rc = 7);
103 rc = write(fd, str, bytes);
105 printf("Write failed!\n");
106 GOTO(out_fd, rc = 8);
111 if (rc < 0 || st.st_size != bytes) {
112 printf("bad file %lu size first write %lu != %lu: rc %d\n",
113 (unsigned long)st.st_ino, (unsigned long)st.st_size,
115 GOTO(out_fd, rc = 9);
118 rc = lseek(fd, bytes / 2, SEEK_SET);
119 if (rc != bytes / 2) {
120 printf("Seek failed!\n");
121 GOTO(out_fd, rc = 10);
124 rc = write(fd, str, bytes);
126 printf("Write failed!\n");
127 GOTO(out_fd, rc = 11);
131 if (rc < 0 || st.st_size != bytes + bytes / 2) {
132 printf("bad file %lu size second write %lu != %lu: rc %d\n",
133 (unsigned long)st.st_ino, (unsigned long)st.st_size,
135 GOTO(out_fd, rc = 12);
138 rc = lseek(fd, 0, SEEK_SET);
140 printf("Seek failed!\n");
141 GOTO(out_fd, rc = 13);
144 rc = read(fd, readbuf, bytes * 2);
146 printf("Read %d bytes instead of %lu.\n", rc, lbytes);
150 printf("%s\n%s\n", readbuf, str2);
152 if (rc < 0 || st.st_size != bytes + bytes / 2) {
153 printf("bad file size after read %lu != %lu: rc %d\n",
154 (unsigned long)st.st_size, bytes + bytes / 2,
156 GOTO(out_fd, rc = 14);
159 GOTO(out_fd, rc = 15);
164 printf("%s\n%s\n", readbuf, str2);
165 if (strcmp(readbuf, str2)) {
166 printf("No match!\n");
167 GOTO(out_fd, rc = 16);