25 #include <netinet/in.h>
35 char inbuf[256], *start = NULL;
36 int index, cur, length;
37 char *usr = getenv (
"USER");
38 char *path = malloc (strlen (
"/tmp/babelsocket-") + strlen (usr) + 1);
39 strcpy (path,
"/tmp/babelsocket-");
41 if (strlen (path) >= 107) {
42 puts (
"path too long");
45 if (stat (path, &statbuf)) {
46 char *args[] = {LIBEXECDIR
"/babelserver", NULL};
48 g_spawn_async (NULL, (
char **) args, NULL, 0, NULL, NULL, NULL, &error);
55 while (stat (path, &statbuf));
57 int babelsocket = socket (AF_UNIX, SOCK_STREAM, 0);
58 if (babelsocket == -1) {
59 perror (
"Could not create the socket");
63 struct sockaddr_un adr_serv;
64 adr_serv.sun_family = AF_UNIX;
65 strcpy (adr_serv.sun_path, path);
67 if (connect (babelsocket, (
const struct sockaddr*) &adr_serv,
sizeof (
struct sockaddr_un)) == -1) {
68 perror (
"Connexion failed");
71 char const *buf =
"-i xyz -o inchi ";
72 write (babelsocket, buf, strlen (buf));
73 buf =
"5\n\nC 0 0 0\nH 0 1.093 0\nH 1.030490282 -0.364333333 0\nH -0.515245141 -0.364333333 0.892430763\nH -0.515245141 -0.364333333 -0.892430763";
74 char *size = g_strdup_printf (
"-l %u -D", strlen (buf));
75 write (babelsocket, size, strlen (size));
76 write (babelsocket, buf, strlen (buf));
78 if ((cur = read (babelsocket, inbuf + index, 255 - index))) {
82 if ((start = strchr (inbuf,
' '))) {
83 length = strtol (inbuf, NULL, 10);
87 if (index - (start - inbuf) == length) {
88 printf (
"answer is: %s\n", start);