#if !defined(_INLINE)
static char cvsid[] = "@(#)$Id$";
static char debugFileId[] = __FILE__;
#endif
#include <sys/types.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <limits.h>
#include <fcntl.h>
#include <errno.h>
#include <time.h>
#include "platform/POSIXPlatform.hpp"
#include "String.hpp"
#include "Debug.hpp"
extern "C" int kill(pid_t pid, int sig);
POSIXPlatform::POSIXPlatform()
{
setImageName( IMAGE_NAME );
}
POSIXPlatform::~POSIXPlatform()
{}
char *POSIXPlatform::getErrorDescription( int errCode )
{
++callsNum;
return strerror( errCode );
}
void POSIXPlatform::exitImmediate( long status )
{
++callsNum;
_exit( status );
}
void POSIXPlatform::suicide()
{
++callsNum;
kill( getpid(), SIGABRT );
while( true )
{}
}
void POSIXPlatform::attachDebugger()
{
int result;
const char *errorMessage;
boolean MelTheProgrammer;
++callsNum;
errorMessage = "Fork failed in attempt to attach debugger";
result = fork();
if( result == 0 )
{
char *pidBuffer;
pidBuffer = new char[ ( 3 * sizeof( pid_t ) ) + 1 ];
this -> sprintf( pidBuffer, "%i", getppid() );
result = execlp
( DEBUGGER_COMMAND, DEBUGGER_COMMAND,
getImageName(), pidBuffer, NULL );
delete []pidBuffer;
errorMessage = "Exec failed in attempt to attach debugger";
}
if( result < 0 )
{
rawErrorPrintf( errorMessage );
suicide();
}
MelTheProgrammer = true;
while( MelTheProgrammer )
{}
}
void POSIXPlatform::setImageName( const char *aName )
{
preC_( aName != NULL );
imageName = aName;
}
const char *POSIXPlatform::getImageName() const
{
return imageName;
}
char *POSIXPlatform::getTimeStampString()
{
static time_t instant;
static char buffer[ 30 ];
if( time( &instant ) == -1 )
{
strcpy( buffer, "UNKNOWN TIME" );
}
else
{
static struct tm *t;
t = localtime( &instant );
sprintf( buffer, "%4.4i/%2.2i/%2.2i %2.2i:%2.2i:%2.2i",
t -> tm_year + 1900, t -> tm_mon + 1,
t -> tm_mday, t -> tm_hour, t -> tm_min,
t -> tm_sec );
}
return buffer;
}
void *POSIXPlatform::allocateMemoryBootstrapPOSIX( Index aSize )
{
return allocateMemoryBootstrapANSI( aSize );
}
void POSIXPlatform::deallocateMemoryBootstrapPOSIX( void *aPointer )
{
deallocateMemoryBootstrapANSI( aPointer );
}
boolean POSIXPlatform::equals( const Top *anOther ) const
{
return( DCAST( anOther, POSIXPlatform ) == this );
}
Top *POSIXPlatform::clone() const
{
return ( Top * ) this;
}
String POSIXPlatform::toString() const
{
return Object::toString() + " calls: " + String( callsNum );
}
String POSIXPlatform::getClassName() const
{
return "POSIXPlatform";
}
#if defined(_INLINE)
#include "../src/Debug.ipp"
#include "../src/String.ipp"
#endif