#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/*
String[]: buffer to store input stream
MaxChar: maximum size of the buffer
return 0 if EOF reached
*/
int ReadStringFromSTDIN ( char String[], int MaxChar )
{
int idx;
int ch;
// retrive characaters from STDIN stream //
for ( idx = 0 ; ( ch = fgetc ( stdin ) ) != EOF ; idx++ )
{
if ( idx < ( MaxChar - 1 ) ) String[idx] = (char)ch;
if ( ch == 0 ) break;
}
String[idx] = 0;
return ( ch == EOF ) ? 1 : 0;
}
/*
Flush a string from STDIN
*/
void FlushSTDIN ()
{
int ch;
while ( ( ch = fgetc ( stdin ) ) != EOF ) if ( ch == 0 ) break;
}
void main ( int argc, char* argv[] )
{
char FromUser[256], FromHost[256], ToUser[256], ToHost[256];
FILE *fpLog;
struct tm *newtime;
time_t aclock;
/* Read fromuser@fromhost */
ReadStringFromSTDIN ( FromUser, 256 );
ReadStringFromSTDIN ( FromHost, 256 );
/* Read touser@tohost */
ReadStringFromSTDIN ( ToUser, 256 );
ReadStringFromSTDIN ( ToHost, 256 );
/* Skip the body of the message */
FlushSTDIN ();
/* keep into a log file */
fpLog = fopen ( argv[1], "a+t" );
if ( !fpLog ) return;
fseek ( fpLog, 0, SEEK_END );
/* get current time */
time( &aclock );
newtime = localtime( &aclock );
/* write to file */
fprintf ( fpLog, "From: <%s@%s> To: <%s@%s> At %s",
FromUser, FromHost, ToUser, ToHost, asctime ( newtime ) );
fflush ( fpLog );
fclose ( fpLog );
}
|