-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathunix.c
70 lines (62 loc) · 1.21 KB
/
unix.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
66
67
68
69
70
#include "11.h"
void
initclock(Clock *clk, long period)
{
clock_gettime(CLOCK_REALTIME, &clk->newtime);
clk->oldtime = clk->newtime;
clk->period = period;
}
int
handleclock(Clock *clk)
{
struct timespec diff;
clock_gettime(CLOCK_REALTIME, &clk->newtime);
diff.tv_sec = clk->newtime.tv_sec - clk->oldtime.tv_sec;
diff.tv_nsec = clk->newtime.tv_nsec - clk->oldtime.tv_nsec;
if(diff.tv_nsec < 0){
diff.tv_nsec += 1000000000;
diff.tv_sec -= 1;
}
if(diff.tv_nsec >= clk->period){
clk->oldtime.tv_nsec += clk->period;
if(clk->oldtime.tv_nsec >= 1000000000){
clk->oldtime.tv_nsec -= 1000000000;
clk->oldtime.tv_sec += 1;
}
return 1;
}
return 0;
}
int
ttyopen(Tty *tty)
{
/* open a tty if it exists */
tty->fd = open("/tmp/tty", O_RDWR);
// tty->fd = open("/dev/ttyUSB0", O_RDWR);
printf("tty connected to %d\n", tty->fd);
return tty->fd >= 0;
}
int
ttyinput(Tty *tty, char *c)
{
if(hasinput(tty->fd)){
read(tty->fd, c, 1);
return 1;
}
return 0;
}
int
timestamp(char *buffer, size_t n)
{
struct tm *tm;
time_t t;
t = time(NULL);
if (t == (time_t)-1)
return -1;
tm = localtime(&t);
if (tm == NULL)
return -1;
if(strftime(buffer, n, "%Y%m%d%H%M%S", tm) == 0)
return -1;
return 0;
}