Hi,

I just ported you latest version of i3 to OpenBSD. Here are the changes I made. OpenBSD does not support wordexp.h, therefore I implemented the same behavior of the glob_path() function using only glob. I think it is more clean in this way, using only one, not two interfaces. I also renamed the function to tilde_expansion(), because it reflects better what the function actually does.

The changes to common.mk where necessary, because overriding PREFIX brakes the ports fake framework. I hope this change doesn't break the debian packaging scrips.


Viele Grüße,

Christopher
>From 1adec4de0f4ff9ad6fa2e43874487ff916f11900 Mon Sep 17 00:00:00 2001
From: Christopher Zimmermann <madro...@pundit.(none)>
Date: Thu, 15 Jul 2010 14:35:17 +0200
Subject: [PATCH 1/4] don't use wordexp.h for tilde expansion

wordexp.h is not supported by OpenBSD. Therefore do tilde expansion only
via glob().
rename glob_path() to resolve_tilde() since it should not do globbing.
---
 include/config.h |    4 +++-
 src/config.c     |   43 ++++++++++++++++++++++++-------------------
 src/ipc.c        |   16 ++++++++--------
 3 files changed, 35 insertions(+), 28 deletions(-)

diff --git a/include/config.h b/include/config.h
index 0c790bf..22b0c47 100644
--- a/include/config.h
+++ b/include/config.h
@@ -126,9 +126,11 @@ struct Config {
 
 /**
  * This function resolves ~ in pathnames.
+ * It may resolve wildcards in the first part of the path, but if no match
+ * or multiple matches are found, it just returns a copy of path as given.
  *
  */
-char *glob_path(const char *path);
+char *resolve_tilde(const char *path);
 
 /**
  * Checks if the given path exists by calling stat().
diff --git a/src/config.c b/src/config.c
index 972e376..593593d 100644
--- a/src/config.c
+++ b/src/config.c
@@ -18,7 +18,6 @@
 #include <sys/types.h>
 #include <stdlib.h>
 #include <glob.h>
-#include <wordexp.h>
 #include <unistd.h>
 
 /* We need Xlib for XStringToKeysym */
@@ -39,26 +38,32 @@ struct modes_head modes;
 
 /*
  * This function resolves ~ in pathnames.
+ * It may resolve wildcards in the first part of the path, but if no match
+ * or multiple matches are found, it just returns a copy of path as given.
  *
  */
-char *glob_path(const char *path) {
+char *resolve_tilde(const char *path) {
         static glob_t globbuf;
-        if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
-                die("glob() failed");
-        char *result = sstrdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : 
path);
-        globfree(&globbuf);
+        char *head, *tail, *result;
 
-        /* If the file does not exist yet, we still may need to resolve tilde,
-         * so call wordexp */
-        if (strcmp(result, path) == 0) {
-                wordexp_t we;
-                wordexp(path, &we, WRDE_NOCMD);
-                if (we.we_wordc > 0) {
-                        free(result);
-                        result = sstrdup(we.we_wordv[0]);
-                }
-                wordfree(&we);
+        tail = strchr(path, '/');
+        head = strndup(path, tail ? tail - path : strlen(path));
+
+        int res = glob(head, GLOB_TILDE, NULL, &globbuf);
+        free(head);
+        /* no match, or many wildcard matches are bad */
+        if(res == GLOB_NOMATCH || globbuf.gl_pathc != 1)
+                result = strdup(path);
+        else if (res != 0) {
+                die("glob() failed");
+        }
+        else {
+                head = globbuf.gl_pathv[0];
+                result = malloc(strlen(head) + (tail ? strlen(tail) : 0) + 1);
+                strncpy(result, head, strlen(head));
+                strncat(result, tail, strlen(tail));
         }
+        globfree(&globbuf);
 
         return result;
 }
@@ -239,7 +244,7 @@ static char *get_config_path() {
         if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
                 xdg_config_home = "~/.config";
 
-        xdg_config_home = glob_path(xdg_config_home);
+        xdg_config_home = resolve_tilde(xdg_config_home);
         if (asprintf(&config_path, "%s/i3/config", xdg_config_home) == -1)
                 die("asprintf() failed");
         free(xdg_config_home);
@@ -255,7 +260,7 @@ static char *get_config_path() {
         char *buf = strdup(xdg_config_dirs);
         char *tok = strtok(buf, ":");
         while (tok != NULL) {
-                tok = glob_path(tok);
+                tok = resolve_tilde(tok);
                 if (asprintf(&config_path, "%s/i3/config", tok) == -1)
                         die("asprintf() failed");
                 free(tok);
@@ -269,7 +274,7 @@ static char *get_config_path() {
         free(buf);
 
         /* 3: check traditional paths */
-        config_path = glob_path("~/.i3/config");
+        config_path = resolve_tilde("~/.i3/config");
         if (path_exists(config_path))
                 return config_path;
 
diff --git a/src/ipc.c b/src/ipc.c
index 1937d55..67d482b 100644
--- a/src/ipc.c
+++ b/src/ipc.c
@@ -502,20 +502,20 @@ void ipc_new_client(EV_P_ struct ev_io *w, int revents) {
 int ipc_create_socket(const char *filename) {
         int sockfd;
 
-        char *globbed = glob_path(filename);
-        DLOG("Creating IPC-socket at %s\n", globbed);
-        char *copy = sstrdup(globbed);
+        char *resolved = resolve_tilde(filename);
+        DLOG("Creating IPC-socket at %s\n", resolved);
+        char *copy = sstrdup(resolved);
         const char *dir = dirname(copy);
         if (!path_exists(dir))
                 mkdirp(dir);
         free(copy);
 
         /* Unlink the unix domain socket before */
-        unlink(globbed);
+        unlink(resolved);
 
         if ((sockfd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0) {
                 perror("socket()");
-                free(globbed);
+                free(resolved);
                 return -1;
         }
 
@@ -524,14 +524,14 @@ int ipc_create_socket(const char *filename) {
         struct sockaddr_un addr;
         memset(&addr, 0, sizeof(struct sockaddr_un));
         addr.sun_family = AF_LOCAL;
-        strcpy(addr.sun_path, globbed);
+        strncpy(addr.sun_path, resolved, sizeof(addr.sun_path) - 1);
         if (bind(sockfd, (struct sockaddr*)&addr, sizeof(struct sockaddr_un)) 
< 0) {
                 perror("bind()");
-                free(globbed);
+                free(resolved);
                 return -1;
         }
 
-        free(globbed);
+        free(resolved);
         set_nonblock(sockfd);
 
         if (listen(sockfd, 5) < 0) {
-- 
1.7.1

>From 7578800257728f293ba9fc655df967628c4307dd Mon Sep 17 00:00:00 2001
From: Christopher Zimmermann <madro...@pundit.(none)>
Date: Thu, 15 Jul 2010 14:38:29 +0200
Subject: [PATCH 2/4] secure strcpy by replacing with strncpy

---
 i3-input/ipc.c |    2 +-
 i3-msg/main.c  |    2 +-
 src/ipc.c      |    2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/i3-input/ipc.c b/i3-input/ipc.c
index 597a86e..2d11f0e 100644
--- a/i3-input/ipc.c
+++ b/i3-input/ipc.c
@@ -60,7 +60,7 @@ int connect_ipc(char *socket_path) {
         struct sockaddr_un addr;
         memset(&addr, 0, sizeof(struct sockaddr_un));
         addr.sun_family = AF_LOCAL;
-        strcpy(addr.sun_path, socket_path);
+        strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
         if (connect(sockfd, (const struct sockaddr*)&addr, sizeof(struct 
sockaddr_un)) < 0)
                 err(EXIT_FAILURE, "Could not connect to i3");
 
diff --git a/i3-msg/main.c b/i3-msg/main.c
index b22d550..33bedc7 100644
--- a/i3-msg/main.c
+++ b/i3-msg/main.c
@@ -175,7 +175,7 @@ int main(int argc, char *argv[]) {
         struct sockaddr_un addr;
         memset(&addr, 0, sizeof(struct sockaddr_un));
         addr.sun_family = AF_LOCAL;
-        strcpy(addr.sun_path, socket_path);
+        strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
         if (connect(sockfd, (const struct sockaddr*)&addr, sizeof(struct 
sockaddr_un)) < 0)
                 err(EXIT_FAILURE, "Could not connect to i3");
 
diff --git a/src/ipc.c b/src/ipc.c
index 67d482b..fcda355 100644
--- a/src/ipc.c
+++ b/src/ipc.c
@@ -91,7 +91,7 @@ static void ipc_send_message(int fd, const unsigned char 
*payload,
         char msg[buffer_size];
         char *walk = msg;
 
-        strcpy(walk, "i3-ipc");
+        strncpy(walk, "i3-ipc", buffer_size - 1);
         walk += strlen("i3-ipc");
         memcpy(walk, &message_size, sizeof(uint32_t));
         walk += sizeof(uint32_t);
-- 
1.7.1

>From 6d240e9685f4ea16d6af1f9676123ece601d15e3 Mon Sep 17 00:00:00 2001
From: Christopher Zimmermann <madro...@pundit.(none)>
Date: Thu, 15 Jul 2010 16:34:20 +0200
Subject: [PATCH 3/4] gitignore

---
 .gitignore |   22 ++++++++++++++++++++++
 1 files changed, 22 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore
index 5761abc..8405357 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,23 @@
 *.o
+
+i3
+i3-input/i3-input
+i3-msg/i3-msg
+
+include/loglevels.h
+loglevels.tmp
+
+src/cfgparse.output
+src/cfgparse.tab.c
+src/cfgparse.tab.h
+src/cfgparse.yy.c
+
+i3-3.e-bf1/
+man/i3-msg.1
+man/i3-msg.xml
+man/i3-wsbar.1
+man/i3.1
+man/i3.xml
+
+
+tags
-- 
1.7.1

>From 360e2b8d13a84f19312c86d681dd7b6348e056ed Mon Sep 17 00:00:00 2001
From: Christopher Zimmermann <madro...@pundit.(none)>
Date: Thu, 15 Jul 2010 16:31:34 +0200
Subject: [PATCH 4/4] set PREFIX and SYSCONFDIR only if not already set

---
 common.mk |   10 +++++-----
 1 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/common.mk b/common.mk
index 0334ac6..c3327d4 100644
--- a/common.mk
+++ b/common.mk
@@ -1,11 +1,11 @@
 UNAME=$(shell uname)
 DEBUG=1
 INSTALL=install
-PREFIX=/usr
-ifeq ($(PREFIX),/usr)
-SYSCONFDIR=/etc
-else
-SYSCONFDIR=$(PREFIX)/etc
+ifndef PREFIX
+  PREFIX=/usr
+endif
+ifndef SYSCONFDIR
+  SYSCONFDIR=/etc
 endif
 GIT_VERSION:="$(shell git describe --tags --always) ($(shell git log 
--pretty=format:%cd --date=short -n1))"
 VERSION:=$(shell git describe --tags --abbrev=0)
-- 
1.7.1

Reply via email to