Add check for --help and --version support
[awesome.git] / awesome-client-common.c
blob2dfb0eca9a7b641ff40ad1218c9276d8c9c2aed5
1 /*
2 * awesome-client-common.c - awesome client, communicate with socket, common functions
4 * Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
5 * Copyright © 2007 daniel@brinkers.de
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <stdio.h>
24 #include <sys/socket.h>
25 #include <sys/un.h>
27 #include "awesome-client.h"
28 #include "util.h"
30 #define CONTROL_UNIX_SOCKET_PATH ".awesome_ctl."
32 struct sockaddr_un *
33 get_client_addr(const char *display)
35 char *homedir, *tmp;
36 const char *real_display = NULL;
37 ssize_t path_len;
38 struct sockaddr_un *addr;
40 addr = p_new(struct sockaddr_un, 1);
41 homedir = getenv("HOME");
42 if(a_strlen(display))
44 if((tmp = strchr(display, ':')))
45 real_display = tmp + 1;
46 else
47 real_display = display;
48 if((tmp = strrchr(display, '.')))
49 *tmp = '\0';
52 /* a_strlen(display) because we strcat on display and
53 * + 2 for / and \0 */
54 path_len = a_strlen(homedir) + a_strlen(CONTROL_UNIX_SOCKET_PATH)
55 + (a_strlen(display) ? (a_strlen(real_display)) : 1) + 2;
57 if(path_len >= ssizeof(addr->sun_path))
59 fprintf(stderr, "error: path of control UNIX domain socket is too long");
60 return NULL;
62 a_strcpy(addr->sun_path, path_len, homedir);
63 a_strcat(addr->sun_path, path_len, "/");
64 a_strcat(addr->sun_path, path_len, CONTROL_UNIX_SOCKET_PATH);
65 if(a_strlen(display))
66 a_strcat(addr->sun_path, path_len, real_display);
67 else
68 a_strcat(addr->sun_path, path_len, "0");
70 addr->sun_family = AF_UNIX;
72 return addr;
75 int
76 get_client_socket(void)
78 int csfd;
80 csfd = socket(AF_UNIX, SOCK_DGRAM, 0);
82 if(csfd < 0)
83 perror("error opening UNIX domain socket");
85 return csfd;
87 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80