Category : Files from Magazines
Archive   : CGAZV5N5.ZIP
Filename : SETV.C

 
Output of file : SETV.C contained in archive : CGAZV5N5.ZIP
/*--- SETV.C ----------------------------- Listing 1 ----------
* Author: John Rex
* Compiler: Borland C++ 2.0, MSC 6.00a
* Purpose: Demonstrate use of setvbuf
* Usage: setv infile outfile [insize [outsize]]
* Method: Copies infile to outfile one character at a time,
* reporting elapsed time. Infile is set to use a
* buffer of insize bytes, while outfile uses an
* outsize bytes buffer.
*-----------------------------------------------------------*/

#include /* for timer function */
#include
#include
#include

#define DEF_BUF 512

#ifdef __TURBOC__ /* Borland */
#define get_clock_ticks(x) x=biostime(0,0L)
#else /* Microsoft */
#define get_clock_ticks(x) \
_bios_timeofday(_TIME_GETCLOCK, &x)
#endif

long copy ( char *infilename, char *outfilename,
size_t insize, size_t outsize )
{
int c;
long starttime, donetime;
FILE *infile, *outfile;


/* open input file and setup its buffer */
if (( infile = fopen ( infilename, "rb" )) == NULL) {
printf ( "Can't open %s\n", infilename );
exit ( 1 );
}

if ( setvbuf ( infile, NULL, _IOFBF, insize )) {
printf ( "Couldn't set infile buffer to %u bytes.\n",
insize );
exit ( 1 );
}


/* open output file and setup its buffer */
unlink ( outfilename );
if (( outfile = fopen ( outfilename, "wb" )) == NULL ) {
printf ( "Can't open %s\n", outfilename );
exit ( 1 );
}

if ( setvbuf ( outfile, NULL, _IOFBF, outsize )) {
printf ( "Couldn't set outfile buffer to %u bytes.\n",
outsize );
exit ( 1 );
}

/* do it */
get_clock_ticks(starttime); /* get timer value */
while (( c = fgetc ( infile )) != EOF )
fputc ( c, outfile );
get_clock_ticks(donetime);
fclose ( infile );
fclose ( outfile );

return donetime - starttime;
}


void main ( int argc, char **argv )
{
size_t insize, outsize;
int i;
long total, average, lo, hi, elapsed;

insize = outsize = DEF_BUF;

if ( argc < 3 || argc > 5 ) {
printf
( "Usage: setv infile outfile [insize [outsize]]\n" );
return;
}

/* get buffer sizes */
if ( argc > 3 )
insize = (unsigned) atoi ( argv[3] );

if ( argc > 4 )
outsize = (unsigned) atoi ( argv[4] );

/* now, copy the file five times */
total = hi = 0;
lo = LONG_MAX;
for ( i = 1; ; i++ ) {
elapsed = copy ( argv[1], argv[2], insize, outsize );
if ( elapsed > hi )
hi = elapsed;
if ( elapsed < lo )
lo = elapsed;
total += elapsed;

if ( total > 500 || i > 4 )
break;
}

average = total / i;

printf ( "Average of %4ld ticks (%4ld - %4ld). "
"Insize = %5u. Outsize = %5u.\n",
average, lo, hi, insize, outsize );
}

  3 Responses to “Category : Files from Magazines
Archive   : CGAZV5N5.ZIP
Filename : SETV.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/