-
Notifications
You must be signed in to change notification settings - Fork 4
/
tmux-ping.c
69 lines (53 loc) · 1.13 KB
/
tmux-ping.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
/*
* tmux-ping.c - a little tmux "applet" for showing whether we can ping hosts
*
* by Lucas Martin-King
*
* [ Licenced under the GPLv2 ]
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <fcntl.h>
#define PING_COUNT "1" /* must be a string! */
#define PING_TIMEOUT "1" /* must be a string! */
#define PING_PROG "/bin/ping"
#define DEFAULT_HOST "127.0.0.1"
#define COLOUR_OKAY "green"
#define COLOUR_ERROR "red"
int main(int argc, char **argv)
{
char *host;
int status;
pid_t pid;
if (argc > 1) {
host = argv[1];
} else {
host = DEFAULT_HOST;
}
pid = fork();
if (pid == 0) {
int nullfd = open("/dev/null", O_RDWR);
dup2(nullfd, 0);
dup2(nullfd, 1);
dup2(nullfd, 2);
execl(PING_PROG, PING_PROG, "-c", PING_COUNT, "-w", PING_TIMEOUT, host, NULL);
} else if (pid < 0) {
printf("ER\n");
return 1;
} else {
int retval;
char *colour = NULL;
waitpid(pid, &status, 0);
retval = WEXITSTATUS(status);
if (retval == 0) {
colour = COLOUR_OKAY;
} else {
colour = COLOUR_ERROR;
}
printf("#[bg=%s] #[default]\n", colour);
}
fflush(stdout);
return 0;
}