/*
 *      $Id: werror.c,v 1.1 1997/01/03 19:42:47 james Exp $
 *      $log$
 */

//static char rcsid[]="$Id: werror.c,v 1.1 1997/01/03 19:42:47 james Exp $";

#include <stdio.h>
#include <fcntl.h>


#ifdef __STDC__
#       include <stdarg.h>
#else
#       include <varargs.h>
#endif


/*** own includes ***/
#include "wtrace.h"

/*** global defines ***/

//#ifdef unix
#       include "process.h" /* getpid() */
#       define  GETPID  getpid()
//#else
//#       include "process.h" /* getpid() */
//#       define  GETPID  _getpid()
//#endif


/* prototyping */

#ifdef __STDC__
#       ifdef unix
                static int      _lockFile( int fh );
                static int      _unlockFile( int fh );
#       endif /* unix */
#else
#       ifdef unix
                static int      _lockFile();
                static int      _unlockFile();
#       endif /* unix */
#endif

/******************/
/*** ErrRecover ***/
/******************/
/*
*/
int
#ifdef __STDC__
        ErrRecover( char* func1, char* func2, char *formatString, ...)
#else
        ErrRecover(va_alist)
        va_dcl
#endif

{
        va_list                 marker;
        static FILE*    fh=NULL;

#ifdef unix
        int                             fd;
#endif /* unix */


#ifdef __STDC__
        va_start(marker, formatString);
#else
        char*   func1;
        char*   func2;
        char*   formatString;

        va_start( marker );
        func1 = va_arg( marker, char* );
        func2 = va_arg( marker, char* );
        formatString = va_arg( marker, char* );
#endif

        if( formatString == NULL )
                return 1;

#ifdef unix
        if ( fh == NULL ) /* if it is not open, open trace-file */
                if( ( fd = open( "t.log", O_WRONLY | O_CREAT | O_APPEND , 0664 ) ) < 0 )
                        return 1;
                else
                        if( ( fh = fdopen( fd, "at" ) ) < 0 )   /* get FILE *fh        */
                                return 1;                                                       /* from filedescriptor */
#else /* end unix */
        if ( fh == NULL ) /* if it is not open, open trace-file */
                if( ( fh = fopen( "t.log", "at" ) ) == NULL )
                        return 1;
#endif


#ifdef unix
        _lockFile( fd );
#endif /* unix */

        fprintf( fh, "%d ", GETPID );
        fprintf( fh, "%s\t%s\t", func1, func2 );
        vfprintf( fh, formatString,  marker );
        fprintf( fh, "\n" );

#ifdef unix
        _unlockFile( fd );
#endif /* unix */

        va_end( marker );

        fclose( fh );
        fh = NULL;
 return 0;
}

/********************/
/*** ErrorRecover ***/
/********************/
/*
*/
int
#ifdef __STDC__
        ErrorRecover( char* func1, char* func2, int e, char *formatString, ...)
#else
        ErrorRecover(va_alist)
        va_dcl
#endif

{
        va_list                 marker;
        static FILE*    fh=NULL;

#ifdef unix
        int                             fd;
#endif /* unix */


#ifdef __STDC__
        va_start(marker, formatString);
#else
        char*   func1;
        char*   func2;
        int             e;
        char*   formatString;

        va_start( marker );
        func1 = va_arg( marker, char* );
        func2 = va_arg( marker, char* );
        formatString = va_arg( marker, char* );
#endif

        if( formatString == NULL )
                return 1;

#ifdef unix
        if ( fh == NULL ) /* if it is not open, open trace-file */
                if( ( fd = open( "t.log", O_WRONLY | O_CREAT | O_APPEND , 0664 ) ) < 0 )
                        return 1;
                else
                        if( ( fh = fdopen( fd, "at" ) ) < 0 )   /* get FILE *fh        */
                                return 1;                                                       /* from filedescriptor */
#else /* end unix */
        if ( fh == NULL ) /* if it is not open, open trace-file */
                if( ( fh = fopen( "t.log", "at" ) ) == NULL )
                        return 1;
#endif


#ifdef unix
        _lockFile( fd );
#endif /* unix */

        fprintf( fh, "%d ", GETPID );
        fprintf( fh, "%s\t%s\t%d\t", func1, func2, e );
        vfprintf( fh, formatString,  marker );
        fprintf( fh, "\n" );

#ifdef unix
        _unlockFile( fd );
#endif /* unix */

        va_end( marker );

        fclose( fh );
        fh = NULL;
 return 0;
}

#ifdef unix

/*****************/
/*** _lockFile ***/
/*****************/
/*
        sperrt eine Datei fuer andere Prozesse
*/
static int
#ifdef __STDC__
        _lockFile( int fh )
#else
        _lockFile( fh )
        int fh;
#endif
{
        struct flock lock;
        lock.l_type=F_WRLCK;
        lock.l_start=0;                         /* lock the whole file */
        lock.l_whence=SEEK_SET;         /* from the beginning  */
        lock.l_len=0;                           /* to the end          */
        return (fcntl(fh, F_SETLKW, &lock));
}


/*******************/
/*** _unlockFile ***/
/*******************/
/*
        hebt die Dateisperre wieder auf
*/
static int
#ifdef __STDC__
        _unlockFile( int fh )
#else
        _unlockFile( fh )
        int fh;
#endif
{
        struct flock lock;
        lock.l_type=F_UNLCK;
        lock.l_start=0;
        lock.l_whence=SEEK_SET;
        lock.l_len=0;
        return (fcntl(fh, F_SETLKW, &lock));
}

#endif /* unix */
