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.
|
2014-08-08 15:36:45 +04:00
|
|
|
* Copyright (c) 2009-2014 Cisco Systems, Inc. All rights reserved.
|
2014-08-08 06:51:56 +04:00
|
|
|
* Copyright (c) 2014 Research Organization for Information Science
|
|
|
|
* and Technology (RIST). All rights reserved.
|
2014-09-04 20:10:38 +04:00
|
|
|
* Copyright (c) 2014 Intel, Inc. All rights reserved.
|
2005-04-14 18:04:41 +04:00
|
|
|
* $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>
|
2007-04-01 20:16:54 +04:00
|
|
|
#ifdef HAVE_STRING_H
|
2005-04-14 18:04:41 +04:00
|
|
|
#include <string.h>
|
2007-04-01 20:16:54 +04:00
|
|
|
#endif /* HAVE_STRING_H */
|
2007-04-01 20:26:54 +04:00
|
|
|
#ifdef HAVE_LIBGEN_H
|
|
|
|
#include <libgen.h>
|
|
|
|
#endif /* HAVE_LIBGEN_H */
|
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"
|
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 */
|
2009-04-23 00:48:24 +04:00
|
|
|
for ( ; p >= filename; p-- ) {
|
2006-08-23 03:49:57 +04:00
|
|
|
if( *p != OPAL_PATH_SEP[0] )
|
2006-08-23 03:25:13 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-04-23 00:48:24 +04:00
|
|
|
for ( ; p >= filename; p-- ) {
|
2006-08-23 03:49:57 +04:00
|
|
|
if( *p == OPAL_PATH_SEP[0] )
|
2006-08-23 03:25:13 +04:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
2014-09-13 04:28:30 +04:00
|
|
|
#if defined(HAVE_DIRNAME) || OPAL_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));
|
2014-09-03 22:13:42 +04:00
|
|
|
/* NOTE: p will be NULL if no path separator was in the filename - i.e.,
|
|
|
|
* if filename is just a local file */
|
2006-08-23 03:25:13 +04:00
|
|
|
|
2014-09-03 22:13:42 +04:00
|
|
|
for( ; NULL != p && p != filename; p-- ) {
|
2006-08-23 03:25:13 +04:00
|
|
|
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 );
|
2007-07-10 07:46:57 +04:00
|
|
|
#ifdef HAVE_STRNCPY_S
|
2006-08-23 03:25:13 +04:00
|
|
|
strncpy_s( ret, (p - filename + 1), filename, p - filename );
|
2007-07-10 07:46:57 +04:00
|
|
|
#else
|
|
|
|
strncpy(ret, filename, p - filename);
|
|
|
|
#endif
|
2006-08-23 03:25:13 +04:00
|
|
|
ret[p - filename] = '\0';
|
|
|
|
return opal_make_filename_os_friendly(ret);
|
|
|
|
}
|
|
|
|
break; /* return the duplicate of "." */
|
|
|
|
}
|
|
|
|
}
|
2007-07-10 07:46:57 +04:00
|
|
|
return strdup(".");
|
2014-09-13 04:28:30 +04:00
|
|
|
#endif /* defined(HAVE_DIRNAME) || OPAL_HAVE_DIRNAME */
|
2005-04-14 18:04:41 +04:00
|
|
|
}
|