i3 - improved tiling WM


Remove references to PATH_MAX macro

Patch status: merged

Patch by Lancelot SIX

Long description:

Since the macro PATH_MAX is not defined on every system (GNU/Hurd being
one of those who do not define it), we remove all references to this
macro. Instead, we use a buffer of arbitraty size and grow it when
needed to contain paths.

To apply this patch, use:
curl http://cr.i3wm.org/patch/330/raw.patch | git am

b/i3-nagbar/main.c

21
@@ -159,8 +159,9 @@ static void handle_button_release(xcb_connection_t *conn, xcb_button_release_eve
22
     fclose(script);
23
 
24
     char *link_path;
25
+    char *exe_path = get_exe_path(argv0);
26
     sasprintf(&link_path, "%s.nagbar_cmd", script_path);
27
-    symlink(get_exe_path(argv0), link_path);
28
+    symlink(exe_path, link_path);
29
 
30
     char *terminal_cmd;
31
     sasprintf(&terminal_cmd, "i3-sensible-terminal -e %s", link_path);
32
@@ -172,6 +173,7 @@ static void handle_button_release(xcb_connection_t *conn, xcb_button_release_eve
33
     free(link_path);
34
     free(terminal_cmd);
35
     free(script_path);
36
+    free(exe_path);
37
 
38
     /* TODO: unset flag, re-render */
39
 }

b/include/libi3.h

44
@@ -372,7 +372,8 @@ char *get_process_filename(const char *prefix);
45
  *
46
  * The implementation follows http://stackoverflow.com/a/933996/712014
47
  *
48
+ * Returned value must be freed by the caller.
49
  */
50
-const char *get_exe_path(const char *argv0);
51
+char *get_exe_path(const char *argv0);
52
 
53
 #endif

b/libi3/get_exe_path.c

58
@@ -3,6 +3,7 @@
59
 #include <stdio.h>
60
 #include <limits.h>
61
 #include <stdlib.h>
62
+#include <errno.h>
63
 
64
 #include "libi3.h"
65
 
66
@@ -11,10 +12,14 @@
67
  *
68
  * The implementation follows http://stackoverflow.com/a/933996/712014
69
  *
70
+ * Returned value must be freed by the caller.
71
  */
72
-const char *get_exe_path(const char *argv0) {
73
-	static char destpath[PATH_MAX];
74
-	char tmp[PATH_MAX];
75
+char *get_exe_path(const char *argv0) {
76
+	size_t destpath_size = 1024;
77
+	size_t tmp_size = 1024;
78
+	char *destpath = smalloc(destpath_size);
79
+	char *tmp = smalloc(tmp_size);
80
+
81
 
82
 #if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
83
 	/* Linux and Debian/kFreeBSD provide /proc/self/exe */
84
@@ -25,30 +30,48 @@ const char *get_exe_path(const char *argv0) {
85
 #endif
86
 	ssize_t linksize;
87
 
88
-	if ((linksize = readlink(exepath, destpath, sizeof(destpath) - 1)) != -1) {
89
+	while ((linksize = readlink(exepath, destpath, destpath_size)) == destpath_size) {
90
+		destpath_size = destpath_size * 2;
91
+		destpath = srealloc(destpath, destpath_size);
92
+	}
93
+	if (linksize != -1) {
94
 		/* readlink() does not NULL-terminate strings, so we have to. */
95
 		destpath[linksize] = '\0';
96
-
97
+		free(tmp);
98
 		return destpath;
99
 	}
100
 #endif
101
 
102
 	/* argv[0] is most likely a full path if it starts with a slash. */
103
-	if (argv0[0] == '/')
104
-		return argv0;
105
+	if (argv0[0] == '/') {
106
+		free(tmp);
107
+		free(destpath);
108
+		return sstrdup(argv0);
109
+	}
110
 
111
 	/* if argv[0] contains a /, prepend the working directory */
112
-	if (strchr(argv0, '/') != NULL &&
113
-		getcwd(tmp, sizeof(tmp)) != NULL) {
114
-		snprintf(destpath, sizeof(destpath), "%s/%s", tmp, argv0);
115
-		return destpath;
116
+	if (strchr(argv0, '/') != NULL) {
117
+		char *retgcwd;
118
+		while ((retgcwd = getcwd(tmp, tmp_size)) == NULL && errno == ERANGE) {
119
+			tmp_size = tmp_size * 2;
120
+			tmp = srealloc(tmp, tmp_size);
121
+		}
122
+		if (retgcwd != NULL) {
123
+			free(destpath);
124
+			sasprintf(&destpath, "%s/%s", tmp, argv0);
125
+			free(tmp);
126
+			return destpath;
127
+		}
128
 	}
129
 
130
 	/* Fall back to searching $PATH (or _CS_PATH in absence of $PATH). */
131
 	char *path = getenv("PATH");
132
 	if (path == NULL) {
133
 		/* _CS_PATH is typically something like "/bin:/usr/bin" */
134
-		confstr(_CS_PATH, tmp, sizeof(tmp));
135
+		while (confstr(_CS_PATH, tmp, tmp_size) > tmp_size) {
136
+			tmp_size = tmp_size * 2;
137
+			tmp = srealloc(tmp, tmp_size);
138
+		}
139
 		sasprintf(&path, ":%s", tmp);
140
 	} else {
141
 		path = strdup(path);
142
@@ -59,16 +82,20 @@ const char *get_exe_path(const char *argv0) {
143
 		if ((component = strtok(str, ":")) == NULL)
144
 			break;
145
 		str = NULL;
146
-		snprintf(destpath, sizeof(destpath), "%s/%s", component, argv0);
147
+		free(destpath);
148
+		sasprintf(&destpath, "%s/%s", component, argv0);
149
 		/* Of course this is not 100% equivalent to actually exec()ing the
150
 		 * binary, but meh. */
151
 		if (access(destpath, X_OK) == 0) {
152
 			free(path);
153
+			free(tmp);
154
 			return destpath;
155
 		}
156
 	}
157
+	free(destpath);
158
 	free(path);
159
+	free(tmp);
160
 
161
 	/* Last resort: maybe it’s in /usr/bin? */
162
-	return "/usr/bin/i3-nagbar";
163
+	return sstrdup("/usr/bin/i3-nagbar");
164
 }

b/src/display_version.c

169
@@ -128,13 +128,18 @@ void display_running_version(void) {
170
     printf("\rRunning i3 version: %s (pid %s)\n", human_readable_version, pid_from_atom);
171
 
172
 #ifdef __linux__
173
-    char exepath[PATH_MAX],
174
-         destpath[PATH_MAX];
175
+    size_t destpath_size = 1024;
176
     ssize_t linksize;
177
+    char *exepath;
178
+    char *destpath = smalloc(destpath_size);
179
 
180
-    snprintf(exepath, sizeof(exepath), "/proc/%d/exe", getpid());
181
+    sasprintf(&exepath, "/proc/%d/exe", getpid());
182
 
183
-    if ((linksize = readlink(exepath, destpath, sizeof(destpath))) == -1)
184
+    while ((linksize = readlink(exepath, destpath, destpath_size)) == destpath_size) {
185
+            destpath_size = destpath_size * 2;
186
+            destpath = srealloc(destpath, destpath_size);
187
+    }
188
+    if (linksize == -1)
189
         err(EXIT_FAILURE, "readlink(%s)", exepath);
190
 
191
     /* readlink() does not NULL-terminate strings, so we have to. */
192
@@ -143,9 +148,14 @@ void display_running_version(void) {
193
     printf("\n");
194
     printf("The i3 binary you just called: %s\n", destpath);
195
 
196
-    snprintf(exepath, sizeof(exepath), "/proc/%s/exe", pid_from_atom);
197
+    free(exepath);
198
+    sasprintf(&exepath, "/proc/%s/exe", pid_from_atom);
199
 
200
-    if ((linksize = readlink(exepath, destpath, sizeof(destpath))) == -1)
201
+    while ((linksize = readlink(exepath, destpath, destpath_size)) == destpath_size) {
202
+        destpath_size = destpath_size * 2;
203
+        destpath = srealloc(destpath, destpath_size);
204
+    }
205
+    if (linksize == -1)
206
         err(EXIT_FAILURE, "readlink(%s)", exepath);
207
 
208
     /* readlink() does not NULL-terminate strings, so we have to. */
209
@@ -159,7 +169,8 @@ void display_running_version(void) {
210
     /* Since readlink() might put a "(deleted)" somewhere in the buffer and
211
      * stripping that out seems hackish and ugly, we read the process’s argv[0]
212
      * instead. */
213
-    snprintf(exepath, sizeof(exepath), "/proc/%s/cmdline", pid_from_atom);
214
+    free(exepath);
215
+    sasprintf(&exepath, "/proc/%s/cmdline", pid_from_atom);
216
 
217
     int fd;
218
     if ((fd = open(exepath, O_RDONLY)) == -1)
219
@@ -169,6 +180,9 @@ void display_running_version(void) {
220
     close(fd);
221
 
222
     printf("The i3 binary you are running: %s\n", destpath);
223
+
224
+    free(exepath);
225
+    free(destpath);
226
 #endif
227
 
228
     yajl_free(handle);

b/src/main.c

233
@@ -488,18 +488,25 @@ int main(int argc, char *argv[]) {
234
 
235
         /* The following code is helpful, but not required. We thus don’t pay
236
          * much attention to error handling, non-linux or other edge cases. */
237
-        char cwd[PATH_MAX];
238
         LOG("CORE DUMPS: You are running a development version of i3, so coredumps were automatically enabled (ulimit -c unlimited).\n");
239
-        if (getcwd(cwd, sizeof(cwd)) != NULL)
240
+        size_t cwd_size = 1024;
241
+        char *cwd = smalloc(cwd_size);
242
+        char *cwd_ret;
243
+        while ((cwd_ret = getcwd(cwd, cwd_size)) == NULL && errno == ERANGE) {
244
+            cwd_size = cwd_size * 2;
245
+            cwd = srealloc(cwd, cwd_size);
246
+        }
247
+        if (cwd_ret != NULL)
248
             LOG("CORE DUMPS: Your current working directory is \"%s\".\n", cwd);
249
         int patternfd;
250
         if ((patternfd = open("/proc/sys/kernel/core_pattern", O_RDONLY)) >= 0) {
251
-            memset(cwd, '\0', sizeof(cwd));
252
-            if (read(patternfd, cwd, sizeof(cwd)) > 0)
253
+            memset(cwd, '\0', cwd_size);
254
+            if (read(patternfd, cwd, cwd_size) > 0)
255
                 /* a trailing newline is included in cwd */
256
                 LOG("CORE DUMPS: Your core_pattern is: %s", cwd);
257
             close(patternfd);
258
         }
259
+        free(cwd);
260
     }
261
 
262
     LOG("i3 " I3_VERSION " starting\n");