campo-sirio/cb5/d4low.c
alex a0f5e0898b This commit was generated by cvs2svn to compensate for changes in r975,
which included commits to RCS files with non-trunk default branches.

git-svn-id: svn://10.65.10.50/trunk@976 c028cbd2-c16b-5b4b-a496-9718f37d4682
1995-02-06 15:33:45 +00:00

846 lines
20 KiB
C
Executable File

/*********************************************************************
d4low.c (c)Copyright Sequiter Software Inc., 1990-1994.
All rights reserved.
This example test program tests low-level CodeBase functions.
*********************************************************************/
#define T4LOCK_POS 1000000000L
/* #define S4TEST_HANDLES */ /* test for the maximum # of allowable file handles */
#include "d4all.h"
#ifdef S4FOX
void t4dbl_to_fox( char *, double ) ;
#endif
#ifdef __TURBOC__
#pragma hdrstop /* use pre-defined headers */
extern unsigned _stklen = 10000 ; /* set stack length to 10000 */
#endif
#ifndef S4SINGLE
#ifndef S4MACINTOSH
#ifdef S4UNIX
#ifdef S4LOCKF
#include <unistd.h>
#else
#include <sys/locking.h>
#endif
#else
#ifndef __TURBOC__
#ifndef __IBMC__
#include <sys\locking.h>
#define S4LOCKING
#endif
#endif
#ifdef __ZTC__
extern int errno ;
#endif
#ifdef _MSC_VER
#include <sys\types.h>
#endif
#ifdef __TURBOC__
/* extern int cdecl errno ; */
#include <sys\locking.h>
#endif
#include <sys\stat.h>
#include <share.h>
#endif
#endif
#endif
#include <fcntl.h>
#include <errno.h>
#ifdef S4DO_ERRNO
extern int errno ;
#endif
void main()
{
CODE4 cb ; /* initialize global variables */
FILE4 file ;
FILE4 new_file ;
char buffer[100] ;
long len ;
unsigned char test_buf[5] ;
unsigned char control_buf[5] ;
int is_error = 0 ;
int rc ;
double d ;
char ch ;
char ptr[20] ;
unsigned char result[8] ;
d4init( &cb ) ;
cb.safety = 0 ; /* will create new database, regardless of */
/* it's current existence */
#ifdef S4TEST_HANDLES
for ( rc = 1; 1 ; rc++ )
{
c4ltoa45( rc, ptr, 10 ) ;
file4create( &file, &cb, ptr, 0 ) ;
if ( file.hand >= 0 )
printf(" FILE HANDLE # %ld. TOTAL NUMBER: %ld\n", file.hand, rc ) ;
else
{
printf(" MAXIMUM FILE HANDLES REACHED: %ld\n", rc ) ;
exit(1) ;
}
}
#endif
printf("Testing byte ordering . . .") ;
len = 0x01020408 ;
memcpy( test_buf, (void *)&len, 4 ) ;
#ifdef S4BYTEORDER_3210
memcpy( control_buf, "\001\002\004\010", 4 ) ;
#else
#ifdef S4BYTEORDER_2301
memcpy( control_buf, "\002\001\010\004", 4 ) ;
#else /* default */
memcpy( control_buf, "\010\004\002\001", 4 ) ;
#endif
#endif
if ( memcmp( control_buf , test_buf, 4 ) )
{
printf("\nERROR: byte ordering is incorrect \n" ) ;
printf("\nUse S4BYTEORDER_3210 or S4BYTEORDER_2301\n" ) ;
printf("\nThis is documented in Multi-Platform Manual\n" ) ;
is_error = 1 ;
#ifdef S4UNIX
getchar() ;
#else
getch() ;
#endif
}
else
printf("OK\n" ) ;
printf("Testing memcmp() . . .") ;
#ifdef S4MEMCMP
rc = c4memcmp( "\320", "\120", 1 ) ;
#else
rc = memcmp( "\320", "\120", 1 ) ;
#endif
if (rc <= 0) /* memcmp assumes signed bytes -- not usable */
{
printf("\nERROR: memcmp() assumes signed bytes\n" ) ;
printf("\nUse S4MEMCMP\n" ) ;
printf("\nThis is documented in Multi-Platform Manual\n" ) ;
is_error = 1 ;
#ifdef S4UNIX
getchar() ;
#else
getch() ;
#endif
}
else
printf("OK\n" ) ;
printf("Testing for signed byte usage . . .") ;
ch = -5 ;
if ( ch != -5 )
{
printf("\nERROR: compiled using unsigned byte default\n" ) ;
printf("\nSee your compiler documentation for\n" ) ;
printf("\n'signed byte compilation' information.\n" ) ;
is_error = 1 ;
#ifdef S4UNIX
getchar() ;
#else
getch() ;
#endif
}
else
printf("OK\n" ) ;
printf("Creating File: file4create() . . .") ;
rc = file4create( &file, &cb, "TEST.FIL", 0 ) ;
if ( rc < 0 )
{
printf("\nERROR: file4create() \n" ) ;
rc = open( "test.fil", O_CREAT | O_TRUNC | O_RDWR , 0666 ) ;
if ( rc < 0 )
{
printf("Function open() is failing with 'O_CREAT | O_TRUNC | O_RDWR'\n" );
printf(" and mode set to 0666\n" );
switch ( (int) errno )
{
case EACCES :
printf("ERRNO == EACCES: Permission denied\n" ) ;
break ;
case EINVAL :
printf("ERRNO == EINVAL: Invalid mode and permission setting\n" ) ;
break ;
case EEXIST :
printf("ERRNO == EEXIST: O_CREAT and O_EXCL specified but file already exists\n" );
break ;
case EMFILE :
printf("ERRNO == EMFILE: No more file handles available\n" ) ;
break ;
case ENOENT :
printf("ERRNO == ENOENT: File not found or directory not present\n" ) ;
break ;
default:
printf("ERRNO VALUE NOT RECOGNIZED\n" ) ;
}
}
else
{
printf("Function open() succeeds but file4open() fails (create) \n" );
printf("Investigate further" );
}
is_error = 1 ;
#ifdef S4UNIX
getchar() ;
#else
getch() ;
#endif
}
else
printf("OK\n" ) ;
printf("Writing To File: file4write() . . .") ;
rc = file4write( &file, 10, "Some Information", 17 ) ;
if ( rc < 0 )
{
printf("\nERROR: file4write() \n" ) ;
rc = lseek( file.hand, 10, 0 ) ;
if ( rc != 10 )
{
printf("Function lseek() is failing to seek past EOF\n" );
switch ( (int) errno )
{
case EBADF :
printf("ERRNO == EBADF: file handle is invalid\n" ) ;
break ;
case EINVAL :
printf("ERRNO == EINVAL: Invalid mode and permission setting\n" ) ;
break ;
default:
printf("ERRNO VALUE NOT RECOGNIZED\n" ) ;
}
}
rc = write( file.hand, "Some Information", 17 ) ;
if ( rc != 17 )
{
printf("Function write() is failing to write past EOF\n" );
switch ( (int) errno )
{
case EBADF :
printf("ERRNO == EBADF: file handle is invalid\n" ) ;
break ;
case ENOSPC :
printf("ERRNO == ENOSPC: system out of disk space\n" ) ;
break ;
default:
printf("ERRNO VALUE NOT RECOGNIZED\n" ) ;
}
}
else
{
printf("Function write() succeeds but file4write() fails \n" );
printf("Investigate further" );
}
is_error = 1 ;
#ifdef S4UNIX
getchar() ;
#else
getch() ;
#endif
}
else
printf("OK\n" ) ;
printf("Checking File Flushing: file4low_flush() . . .") ;
rc = file4low_flush( &file, 1 ) ;
if ( rc != 0 )
{
printf("\nERROR: file4low_flush() \n" ) ;
printf("Function file4low_flush() is failing\n" );
printf("See S4USE_DUP or S4USE_EBX usage\n" );
printf("Investigate further" );
is_error = 1 ;
#ifdef S4UNIX
getchar() ;
#else
getch() ;
#endif
}
else
printf("OK\n" ) ;
printf("Checking File Size: file4len() . . .") ;
len = file4len( &file ) ;
if ( len != 27 )
{
printf("\nERROR: file4len() \n" ) ;
len = filelength( file.hand ) ;
if ( len != 27 )
{
printf("Function filelength() is returning bad filelength\n" );
}
else
{
printf("Function filelength() succeeds but file4len() fails \n" );
printf("Investigate further" );
}
is_error = 1 ;
#ifdef S4UNIX
getchar() ;
#else
getch() ;
#endif
}
else
printf("OK\n" ) ;
printf("Reading From File: file4read() . . .") ;
rc = (int) file4read( &file, 10L, buffer, 17 ) ;
if ( rc != 17 )
{
printf("\nERROR: file4read() \n" ) ;
rc = lseek( file.hand, 10, 0 ) ;
if ( rc != 10 )
{
printf("Function lseek() is failing to seek past EOF\n" );
switch ( (int) errno )
{
case EBADF :
printf("ERRNO == EBADF: file handle is invalid\n" ) ;
break ;
case EINVAL :
printf("ERRNO == EINVAL: Invalid mode and permission setting\n" ) ;
break ;
default:
printf("ERRNO VALUE NOT RECOGNIZED\n" ) ;
}
}
rc = read( file.hand, buffer, 17 ) ;
if ( rc != 17 )
{
printf("Function write() is failing to write past EOF\n" );
switch ( (int) errno )
{
case EBADF :
printf("ERRNO == EBADF: file handle is invalid\n" ) ;
break ;
default:
printf("ERRNO VALUE NOT RECOGNIZED\n" ) ;
}
}
else
{
printf("Function read() succeeds but file4read() fails \n" );
printf("Investigate further" );
}
is_error = 1 ;
#ifdef S4UNIX
getchar() ;
#else
getch() ;
#endif
}
else
{
if ( memcmp( "Some Information", buffer, 17 ) )
{
printf("\nERROR: file4read() \n" ) ;
printf("Information read is incorrect \n" );
printf("MEMCMP: 'Some Information' vs '%s'\n", buffer ) ;
is_error = 1 ;
#ifdef S4UNIX
getchar() ;
#else
getch() ;
#endif
}
else
printf("OK\n" ) ;
}
printf("Closing File: file4close() . . .") ;
rc = file4close( &file ) ;
if ( rc < 0 )
{
printf("\nERROR: file4close() \n" ) ;
rc = close( file.hand ) ;
if ( rc < 0 )
{
switch ( (int) errno )
{
case EBADF :
printf("ERRNO == EBADF: file handle is invalid\n" ) ;
break ;
default:
printf("ERRNO VALUE NOT RECOGNIZED\n" ) ;
}
}
else
{
printf("Function close() succeeds but file4close() fails \n" );
printf("Investigate further" );
}
is_error = 1 ;
#ifdef S4UNIX
getchar() ;
#else
getch() ;
#endif
}
else
printf("OK\n" ) ;
printf("Opening File: file4open() . . .") ;
rc = file4open( &file, &cb, "TEST.FIL", 0 ) ;
if ( rc < 0 )
{
printf("\nERROR: file4open() \n" ) ;
rc = open( "test.fil", O_RDWR , 0666 ) ;
if ( rc < 0 )
{
printf("Function open() is failing with 'O_RDWR'\n" );
printf(" and mode set to 0666\n" );
switch ( (int) errno )
{
case EACCES :
printf("ERRNO == EACCES: Permission denied\n" ) ;
break ;
case EINVAL :
printf("ERRNO == EINVAL: Invalid mode and permission setting\n" ) ;
break ;
case EEXIST :
printf("ERRNO == EEXIST: O_CREAT and O_EXCL specified but file already exists\n" );
break ;
case EMFILE :
printf("ERRNO == EMFILE: No more file handles available\n" ) ;
break ;
case ENOENT :
printf("ERRNO == ENOENT: File not found or directory not present\n" ) ;
break ;
default:
printf("ERRNO VALUE NOT RECOGNIZED\n" ) ;
}
}
else
{
printf("Function open() succeeds but file4open() fails (open) \n" );
printf("Investigate further" );
}
is_error = 1 ;
#ifdef S4UNIX
getchar() ;
#else
getch() ;
#endif
}
else
printf("OK\n" ) ;
printf("Checking File Size(2): file4len() . . .") ;
len = file4len( &file ) ;
if ( len != 27 )
{
printf("\nERROR: file4len() \n" ) ;
len = filelength( file.hand ) ;
if ( len != 27 )
{
printf("Function filelength() is returning bad filelength\n" );
}
else
{
printf("Function filelength() succeeds but file4len() fails \n" );
printf("Investigate further" );
}
is_error = 1 ;
#ifdef S4UNIX
getchar() ;
#else
getch() ;
#endif
}
else
printf("OK\n" ) ;
printf("Changing File Size: file4len_set() . . .") ;
len = file4len_set( &file, 22 ) ;
if ( len < 0 )
{
printf("\nERROR: file4len_set() \n" ) ;
}
else
printf("OK\n" ) ;
printf("Checking File Size(3): file4len() . . .") ;
len = file4len( &file ) ;
if ( len != 22 )
{
printf("\nERROR: file4len() \n" ) ;
len = filelength( file.hand ) ;
if ( len != 22 )
{
printf("Function filelength() is returning bad filelength\n" );
}
else
{
printf("Function filelength() succeeds but file4len() fails \n" );
printf("Investigate further" );
}
is_error = 1 ;
#ifdef S4UNIX
getchar() ;
#else
getch() ;
#endif
}
else
printf("OK\n" ) ;
#ifndef S4SINGLE
#ifndef S4MACINTOSH
#ifndef S4LOCKING
printf("Locking File: file4lock() . . .") ;
rc = file4lock( &file, T4LOCK_POS, 1 ) ;
if ( rc == r4locked )
{
printf("\nERROR: file4lock() \n" ) ;
printf("file locked by another user\n" ) ;
is_error = 1 ;
#ifdef S4UNIX
getchar() ;
#else
getch() ;
#endif
}
if ( rc < 0 )
{
printf("\nERROR: file4lock() \n" ) ;
len = lseek( file.hand, T4LOCK_POS, 0 ) ;
if ( len != T4LOCK_POS )
{
printf("Function lseek() is failing to seek past EOF\n" );
switch ( (int) errno )
{
case EBADF :
printf("ERRNO == EBADF: file handle is invalid\n" ) ;
break ;
case EINVAL :
printf("ERRNO == EINVAL: Invalid mode and permission setting\n" ) ;
break ;
default:
printf("ERRNO VALUE NOT RECOGNIZED\n" ) ;
}
}
#ifdef S4LOCKF
rc = lockf( file.hand, F_TLOCK, 1 ) ;
#else
rc = locking( file.hand, LK_NBLCK, 1 ) ;
#endif
if ( rc < 0 )
{
printf("Function lock() is failing to lock past EOF\n" );
switch ( (int) errno )
{
case EBADF :
printf("ERRNO == EBADF: file handle is invalid\n" ) ;
break ;
case EINVAL :
printf("ERRNO == EINVAL: Invalid mode and permission setting\n" ) ;
break ;
default:
printf("ERRNO VALUE NOT RECOGNIZED\n" ) ;
}
}
else
{
printf("Function lock() succeeds but file4lock() fails \n" );
printf("Investigate further" );
}
is_error = 1 ;
#ifdef S4UNIX
getchar() ;
#else
getch() ;
#endif
}
else
printf("OK\n" ) ;
printf("Unlocking File: file4unlock() . . .") ;
rc = file4unlock( &file, T4LOCK_POS, 1 ) ;
if ( rc < 0 )
{
printf("\nERROR: file4unlock() \n" ) ;
len = lseek( file.hand, T4LOCK_POS, 0 ) ;
if ( len != T4LOCK_POS )
{
printf("Function lseek() is failing to seek past EOF\n" );
switch ( (int) errno )
{
case EBADF :
printf("ERRNO == EBADF: file handle is invalid\n" ) ;
break ;
case EINVAL :
printf("ERRNO == EINVAL: Invalid mode and permission setting\n" ) ;
break ;
default:
printf("ERRNO VALUE NOT RECOGNIZED\n" ) ;
}
}
#ifdef S4LOCKF
rc = lockf( file.hand, F_ULOCK, 1 ) ;
#else
rc = locking( file.hand, LK_UNLCK, 1 ) ;
#endif
if ( rc < 0 )
{
printf("Function unlock() is failing to unlock file\n" );
switch ( (int) errno )
{
case EBADF :
printf("ERRNO == EBADF: file handle is invalid\n" ) ;
break ;
case EINVAL :
printf("ERRNO == EINVAL: Invalid mode and permission setting\n" ) ;
break ;
default:
printf("ERRNO VALUE NOT RECOGNIZED\n" ) ;
}
}
else
{
printf("Function unlock() succeeds but file4unlock() fails \n" );
printf("Investigate further" );
}
is_error = 1 ;
#ifdef S4UNIX
getchar() ;
#else
getch() ;
#endif
}
else
printf("OK\n" ) ;
#endif /* ifdef S4LOCKING */
#endif /* ifndef S4MACINTOSH */
#endif /* ifndef S4SINGLE */
printf("Closing File: file4close() . . .") ;
rc = file4close( &file ) ;
if ( rc < 0 )
{
printf("\nERROR: file4close() \n" ) ;
rc = close( file.hand ) ;
if ( rc < 0 )
{
switch ( (int) errno )
{
case EBADF :
printf("ERRNO == EBADF: file handle is invalid\n" ) ;
break ;
default:
printf("ERRNO VALUE NOT RECOGNIZED\n" ) ;
}
}
else
{
printf("Function close() succeeds but file4close() fails \n" );
printf("Investigate further" );
}
is_error = 1 ;
#ifdef S4UNIX
getchar() ;
#else
getch() ;
#endif
}
else
printf("OK\n" ) ;
printf("Removing NEW_TEST.FIL (if exists) . . .") ;
rc = u4remove( "NEW_TEST.FIL" ) ;
if ( (rc < 0) && ((int) errno != ENOENT) )
{
printf("\nERROR: u4move() \n" ) ;
switch ( (int) errno )
{
case EACCES :
printf("ERRNO == EACCES: Permission denied\n" ) ;
break ;
default:
printf("ERRNO VALUE NOT RECOGNIZED\n" ) ;
}
is_error = 1 ;
#ifdef S4UNIX
getchar() ;
#else
getch() ;
#endif
}
else
printf("OK\n" ) ;
printf("Renaming TEST.FIL to NEW_TEST.FIL . . .") ;
rc = u4rename( "TEST.FIL", "NEW_TEST.FIL" ) ;
if ( rc < 0 )
{
printf("\nERROR: u4rename() \n" ) ;
switch ( (int) errno )
{
case EACCES :
printf("ERRNO == EACCES: Permission denied\n" ) ;
break ;
case ENOENT :
printf("ERRNO == ENOENT: No such file or directory\n" ) ;
break ;
default:
printf("ERRNO VALUE NOT RECOGNIZED\n" ) ;
}
is_error = 1 ;
#ifdef S4UNIX
getchar() ;
#else
getch() ;
#endif
}
else
printf("OK\n" ) ;
printf("\n\nLOW LEVEL FILE TESTS SUCCESSFUL\n") ;
#ifdef S4FOX
printf("\n\nS4FOX COMPATIBILITY TESTING . . .\n") ;
t4dbl_to_fox( result, 1.0 ) ;
if ( is_error=memcmp( result, "\277\360\000\000\000\000\000\000", 8 ) )
printf("ERROR: t4dbl_to_fox %f\n", *(double *)result ) ;
t4dbl_to_fox( result, 22.34 ) ;
if ( is_error=memcmp( result, "\300\066\127\012\075\160\243\327", 8 ) )
printf("ERROR: t4dbl_to_fox %f\n", *(double *)result) ;
t4dbl_to_fox( result, 1191.575675 ) ;
if ( is_error=memcmp( result, "\300\222\236\115\175\277\110\200", 8 ) )
printf("ERROR: t4dbl_to_fox %f\n", *(double *)result ) ;
t4dbl_to_fox( result, 0.0 ) ;
if ( is_error=memcmp( result, "\200\000\000\000\000\000\000\000", 8 ) )
printf("ERROR: t4dbl_to_fox %f\n", *(double *)result ) ;
t4dbl_to_fox( result, 9999.000 ) ;
if ( is_error=memcmp( result, "\300\303\207\200\000\000\000\000", 8 ) )
printf("ERROR: t4dbl_to_fox %f\n", *(double *)result ) ;
t4dbl_to_fox( result, 0.000001 ) ;
if ( is_error=memcmp( result, "\276\260\306\367\240\265\355\215", 8 ) )
printf("ERROR: t4dbl_to_fox %f\n", *(double *)result ) ;
t4dbl_to_fox( result, 0.990001 ) ;
if ( is_error=memcmp( result, "\277\357\256\026\223\300\073\305", 8 ) )
printf("ERROR: t4dbl_to_fox %f\n", *(double *)result ) ;
t4dbl_to_fox( result, 1.00000 ) ;
if ( is_error=memcmp( result, "\277\360\000\000\000\000\000\000", 8 ) )
printf("ERROR: t4dbl_to_fox %f\n", *(double *)result ) ;
t4dbl_to_fox( result, 3.0 ) ;
if ( is_error=memcmp( result, "\300\010\000\000\000\000\000\000", 8 ) )
printf("ERROR: t4dbl_to_fox %f\n", *(double *)result ) ;
t4dbl_to_fox( result, 12345678.90 ) ;
if ( is_error=memcmp( result, "\301\147\214\051\334\314\314\315", 8 ) )
printf("ERROR: t4dbl_to_fox %f\n", *(double *)result ) ;
t4dbl_to_fox( result, .000000 ) ;
if ( is_error=memcmp( result, "\200\000\000\000\000\000\000\000", 8 ) )
printf("ERROR: t4dbl_to_fox %f\n", *(double *)result ) ;
t4dbl_to_fox( result, -1.0 ) ;
if ( is_error=memcmp( result, "\100\017\377\377\377\377\377\377", 8 ) )
printf("ERROR: t4dbl_to_fox %f\n", *(double *)result ) ;
t4dbl_to_fox( result, -22.34 ) ;
if ( is_error=memcmp( result, "\077\311\250\365\302\217\134\050", 8 ) )
printf("ERROR: t4dbl_to_fox %f\n", *(double *)result ) ;
t4dbl_to_fox( result, -1191.575675 ) ;
if ( is_error=memcmp( result, "\077\155\141\262\202\100\267\177", 8 ) )
printf("ERROR: t4dbl_to_fox %f\n", *(double *)result ) ;
t4dbl_to_fox( result, -9999.000 ) ;
if ( is_error=memcmp( result, "\077\074\170\177\377\377\377\377", 8 ) )
printf("ERROR: t4dbl_to_fox %f\n", *(double *)result ) ;
t4dbl_to_fox( result, -0.000001 ) ;
if ( is_error=memcmp( result, "\101\117\071\010\137\112\022\162", 8 ) )
printf("ERROR: t4dbl_to_fox %f\n", *(double *)result ) ;
t4dbl_to_fox( result, -0.990001 ) ;
if ( is_error=memcmp( result, "\100\020\121\351\154\077\304\072", 8 ) )
printf("ERROR: t4dbl_to_fox %f\n", *(double *)result ) ;
t4dbl_to_fox( result, -1.00000 ) ;
if ( is_error=memcmp( result, "\100\017\377\377\377\377\377\377", 8 ) )
printf("ERROR: t4dbl_to_fox %f\n", *(double *)result ) ;
t4dbl_to_fox( result, -3.0 ) ;
if ( is_error=memcmp( result, "\077\367\377\377\377\377\377\377", 8 ) )
printf("ERROR: t4dbl_to_fox %f\n", *(double *)result ) ;
t4dbl_to_fox( result, -12345678.90 ) ;
if ( is_error=memcmp( result, "\076\230\163\326\043\063\063\062", 8 ) )
printf("ERROR: t4dbl_to_fox %f\n", *(double *)result ) ;
if ( is_error )
printf("\n\nS4FOX TESTS: ERROR(S) FOUND\n") ;
else
printf("\n\nS4FOX TESTS: SUCCESSFUL\n") ;
#endif
if ( is_error )
printf("\n\nD4LOW: ERROR(S) FOUND\n") ;
else
printf("\n\nD4LOW: SUCCESSFUL\n") ;
}