_dostest.c
来自「开放源码的编译器open watcom 1.6.0版的源代码」· C语言 代码 · 共 508 行 · 第 1/2 页
C
508 行
/****************************************************************************
*
* Open Watcom Project
*
* Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
*
* ========================================================================
*
* This file contains Original Code and/or Modifications of Original
* Code as defined in and that are subject to the Sybase Open Watcom
* Public License version 1.0 (the 'License'). You may not use this file
* except in compliance with the License. BY USING THIS FILE YOU AGREE TO
* ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
* provided with the Original Code and Modifications, and is also
* available at www.sybase.com/developer/opensource.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
* ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
* NON-INFRINGEMENT. Please see the License for the specific language
* governing rights and limitations under the License.
*
* ========================================================================
*
* Description: Non-exhaustive test of the C library DOS functions.
*
****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <fcntl.h>
#include <share.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#define MAX_OPEN 10
#ifdef __386__
#ifdef __PHARLAP__
#define NUM_PARA 63
#define SIZE_PARA 4096
#else
#define NUM_PARA 2047
#define SIZE_PARA 16
#endif
#else
#define NUM_PARA 2047
#define SIZE_PARA 16
#endif
#define INTERRUPT_TIME 2 // seconds
#define TICK_COUNT (INTERRUPT_TIME * 18)
#ifdef __SW_BW
#include <wdefwin.h>
#define PROG_ABORT( num ) { printf( "Line: %d\n" \
"Abnormal termination.\n", num ); \
exit( -1 ); }
#else
#define PROG_ABORT( num ) { printf( "Line: %d\n", num ); exit(-1); }
#endif
#define YEAR(t) (((t & 0xFE00) >> 9) + 1980 )
#define MONTH(t) ((t & 0x01E0) >> 5)
#define DAY(t) (t & 0x001F)
#define HOUR(t) ((t & 0xF800) >> 11)
#define MINUTE(t) ((t & 0x07E0) >> 5)
#define SECOND(t) ((t & 0x001F) << 1 )
#define LFN_FNAME "Long File Name.with.long.extension"
#if defined(__OS2__) \
|| defined(__NT__) \
|| defined(__WINDOWS__) \
|| defined(__WINDOWS_386__) \
|| defined(__PHARLAP__)
#undef DO_INTERRUPT
#else
#define DO_INTERRUPT
#endif
#ifdef DO_INTERRUPT
volatile int clock_ticks = 0;
void (__interrupt __far *prev_int_1c)();
volatile int delay_flag = 1;
#endif
struct dosdate_t ddate;
struct dostime_t dtime;
int verbose = 0;
char *runtime;
void NowTest( char *funcname )
{
if( verbose ) {
printf( "Testing %s...\n", funcname );
fflush( stdout );
}
}
void TestDateTimeOperations( void )
{
struct dosdate_t newdate;
struct dostime_t newtime;
NowTest( "_dos_getdate(), and _dos_gettime()" );
_dos_getdate( &ddate );
_dos_gettime( &dtime );
if( verbose ) {
printf( "The date (MM-DD-YYYY) is: %.2d-%.2d-%.4d\n",
ddate.month, ddate.day, ddate.year );
printf( "The time (HH:MM:SS) is: %.2d:%.2d:%.2d\n",
dtime.hour, dtime.minute, dtime.second );
}
// run286 doesn't support the DosSetDateTime call
if( stricmp( runtime, "run286" ) != 0 ) {
NowTest( "_dos_setdate(), and _dos_settime()" );
newdate.year = 1999;
newdate.month = 12;
newdate.day = 31;
newtime.hour = 19;
newtime.minute = 19;
newtime.second = 30;
_dos_setdate( &newdate );
_dos_settime( &newtime );
newdate.year = 1980;
newdate.month = 1;
newdate.day = 1;
newtime.hour = 0;
newtime.minute = 0;
newtime.second = 0;
_dos_getdate( &newdate );
_dos_gettime( &newtime );
if( ( newdate.year != 1999 ) || ( newdate.month != 12 ) ||
( newdate.day != 31 ) || ( newtime.hour != 19 ) ||
( newtime.minute != 19 ) ) {
printf( "FAIL: getting/setting date/time functions.\n" );
printf( "Note: date/time info might have been corrupted.\n" );
PROG_ABORT( __LINE__ );
}
_dos_setdate( &ddate );
_dos_settime( &dtime );
}
}
#if !( defined(__WINDOWS__) || defined(__WINDOWS_386__) )
void TestMemoryOperations( void )
{
#if defined(__NT__) || (defined(__OS2__) && (defined(__386__)||defined(__PPC__)))
void *segment;
char *cptr;
#else
unsigned short segment;
char __far *cptr;
#endif
unsigned ctr;
NowTest( "_dos_allocmem(), and _dos_freemem()" );
if( _dos_allocmem( NUM_PARA, &segment ) != 0 ) {
printf( "_dos_allocmem() failed. " );
printf( "Only %u paragraphs available.\n", segment );
PROG_ABORT( __LINE__ );
}
// try to access it
#if defined(__NT__) || (defined(__OS2__) && (defined(__386__)||defined(__PPC__)))
cptr = (char *) segment;
#else
cptr = (char __far *) MK_FP(segment,0);
#endif
for( ctr = 0; ctr < NUM_PARA * SIZE_PARA; ctr += SIZE_PARA ) {
cptr[ctr] = '#';
}
#if !( defined(__OS2__) || defined(__NT__) || defined(__DOS4G__) )
NowTest( "_dos_setblock()" );
if( _dos_setblock( NUM_PARA * 2, segment, &ctr ) != 0 ) {
printf( "_dos_setblock() failed. " );
printf( "Can only resize to %u paragraphs.\n", ctr );
PROG_ABORT( __LINE__ );
}
// try to access it
for( ctr = 0; ctr < NUM_PARA * SIZE_PARA * 2; ctr += SIZE_PARA ) {
cptr[ctr] = '^';
}
#endif
if( _dos_freemem( segment ) != 0 ) {
printf( "_dos_freemem() failed.\n" );
PROG_ABORT( __LINE__ );
}
}
#endif
void TestFileOperations( void )
{
int fh[10];
int ctr;
unsigned rc;
char str[20];
struct find_t fileinfo;
unsigned long flag;
unsigned attribute;
unsigned short date,time;
char buffer[] = "This is a test for _dos_write().";
char tmpbuff[80];
unsigned len;
NowTest( "_dos_creatnew()" );
if( _dos_creatnew( "tmpfile", _A_RDONLY, &fh[0] ) != 0 ) {
printf( "Couldn't create the file \"tmpfile\"\n" );
PROG_ABORT( __LINE__ );
} else {
if( verbose ) printf( "Created \"tmpfile\"\n" );
if( _dos_creatnew( "tmpfile", _A_NORMAL, &fh[1] ) == 0 ) {
printf( "Error: _dos_creatnew() succeeded in creating " );
printf( "an existing file\n" );
}
_dos_close( fh[0] );
}
NowTest( "_dos_setfileattr()" ) ;
if( _dos_getfileattr( "tmpfile", &attribute ) != 0 ) {
printf( "_dos_getfileattr() failed.\n" );
PROG_ABORT( __LINE__ );
}
if( _dos_setfileattr( "tmpfile", attribute & ~_A_ARCH ) != 0 ) {
printf( "_dos_setfileattr() failed.\n" );
PROG_ABORT( __LINE__ );
}
if( _dos_getfileattr( "tmpfile", &attribute ) != 0 ) {
printf( "_dos_getfileattr() failed.\n" );
PROG_ABORT( __LINE__ );
}
if( ( attribute & _A_ARCH ) == _A_ARCH ) {
printf( "_dos_setfileattr() failed.\n" );
PROG_ABORT( __LINE__ );
}
if( verbose ) printf( "Creating %u temporary files...", MAX_OPEN );
strcpy( str, "_tmpfile." );
for( ctr = 0; ctr < MAX_OPEN; ++ctr ) {
itoa( 999 - ctr, &str[9], 10 );
if( _dos_creat( str, _A_NORMAL, &fh[ctr] ) != 0 ) {
printf( "failed when creating the file \"%s\"\n", str );
while( --ctr >= 0 ) _dos_close( fh[ctr] );
PROG_ABORT( __LINE__ );
}
}
if( verbose ) printf( "done.\n" );
for( ctr = 0; ctr < MAX_OPEN; ++ctr ) {
if( _dos_close( fh[ctr] ) != 0 ) {
printf( "Failed to close all the opened files\n" );
PROG_ABORT( __LINE__ );
}
}
// Now we have 20 tmpfiles. Use them to test _dos_find...()
NowTest( "_dos_findfirst(), _dos_findnext, and _dos_findclose()" );
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?