chiark / gitweb /
*** empty log message ***
[sympathy.git] / apps / sympathy.c
index 5e52062442c4be7864ed8bac9b9400dccd464e47..41d0bbb6961841d740a9e7f8cf52c37715ac141a 100644 (file)
@@ -11,6 +11,9 @@ static char rcsid[] =
 
 /*
  * $Log$
+ * Revision 1.27  2008/03/02 09:55:37  james
+ * *** empty log message ***
+ *
  * Revision 1.26  2008/02/29 14:55:09  james
  * *** empty log message ***
  *
@@ -106,9 +109,11 @@ static char rcsid[] =
 #include "mainloop.h"
 
 extern void usage (void);
-
 static char hostname[1024];
 
+char *socket_dirs[] =
+  { ".sympathy", "sympathy", "/etc/sympathy", "/var/sympathy", NULL };
+
 int
 safe_atoi (char *a)
 {
@@ -171,6 +176,9 @@ mome (char *fmt, ...)
   strcpy (buf, home);
   strcat (buf, "/");
 
+  if (!fmt)
+    return buf;
+
   while (1)
     {
 
@@ -194,15 +202,32 @@ mome (char *fmt, ...)
     }
 }
 
+char *
+gloo_paths (char *dir, char *leaf)
+{
+  int i;
+  char *ret, *ptr;
+  ret = ptr = malloc (strlen (dir) + strlen (leaf) + 2);
+
+  while (*dir)
+    *(ptr++) = *(dir++);
+  *(ptr++) = '/';
+  while (*leaf)
+    *(ptr++) = *(leaf++);
+  *ptr = 0;
+
+  return ret;
+}
+
+
 
 int
-list_sockets (void)
+list_sockets_in_dir (char *sockdir)
 {
   struct dirent *ent;
   struct stat buf;
   char *sn = NULL;
   Socket *s;
-  char *sockdir = mome ("/.sympathy/");
   DIR *dir = opendir (sockdir);
 
 
@@ -217,7 +242,7 @@ list_sockets (void)
 
   while ((ent = readdir (dir)))
     {
-      sn = mome (".sympathy/%s", ent->d_name);
+      sn = gloo_paths (sockdir, ent->d_name);
       if (stat (sn, &buf) || (!S_ISSOCK (buf.st_mode)))
         {
           free (sn);
@@ -228,18 +253,18 @@ list_sockets (void)
 
       if (s)
         {
-          printf ("\t%s        (Active)\n", ent->d_name);
+          printf ("\t%s        (Active)\n", sn);
           socket_free (s);
         }
       else
         {
           if (strncmp (ent->d_name, hostname, hostname_len))
             {
-              printf ("\t%s    (Unknown - not this host)\n", ent->d_name);
+              printf ("\t%s    (Unknown - not this host)\n", sn);
             }
           else
             {
-              printf ("\t%s    (Dead, wiped)\n", ent->d_name);
+              printf ("\t%s    (Dead, wiped)\n", sn);
               unlink (sn);
             }
         }
@@ -252,6 +277,27 @@ list_sockets (void)
   return 0;
 }
 
+int
+list_sockets (void)
+{
+  char **ptr, *h;
+
+
+  for (ptr = socket_dirs; *ptr; ptr++)
+    {
+      h = mome (*ptr);
+
+      if (h)
+        {
+          list_sockets_in_dir (h);
+          free (h);
+        }
+
+    }
+
+  return 0;
+}
+
 
 void
 get_hostname (void)
@@ -375,25 +421,9 @@ main (int argc, char *argv[])
         fatal_moan ("agrument to -n must be greater than zero");
     }
 
-  /*Fold -r into -c */
+  /*Fold -r implies -c */
   if (oflags['r'])
-    {
-      char *id = oargs['r'];
-      if (id < 0)
-        fatal_moan ("cannot parse -r %s as an integer", oargs['r']);
-
-      oflags['k']++;
-      if (safe_atoi (id) > 0)
-        {
-          oargs['k'] = mome ("/.sympathy/%s%d", hostname, safe_atoi (id));
-        }
-      else
-        {
-          oargs['k'] = mome ("/.sympathy/%s", id);
-        }
-      oflags['r'] = 0;
-      oflags['c']++;
-    }
+    oflags['c']++;
 
   if (oflags['p'] && oflags['d'])
     fatal_moan ("-p incompatible with -d");
@@ -426,7 +456,7 @@ main (int argc, char *argv[])
           break;
         case -1:
           fatal_moan ("fork failed");
-        default:             /*parent becomes client*/
+        default:               /*parent becomes client */
           oflags['s'] = 0;
           oflags['K'] = 0;
           oflags['d'] = 0;
@@ -437,13 +467,13 @@ main (int argc, char *argv[])
           oflags['n'] = 0;
           oflags['w'] = 0;
 
-         /*Collect the child*/
+          /*Collect the child */
           waitpid (pid, NULL, 0);
 
-          /*if there was no k argument we need to find the*/
-          /*pid of the server process so that we can work out*/
-          /*what the socket is called. The server tells us on*/
-         /*a pipe*/
+          /*if there was no k argument we need to find the */
+          /*pid of the server process so that we can work out */
+          /*what the socket is called. The server tells us on */
+          /*a pipe */
 
           if (csnok)
             {
@@ -461,8 +491,8 @@ main (int argc, char *argv[])
     }
 
 
-  if (oflags['c'] && !oflags['k'])
-    fatal_moan ("-c requires a socket to be specified with -s or -k");
+  if (oflags['c'] && !oflags['k'] && !oflags['r'])
+    fatal_moan ("-c requires a socket to be specified with -s or -k or -r");
 
   if ((oflags['p'] || oflags['d'] || oflags['K'] || oflags['b'] || oflags['f']
        || oflags['L']) && oflags['c'])
@@ -504,7 +534,7 @@ main (int argc, char *argv[])
     }
 
 
-  if (oflags['s'] )
+  if (oflags['s'])
     {
       char *path;
       path = mome ("/.sympathy");
@@ -512,12 +542,13 @@ main (int argc, char *argv[])
       free (path);
 
 
-      if (!oflags['k']) {
-      pid = getpid ();
-       
-      oargs['k'] = mome ("/.sympathy/%s%d", hostname, pid);
-      oflags['k']++;
-       }
+      if (!oflags['k'])
+        {
+          pid = getpid ();
+
+          oargs['k'] = mome ("/.sympathy/%s%d", hostname, pid);
+          oflags['k']++;
+        }
 
       server_socket = socket_listen (oargs['k']);
 
@@ -602,6 +633,26 @@ main (int argc, char *argv[])
 
     }
 
+
+  FISH
+  {
+    char *id = oargs['r'];
+    if (id < 0)
+      fatal_moan ("cannot parse -r %s as an integer", oargs['r']);
+
+    oflags['k']++;
+    if (safe_atoi (id) > 0)
+      {
+        oargs['k'] = find_socket ("%s%d", hostname, safe_atoi (id));
+      }
+    else
+      {
+        oargs['k'] = find_socket ("%s", id);
+      }
+    oflags['r'] = 0;
+    oflags['c']++;
+  }
+
   if (oflags['c'])
     {