home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 11 Util
/
11-Util.zip
/
TIMEXSRC.ZIP
/
BA_MISC.C
< prev
next >
Wrap
C/C++ Source or Header
|
1990-03-29
|
9KB
|
357 lines
/* ba_misc.c -- Miscellaneous routines for TIMEXBA
February 1990 Mark E. Mallett, Personal Workstation Magazine
This file contains routines for TIMEXBA that don't fit into the
other modules, classification-wise.
Included are the following routines:
err_report Error report routine for errhand stuff
get_sem Access a resource via a semaphore
log Log a message
log_begin Begin logging
log_end End logging
rel_sem Release a semaphore
set_priority Set thread priority
*/
#define INCL_BASE
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
#include <os2.h>
#include "timex.h"
#include "ba_timex.h"
/* Local definitions */
/* External data referenced */
extern char *Logfname; /* Logfile name */
/* External routines used */
extern void bufdtm( char *bufP, time_t timeval );
/* Local data publicly available */
/* Local routines and forward declarations */
void log_begin( void );
void log_end( void );
/* Private data */
static int LogC; /* Log group count */
static HFILE LogH; /* Logfile handle */
static HFILE Stdinsave; /* Saved stdin */
static HFILE Stdoutsave; /* Saved stdout */
static HFILE Stderrsave; /* Saved stderr */
/*
*//* get_sem( facP, itemP, sem )
Access a semaphore
Accepts :
facP Name of facility making the request
itemP Descriptive name of the semaphore
sem Semaphore to acquire
Returns :
*/
void
get_sem(
char *facP, /* Facility name */
char *itemP, /* Semaphore description */
HSEM sem /* Semaphore to acquire */
) {
int status;
/* Attempt to open the semaphore */
status = DosSemWait( sem, (long) -1 );
if ( status != 0 )
error( EC_SEMAPHORE, "get_sem() can't access %s semaphore for %s",
itemP, facP );
}
/*
*//* rel_sem( facP, itemP, sem )
Release a semaphore
Accepts :
facP Name of facility making the request
itemP Descriptive name of the semaphore
sem Semaphore to release
Returns :
*/
void
rel_sem(
char *facP, /* Facility name */
char *itemP, /* Semaphore description */
HSEM sem /* Semaphore to release */
) {
int status;
status = DosSemClear( sem );
if ( status != 0 )
error( EC_SEMAPHORE, "rel_sem() can't release %s semaphore for %s",
itemP, facP );
}
/*
*//* set_priority( tid, class, value )
Set a thread priority
Accepts :
tid Thread ID (0 for self )
class New priority class
value New priority value
Returns :
*/
void
set_priority(
TID tid, /* Thread ID */
int class, /* New priority class */
int value /* New priority value */
) {
int status;
/* Since set priority can only be done via a relative adjustment,
to set an absolute priority we first have to set to the
minimum, then offset that by the desired priority */
DosSetPrty( PRTYS_THREAD, class, -31, tid );
status = DosSetPrty( PRTYS_THREAD, class, value, tid );
if ( status != 0 )
warning( EC_NOTOK, "Can't set priority to class=%d value=%d",
class, value );
}
/*
*//* err_report( cond, msgP )
Print an error message on behalf of intercept_error.
Accepts :
code Error code reported
msgP Error message given.
Returns :
0
*/
void
err_report(
int code, /* Error code */
char *msgP /* Message text */
) {
log( "timex(%d): %s", code, msgP );
}
/*
*//* log( fmtP, args... )
Format a message to the log file
Accepts :
fmtP printf-style format string
args printf arguments. Quantity is limited.
Returns :
Nothing. Formats the string and writes to the log file
Notes :
Handling of the arguments is kludgy.
*/
void
log(
char *fmtP, /* message format string */
int a1,
int a2,
int a3,
int a4,
int a5,
int a6,
int a7,
int a8,
int a9,
int a10
) {
USHORT len;
char *strP; /* String ptr */
char buf[200]; /* Message buffer */
strP = &buf[0];
bufdtm( strP, time( (time_t *)NULL ) );
strP += strlen( strP );
*strP++ = ':';
*strP++ = ' ';
sprintf( strP, fmtP, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10 );
log_begin();
if ( LogH != 0xffff ) {
/* Append CRLF to the string */
strP = &buf[ strlen( &buf[0] ) ];
*strP++ = '\r';
*strP++ = '\n';
*strP = NUL;
DosWrite( LogH, &buf[0], strlen( &buf[0] ), &len );
}
else
fprintf( stderr, "%s\n", &buf[0] );
log_end();
}
/*
*//* log_begin(), log_end()
Begin and end a group of logging
Accepts :
Returns :
LogFP Set or not set depending on open status
Notes :
The log_begin() / log_end() sequence is used to keep the log file
open while a bunch of log writes is being made. This avoids the
overhead of opening the file for each batch of writes. It also
helps generalize the log open/close procedure. begin/end sequences
can be nested.
log_begin() also points stdout and stderr to the log file, and
closes stdin. This is done for the benefit of exec'ed programs,
which will point to the log file by default. log_end() resets
the handles to their prior state.
*/
void
log_begin( void ) {
USHORT status;
USHORT action;
ULONG fileptr;
HFILE dupH;
/* Protect this code section... */
DosEnterCritSec();
if ( ++LogC == 1 ) {
/* Try to open the logfile */
status = DosOpen( Logfname, &LogH, &action, 0L, 0,
FILE_CREATE|FILE_OPEN,
OPEN_ACCESS_WRITEONLY|OPEN_SHARE_DENYNONE|
OPEN_FLAGS_WRITE_THROUGH,
0L );
if ( status != 0 )
LogH = 0xffff;
else {
/* Make sure stdout and stderr are written */
fflush( stdout );
fflush( stderr );
/* Remember stdin, stdout, stderr handles */
Stdinsave = 0xffff;
DosDupHandle( 0, &Stdinsave );
Stdoutsave = 0xffff;
DosDupHandle( 1, &Stdoutsave );
DosClose( 1 );
dupH = 1;
DosDupHandle( LogH, &dupH );
Stderrsave = 0xffff;
DosDupHandle( 2, &Stderrsave );
DosClose( 2 );
dupH = 2;
DosDupHandle( LogH, &dupH );
/* Close stdin now that it can't be taken over */
DosClose( 0 );
}
}
/* Set to the end of the logfile if it's open */
if ( LogH != 0xffff )
DosChgFilePtr( LogH, 0L, FILE_END, &fileptr );
DosExitCritSec();
}
void
log_end( void ) {
HFILE dupH; /* For duplicating handles */
DosEnterCritSec(); /* Protect this section */
if ( --LogC == 0 ) {
if ( LogH != 0xffff ) {
/* Reset stdin et al */
dupH = 0;
DosDupHandle( Stdinsave, &dupH );
DosClose( Stdinsave );
DosClose( 1 );
dupH = 1;
DosDupHandle( Stdoutsave, &dupH );
DosClose( Stdoutsave );
DosClose( 2 );
dupH = 2;
DosDupHandle( Stderrsave, &dupH );
DosClose( Stderrsave );
DosClose( LogH );
LogH = 0xffff;
}
}
DosExitCritSec();
}