-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmsg.c
77 lines (68 loc) · 1.47 KB
/
msg.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
71
72
73
74
75
76
/*
* msg.c
*
* Stuff for the one-line message line in the vtclock.
*/
#include "msg.h"
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
static char *msg = NULL;
static int msg_cols = 0;
static int msg_from_pipe = 0;
static int msg_delay = 5;
static char *msg_filename = NULL;
int
message_ready (void)
{
static time_t last_ready = 0;
static time_t now;
now = time(NULL);
if (last_ready == 0) {
last_ready = now;
return 1;
}
if ((now - last_ready) >= msg_delay) {
last_ready = now;
return 1;
}
return 0;
}
void
init_message (int cols, char *filename, int is_pipe, int delay)
{
if (msg != NULL) free(msg);
msg = (char *)malloc(cols + 1);
msg[0] = '\0';
msg_cols = cols;
msg_from_pipe = is_pipe;
msg_delay = delay;
msg_filename = filename;
}
static FILE *msg_file;
FILE *
open_msg_file (void)
{
if (msg_file != NULL) return msg_file;
if (msg_from_pipe) {
msg_file = popen(msg_filename, "r");
} else {
msg_file = fopen(msg_filename, "r");
}
if (msg_file == NULL) return NULL;
return msg_file;
}
char *
get_next_message (void)
{
if (msg == NULL) return NULL; /* sanity */
if (msg_filename == NULL) return NULL; /* sanity */
if (!message_ready()) return NULL;
if (open_msg_file() == NULL) return NULL;
if (fgets(msg, msg_cols, msg_file) != NULL) return msg;
fclose(msg_file);
msg_file = NULL;
if (open_msg_file() == NULL) return NULL;
if (fgets(msg, msg_cols, msg_file) != NULL) return msg;
return NULL;
}