sleepenh-1.2/0000755000175000017500000000000011002737245011360 5ustar pznpznsleepenh-1.2/sleepenh.c0000644000175000017500000001027711002737233013333 0ustar pznpzn/* * sleepenh.c - enhanced sleep command * * Copyright (C) 2003 - Pedro Zorzenon Neto * * 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., 51 Franklin St, Fifth Floor, Boston, * MA 02110-1301, USA. * * USAGE INSTRUCTIONS * * this program is an enhanced version of sleep, you can use as * a simple sleep program, with microseconds * Example: (sleeps 45.123234 seconds) * sleepenh 45.123234 * * if it is called with two arguments, it acts as a sleep program * that can be called in sequence and will not have cumulative * errors from one call to another. * Example: (time from one 'ls' to other is 12.4 seconds) * VAL=`sleepenh 0` ; while true; do ls; VAL=`sleepenh 12.4`; done * * Exit status: * 0 - success. I needed to sleep sometime * 1 - success. I did not need to sleep * >9 - failure. * 10 - failure. not enough command line arguments * 11 - failure. did not receive sigalrm * 12 - failure. argument is not a finite number * 13 - failure. could not get current time (gettimeofday) * * shell script usage example: see manpage */ #include #include #include #include #include #include #include #include #include #define SHORTEST_SLEEP 0.00001 /* 10msec, a timeslice */ #define RCSID "$Id: sleepenh.c,v 1.2 2003/02/24 17:17:48 pzn Exp $" #define RCSREV "$Revision: 1.2 $" #define RCSDATE "$Date: 2003/02/24 17:17:48 $" static int sigflag=0; void got_signal() { sigflag=1; } int main(int argc, char *argv[]) { struct timeval tv; struct timezone tz; struct itimerval itv; struct sigaction sigact; double st; /* start time */ double et; /* end time */ double now; /* now */ double it; /* interval */ if (argc==1) { fprintf(stderr, "sleepenh -- an enhanced sleep program.\n" " -- " RCSREV "\n" " -- " RCSDATE "\n" "\n" "Copyright (C) 2003 - Pedro Zorzenon Neto\n" "Distributed under the conditions of FSF/GPL2 License.\n" "See the source code for more copyright and license information.\n" "\n" "Usage: %s timetosleep\n" " or: %s initialtime timetosleep\n" "\n" "timetosleep is in seconds, microsecond resolution. Ex: 80.123456\n" "initialtime is the output value of a previous execution of sleepenh.\n" , argv[0], argv[0]); return 10; /* failure, bad arguments */ } if(gettimeofday(&tv,&tz)!=0) { return 13; /* failure, could not get current time */ } st=strtod(argv[argc-1],NULL); if(finite(st)==0) { return 12; /* failure, argument is not a finite number */ } now=tv.tv_sec+(tv.tv_usec*0.000001); if (argc==2) { /* without initialtime */ it=now; } else { /* with initialtime */ it=strtod(argv[1],NULL); if(finite(it)==0) { return 12; /* failure, argument is not a finite number */ } } et=it+st; it=et-now; if ( it < SHORTEST_SLEEP ) { /* has already timed out, shorted than a timeslice */ printf("%f\n",et); return 1; /* success, time out */ } /* set signal handler */ memset(&sigact, 0, sizeof(sigact)); sigact.sa_handler=&got_signal; sigaction (SIGALRM, &sigact, NULL); /* set timer */ itv.it_value.tv_sec=(long int) it; itv.it_value.tv_usec=((long int) (it*1000000)) % 1000000; itv.it_interval.tv_sec=0; itv.it_interval.tv_usec=0; setitimer(ITIMER_REAL,&itv,NULL); pause(); /* wait for signal */ printf("%f\n",et); if (sigflag==1) { return 0; /* success */ } return 11; /* failure */ } sleepenh-1.2/Makefile0000644000175000017500000000077311002736511013022 0ustar pznpzn# just a simple makefile all: sleepenh manpage clean: rm -fv sleepenh sleepenh.1.gz distclean: clean sleepenh: gcc -o sleepenh -Wall -O2 sleepenh.c manpage: if [ ! -e "sleepenh.1.gz" ]; then \ cat sleepenh.1 | gzip -9 > sleepenh.1.gz ; fi install: sleepenh manpage mkdir -p ${DESTDIR}/usr/bin/ install -m 0755 -o root -g root sleepenh ${DESTDIR}/usr/bin/sleepenh mkdir -p ${DESTDIR}/usr/share/man/man1/ install -m 0644 -o root -g root sleepenh.1.gz ${DESTDIR}/usr/share/man/man1/sleepenh.1.gz sleepenh-1.2/sleepenh.10000644000175000017500000000546311002737233013252 0ustar pznpzn.\" Hey, EMACS: -*- nroff -*- .TH SLEEPENH 1 "2008/04/20" .\" Please adjust this date whenever revising the manpage. .\" .\" Some roff macros, for reference: .\" .nh disable hyphenation .\" .hy enable hyphenation .\" .ad l left justify .\" .ad b justify to both left and right margins .\" .nf disable filling .\" .fi enable filling .\" .br insert line break .\" .sp insert n+1 empty lines .\" for manpage-specific macros, see man(7) .SH NAME sleepenh \- an enhanced sleep program. .SH SYNOPSIS .B sleepenh .RI [ initial\-time ] .RI sleep\-time .SH DESCRIPTION \fBsleepenh\fP is a program that can be used when there is a need to execute some functions periodically in a shell script. It was not designed to be accurate for a single sleep, but to be accurate in a sequence of consecutive sleeps. .br After a successful execution, it returns to stdout the timestamp it finished running, that can be used as \fBinitial\-time\fP to a successive execution of \fBsleepenh\fP. .SH OPTIONS There are no command line options. Run it without any option to get a brief help and version. .SH ARGUMENTS \fBsleep\-time\fP is a real number in seconds, with microseconds resolution (1 minute, 20 seconds and 123456 microseconds would be 80.123456). .br \fBinitial\-time\fP is a real number in seconds, with microseconds resolution. This number is system dependent. In GNU/Linux systems, it is the number of seconds since midnight 1970\-01\-01 GMT. Do not try to get a good value of \fBinitial\-time\fP. Use the value supplied by a previous execution of \fBsleepenh\fP. .br If you don't specify \fBinitial\-time\fP, it is assumed the current\-time. .SH EXIT STATUS An exit status greater or equal to 10 means failure. Known exit status: .TP .B 0 Success. .TP .B 1 Success. There was no need to sleep. (means that initial\-time + sleep\-time was greater than current\-time). .TP .B 10 Failure. Missing command line arguments. .TP .B 11 Failure. Did not receive SIGALRM. .TP .B 12 Failure. Argument is not a number. .TP .B 13 Failure. System error, could not get current time. .SH USAGE EXAMPLE Suppose you need to send the char 'A' to the serial port ttyS0 every 4 seconds. This will do that: .RS #!/bin/sh TIMESTAMP=`sleepenh 0` while true; do # send the byte to ttyS0 echo \-n "A" > /dev/ttyS0; # just print a nice message on screen echo \-n "I sent 'A' to ttyS0, time now is "; sleepenh 0; # wait the required time TIMESTAMP=`sleepenh $TIMESTAMP 4.0`; done .RE .SH HINT This program can be used to get the current time. Just execute: .TP sleepenh 0 .SH BUGS It is not accurate for a single sleep. Short sleep\-times will also not be accurate. .SH SEE ALSO .BR date (1), .BR sleep (1). .br .SH AUTHOR This manual page was written by Pedro Zorzenon Neto.