Skip to content
Browse files

initial commit

  • Loading branch information...
0 parents commit bfae985b1473ceda044999a6b200fdfbffed08f4 @pbrisbin committed Jan 8, 2011
Showing with 128 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +20 −0 Makefile
  3. +10 −0 README.md
  4. +82 −0 android-receiver.c
  5. +13 −0 config.mk
3 .gitignore
@@ -0,0 +1,3 @@
+*.out
+*.o
+android-receiver
20 Makefile
@@ -0,0 +1,20 @@
+
+include config.mk
+
+SRC = mail-query.c
+OBJ = ${SRC:.c=.o}
+
+all: android-receiver
+
+.c.o:
+ ${CC} -c ${CFLAGS} $<
+
+${OBJ}: config.mk
+mail-query: ${OBJ}
+ ${CC} -o $@ ${OBJ} ${LDFLAGS}
+
+
+clean:
+ rm -f android-receiver ${OBJ}
+
+.PHONY: all clean
10 README.md
@@ -0,0 +1,10 @@
+# Android Receiver
+
+Listen for broadcast messages from
+[android-notifer](http://code.google.com/p/android-notifier/) coming
+over a specific port.
+
+Format that message (todo) and pass it off to a handling application
+like notify-osd, dzen2, or some other script.
+
+Done mostly as a learning exercise in C.
82 android-receiver.c
@@ -0,0 +1,82 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <netinet/in.h>
+#include <sys/socket.h>
+
+int execvp(const char *file, char *const argv[]);
+void bzero(void *s, size_t n);
+pid_t fork(void);
+
+void error(char *msg);
+void handle_message(char *msg);
+
+/* error and die */
+void error(char *msg)
+{
+ perror(msg);
+ exit(EXIT_FAILURE);
+}
+
+/* for now we just hand off to my existing bash script */
+void handle_message(char *msg)
+{
+ char *app = "dzen-handler";
+ char *flags[] = {app, msg, NULL};
+
+ printf("executing handler: %s %s\n", flags[0], flags[1]);
+ execvp(app, flags);
+}
+
+/* connect to <port> and listen for messages */
+int main(int argc, char *argv[])
+{
+ unsigned int fromlen;
+ int sock;
+ int length;
+ int n;
+
+ struct sockaddr_in server;
+ struct sockaddr_in from;
+
+ char buf[1024];
+
+ pid_t pid;
+
+ if (argc != 2) {
+ fprintf(stderr, "argc was %i\n", argc);
+ fprintf(stderr, "saw %s, %s, %s\n", argv[0], argv[1], argv[2]);
+ fprintf(stderr, "usage: %s <port>\n", argv[0]);
+ return(EXIT_FAILURE);
+ }
+
+ sock = socket(AF_INET, SOCK_DGRAM, 0);
+
+ if (sock < 0)
+ error("Opening socket");
+
+ length = sizeof(server);
+
+ bzero(&server,length);
+
+ server.sin_family = AF_INET;
+ server.sin_addr.s_addr = INADDR_ANY;
+ server.sin_port = htons(atoi(argv[1]));
+
+ if (bind(sock, (struct sockaddr *)&server, length) < 0)
+ error("binding");
+
+ fromlen = sizeof(struct sockaddr_in);
+
+ while (1) {
+ n = recvfrom(sock, buf, 1024, 0, (struct sockaddr * restrict)&from, &fromlen);
+
+ if (n < 0)
+ error("recvfrom");
+
+ pid = fork();
+
+ if (pid == 0)
+ handle_message(buf);
+ }
+}
13 config.mk
@@ -0,0 +1,13 @@
+# version
+VERSION = $(shell git describe)
+
+# paths
+PREFIX ?= /usr/local
+MANPREFIX ?= ${PREFIX}/share/man
+
+# compiler flags
+CC = c99
+CPPFLAGS =
+CFLAGS += -g -pedantic -Wall -Wextra ${CPPFLAGS}
+LDFLAGS =
+

0 comments on commit bfae985

Please sign in to comment.
Something went wrong with that request. Please try again.