Permalink
Browse files

Added tmux-ping

  • Loading branch information...
1 parent b05d685 commit fd0e7f32676b96008851f7d97859c5ea12ca368c @lmartinking committed Mar 24, 2011
Showing with 69 additions and 0 deletions.
  1. +69 −0 tmux-ping.c
View
@@ -0,0 +1,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"
+#define PING_TIMEOUT "1"
+
+#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;
+}

0 comments on commit fd0e7f3

Please sign in to comment.