From b58921c7addb45d028e52954f75309a6a9ffa84d Mon Sep 17 00:00:00 2001 From: George Bosilca Date: Thu, 19 Jan 2006 06:58:49 +0000 Subject: [PATCH] Correctly compute the length of the path (including systems where the delimitator is not just one char) and use the computed length. Handle Windows paths as they don't have to start with /. This commit was SVN r8744. --- opal/util/os_path.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/opal/util/os_path.c b/opal/util/os_path.c index d23992e837..0bd2f13d2b 100644 --- a/opal/util/os_path.c +++ b/opal/util/os_path.c @@ -64,7 +64,7 @@ char *opal_os_path(bool relative, ...) if (0 == num_elements) { /* must be looking for a simple answer */ path = (char *)malloc(3); - path[0] = '\0'; + path[0] = '\0'; if (relative) { strcpy(path, "."); strcat(path, path_sep); @@ -79,21 +79,30 @@ char *opal_os_path(bool relative, ...) /* setup path with enough room for the string terminator, the elements, and the separator between each of the elements */ - total_length = total_length + num_elements + 1; + total_length = total_length + num_elements * strlen(path_sep) + 1; if (total_length > MAXPATHLEN) { /* path length is too long - reject it */ return(NULL); } - path = (char *)malloc(2 + total_length + num_elements-1); + path = (char *)malloc(total_length); if (NULL == path) { - return(NULL); + return(NULL); } path[0] = 0; if (relative) { - strcpy(path, "."); + strcpy(path, "."); } + /* Windows does not require to have the initial separator. */ + if( NULL != (element=va_arg(ap1, char*)) ) { +#ifndef __WINDOWS__ + if (path_sep[0] != element[0]) { + strcat(path, path_sep); + } +#endif /* __WINDOWS__ */ + strcat(path, element); + } while (NULL != (element=va_arg(ap1, char*))) { if (path_sep[0] != element[0]) { strcat(path, path_sep);