Makefile: add pickrand
[cmccabe-bin] / simple_time.c
1 #define _POSIX_PTHREAD_SEMANTICS // needed for Solaris for asctime_r
2
3 #include <stdio.h>
4 #include <string.h>
5 #include <time.h>
6 #include <unistd.h>
7
8 /* Prints a time on a single line of the console.
9  * I wrote this app so that I could measure times by repeatedly pressing
10  * return.
11  *
12  * Colin McCabe
13  */
14 void strip_newlines(char *str)
15 {
16         size_t i;
17         size_t str_len = strlen(str);
18
19         for (i = 0; i < str_len; i++) {
20                 if (str[i] == '\n')
21                         str[i] = ' ';
22         }
23 }
24
25 int main(void)
26 {
27         while (1) {
28                 char str[27];
29                 time_t t = time(NULL);
30                 struct tm *tm = localtime(&t);
31
32                 str[0] = '\0';
33                 asctime_r(tm, str);
34                 printf("\r");
35                 strip_newlines(str);
36                 printf("%s", str);
37                 fflush(stdout);
38                 sleep(1);
39         }
40
41         return 0;
42 }