[tpwd] Fix segfault when exactly one argument given
[tinyapps.git] / timer.c
blob9cbd729fbbd88d2ee8e42b9c553c6c6dda8acdce
1 /*
2 * Timer.
3 * Copyright (c) 2005-2007 by Michal Nazareicz <mina86@mina86.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 * This is part of Tiny Applications Collection
19 * -> http://tinyapps.sourceforge.net/
22 #define _POSIX_C_SOURCE 199309
23 #include <unistd.h>
24 #include <time.h>
25 #include <signal.h>
26 #include <stdio.h>
28 volatile sig_atomic_t signum = 0;
30 static void sighandler(int sig) {
31 signum = sig;
35 int main(void) {
36 struct timespec wait = { 0, 10000000 };
37 unsigned int m = 0, ss = 0;
38 char buffer[11] = "\r 0:00.00";
40 #ifdef SIGHUP
41 signal(SIGHUP, sighandler);
42 #endif
43 #ifdef SIGINT
44 signal(SIGINT, sighandler);
45 #endif
46 #ifdef SIGQUIT
47 signal(SIGQUIT, sighandler);
48 #endif
49 #ifdef SIGPIPE
50 signal(SIGPIPE, sighandler);
51 #endif
52 #ifdef SIGTERM
53 signal(SIGTERM, sighandler);
54 #endif
55 #ifdef SIGSTOP
56 signal(SIGSTOP, sighandler);
57 #endif
58 #ifdef SIGTSTP
59 signal(SIGTSTP, sighandler);
60 #endif
62 do {
63 if (write(1, buffer, 11)!=11) return 1;
65 while (!signum && nanosleep(&wait, &wait));
66 if (signum) break;
67 wait.tv_nsec = 10000000;
69 if ((ss = (ss+1)%6000)==0) {
70 ++m;
71 buffer[ 1] = m<1000 ? ' ' : (m/1000%10 + '0');
72 buffer[ 1] = m<100 ? ' ' : (m/100 %10 + '0');
73 buffer[ 3] = m<10 ? ' ' : (m/10 %10 + '0');
74 buffer[ 4] = m %10 + '0';
77 buffer[ 6] = ss/1000 + '0';
78 buffer[ 7] = ss/100 %10 + '0';
79 buffer[ 9] = ss/10 %10 + '0';
80 buffer[10] = ss %10 + '0';
81 } while (!signum);
83 printf("%07ld\n", 10000000 - wait.tv_nsec);
84 return 0;