Introduction
Introduction Statistics Contact Development Disclaimer Help
ttimer.c - stopwatch - simple timer for console or x root window
git clone git://src.adamsgaard.dk/stopwatch
Log
Files
Refs
README
LICENSE
---
ttimer.c (2241B)
---
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <limits.h>
4 #include <unistd.h>
5 #include <time.h>
6 #include <string.h>
7 #include <err.h>
8 #include <X11/Xlib.h>
9
10 #include "timeutil.h"
11
12 char *argv0;
13
14 void
15 usage(void)
16 {
17 errx(1, "usage: %s [-p prefix] [-P postfix] [-i interval] [-x] H…
18 }
19
20 void
21 print_loop(unsigned int interval, char *prefix, char *postfix, time_t du…
22 {
23 char buf[LINE_MAX];
24 time_t t_start = time(NULL);
25 time_t t_elapsed = 0;
26
27 while ((t_elapsed = time(NULL) - t_start) < duration) {
28 format_time(buf, sizeof(buf), duration - t_elapsed, pref…
29 printf("\r%s ", buf);
30 fflush(stdout);
31 sleep(interval);
32 }
33 printf("\a\n");
34 }
35
36 void
37 xroot_loop(unsigned int interval, char *prefix, char *postfix, time_t du…
38 {
39 Display *dpy;
40 char buf[LINE_MAX];
41 time_t t_start = time(NULL);
42 time_t t_elapsed = 0;
43
44 dpy = XOpenDisplay(NULL);
45 if (dpy == NULL)
46 errx(1, "cannot open display");
47 while ((t_elapsed = time(NULL) - t_start) < duration) {
48 format_time(buf, sizeof(buf), duration - t_elapsed, pref…
49 XStoreName(dpy, DefaultRootWindow(dpy), buf);
50 XSync(dpy, False);
51 sleep(interval);
52 }
53 snprintf(buf, sizeof(buf), "%sEND%s", prefix, postfix);
54 XStoreName(dpy, DefaultRootWindow(dpy), buf);
55 XSync(dpy, False);
56 }
57
58 int
59 main(int argc, char *argv[])
60 {
61 int h, m, s;
62 time_t duration;
63 int ch, xflag = 0;
64 unsigned int interval = 1;
65 char prefix[LINE_MAX] = "", postfix[LINE_MAX] = "";
66 const char *errstr;
67
68 argv0 = *argv;
69
70 while ((ch = getopt(argc, argv, "p:P:i:x")) != -1) {
71 switch (ch) {
72 case 'p':
73 strlcpy(prefix, optarg, sizeof(prefix));
74 break;
75 case 'P':
76 strlcpy(postfix, optarg, sizeof(postfix));
77 break;
78 case 'i':
79 interval = strtonum(optarg, 1, UINT_MAX, &errstr…
80 if (errstr != NULL)
81 errx(1, "interval is %s: %s", errstr, op…
82 break;
83 case 'x':
84 xflag = 1;
85 break;
86 default:
87 usage();
88 }
89 }
90 argc -= optind;
91 argv += optind;
92 if (argc != 1)
93 usage();
94
95 if (sscanf(argv[0], "%d:%d:%d", &h, &m, &s) != 3)
96 errx(2, "could not parse time in HH:MM:SS format");
97 duration = h * 3600 + m * 60 + s;
98
99 if (xflag)
100 xroot_loop(interval, prefix, postfix, duration);
101 else
102 print_loop(interval, prefix, postfix, duration);
103
104 return 0;
105 }
You are viewing proxied material from mx1.adamsgaard.dk. The copyright of proxied material belongs to its original authors. Any comments or complaints in relation to proxied material should be directed to the original authors of the content concerned. Please see the disclaimer for more details.