tstopwatch.c - stopwatch - simple timer for console or x root window | |
git clone git://src.adamsgaard.dk/stopwatch | |
Log | |
Files | |
Refs | |
README | |
LICENSE | |
--- | |
tstopwatch.c (1724B) | |
--- | |
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() | |
16 { | |
17 errx(1, "usage: %s [-p prefix] [-P postfix] [-i interval] [-x]",… | |
18 } | |
19 | |
20 void | |
21 print_loop(unsigned int interval, char *prefix, char *postfix) | |
22 { | |
23 char buf[LINE_MAX]; | |
24 time_t t_start = time(NULL); | |
25 | |
26 while (1) { | |
27 format_time(buf, sizeof(buf), time(NULL) - t_start, pref… | |
28 printf("\r%s", buf); | |
29 fflush(stdout); | |
30 sleep(interval); | |
31 } | |
32 } | |
33 | |
34 void | |
35 xroot_loop(unsigned int interval, char *prefix, char *postfix) | |
36 { | |
37 Display *dpy; | |
38 char buf[LINE_MAX]; | |
39 time_t t_start = time(NULL); | |
40 | |
41 dpy = XOpenDisplay(NULL); | |
42 if (dpy == NULL) | |
43 errx(1, "cannot open display"); | |
44 while (1) { | |
45 format_time(buf, sizeof(buf), time(NULL) - t_start, pref… | |
46 XStoreName(dpy, DefaultRootWindow(dpy), buf); | |
47 XSync(dpy, False); | |
48 sleep(interval); | |
49 } | |
50 } | |
51 | |
52 int | |
53 main(int argc, char *argv[]) | |
54 { | |
55 int ch, xflag = 0; | |
56 unsigned int interval = 1; | |
57 char prefix[LINE_MAX] = "", postfix[LINE_MAX] = ""; | |
58 const char *errstr; | |
59 | |
60 argv0 = *argv; | |
61 | |
62 while ((ch = getopt(argc, argv, "p:P:i:x")) != -1) { | |
63 switch (ch) { | |
64 case 'p': | |
65 strlcpy(prefix, optarg, sizeof(prefix)); | |
66 break; | |
67 case 'P': | |
68 strlcpy(postfix, optarg, sizeof(postfix)); | |
69 break; | |
70 case 'i': | |
71 interval = strtonum(optarg, 1, UINT_MAX, &errstr… | |
72 if (errstr != NULL) | |
73 errx(1, "interval is %s: %s", errstr, op… | |
74 break; | |
75 case 'x': | |
76 xflag = 1; | |
77 break; | |
78 default: | |
79 usage(); | |
80 } | |
81 } | |
82 argc -= optind; | |
83 argv += optind; | |
84 if (argc > 0) | |
85 usage(); | |
86 | |
87 if (xflag) | |
88 xroot_loop(interval, prefix, postfix); | |
89 else | |
90 print_loop(interval, prefix, postfix); | |
91 | |
92 return 0; | |
93 } |