i3 - improved tiling WM


Remove references to PATH_MAX macro

Patch status: needinfo

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/323/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
@@ -382,7 +382,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,51 @@ 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
+			while (snprintf(destpath, destpath_size, "%s/%s", tmp, argv0) >= destpath_size) {
124
+				destpath_size = destpath_size * 2;
125
+				destpath = srealloc(destpath, destpath_size);
126
+			}
127
+
128
+			free(tmp);
129
+			return destpath;
130
+		}
131
 	}
132
 
133
 	/* Fall back to searching $PATH (or _CS_PATH in absence of $PATH). */
134
 	char *path = getenv("PATH");
135
 	if (path == NULL) {
136
 		/* _CS_PATH is typically something like "/bin:/usr/bin" */
137
-		confstr(_CS_PATH, tmp, sizeof(tmp));
138
+		while (confstr(_CS_PATH, tmp, tmp_size) > tmp_size) {
139
+			tmp_size = tmp_size * 2;
140
+			tmp = srealloc(tmp, tmp_size);
141
+		}
142
 		sasprintf(&path, ":%s", tmp);
143
 	} else {
144
 		path = strdup(path);
145
@@ -59,16 +85,22 @@ const char *get_exe_path(const char *argv0) {
146
 		if ((component = strtok(str, ":")) == NULL)
147
 			break;
148
 		str = NULL;
149
-		snprintf(destpath, sizeof(destpath), "%s/%s", component, argv0);
150
+		while(snprintf(destpath, destpath_size, "%s/%s", component, argv0) >= destpath_size) {
151
+			destpath_size = destpath_size * 2;
152
+			destpath = srealloc(destpath, destpath_size);
153
+		}
154
 		/* Of course this is not 100% equivalent to actually exec()ing the
155
 		 * binary, but meh. */
156
 		if (access(destpath, X_OK) == 0) {
157
 			free(path);
158
+			free(tmp);
159
 			return destpath;
160
 		}
161
 	}
162
-	free(path);
163
 
164
 	/* Last resort: maybe it’s in /usr/bin? */
165
-	return "/usr/bin/i3-nagbar";
166
+	free(destpath);
167
+	free(path);
168
+	free(tmp);
169
+	return sstrdup("/usr/bin/i3-nagbar");
170
 }

b/src/display_version.c

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

b/src/main.c

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