Automatically check for sockets without screen name.

This commit is contained in:
Michael Andrews 2014-11-09 15:56:26 -05:00
parent c49bc92d12
commit 22c69d415d

51
bspc.c
View file

@ -22,6 +22,7 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include <string.h>
#include <stdlib.h> #include <stdlib.h>
#ifdef __OpenBSD__ #ifdef __OpenBSD__
#include <sys/types.h> #include <sys/types.h>
@ -33,6 +34,8 @@
#include "helpers.h" #include "helpers.h"
#include "common.h" #include "common.h"
extern char *strdup(const char*);
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int fd; int fd;
@ -43,11 +46,45 @@ int main(int argc, char *argv[])
err("No arguments given.\n"); err("No arguments given.\n");
sock_address.sun_family = AF_UNIX; sock_address.sun_family = AF_UNIX;
char *sp = getenv(SOCKET_ENV_VAR); char *sp, *dp, *tdp;
if (sp != NULL) int con, it;
unsigned int dplen;
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
err("Failed to create the socket.\n");
sp = getenv(SOCKET_ENV_VAR);
if (sp != NULL) {
snprintf(sock_address.sun_path, sizeof(sock_address.sun_path), "%s", sp); snprintf(sock_address.sun_path, sizeof(sock_address.sun_path), "%s", sp);
else con = connect(fd, (struct sockaddr *) &sock_address, sizeof(sock_address));
snprintf(sock_address.sun_path, sizeof(sock_address.sun_path), SOCKET_PATH_TPL, getenv("DISPLAY")); } else {
con = -1;
}
dp = getenv("DISPLAY");
if (con == -1 && dp != NULL) {
snprintf(sock_address.sun_path, sizeof(sock_address.sun_path), SOCKET_PATH_TPL, dp);
con = connect(fd, (struct sockaddr *) &sock_address, sizeof(sock_address));
}
if (con == -1 && dp != NULL) {
tdp = strdup(dp);
dplen = strlen(dp);
for (it = dplen - 1; (it > 1) && (tdp[it] != ':'); it--) {
if (tdp[it] == '.') {
tdp[it] = '\0';
break;
}
}
if (strlen(tdp) != dplen) {
snprintf(sock_address.sun_path, sizeof(sock_address.sun_path), SOCKET_PATH_TPL, tdp);
con = connect(fd, (struct sockaddr *) &sock_address, sizeof(sock_address));
}
free(tdp);
}
if (con == -1)
err("Failed to connect to the socket.\n");
argc--, argv++; argc--, argv++;
int msg_len = 0; int msg_len = 0;
@ -57,12 +94,6 @@ int main(int argc, char *argv[])
msg_len += n; msg_len += n;
} }
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
err("Failed to create the socket.\n");
if (connect(fd, (struct sockaddr *) &sock_address, sizeof(sock_address)) == -1)
err("Failed to connect to the socket.\n");
if (send(fd, msg, msg_len, 0) == -1) if (send(fd, msg, msg_len, 0) == -1)
err("Failed to send the data.\n"); err("Failed to send the data.\n");