home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 11 Util
/
11-Util.zip
/
TIMEXSRC.ZIP
/
MISC.C
< prev
next >
Wrap
Text File
|
1990-03-29
|
13KB
|
678 lines
/* misc.c -- Miscellaneous routines
February 1990 Mark E. Mallett, Personal Workstation Magazine
This file contains misc routines for the TIMEX program. Most, if not all,
were lifted from code in previous columns.
Included are:
atonum Convert ASCII to LONG number
emalloc malloc() with error message
erealloc realloc() with error message.
error Handle error message
getline Input a line of text from a file.
gettkline Input a line of text && tokenize it
nextonum Convert ASCII hex to LONG number
prtdat Print date
prtdtm Print date and time
prttim Print time
prtndat Print date as a number string
prtndtm Print date and time as a number string
prtntim Print time as a number string
warning Handle warning message.
*/
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <sys/types.h>
#include <ctype.h>
#include <time.h>
#include <os2.h>
#include "timex.h"
/* Local Definitions */
#ifndef NUL
#define NUL '\0'
#endif /* NUL */
/* External data referenced */
/* External routines used */
extern struct tm *xlocaltime( time_t * );
/* Local data publicly available */
/* Local routines and forward references */
LONG atonum( char *bufP );
void bufdat( char *bufP, time_t timeval );
void bufdtm( char *bufP, time_t timeval );
void buftim( char *bufP, time_t timeval );
int getline( FILE *fP, char *bufP, int bufsize );
int gettkline( FILE *fP, char *bufP, int bufsize,
int *tokCP, char **tokV, int tokmax );
LONG hextonum( char *bufP );
void prtdat( FILE *fP, time_t timeval );
void prtdtm( FILE *fP, time_t timeval );
void prtndat( FILE *fP, time_t timeval );
void prtndtm( FILE *fP, time_t timeval );
void prtntim( FILE *fP, time_t timeval );
void prttim( FILE *fP, time_t timeval );
/* Private data */
static char Errbuf[100]; /* Scratch buffer for errors */
static char *Months[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
/*
*//* atonum( bufP )
Convert ASCII string to a LONG number
Accepts :
bufP Ptr to string
Returns :
< value > The number
Notes :
knows about prefixes and suffixes, particularly K.
*/
LONG
atonum( bufP )
char *bufP; /* The string */
{
char ch; /* Next char */
int neg; /* Negative */
LONGINT val; /* Accumulated value */
/* Check +/- */
neg = FALSE;
if ( *bufP == '+' )
++bufP;
else if ( *bufP == '-' ) {
neg = TRUE;
++bufP;
}
/* Accumulate the value */
for( val = 0L; ( ch = *bufP++ ) != NUL; ) {
if ( !isdigit( ch ) )
break;
val = ( val * 10 ) + ( ch - '0' );
}
/* Check suffix */
if ( ( ch == 'K' ) || ( ch == 'k' ) )
val = val * 1024;
/* Apply sign if negative */
if ( neg )
val = val;
return( val );
}
/*
*//* hextonum( bufP )
Convert ASCII hex string to a LONG number
Accepts :
bufP Ptr to string
Returns :
< value > The number
*/
LONG
hextonum( bufP )
char *bufP; /* The string */
{
char ch; /* Next char */
int neg; /* Negative */
LONGINT val; /* Accumulated value */
/* Check +/- */
neg = FALSE;
if ( *bufP == '+' )
++bufP;
else if ( *bufP == '-' ) {
neg = TRUE;
++bufP;
}
/* Accumulate the value */
for( val = 0L; ( ch = *bufP++ ) != NUL; ) {
if ( ( ch >= '0' ) && ( ch <= '9' ) )
ch -= '0';
else if ( ( ch >= 'a' ) && ( ch <= 'f' ) )
ch = ( ch - 'a' ) + 10;
else if ( ( ch >= 'A' ) && ( ch <= 'F' ) )
ch = ( ch - 'A' ) + 10;
else
break;
val = ( val * 16 ) + ch;
}
/* Apply sign if negative */
if ( neg )
val = val;
return( val );
}
/*
*//* emalloc( facP, nameP, size )
Allocate memory, give error if failure.
Accepts :
facP Name of facility (e.g., subroutine name)
nameP Name of item being allocated
size Desired size
Returns :
< value > Ptr to thing.
Notes :
error return is taken if allocation fails.
*/
void *
emalloc( facP, nameP, size )
char *facP; /* Ptr to facility name */
char *nameP; /* Ptr to name of thing */
int size; /* Number of bytes */
{
char *newP; /* Ptr to memory */
if ( ( newP = malloc( size ) ) == NULL )
error( EC_MEMORY, "%s: Error allocating %d bytes for %s",
facP, size, nameP );
return( (void *)newP );
}
/*
*//* erealloc( facP, nameP, oldP, size )
Reallocate memory, give error if failure.
Accepts :
facP Name of facility (e.g., subroutine name)
nameP Name of item being allocated
oldP Ptr to old thing (may be NULL)
size Desired size
Returns :
< value > Ptr to thing.
Notes :
error return is taken if allocation fails.
*/
void *
erealloc( facP, nameP, oldP, size )
char *facP; /* Ptr to facility name */
char *nameP; /* Ptr to name of thing */
char *oldP; /* Ptr to existing thing */
int size; /* Number of bytes */
{
char *newP; /* Ptr to memory */
if ( oldP == NULL )
newP = malloc( size );
else
newP = realloc( oldP, size );
if ( newP == NULL )
error( EC_MEMORY, "%s: Error re-allocating %d bytes for %s",
facP, size, nameP );
return( (void *)newP );
}
/*
*//* gettkline( fP, bufP, bufsize, tokcP, tokv, tokmax )
Input a line of text and tokenize it.
Accepts :
fP Stream for input file
bufP Buffer for line
bufsize Size of the buffer
tokcP Ptr to tokc variable -- # of tokens
tokv Ptr to tokv array -- ptrs to tokens
tokmax Maximum number of tokens to return
Returns :
<value> Number of characters read (-1 if EOF)
*tokcP Number of tokens received
*tokv Ptrs to tokens
Notes :
Excess input (up to a newline) is ignored.
The line buffer is used for the token buffer as well.
*/
int
gettkline( fP, bufP, bufsize, tokcP, tokv, tokmax )
FILE *fP; /* Input file ptr */
char *bufP; /* Buffer ptr */
int bufsize; /* Room in buffer */
int *tokcP; /* Variable to hold token count */
char **tokv; /* Arg vectors */
int tokmax; /* Max # tokens */
{
int cC; /* Characters */
/* Input the line */
cC = getline( fP, ++bufP, bufsize-1 );
/* Use tkline() to tokenize it */
*tokcP = tkline( bufP, bufP-1, tokv, tokmax, "", " \t,;:" );
return( cC );
}
/*
*//* getline( fP, bufP, bufsize )
Inputs a line of text from a file.
Accepts :
fP Stream for input file
bufP Buffer for line
bufsize Size of the buffer
Returns :
<value> Number of characters read (-1 if EOF)
Notes :
Excess input (up to a newline) is ignored.
*/
getline( fP, bufP, bufsize )
FILE *fP; /* Input file ptr */
char *bufP; /* Buffer ptr */
int bufsize; /* Room in buffer */
{
int cC; /* Characters */
int ch; /* Character */
for( --bufsize, cC = 0; ( ( ch = getc( fP ) ) != '\n' ) && ( ch != EOF ); )
if ( cC < bufsize )
bufP[cC++] = (char)ch;
bufP[cC] = NUL;
if ( ( cC == 0 ) && ( ch == EOF ) )
cC = -1;
return( cC );
}
/*
*//* bufdtm( bufP, timeval )
Print date and time of a time value into a buffer
Accepts :
bufP Destination buffer
timeval Time value
Returns :
<nothing>
*/
void
bufdtm( bufP, timeval )
char *bufP; /* Destination buffer */
time_t timeval; /* The time */
{
bufdat( bufP, timeval );
bufP += strlen( bufP );
*bufP++ = ' ';
buftim( bufP, timeval );
}
/*
*//* bufdat( bufP, timeval )
Print date portion of a time value into a buffer
Accepts :
bufP Destination buffer
timeval Time value
Returns :
<nothing>
*/
void
bufdat( bufP, timeval )
char *bufP; /* Buffer ptr */
time_t timeval; /* The time */
{
struct tm *tmP; /* Time components struct */
tmP = xlocaltime( &timeval );
sprintf( bufP, "%02d-%s-%02d", tmP->tm_mday, Months[tmP->tm_mon],
tmP->tm_year + 1900 );
}
/*
*//* buftim( bufP, timeval )
Print time portion of a time value into a buffer
Accepts :
bufP Destination buffer
timeval Time value
Returns :
<nothing>
*/
void
buftim( bufP, timeval )
char *bufP; /* Destination buffer */
time_t timeval; /* The time */
{
struct tm *tmP; /* Time components struct */
tmP = xlocaltime( &timeval );
sprintf( bufP, "%02d:%02d:%02d", tmP->tm_hour, tmP->tm_min,
tmP->tm_sec );
}
/*
*//* prtdtm( fP, timeval )
Print date and time of a time value
Accepts :
fP FILE block ptr
timeval Time value
Returns :
<nothing>
*/
void
prtdtm( fP, timeval )
FILE *fP; /* File to print to */
time_t timeval; /* The time */
{
prtdat( fP, timeval );
fprintf( fP, " " );
prttim( fP, timeval );
}
/*
*//* prtdat( fP, timeval )
Print date portion of a time value
Accepts :
fP FILE block ptr
timeval Time value
Returns :
<nothing>
*/
void
prtdat( fP, timeval )
FILE *fP; /* File to print to */
time_t timeval; /* The time */
{
struct tm *tmP; /* Time components struct */
tmP = xlocaltime( &timeval );
fprintf( fP, "%02d-%s-%02d", tmP->tm_mday, Months[tmP->tm_mon],
tmP->tm_year + 1900 );
}
/*
*//* prttim( fP, timeval )
Print time portion of a time value
Accepts :
fP FILE block ptr
timeval Time value
Returns :
<nothing>
*/
void
prttim( fP, timeval )
FILE *fP; /* File to print to */
time_t timeval; /* The time */
{
struct tm *tmP; /* Time components struct */
tmP = xlocaltime( &timeval );
fprintf( fP, "%02d:%02d:%02d", tmP->tm_hour, tmP->tm_min,
tmP->tm_sec );
}
/*
*//* prtndtm( fP, timeval )
Print date and time of a time value as a single number string
Accepts :
fP FILE block ptr
timeval Time value
Returns :
<nothing>
*/
void
prtndtm( fP, timeval )
FILE *fP; /* File to print to */
time_t timeval; /* The time */
{
prtndat( fP, timeval );
prtntim( fP, timeval );
}
/*
*//* prtndat( fP, timeval )
Print date portion of a time value as a single number string
Accepts :
fP FILE block ptr
timeval Time value
Returns :
<nothing>
Notes :
Prints the date as yyyymmdd format
*/
void
prtndat( fP, timeval )
FILE *fP; /* File to print to */
time_t timeval; /* The time */
{
struct tm *tmP; /* Time components struct */
tmP = xlocaltime( &timeval );
fprintf( fP, "%04d%02d%02d", tmP->tm_year + 1900,
tmP->tm_mon +1,
tmP->tm_mday );
}
/*
*//* prtntim( fP, timeval )
Print time portion of a time value as a digit string
Accepts :
fP FILE block ptr
timeval Time value
Returns :
<nothing>
Notes :
Prints the time in hhmmss format
*/
void
prtntim( fP, timeval )
FILE *fP; /* File to print to */
time_t timeval; /* The time */
{
struct tm *tmP; /* Time components struct */
tmP = xlocaltime( &timeval );
fprintf( fP, "%02d%02d%02d", tmP->tm_hour, tmP->tm_min,
tmP->tm_sec );
}
/*
*//* error( code, fmtP, args... )
One-up interface to return_error
Accepts :
code Status code
fmtP printf-style format string
args printf arguments. Quantity is limited.
Returns :
Nothing. Formats the string and calls return_error.
Notes :
Handling of the arguments is kludgy.
*/
void
error( code, fmtP, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10 )
int code; /* Status code */
char *fmtP; /* message format string */
int a1, a2, a3, /* Like I said... */
a4, a5, a6, /* ... it's a kludge. */
a7, a8, a9, a10;
{
sprintf( &Errbuf[0], fmtP, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10 );
return_error( code, &Errbuf[0] );
}
/*
*//* warning( code, fmtP, args... )
Issue a warning (a la error(), but using note_error)
Accepts :
code Status code
fmtP printf-style format string
args printf arguments. Quantity is limited.
Returns :
Nothing; calls note_error and returns.
Notes :
Handling of the arguments is kludgy.
*/
void
warning( code, fmtP, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10 )
int code; /* Status code */
char *fmtP; /* message format string */
int a1, a2, a3, /* Like I said... */
a4, a5, a6, /* ... it's a kludge. */
a7, a8, a9, a10;
{
sprintf( &Errbuf[0], fmtP, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10 );
note_error( code, &Errbuf[0] );
}