2004-05-26 06:23:01 +04:00
|
|
|
/*
|
2005-11-05 22:57:48 +03:00
|
|
|
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
|
|
|
|
* University Research and Technology
|
|
|
|
* Corporation. All rights reserved.
|
|
|
|
* Copyright (c) 2004-2005 The University of Tennessee and The University
|
|
|
|
* of Tennessee Research Foundation. All rights
|
|
|
|
* reserved.
|
2004-11-28 23:09:25 +03:00
|
|
|
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
|
|
|
|
* University of Stuttgart. All rights reserved.
|
2005-03-24 15:43:37 +03:00
|
|
|
* Copyright (c) 2004-2005 The Regents of the University of California.
|
|
|
|
* All rights reserved.
|
2004-11-22 04:38:40 +03:00
|
|
|
* $COPYRIGHT$
|
|
|
|
*
|
|
|
|
* Additional copyrights may follow
|
|
|
|
*
|
2004-05-26 06:23:01 +04:00
|
|
|
* $HEADER$
|
|
|
|
*/
|
|
|
|
|
2006-02-12 04:33:29 +03:00
|
|
|
#include "opal_config.h"
|
2004-05-26 06:23:01 +04:00
|
|
|
|
|
|
|
#include <string.h>
|
2004-10-20 05:03:09 +04:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2004-05-26 06:23:01 +04:00
|
|
|
#include <unistd.h>
|
2007-04-01 20:16:54 +04:00
|
|
|
#endif /* HAVE_UNISTD_H */
|
2004-10-20 05:03:09 +04:00
|
|
|
#ifdef HAVE_SYS_PARAM_H
|
2004-05-26 06:23:01 +04:00
|
|
|
#include <sys/param.h>
|
2007-04-01 20:16:54 +04:00
|
|
|
#endif /* HAVE_SYS_PARAM_H */
|
2004-07-15 19:08:54 +04:00
|
|
|
#include <stdlib.h>
|
2004-10-14 23:39:21 +04:00
|
|
|
#include <stdarg.h>
|
2004-05-26 06:23:01 +04:00
|
|
|
|
2005-07-04 05:59:52 +04:00
|
|
|
#include "opal/util/os_path.h"
|
2004-05-26 06:23:01 +04:00
|
|
|
|
2006-08-23 03:25:13 +04:00
|
|
|
static const char *path_sep = OPAL_PATH_SEP;
|
2005-08-27 01:03:41 +04:00
|
|
|
|
2005-07-04 05:59:52 +04:00
|
|
|
char *opal_os_path(bool relative, ...)
|
2004-05-26 06:23:01 +04:00
|
|
|
{
|
2007-08-04 04:41:26 +04:00
|
|
|
va_list ap;
|
2004-05-26 06:23:01 +04:00
|
|
|
char *element, *path;
|
2006-08-23 03:25:13 +04:00
|
|
|
size_t num_elements, total_length;
|
2004-05-26 06:23:01 +04:00
|
|
|
|
|
|
|
va_start(ap, relative);
|
|
|
|
|
2005-08-27 01:03:41 +04:00
|
|
|
/* no way to protect ourselves from reading too far, so have to
|
|
|
|
trust caller that they ended the list with the NULL */
|
2004-05-26 06:23:01 +04:00
|
|
|
|
|
|
|
num_elements = 0;
|
|
|
|
total_length = 0;
|
2007-07-01 21:51:34 +04:00
|
|
|
while (NULL != (element = va_arg(ap, char*))) {
|
|
|
|
num_elements++;
|
|
|
|
total_length = total_length + strlen(element);
|
|
|
|
if( path_sep[0] != element[0] ) total_length++;
|
2004-05-26 06:23:01 +04:00
|
|
|
}
|
2007-08-04 04:41:26 +04:00
|
|
|
va_end(ap);
|
2004-05-26 06:23:01 +04:00
|
|
|
|
|
|
|
if (0 == num_elements) { /* must be looking for a simple answer */
|
2005-08-25 20:28:41 +04:00
|
|
|
path = (char *)malloc(3);
|
2006-01-19 09:58:49 +03:00
|
|
|
path[0] = '\0';
|
2005-03-29 23:58:32 +04:00
|
|
|
if (relative) {
|
|
|
|
strcpy(path, ".");
|
2005-08-27 01:03:41 +04:00
|
|
|
strcat(path, path_sep);
|
2007-07-01 21:51:34 +04:00
|
|
|
} else {
|
2005-08-27 01:03:41 +04:00
|
|
|
strcpy(path, path_sep);
|
2005-03-29 23:58:32 +04:00
|
|
|
}
|
|
|
|
return(path);
|
2004-05-26 06:23:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* setup path with enough room for the string terminator, the elements, and
|
|
|
|
the separator between each of the elements */
|
2006-01-19 09:58:49 +03:00
|
|
|
total_length = total_length + num_elements * strlen(path_sep) + 1;
|
2007-06-05 18:50:15 +04:00
|
|
|
if(relative) {
|
|
|
|
total_length++;
|
|
|
|
}
|
|
|
|
|
2009-05-07 00:11:28 +04:00
|
|
|
if (total_length > OPAL_PATH_MAX) { /* path length is too long - reject it */
|
2005-03-29 23:58:32 +04:00
|
|
|
return(NULL);
|
2004-05-26 06:23:01 +04:00
|
|
|
}
|
|
|
|
|
2006-01-19 09:58:49 +03:00
|
|
|
path = (char *)malloc(total_length);
|
2004-05-26 06:23:01 +04:00
|
|
|
if (NULL == path) {
|
2006-01-19 09:58:49 +03:00
|
|
|
return(NULL);
|
2004-05-26 06:23:01 +04:00
|
|
|
}
|
2004-06-17 21:29:47 +04:00
|
|
|
path[0] = 0;
|
2004-05-26 06:23:01 +04:00
|
|
|
|
|
|
|
if (relative) {
|
2006-01-19 09:58:49 +03:00
|
|
|
strcpy(path, ".");
|
2004-11-02 22:12:11 +03:00
|
|
|
}
|
|
|
|
|
2007-08-04 04:41:26 +04:00
|
|
|
va_start(ap, relative);
|
|
|
|
if( NULL != (element = va_arg(ap, char*)) ) {
|
2006-01-19 09:58:49 +03:00
|
|
|
if (path_sep[0] != element[0]) {
|
2013-02-28 21:31:47 +04:00
|
|
|
strcat(path, path_sep);
|
2007-07-01 21:51:34 +04:00
|
|
|
}
|
2006-01-19 09:58:49 +03:00
|
|
|
strcat(path, element);
|
|
|
|
}
|
2007-08-04 04:41:26 +04:00
|
|
|
while (NULL != (element=va_arg(ap, char*))) {
|
2005-08-27 01:03:41 +04:00
|
|
|
if (path_sep[0] != element[0]) {
|
|
|
|
strcat(path, path_sep);
|
2005-04-07 23:19:48 +04:00
|
|
|
}
|
2004-05-26 06:23:01 +04:00
|
|
|
strcat(path, element);
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end(ap);
|
2006-08-23 03:25:13 +04:00
|
|
|
return opal_make_filename_os_friendly(path);
|
2004-05-26 06:23:01 +04:00
|
|
|
}
|