#include #include #include #include #include #include #include #include #include // HAL polled output //========================================================================== #define SHOW_RESULT( _fn, _res ) \ diag_printf("FAIL: " #_fn "() returned %ld %s\n", \ (unsigned long)_res, _res<0?strerror(errno):""); //========================================================================== cyg_uint8 buf[256]; cyg_uint8 buf1[256]; //========================================================================== // main int main( int argc, char **argv ) { int err; FILE *stream; long pos; unsigned int i; cyg_interrupt_disable(); CYG_TEST_INIT(); // -------------------------------------------------------------- CYG_TEST_INFO("mount /"); err = mount( "", "/", "ramfs" ); if( err < 0 ) SHOW_RESULT( mount, err ); CYG_TEST_INFO("creating /fseek"); stream = fopen("/fseek","w+"); if (!stream) { diag_printf("FAIL: fopen() returned NULL, %s\n", strerror(errno)); CYG_TEST_FINISH("done"); \ } for (i = 0; i < sizeof(buf); i++) { buf[i] = i % 256; } CYG_TEST_INFO("writing test pattern"); err=fwrite(buf,sizeof(buf), 1, stream); if ( err < 0 ) SHOW_RESULT( fwrite, err ); pos = ftell(stream); if (pos < 0) SHOW_RESULT( ftell, pos ); if (pos != sizeof(buf)) diag_printf(": ftell is not telling the truth."); CYG_TEST_INFO("fseek()ing to 85"); err = fseek(stream, 85, SEEK_SET); if ( err < 0 ) SHOW_RESULT( fseek, err ); pos = ftell(stream); if (pos < 0) SHOW_RESULT( ftell, pos ); if (pos != 85) CYG_TEST_FAIL("ftell is not telling the truth"); char header[3]; err = fread(header,3,1,stream); if ( err < 0 ) SHOW_RESULT( fwrite, err ); pos = ftell(stream); if (pos < 0) SHOW_RESULT( ftell, pos ); if (pos != 88) CYG_TEST_FAIL("ftell is not telling the truth"); for (i = 88; i < 161; i++) { buf[i] = 0; } CYG_TEST_INFO("writing"); err = fwrite(buf+88, 73, 1, stream); if ( err < 0 ) SHOW_RESULT( fwrite, err ); pos = ftell(stream); if (pos < 0) SHOW_RESULT( ftell, pos ); if (pos != 161) CYG_TEST_FAIL("ftell is not telling the truth"); CYG_TEST_INFO("closing file"); err = fclose(stream); if (err != 0) SHOW_RESULT( fclose, err ); CYG_TEST_INFO("open file /fseek"); stream = fopen("/fseek", "r+"); if (!stream) { diag_printf(": fopen() returned NULL, %s\n", strerror(errno)); } err = fseek(stream, 0, SEEK_SET); if ( err < 0 ) SHOW_RESULT( fseek, err ); err = fread(buf1,sizeof(buf1),1, stream); if (err != 1) SHOW_RESULT( fread, err ); CYG_TEST_INFO("Comparing contents"); if (memcmp(buf, buf1, sizeof(buf1))) { CYG_TEST_FAIL("File contents inconsistent"); int t = 0; cyg_uint8 c; err = fseek(stream, 0, SEEK_SET); if ( err < 0 ) SHOW_RESULT( fseek, err ); CYG_TEST_INFO("file dump"); while( !feof(stream) ) { c = (cyg_uint8)getc(stream); if( !feof(stream) ) { diag_printf("%02x ",c); t++; if( t == 16 ) { diag_printf("\n"); t = 0; } } } diag_printf("\n"); t=0; CYG_TEST_INFO("buf dump"); for (i = 0; i < sizeof(buf); i++) { diag_printf("%02x ",buf[i]); t++; if( t == 16 ) { diag_printf("\n"); t = 0; } } diag_printf("\n"); } CYG_TEST_INFO("closing file"); err = fclose(stream); if (err != 0) SHOW_RESULT( fclose, err ); CYG_TEST_INFO("umount /"); err = umount( "/" ); if( err < 0 ) SHOW_RESULT( umount, err ); cyg_interrupt_enable(); CYG_TEST_PASS_FINISH("jffs2_2"); }