2005-04-14 18:04:41 +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.
|
2005-04-14 18:04:41 +04:00
|
|
|
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
|
|
|
|
* University of Stuttgart. All rights reserved.
|
|
|
|
* Copyright (c) 2004-2005 The Regents of the University of California.
|
|
|
|
* All rights reserved.
|
|
|
|
* $COPYRIGHT$
|
|
|
|
*
|
|
|
|
* Additional copyrights may follow
|
|
|
|
*
|
|
|
|
* $HEADER$
|
|
|
|
*/
|
|
|
|
|
2006-02-12 04:33:29 +03:00
|
|
|
#include "opal_config.h"
|
2005-04-14 18:04:41 +04:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2006-08-23 03:49:57 +04:00
|
|
|
#if HAVE_DIRNAME
|
|
|
|
#include <libgen.h>
|
|
|
|
#endif /* HAVE_DIRNAME */
|
2005-04-14 18:04:41 +04:00
|
|
|
|
2005-07-04 04:13:44 +04:00
|
|
|
#include "opal/util/basename.h"
|
2006-08-23 03:25:13 +04:00
|
|
|
#include "opal/util/os_path.h"
|
2006-02-12 04:33:29 +03:00
|
|
|
#include "opal/constants.h"
|
2005-04-14 18:04:41 +04:00
|
|
|
|
2006-08-23 03:25:13 +04:00
|
|
|
/**
|
|
|
|
* Return a pointer into the original string where the last PATH delimiter
|
|
|
|
* was found. It does not modify the original string. Moreover, it does not
|
|
|
|
* scan the full string, but only the part allowed by the specified number
|
|
|
|
* of characters.
|
|
|
|
* If the last character on the string is a path separator, it will be skipped.
|
|
|
|
*/
|
|
|
|
static inline char* opal_find_last_path_separator( const char* filename, size_t n )
|
|
|
|
{
|
|
|
|
char* p = (char*)filename + n;
|
|
|
|
|
|
|
|
/* First skip the latest separators */
|
|
|
|
for( ; p != filename; p-- ) {
|
|
|
|
#if defined(__WINDOWS__)
|
|
|
|
if( (*p != '\\') && (*p != '/') )
|
|
|
|
break;
|
|
|
|
#else
|
2006-08-23 03:49:57 +04:00
|
|
|
if( *p != OPAL_PATH_SEP[0] )
|
2006-08-23 03:25:13 +04:00
|
|
|
break;
|
|
|
|
#endif /* defined(__WINDOWS__) */
|
|
|
|
}
|
|
|
|
|
|
|
|
for( ; p != filename; p-- ) {
|
|
|
|
#if defined(__WINDOWS__)
|
|
|
|
if( (*p == '\\') || (*p == '/') )
|
|
|
|
return p;
|
|
|
|
#else
|
2006-08-23 03:49:57 +04:00
|
|
|
if( *p == OPAL_PATH_SEP[0] )
|
2006-08-23 03:25:13 +04:00
|
|
|
return p;
|
|
|
|
#endif /* defined(__WINDOWS__) */
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL; /* nothing found inside the filename */
|
|
|
|
}
|
2005-04-14 18:04:41 +04:00
|
|
|
|
2005-07-04 04:13:44 +04:00
|
|
|
char *opal_basename(const char *filename)
|
2005-04-14 18:04:41 +04:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
char *tmp, *ret = NULL;
|
2006-08-23 03:25:13 +04:00
|
|
|
const char sep = OPAL_PATH_SEP[0];
|
2005-04-14 18:04:41 +04:00
|
|
|
|
|
|
|
/* Check for the bozo case */
|
|
|
|
if (NULL == filename) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-01-25 03:15:56 +03:00
|
|
|
if (0 == strlen(filename)) {
|
|
|
|
return strdup("");
|
|
|
|
}
|
|
|
|
if (sep == filename[0] && '\0' == filename[1]) {
|
2005-04-14 18:04:41 +04:00
|
|
|
return strdup(filename);
|
|
|
|
}
|
|
|
|
|
2007-01-25 03:15:56 +03:00
|
|
|
/* On Windows, automatically exclude the drive designator */
|
|
|
|
|
|
|
|
#ifdef __WINDOWS__
|
|
|
|
if( isalpha(filename[0]) && (':' == filename[1]) ) {
|
|
|
|
if( strlen(filename) == 2 ) {
|
|
|
|
return strdup(filename);
|
|
|
|
} else if( strlen(filename) == 3 && (sep == filename[2]) ) {
|
|
|
|
return strdup(filename);
|
|
|
|
}
|
2005-04-14 18:04:41 +04:00
|
|
|
filename += 2;
|
2007-01-25 03:15:56 +03:00
|
|
|
if( sep == filename[0] ) {
|
2005-04-14 18:04:41 +04:00
|
|
|
++filename;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Remove trailing sep's (note that we already know that strlen > 0) */
|
|
|
|
tmp = strdup(filename);
|
|
|
|
for (i = strlen(tmp) - 1; i > 0; --i) {
|
|
|
|
if (sep == tmp[i]) {
|
|
|
|
tmp[i] = '\0';
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (0 == i) {
|
|
|
|
tmp[0] = sep;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Look for the final sep */
|
2007-01-25 03:15:56 +03:00
|
|
|
ret = opal_find_last_path_separator( tmp, strlen(tmp) );
|
2005-04-14 18:04:41 +04:00
|
|
|
if (NULL == ret) {
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
ret = strdup(ret + 1);
|
|
|
|
free(tmp);
|
2007-01-23 20:58:17 +03:00
|
|
|
return ret;
|
2006-08-23 03:25:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
char* opal_dirname(const char* filename)
|
|
|
|
{
|
|
|
|
#if defined(HAVE_DIRNAME)
|
2006-08-27 09:09:40 +04:00
|
|
|
char* safe_tmp = strdup(filename), *result;
|
|
|
|
result = strdup(dirname(safe_tmp));
|
|
|
|
free(safe_tmp);
|
|
|
|
return result;
|
2006-08-23 03:25:13 +04:00
|
|
|
#else
|
|
|
|
const char* p = opal_find_last_path_separator(filename, strlen(filename));
|
|
|
|
|
|
|
|
for( ; p != filename; p-- ) {
|
|
|
|
if( (*p == '\\') || (*p == '/') ) {
|
|
|
|
/* If there are several delimiters remove them all */
|
|
|
|
for( --p; p != filename; p-- ) {
|
|
|
|
if( (*p != '\\') && (*p != '/') ) {
|
|
|
|
p++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( p != filename ) {
|
|
|
|
char* ret = (char*)malloc( p - filename + 1 );
|
|
|
|
strncpy_s( ret, (p - filename + 1), filename, p - filename );
|
|
|
|
ret[p - filename] = '\0';
|
|
|
|
return opal_make_filename_os_friendly(ret);
|
|
|
|
}
|
|
|
|
break; /* return the duplicate of "." */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return _strdup(".");
|
|
|
|
#endif /* defined(HAVE_DIRNAME) */
|
2005-04-14 18:04:41 +04:00
|
|
|
}
|