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>
|
2004-10-20 05:03:09 +04:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_LIBGEN_H
|
2004-05-26 06:23:01 +04:00
|
|
|
#include <libgen.h>
|
2004-10-20 05:03:09 +04:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_PARAM_H
|
2004-05-26 06:23:01 +04:00
|
|
|
#include <sys/param.h>
|
2004-10-20 05:03:09 +04:00
|
|
|
#endif
|
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"
|
2006-02-12 04:33:29 +03:00
|
|
|
#include "opal/constants.h"
|
2004-05-26 06:23:01 +04:00
|
|
|
|
2005-12-13 01:01:51 +03:00
|
|
|
#ifdef __WINDOWS__
|
2005-08-27 01:03:41 +04:00
|
|
|
#define PATH_SEP "\\"
|
|
|
|
#else
|
|
|
|
#define PATH_SEP "/"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static const char *path_sep = PATH_SEP;
|
|
|
|
|
|
|
|
|
2005-07-04 05:59:52 +04:00
|
|
|
char *opal_os_path(bool relative, ...)
|
2004-05-26 06:23:01 +04:00
|
|
|
{
|
|
|
|
va_list ap, ap1;
|
|
|
|
char *element, *path;
|
|
|
|
int num_elements, total_length;
|
|
|
|
|
|
|
|
va_start(ap, relative);
|
|
|
|
va_start(ap1, 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;
|
|
|
|
while (NULL != (element=va_arg(ap, char*))) {
|
2005-03-29 23:58:32 +04:00
|
|
|
num_elements++;
|
|
|
|
total_length = total_length + strlen(element);
|
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);
|
2005-03-29 23:58:32 +04:00
|
|
|
}
|
|
|
|
else {
|
2005-12-13 01:01:51 +03:00
|
|
|
#ifndef __WINDOWS__
|
2005-08-27 01:03:41 +04:00
|
|
|
strcpy(path, path_sep);
|
2004-11-02 22:12:11 +03:00
|
|
|
#endif
|
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;
|
2004-05-26 06:23:01 +04:00
|
|
|
if (total_length > MAXPATHLEN) { /* 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
|
|
|
}
|
|
|
|
|
2006-01-19 09:58:49 +03:00
|
|
|
/* 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);
|
|
|
|
}
|
2004-05-26 06:23:01 +04:00
|
|
|
while (NULL != (element=va_arg(ap1, 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);
|
|
|
|
va_end(ap1);
|
|
|
|
return(path);
|
|
|
|
}
|