pax_global_header00006660000000000000000000000064124310552140014507gustar00rootroot0000000000000052 comment=daabd47ed7dead927c73f86138e7142526225938 sleepenh-1.4/000077500000000000000000000000001243105521400131565ustar00rootroot00000000000000sleepenh-1.4/.gitignore000066400000000000000000000000421243105521400151420ustar00rootroot00000000000000**~ **.swp sleepenh sleepenh.1.gz sleepenh-1.4/Makefile000066400000000000000000000011411243105521400146130ustar00rootroot00000000000000Q ?= @ ifneq ($(Q),@) Q := endif CC = gcc CFLAGS = -std=gnu11 -Wall -Wextra -O2 targets := sleepenh sleepenh.1.gz all: $(targets) .clean = $(wildcard $(targets)) distclean clean: ifneq ($(.clean),) rm -f $(.clean) endif sleepenh: CFLAGS += -DVCSVERSION=$(call vcsversion) %.1.gz: %.1 gzip -9 < $< > $@ install: $(targets) install -D -m 0755 -o root -g root sleepenh ${DESTDIR}/usr/bin/sleepenh install -D -m 0644 -o root -g root sleepenh.1.gz ${DESTDIR}/usr/share/man/man1/sleepenh.1.gz define vcsversion '$(shell git describe --dirty | sed -e 's,^\(.*\)/\([^/]\+\),\2/\1,;s,^.*$$,\"&\",')' endef sleepenh-1.4/sleepenh.1000066400000000000000000000061151243105521400150460ustar00rootroot00000000000000.\" Hey, EMACS: -*- nroff -*- .TH SLEEPENH 1 "November 2014" "sleepenh" "User commands" .\" 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 [[\fI\,--warp|-w\/\fR] \fI\,INITIALTIME\/\fR] \fI\,TIMETOSLEEP\/\fR .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 \fBINITIALTIME\fP to a successive execution of \fBsleepenh\fP. .SH OPTIONS .TP \fB\-h\fR, \fB\-\-help\fR display this help and exit .TP \fB\-w\fR, \fB\-\-warp\fR warp resulting timestamp, when there is no need to sleep. An immediatly following call of sleepenh with the resulting TIMESTAMP would most probably result in a real sleep. .TP \fB\-V\fR, \fB\-\-version\fR output version information and exit .SH ARGUMENTS \fBTIMETOSLEEP\fP is a real number in seconds, with microseconds resolution (1 minute, 20 seconds and 123456 microseconds would be 80.123456). .br \fBINITIALTIME\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 \fBINITIALTIME\fP. Use the value supplied by a previous execution of \fBsleepenh\fP. .br If you don't specify \fBINITIALTIME\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 INITIALTIME + TIMETOSLEEP 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 TIMETOSLEEPs 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. sleepenh-1.4/sleepenh.c000066400000000000000000000123041243105521400151250ustar00rootroot00000000000000/* * sleepenh.c - enhanced sleep command * * Copyright (C) 2003 Pedro Zorzenon Neto * Copyright (C) 2014 Nicolas Schier * * 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 */ #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #define SHORTEST_SLEEP 0.00001 /* 10msec, a timeslice */ static int sigflag=0; void got_signal() { sigflag=1; } void version(FILE *f) { fprintf(f, "sleepenh " VCSVERSION "\n" "\n" "Copyright (C) 2003 Pedro Zorzenon Neto\n" "Copyright (C) 2014 Nicolas Schier\n" "License GPLv2+: GNU GPL version 2 or later .\n" "This is free software: you are free to change and redistribute it.\n" "There is NO WARRANTY, to the extent permitted by law.\n"); } void usage(FILE *f) { fprintf(f, "Usage: %s [[--warp|-w] INITIALTIME] TIMETOSLEEP\n" "\n" "An enhanced sleep program.\n" "\n" "Options:\n" " -h, --help display this help and exit\n" " -w, --warp warp resulting timestamp, when there is no need\n" " to sleep. An immediatly following call of\n" " sleepenh with the resulting TIMESTAMP would\n" " most probably result in a real sleep.\n" " -V, --version output version information and exit\n" "\n" "TIMETOSLEEP is in seconds, microsecond resolution, ex: 80.123456.\n" "INITIALTIME is the output value of a previous execution of sleepenh.\n", program_invocation_short_name); } 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 */ int warp = 0; if ((argc > 1) && (!strcmp(argv[1], "--warp") || !strcmp(argv[1], "-w"))) { warp = 1; argc--, argv++; } if (argc==1) { version(stderr); fprintf(stderr, "\n"); usage(stderr); return 10; /* failure, bad arguments */ } if (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--usage") || !strcmp(argv[1], "-u")) { usage(stdout); return 0; } if (!strcmp(argv[1], "--version") || !strcmp(argv[1], "-V")) { version(stdout); return 0; } 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 ) { if (warp) { /* warp in time -> loose events, but keep event regularity */ int tmp = -it / st; double div = tmp * st; et += div; } /* 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 */ }