home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Source Code 1992 March
/
Source_Code_CD-ROM_Walnut_Creek_March_1992.iso
/
usenet
/
altsrcs
/
1
/
1320
< prev
next >
Wrap
Internet Message Format
|
1990-12-28
|
3KB
From: eichin@athena.mit.edu (Mark W. Eichin)
Newsgroups: alt.sources
Subject: sleep hack
Message-ID: <1990May12.074200.12109@athena.mit.edu>
Date: 12 May 90 07:42:00 GMT
This is twisted and sick, but was inspired thus: I have a habit of
doing
(sleep 7200; echo "Wake up bozo")&
to wake me up in two hours in for whatever reason... This gives me
good control of messages ("jobs", "kill", and "ps"). However, it
doesn't tell me how much time is remaining on the clock.
In the grand UNIX tradition of using a small hack to avoid
solving a real (and large) problem, I hacked sleep to update the time
stamp in argv. Since this is what "ps" reads to get its process names,
you can quickly check and see how much time is left.
This works on BSD systems. I have no idea if it will work on
anything else. I'm not sure that I care :-). The code is all mine;
feel free to post enhancements or bug fixes to alt.sources, especially
since I don't read it.
Mark Eichin
<eichin@athena.mit.edu>
/*
* Usage: sleep [-i interval] delay
*
* without -i, merely sleeps delay seconds using sleep(3).
* with -i, uses select(2) to sleep, and mutates the "delay" string every
* "interval" seconds to allow the user to see how much time is left
* using ps(3).
*/
#include <sys/types.h>
#include <sys/time.h>
#include <strings.h>
#include <stdio.h>
static char *rcsid_sleep_c = "$Header: /afs/athena.mit.edu/user/e/eichin/Src/RCS/sleep.c,v 1.1 90/05/12 02:54:39 eichin Exp $";
void Usage(pn)
char *pn;
{
fprintf(stderr,"Usage: %s [-i interval] delay\n%s\n%s\n%s\n%s\n",
pn,
"\twithout -i, merely sleeps delay seconds using sleep(3).",
"\twith -i, uses select(2) to sleep, and mutates the 'delay' string every",
"\t'interval' seconds to allow the user to see how much time is left",
"\tusing ps(3).");
}
void do_sleep(n,interv,mutate)
int n, interv;
char *mutate;
{
struct timeval timeout;
timeout.tv_usec = 0;
while (n) {
if(n>interv)
timeout.tv_sec = interv;
else
timeout.tv_sec = n;
n -= timeout.tv_sec;
select(0,0,0,0,&timeout);
sprintf(mutate,"%d",n);
}
}
int main(argc, argv)
int argc;
char **argv;
{
int interval = 0;
int delay = 0;
char *progname = argv[0];
if(!strcmp("-i",argv[1])) {
if(argc != 4) {
Usage(progname);
return 1;
}
interval = atoi(argv[2]);
argc--; argv++;
argc--; argv++;
}
if(argc != 2) {
Usage(progname);
return 1;
}
delay = atoi(argv[1]);
if(interval) {
do_sleep(delay, interval, argv[1]);
} else {
sleep(delay);
}
return 0;
}
/* end of sleep.c */