Add sanity check to uzblctrl to top segfault if -s or -c is omitted.
authorBarrucadu <mike@barrucadu.co.uk>
Wed, 20 May 2009 18:19:14 +0000 (19:19 +0100)
committerBarrucadu <mike@barrucadu.co.uk>
Wed, 20 May 2009 18:19:14 +0000 (19:19 +0100)
uzblctrl.c

index 46f3a23..55a3aac 100644 (file)
@@ -1,3 +1,4 @@
+/* -*- c-basic-offset: 4; -*- */
 /* Socket code more or less completely copied from here: http://www.ecst.csuchico.edu/~beej/guide/ipc/usock.html */
 
 #include <gtk/gtk.h>
@@ -35,30 +36,37 @@ main(int argc, char* argv[]) {
     g_option_context_add_group        (context, gtk_get_option_group (TRUE));
     g_option_context_parse            (context, &argc, &argv, &error);
 
-    int s, len;
-    struct sockaddr_un remote;
 
-    if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) == -1) {
-        perror ("socket");
-        exit (1);
+    if (sockpath && command) {
+        int s, len;
+        struct sockaddr_un remote;
+        
+        if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) == -1) {
+            perror ("socket");
+            exit (1);
+        }
+        
+        remote.sun_family = AF_UNIX;
+        strcpy (remote.sun_path, (char *) sockpath);
+        len = strlen (remote.sun_path) + sizeof (remote.sun_family);
+        
+        if (connect (s, (struct sockaddr *) &remote, len) == -1) {
+            perror ("connect");
+            exit (1);
+        }
+        
+        if (send (s, command, strlen (command), 0) == -1) {
+            perror ("send");
+            exit (1);
+        }
+        
+        close(s);
+        
+        return 0;
+    } else {
+        printf ("You need to specify the -s and -c parameters for uzblctrl to do anything of use.\n");
+        printf ("Usage: uzblctrl -s /path/to/socket -c \"command\"\n");
+        return 1;
     }
-
-    remote.sun_family = AF_UNIX;
-    strcpy (remote.sun_path, (char *) sockpath);
-    len = strlen (remote.sun_path) + sizeof (remote.sun_family);
-
-    if (connect (s, (struct sockaddr *) &remote, len) == -1) {
-        perror ("connect");
-        exit (1);
-    }
-
-    if (send (s, command, strlen (command), 0) == -1) {
-        perror ("send");
-        exit (1);
-    }
-
-    close(s);
-
-    return 0;
 }
 /* vi: set et ts=4: */