1 //==========================================================================
5 // Test fseek on a filesystem
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 2004 Andrew Lunn
12 // Copyright (C) 2004 eCosCentric Limited
14 // eCos is free software; you can redistribute it and/or modify it under
15 // the terms of the GNU General Public License as published by the Free
16 // Software Foundation; either version 2 or (at your option) any later version.
18 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
19 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 // You should have received a copy of the GNU General Public License along
24 // with eCos; if not, write to the Free Software Foundation, Inc.,
25 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
27 // As a special exception, if other files instantiate templates or use macros
28 // or inline functions from this file, or you compile this file and link it
29 // with other works to produce a work based on this file, this file does not
30 // by itself cause the resulting work to be covered by the GNU General Public
31 // License. However the source code for this file must still be made available
32 // in accordance with section (3) of the GNU General Public License.
34 // This exception does not invalidate any other reasons why a work based on
35 // this file might be covered by the GNU General Public License.
37 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
38 // at http://sources.redhat.com/ecos/ecos-license/
39 // -------------------------------------------
40 //####ECOSGPLCOPYRIGHTEND####
41 //==========================================================================
42 //#####DESCRIPTIONBEGIN####
47 // Purpose: Test fseek on a filesystem
48 // Description: This test uses the ramfs to check out the fseek
49 // operation on a filesystem.
57 //####DESCRIPTIONEND####
59 //==========================================================================
66 #include <cyg/fileio/fileio.h>
68 #include <cyg/infra/testcase.h>
69 #include <cyg/infra/diag.h> // HAL polled output
70 //==========================================================================
72 #define SHOW_RESULT( _fn, _res ) \
73 diag_printf("FAIL: " #_fn "() returned %ld %s\n", (long)_res, _res<0?strerror(errno):"");
75 //==========================================================================
80 //==========================================================================
83 int main( int argc, char **argv )
92 // --------------------------------------------------------------
94 CYG_TEST_INFO("mount /");
95 err = mount( "", "/", "ramfs" );
96 if( err < 0 ) SHOW_RESULT( mount, err );
98 CYG_TEST_INFO("creating /fseek");
99 stream = fopen("/fseek","w+");
101 diag_printf("FAIL: fopen() returned NULL, %s\n", strerror(errno));
102 CYG_TEST_FINISH("done"); \
105 /* Write a buffer full of cyclic numbers */
106 for (i = 0; i < sizeof(buf); i++) {
110 CYG_TEST_INFO("writing test pattern");
111 err=fwrite(buf,sizeof(buf), 1, stream);
112 if ( err < 0 ) SHOW_RESULT( fwrite, err );
114 /* The current position should be the same size as the buffer */
117 if (pos < 0) SHOW_RESULT( ftell, pos );
118 if (pos != sizeof(buf))
119 diag_printf("<FAIL>: ftell is not telling the truth.");
121 CYG_TEST_INFO("fseek()ing to beginning and writing");
123 /* Seek back to the beginning of the file */
124 err = fseek(stream, 0, SEEK_SET);
125 if ( err < 0 ) SHOW_RESULT( fwrite, err );
129 if (pos < 0) SHOW_RESULT( ftell, pos );
130 if (pos != 0) CYG_TEST_FAIL("ftell is not telling the truth");
132 /* Write 4 zeros to the beginning of the file */
133 for (i = 0; i < 4; i++) {
137 err = fwrite(buf, 4, 1, stream);
138 if ( err < 0 ) SHOW_RESULT( fwrite, err );
140 /* Check the pointer is at 4 */
143 if (pos < 0) SHOW_RESULT( ftell, pos );
144 if (pos != 4) CYG_TEST_FAIL("ftell is not telling the truth");
146 CYG_TEST_INFO("closing file");
148 /* Close the file, open it up again and read it back */
149 err = fclose(stream);
150 if (err != 0) SHOW_RESULT( fclose, err );
152 CYG_TEST_INFO("open file /fseek");
153 stream = fopen("/fseek", "r+");
155 diag_printf("<FAIL>: fopen() returned NULL, %s\n", strerror(errno));
158 err = fread(buf1,sizeof(buf1),1, stream);
159 if (err != 1) SHOW_RESULT( fread, err );
161 CYG_TEST_INFO("Comparing contents");
162 if (memcmp(buf, buf1, sizeof(buf1))) {
163 CYG_TEST_FAIL("File contents inconsistent");
166 err = fclose(stream);
167 if (err != 0) SHOW_RESULT( fclose, err );
169 CYG_TEST_INFO("umount /");
171 if( err < 0 ) SHOW_RESULT( umount, err );
173 CYG_TEST_PASS_FINISH("ramfs2");