-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathclock.c
65 lines (57 loc) · 1.43 KB
/
clock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <locale.h>
#include "common.h"
#define INTERVAL 3
#define FORMAT "%a %H:%M"
int put_infos(char *format)
{
char buf[MAXLEN];
time_t ct;
struct tm *lt;
if ((ct = time(NULL)) != -1 &&
(lt = localtime(&ct)) != NULL &&
strftime(buf, sizeof(buf), format, lt) > 0 &&
printf("%s\n", buf) > 0 &&
fflush(stdout) != EOF)
return EXIT_SUCCESS;
else
return EXIT_FAILURE;
}
int main(int argc, char *argv[])
{
char *format = FORMAT;
int interval = INTERVAL;
bool snoop = false;
int opt;
while ((opt = getopt(argc, argv, "hsf:i:")) != -1) {
switch (opt) {
case 'h':
printf("clock [-h|-s|-f FORMAT|-i INTERVAL]\n");
exit(EXIT_SUCCESS);
break;
case 's':
snoop = true;
break;
case 'f':
format = optarg;
break;
case 'i':
interval = atoi(optarg);
break;
}
}
int exit_code;
if (setlocale(LC_ALL, "") == NULL)
perror("setlocale");
if (snoop)
while ((exit_code = put_infos(format)) != EXIT_FAILURE)
sleep(interval);
else
exit_code = put_infos(format);
return exit_code;
}