📄 timerdos.c
字号:
/*
* TIMERDOS.C - Timer implementation dor DOS.
*
*
* PROGRAMMER: Martti Ylikoski
* CREATED: 21.3.1992
*/
static char *VERSION = "Version 1.0. Copyright (c) Martti Ylikosi, 1992" ;
/*
*/
static char *progname ;
static char *deffile = "C:\\timersrv.dat" ;
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <io.h>
#define TRUE 1
#define FALSE 0
#define MAXTIMERLEN 25
typedef struct {
char timer[MAXTIMERLEN] ;
time_t dt ;
} _START_TMR ;
_START_TMR start_tmr ;
static int push_timer( char *timer ) ;
static int pop_timer( char *timer ) ;
static int query_timers( void ) ;
int main(int argc, char *argv[])
{
char *tmp ;
progname = argv[0] ;
if ( (tmp = getenv("TIMERSERV")) != NULL)
deffile = tmp ;
if (argc == 1 )
{
query_timers() ;
return( 0 ) ;
}
if (argc == 2 && (strcmpi(argv[1], "-q") == 0
|| strcmpi(argv[1], "/q") == 0))
{
unlink(deffile) ;
// send_queue("\\queues\\timerpop", " ", 2, 1) ; /* send EXIT command */
printf("Timer server removed. \n") ;
return( 0 ) ;
}
if (argc == 2 || argc >3 )
{
puts("TIMER - start and stop timers.") ;
puts("Copyright (C) Martti Ylikoski, 1992.") ;
puts("Usage: timer [/start timernum | /stop timernum | -Q ]") ;
puts(" /start will start a timer of the specified number.") ;
puts(" /stop will stop it and output the time the timer has counted.") ;
puts(" /q will stop the timer server") ;
return( 1 ) ;
}
if (strcmpi(argv[1], "/START") == 0
|| strcmpi(argv[1], "-START") == 0 )
{
push_timer(argv[2]) ;
}
else if (strcmpi(argv[1], "/STOP") == 0
|| strcmpi(argv[1], "-STOP") == 0 )
{
pop_timer(argv[2]) ;
}
return( 0 ) ;
}
static int push_timer( char *timer )
{
FILE *fp ;
if ((fp = fopen(deffile, "ab")) == NULL)
{
fprintf(stderr, "%s: error opening file %s...\nExiting...\n", progname, deffile) ;
return( 1 ) ;
}
strcpy(start_tmr.timer, timer) ;
time(&start_tmr.dt) ;
fwrite(&start_tmr, sizeof(start_tmr), 1, fp ) ;
fclose(fp) ;
printf("Timer %s started\n", timer) ;
return( 0 ) ;
}
static int pop_timer( char *timer )
{
int found, output ;
FILE *fp, *fptmp ;
time_t new_time ;
int hours, minutes,seconds ;
char *tmpfile ;
found = FALSE ;
output = FALSE ;
if ((fp = fopen(deffile, "rb")) == NULL )
{
fprintf(stderr, "%s: error opening file %s...\nExiting...\n", progname, deffile) ;
return( 1 ) ;
}
tmpfile = mktemp("aaXXXXXX") ;
if ((fptmp = fopen(tmpfile, "wb")) == NULL )
{
fprintf(stderr, "%s: error opening file %s...\nExiting...\n", progname, tmpfile) ;
return( 1 ) ;
}
while (fread(&start_tmr, sizeof(start_tmr), 1, fp) != 0)
{
if(strnicmp(start_tmr.timer, timer, strlen(timer)) == 0)
{
found = TRUE ;
time (&new_time) ;
new_time -= start_tmr.dt ; /* difference in seconds */
printf("TIMER %s \n", start_tmr.timer) ;
hours = 0 ; minutes = 0 ; seconds = 0 ;
hours = new_time /3600 ;
minutes = (new_time - hours *3600)/60 ;
seconds = (new_time - hours*3600 - minutes*60) ;
if (hours != 0)
{
output = TRUE ;
printf("%d hours, ", hours) ;
}
if (minutes != 0 || output == TRUE)
{
output = TRUE ;
printf("%d minutes, ", minutes ) ;
}
if (seconds != 0 || output == TRUE)
{
output = TRUE ;
printf("%d seconds. ", seconds ) ;
}
}
else
fwrite(&start_tmr, sizeof(start_tmr), 1, fptmp ) ;
}
if (found == FALSE)
printf("Timer %s was not started.\n", timer) ;
fclose(fp) ;
fclose(fptmp) ;
unlink(deffile) ;
rename(tmpfile, deffile) ;
return( 0 ) ;
}
static int query_timers( void )
{
int found ;
FILE *fp ;
found = FALSE ;
printf("TIMERS for DOS\n") ;
if ((fp = fopen(deffile, "rb")) == NULL)
{
fprintf(stderr, "%s: error opening file %s...\nExiting...\n", progname, deffile) ;
return( 1 ) ;
}
while (fread(&start_tmr, sizeof(start_tmr), 1, fp) != 0)
{
found = TRUE ;
printf("Timer %s\n", start_tmr.timer) ;
}
if (found == FALSE)
printf("There are no timers set.\n") ;
fclose(fp) ;
return( 0 ) ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -