📄 timer.c
字号:
/*
* TIMER.C - Testclient for message queues.
*
*
* PROGRAMMER: Martti Ylikoski
* CREATED: 3.4.1991
*/
static char *VERSION = "Version 1.1. Copyright (c) Martti Ylikoski, 1991" ;
/*
*/
static char *progname ;
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define INCL_DOSDATETIME
#define INCL_DOSINFOSEG
#define INCL_DOS
#include <os2.h>
#include "queue.h"
#define MAXTIMERLEN 25
typedef struct {
int sg ;
char timer[MAXTIMERLEN] ;
DATETIME dt ;
} _START_TMR ;
_START_TMR start_tmr ;
_START_TMR *pstart_tmr, stim ;
typedef struct aa {
int sg ;
char timer[MAXTIMERLEN] ;
} _STOP_TMR ;
_STOP_TMR stop_tmr ;
static int push_timer( char *timer ) ;
static int pop_timer( char *timer ) ;
static int query_timers( void ) ;
// int send_queue(char *queue_name, char *message, USHORT cbElement, USHORT usRequest ) ;
int main(int argc, char *argv[])
{
progname = argv[0] ;
if (argc == 1 )
{
query_timers() ;
return( 0 ) ;
}
if (argc == 2 && (strcmpi(argv[1], "-q") == 0
|| strcmpi(argv[1], "/q") == 0))
{
QueSend( "\\queues\\timerpop", " ", 2, 1, "START /C timersrv.exe", progname) ;
// 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, 1991.") ;
puts("Usage: timer [/start timername | /stop timername | -Q ]") ;
puts(" /start will start a timer of the specified name.") ;
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 )
{
USHORT ret ;
int i ;
USHORT cbPath;
SEL selPath;
SEL ginfo, linfo ;
LINFOSEG FAR *plis ;
if (( ret = DosGetInfoSeg(&ginfo, &linfo)) != 0)
{
printf("%s: error in DosGetInfoSeg", progname) ;
return(1) ;
}
plis = MAKEPLINFOSEG(linfo) ;
start_tmr.sg = plis->sgCurrent ;
if ((ret = DosAllocSeg(sizeof(start_tmr), &selPath, SEG_GIVEABLE)) != 0 ) /* allocates memory */
{
fprintf(stderr, "%s: error in DosAllocSeg...\nExiting...\n", progname) ;
return( 1 ) ;
}
strcpy(start_tmr.timer, timer) ;
DosGetDateTime(&start_tmr.dt) ;
QueSend( "\\queues\\timerpsh", (char *) &start_tmr, sizeof(start_tmr), 0, "START /C timersrv.exe", progname) ;
// send_queue("\\queues\\timerpsh", (char *) &start_tmr, sizeof(start_tmr), 0) ;
printf("Timer %s started\n", timer) ;
}
static int pop_timer( char *timer )
{
HQUEUE hpopresp ; /* queuehandle */
USHORT ret ;
int i ;
QUEUERESULT qres ;
PVOID pmesg ;
USHORT cbElement ;
BYTE bElemprty ;
char buf[512], bufdrive[3], *ptmp ;
SEL ginfo, linfo ;
LINFOSEG FAR *plis ;
char sga[4], queue_name[80] ;
DATETIME dt2 ;
int hours, minutes, seconds, hundredths ;
int output ;
if (( ret = DosGetInfoSeg(&ginfo, &linfo)) != 0)
{
printf("%s: error in DosGetInfoSeg", progname) ;
return(1) ;
}
plis = MAKEPLINFOSEG(linfo) ;
itoa(plis->sgCurrent, sga, 10) ;
strcpy(queue_name, "\\queues\\tmr") ; /* tmr prefix - somebody might use PIDs
as a name base. We want to minimize
risk for a name clash */
strcat(queue_name, sga) ;
if (( ret = DosCreateQueue(&hpopresp, QUE_FIFO, queue_name)) != 0)
{
fprintf(stderr, "%s: error creating queues...\Exiting...\n", progname ) ;
return( 1 ) ;
}
stop_tmr.sg = plis->sgCurrent ;
strcpy(stop_tmr.timer, timer) ;
QueSend( "\\queues\\timerpop", (char *) &stop_tmr, sizeof(stop_tmr), 0, "START /C timersrv.exe", progname) ;
// send_queue("\\queues\\timerpop", (char *)&stop_tmr, sizeof(stop_tmr), 0) ;
if (( ret = DosReadQueue(hpopresp, &qres, &cbElement, &pstart_tmr, 0, DCWW_WAIT,
&bElemprty, NULL)) != 0)
{
return( 1 ) ;
}
if ( qres.usEventCode == 3)
printf("Timer %s was not started.\n", stop_tmr.timer) ;
else
{
printf("TIMER %s \n", stop_tmr.timer) ;
/* Output time difference */
DosGetDateTime(&dt2) ;
hours = 0 ; minutes = 0 ; seconds = 0 ; hundredths = 0 ;
output = FALSE ;
if (dt2.hours < pstart_tmr->dt.hours) /* during midnight */
dt2.hours += 24 ;
hours = dt2.hours - pstart_tmr->dt.hours ;
minutes = dt2.minutes - pstart_tmr->dt.minutes ;
if (minutes < 0)
{
minutes += 60 ;
hours -- ;
}
seconds = dt2.seconds - pstart_tmr->dt.seconds ;
if (seconds < 0)
{
seconds += 60 ;
minutes -- ;
}
hundredths = dt2.hundredths - pstart_tmr->dt.hundredths ;
if (hundredths < 0)
{
hundredths += 100 ;
seconds -- ;
}
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 ) ;
}
printf("%d hundredths.", hundredths ) ;
}
DosFreeSeg(SELECTOROF(pstart_tmr)) ;
DosCloseQueue(hpopresp) ;
}
/*
int send_queue(char *queue_name, char *message, USHORT cbElement, USHORT usRequest)
{
HQUEUE hreq ;
USHORT ret, qowner ;
SEL mesg, selRecipient ;
PCH ptr ;
int i ;
i = 0 ;
while (( ret = DosOpenQueue(&qowner, &hreq, queue_name)) != 0 && i < 2)
{
system("START /C timersrv.exe") ;
DosSleep(2000L) ;
i ++ ;
}
if ( i >= 2)
{
fprintf(stderr, "%s: error opening queue %s...\nExiting...\n", progname, queue_name) ;
return( 1 ) ;
}
if (( ret = DosAllocSeg(512, &mesg, SEG_GIVEABLE)) != 0)
{
fprintf(stderr, "%s: error in DosAllocSeg...\nExiting...\n", progname) ;
return( 1 ) ;
}
if (( ret = DosGiveSeg(mesg, qowner, &selRecipient)) != 0)
{
fprintf(stderr, "%s: error in DosGiveSeg...\nExiting...\n", progname) ;
return( 1 ) ;
}
ptr = MAKEP(mesg, 0) ;
memcpy(ptr, message, (size_t) cbElement) ;
if (( ret = DosWriteQueue(hreq, usRequest
, cbElement, ptr, 0)) != 0)
{
fprintf(stderr, "%s: error in DosWriteQueue...\nExiting...\n", progname) ;
return( 1 ) ;
}
DosCloseQueue(hreq) ;
return( 0 ) ;
}
*/
static int query_timers( void )
{
HQUEUE htimerresp ; /* queuehandle */
USHORT ret ;
int i, count ;
QUEUERESULT qres ;
PVOID pmesg ;
USHORT cbElement ;
BYTE bElemprty ;
char *ptmp ;
SEL ginfo, linfo ;
LINFOSEG FAR *plis ;
char sga[4], queue_name[80] ;
if (( ret = DosGetInfoSeg(&ginfo, &linfo)) != 0)
{
printf("%s: error in DosGetInfoSeg", progname) ;
return(1) ;
}
plis = MAKEPLINFOSEG(linfo) ;
itoa(plis->sgCurrent, sga, 10) ;
strcpy(queue_name, "\\queues\\tmr") ; /* tmr prefix - somebody might use PIDs
as a name base. We want to minimize
risk for a name clash */
strcat(queue_name, sga) ;
if (( ret = DosCreateQueue(&htimerresp, QUE_FIFO, queue_name)) != 0)
{
fprintf(stderr, "%s: error creating queues...\Exiting...\n", progname ) ;
return( 1 ) ;
}
stop_tmr.sg = plis->sgCurrent ;
//stop_tmr.timer = 0 ;
strcpy(stop_tmr.timer, " ") ;
QueSend( "\\queues\\timerpop", (char *) &stop_tmr, sizeof(stop_tmr), 2, "START /C timersrv.exe", progname) ;
// send_queue("\\queues\\timerpop", (char *) &stop_tmr, sizeof(stop_tmr), 2 ) ; /* send DIRS-command */
printf("TIMERS for session %d\n", stop_tmr.sg) ;
count = 0 ;
while (( ret = DosReadQueue(htimerresp, &qres, &cbElement, &pstart_tmr, 0, DCWW_WAIT,
&bElemprty, NULL)) == 0 && qres.usEventCode != 3)
{
count ++ ;
printf("Timer %s\n", pstart_tmr->timer) ;
DosFreeSeg(SELECTOROF(pstart_tmr)) ;
}
if (count == 0)
printf("There are no timers in this session.\n") ;
DosCloseQueue(htimerresp) ;
return( 0 ) ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -