-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime.c
55 lines (45 loc) · 1.19 KB
/
time.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
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include "sntp.h"
void usage();
static char* host = "1.pool.ntp.org";
int
main(int argc, char** argv){
int ch;
time_t mytime;
if( argc == 1){
mytime = get_time(host);
printf( "%s\n", ctime( ( const time_t* ) &mytime ) );
exit(EXIT_SUCCESS);
}
while (( ch = getopt(argc, argv, "ruh")) != -1){
switch(ch){
case 'r':
mytime = get_time(host);
printf( "%s\n", ctime( ( const time_t* ) &mytime ) );
break;
case 'u':
mytime = get_time(host);
printf( "%llu\n", (unsigned long long) mytime);
break;
case 'h':
usage(EXIT_SUCCESS);
break;
default:
usage(EXIT_FAILURE);
break;
}
}
return(0);
}
void
usage(int status){
(void)fprintf(stdout, "%s\n%s\n%s\n%s\n",
"usage: time <flag>",
" time [-r :time in regular format]",
" time [-u :time in unix format]",
" time [-h :help]");
exit(status);
}