dovecot-2.0: Added t_binary_abspath().

dovecot at dovecot.org dovecot at dovecot.org
Sat Mar 27 02:58:59 EET 2010


details:   http://hg.dovecot.org/dovecot-2.0/rev/f483bbface26
changeset: 11001:f483bbface26
user:      Timo Sirainen <tss at iki.fi>
date:      Sat Mar 27 02:58:57 2010 +0200
description:
Added t_binary_abspath().

diffstat:

 src/lib-master/master-service-settings.c |  20 ++------------------
 src/lib/abspath.c                        |  32 ++++++++++++++++++++++++++++++++
 src/lib/abspath.h                        |   8 ++++++++
 3 files changed, 42 insertions(+), 18 deletions(-)

diffs (95 lines):

diff -r 679e99e430d7 -r f483bbface26 src/lib-master/master-service-settings.c
--- a/src/lib-master/master-service-settings.c	Sat Mar 27 02:39:49 2010 +0200
+++ b/src/lib-master/master-service-settings.c	Sat Mar 27 02:58:57 2010 +0200
@@ -66,25 +66,9 @@
 master_service_exec_config(struct master_service *service,
 			   const struct master_service_settings_input *input)
 {
-	const char **conf_argv, *path, *const *paths, *binary_path;
+	const char **conf_argv, *binary_path = service->argv[0];
 
-	binary_path = service->argv[0];
-	if (*service->argv[0] == '/') {
-		/* already have the path */
-	} else if (strchr(service->argv[0], '/') != NULL) {
-		/* relative to current directory */
-		binary_path = t_abspath(service->argv[0]);
-	} else if ((path = getenv("PATH")) != NULL) {
-		/* we have to find our executable from path */
-		paths = t_strsplit(path, ":");
-		for (; *paths != NULL; paths++) {
-			path = t_strconcat(*paths, "/", binary_path, NULL);
-			if (access(path, X_OK) == 0) {
-				binary_path = path;
-				break;
-			}
-		}
-	}
+	(void)t_binary_abspath(&binary_path);
 
 	if (!service->keep_environment)
 		master_service_env_clean(input->preserve_home);
diff -r 679e99e430d7 -r f483bbface26 src/lib/abspath.c
--- a/src/lib/abspath.c	Sat Mar 27 02:39:49 2010 +0200
+++ b/src/lib/abspath.c	Sat Mar 27 02:58:57 2010 +0200
@@ -1,8 +1,10 @@
 /* Copyright (c) 2009-2010 Dovecot authors, see the included COPYING file */
 
 #include "lib.h"
+#include "str.h"
 #include "abspath.h"
 
+#include <stdlib.h>
 #include <unistd.h>
 
 const char *t_abspath(const char *path)
@@ -63,3 +65,33 @@
 	*dest_r = dest;
 	return 0;
 }
+
+bool t_binary_abspath(const char **binpath)
+{
+	const char *path_env, *const *paths;
+	string_t *path;
+
+	if (**binpath == '/') {
+		/* already have absolute path */
+		return TRUE;
+	} else if (strchr(*binpath, '/') != NULL) {
+		/* relative to current directory */
+		*binpath = t_abspath(*binpath);
+		return TRUE;
+	} else if ((path_env = getenv("PATH")) != NULL) {
+		/* we have to find our executable from path */
+		path = t_str_new(256);
+		paths = t_strsplit(path_env, ":");
+		for (; *paths != NULL; paths++) {
+			str_append(path, *paths);
+			str_append_c(path, '/');
+			str_append(path, *binpath);
+			if (access(str_c(path), X_OK) == 0) {
+				*binpath = str_c(path);
+				return TRUE;
+			}
+			str_truncate(path, 0);
+		}
+	}
+	return FALSE;
+}
diff -r 679e99e430d7 -r f483bbface26 src/lib/abspath.h
--- a/src/lib/abspath.h	Sat Mar 27 02:39:49 2010 +0200
+++ b/src/lib/abspath.h	Sat Mar 27 02:58:57 2010 +0200
@@ -12,4 +12,12 @@
 /* Returns symlink destination, allocated from data stack. */
 int t_readlink(const char *path, const char **dest_r);
 
+/* Update binpath to be absolute:
+   a) begins with '/' -> no change
+   b) contains '/' -> assume relative to working directory
+   c) set to first executable that's found from $PATH
+
+   If no usable binary was found, return FALSE. */
+bool t_binary_abspath(const char **binpath);
+
 #endif


More information about the dovecot-cvs mailing list