[mcountd] bug fixes
Brought to you by:
kms
|
From: Joe B. <rbe...@Pi...> - 2000-01-16 13:58:42
|
In the sourceforge web page, in the section
'About Mcountd-general'
-- 'is a very simple porgram'
++ 'is a very simple program'
The next set of bugs deal with the .c file.
The most serious bug is the use of the variable
timeleft ( declared in main() ). The source had
had two lines that referred to the address of
timeleft. I think one started with
sprintf( &timeleft, . . .
and the other with
print_number( &timeleft );
The problem here is that timeleft is declared as
char * timeleft;
sprintf and print_number require a 'char *'.
Taking the address of a 'char *' results in a
'char **'. This is not good. timeleft starts out
pointing to some random location in memory. The
sprintf overwrites the value of timeleft and any
nearby local variables.
Some other things I fixed were making print_number
a little clearer and faster, taking out some
superfluous code at the end of the options parsing,
using usleep(3) so that the display wouldnt miss
seconds, simplifying some of the time computations,
changing the display format so each field ( hours,
minutes, seconds ) displays with two digits, and
removing the redundant parentheses from the main
return ( ugh, modula2 ).
I am appending the modified files. Everything I
changed I marked with a '//'. You can remove the
'//' once you verify the changes.
### from mcountd.h
#define CHARACTER_IMAGE_HEIGHT 13
char *colon_pointer[ CHARACTER_IMAGE_HEIGHT ]=
. . .
char *number_pointer[ '9'-'0'+1 ][ CHARACTER_IMAGE_HEIGHT ]={
. . .
### from mcountd.c
/*
* mcountd.c - millennium count down
* version 0.3
*
* Copyright (c) by Karl-Martin Skontorp <ksk...@cs...>
* Martin Dahl <da...@vf...>
* Anders Hermansen <ahe...@vf...>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <unistd.h> //
#include "mcountd.h"
int main(int argc, char *argv[]);
void print_number(char *nmbr );
void show_params();
void show_version();
int main(int argc, char **argv)
{
/* Time for January 1, 2000 00:00:00 */
long targettime = 946681200;
long curtime;
long difftime;
long last_time_printed;
int rollover = 0;
int dayss;
int hours;
int mins;
int secs;
int filler_lines; //
char timeleft[ 128 ]; //
int a;
#ifdef i86
int i;
#endif
/* Parse the command line */
if (argc!= 1)
{
for (a=1; a<argc; a++)
{
if (argv[a][0]!='-')
show_params();
else /* -* */
{
if (argv[a][1]=='t') /* -t targettime */
{
a++;
targettime = atol(argv[a]);
} else
{
if (argv[a][1]=='u') /* -u updaterate */
{
a++;
updaterate = atol(argv[a]);
} else
{
if (argv[a][1]=='h')
{
show_params();
} else
{
if (argv[a][1]=='v')
{
show_version();
} else
{
if (argv[a][1]!=104)
{
printf("Unknown parameter.\n");
show_params();
}
}
}
}
}
}
}
}
//
last_time_printed = -1; //
while (1)
{
/* Compute times */
curtime = time(0);
if( last_time_printed != curtime ) //
{
rollover = targettime < curtime; //
difftime = abs( targettime - curtime ); //
dayss=difftime/86400;
hours=(difftime%86400)/3600;
mins=(difftime%3600)/60;
secs=difftime%60;
if (rollover)
{
printf("%i days since roll-over.\n\n", dayss);
} else
{
printf("%i days left.\n\n", dayss);
}
sprintf( timeleft, "%2.2i:%2.2i:%2.2i", hours,mins,secs); //
print_number( timeleft );
filler_lines = tlines - ( CHARACTER_IMAGE_HEIGHT + 2 );
while( filler_lines-- )
fputc( '\n', stdout );
last_time_printed = curtime;
}
#ifdef i386
usleep( 250 ); //
#endif
#ifdef i86
for (i = 0; i < c ; i++)
{
}
#endif
}
return 0; //
}
//
void print_number(char *nmbr )
{
unsigned y;
int c;
char * nmbr_index,
** image_source;
for( y = 0; y < CHARACTER_IMAGE_HEIGHT; y++ )
{
for( nmbr_index = nmbr; ( c = *nmbr_index++ ); )
{
image_source = isdigit( c )
? number_pointer[ c - '0' ]
: colon_pointer;
printf( "%s ", image_source[ y ] );
}
fputc( '\n', stdout );
}
}
void show_version(){
printf("mcountd - millennium count down, version 0.3\n");
printf("This is free software with ABSOLUTELY NO WARRANTY.\n");
printf("See included file COPYING for more information.\n");
exit(1);
}
void show_params() {
printf("mcountd - millennium count down, version 0.3\n");
printf(" by Karl-Martin Skontorp,\n");
printf(" Martin Dahl and Anders Hermansen.\n");
printf(" http://mcountd.sourceforge.net\n");
printf("\n");
printf("This is free software with ABSOLUTELY NO WARRANTY.\n");
printf("See included file COPYING for more information.\n");
printf("\n");
printf("options: -t Targettime for count down in seconds since\n");
printf(" 01.01.1970 00:00:00. Default: 01.01.2000 00:00:00.\n");
printf(" -u Seconds between update. Default: 1.\n");
printf(" -v Show version.\n");
printf(" -h Show this message.\n");
exit(1);
}
|