+extern void usage (void);
+
+static char hostname[1024];
+
+int
+safe_atoi (char *a)
+{
+ char *end;
+ int ret;
+
+ if (!a)
+ return -1;
+
+ ret = (int) strtol (a, &end, 0);
+
+ if (end == a)
+ return -1;
+
+ return ret;
+}
+
+char *
+fatal_moan (char *fmt, ...)
+{
+ va_list ap;
+
+
+ va_start (ap, fmt);
+ vfprintf (stderr, fmt, ap);
+ va_end (ap);
+
+ putc ('\n', stderr);
+ exit (1);
+}
+
+
+
+/*make the path in fmt from home (hence the name) */
+
+char *
+mome (char *fmt, ...)
+{
+
+ int n;
+ int homelen;
+ char *buf, *home;
+ va_list ap;
+ int size;
+
+
+ home = getenv ("HOME");
+ if (!home)
+ return NULL;
+
+ homelen = strlen (home) + 1;
+
+ size = 1024 + homelen;
+
+ buf = malloc (size);
+
+ if (!buf)
+ fatal_moan ("malloc failed");
+
+ strcpy (buf, home);
+ strcat (buf, "/");
+
+ while (1)
+ {
+
+
+ va_start (ap, fmt);
+ n = vsnprintf (buf + homelen, size - homelen, fmt, ap);
+ va_end (ap);
+
+ if (n > -1 && n < (size - homelen))
+ return buf;
+
+ if (n > -1) /* glibc 2.1 */
+ size = homelen + n + 1;
+ else /* glibc 2.0 */
+ size *= 2; /* twice the old size */
+
+ buf = realloc (buf, size);
+
+ if (!buf)
+ fatal_moan ("malloc failed");
+ }
+}
+
+
+int
+list_sockets (void)
+{
+ struct dirent *ent;
+ struct stat buf;
+ char *sn = NULL;
+ Socket *s;
+ char *sockdir = mome ("/.sympathy/");
+ DIR *dir = opendir (sockdir);
+
+
+
+ int hostname_len = strlen (hostname);
+
+ if (!dir)
+ fatal_moan ("can't examine %s for sockets", sockdir);
+
+ rewinddir (dir);
+
+
+ while ((ent = readdir (dir)))
+ {
+ sn = mome (".sympathy/%s", ent->d_name);
+ if (stat (sn, &buf) || (!S_ISSOCK (buf.st_mode)))
+ {
+ free (sn);
+ continue;
+ }
+
+ s = socket_connect (sn);
+
+ if (s)
+ {
+ printf ("\t%s (Active)\n", ent->d_name);
+ socket_free (s);
+ }
+ else
+ {
+ if (strncmp (ent->d_name, hostname, hostname_len))
+ {
+ printf ("\t%s (Unknown - not this host)\n", ent->d_name);
+ }
+ else
+ {
+ printf ("\t%s (Dead, wiped)\n", ent->d_name);
+ unlink (sn);
+ }
+ }
+
+ free (sn);
+ }
+
+ closedir (dir);
+
+ return 0;
+}
+
+
+void
+get_hostname (void)
+{
+ struct utsname name;
+
+ if (uname (&name))
+ {
+ strcpy (hostname, "unknown.");
+ return;
+ }
+
+
+ strcpy (hostname, name.nodename);
+ strcat (hostname, ".");
+}
+
+