File: basename.c

package info (click to toggle)
openmpi 1.2.7~rc2-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 41,300 kB
  • ctags: 24,303
  • sloc: ansic: 224,835; sh: 22,627; makefile: 7,037; cpp: 6,353; asm: 3,547; lex: 528; objc: 383; perl: 348; csh: 89; f90: 49; fortran: 47; tcl: 12
file content (162 lines) | stat: -rw-r--r-- 4,453 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 * 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.
 * 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$
 */

#include "opal_config.h"

#include <stdlib.h>
#include <string.h>
#if HAVE_DIRNAME
#include <libgen.h>
#endif  /* HAVE_DIRNAME */

#include "opal/util/basename.h"
#include "opal/util/os_path.h"
#include "opal/constants.h"

/**
 * 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
        if( *p != OPAL_PATH_SEP[0] )
            break;
#endif  /* defined(__WINDOWS__) */
    }

    for( ; p != filename; p-- ) {
#if defined(__WINDOWS__)
        if( (*p == '\\') || (*p == '/') )
            return p;
#else
        if( *p == OPAL_PATH_SEP[0] )
            return p;
#endif  /* defined(__WINDOWS__) */
    }

    return NULL;  /* nothing found inside the filename */
}

char *opal_basename(const char *filename)
{
    size_t i;
    char *tmp, *ret = NULL;
    const char sep = OPAL_PATH_SEP[0];

    /* Check for the bozo case */

    if (NULL == filename) {
        return NULL;
    }

    /* On Windows, automatically exclude a drive designator */

#ifdef __WINDOWS__
    if (strlen(filename) == 2 &&
        isalpha(filename[0]) && ':' == filename[1]) {
        return strdup(filename);
    } else if (strlen(filename) == 3 &&
        isalpha(filename[0]) && ':' == filename[1] && sep == filename[2]) {
        return strdup(filename);
    }

    if (':' == filename[1] && isalpha(filename[0])) {
        filename += 2;
        if (sep == filename[0]) {
            ++filename;
        }
    }
#endif

    /* Check for the bozo cases */

    if (0 == strlen(filename)) {
        return strdup("");
    }
    if (sep == filename[0] && '\0' == filename[1]) {
        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 */

    ret = strrchr(tmp, sep);
    if (NULL == ret) {
        return tmp;
    }
    ret = strdup(ret + 1);
    free(tmp);
    return opal_make_filename_os_friendly(ret);
}

char* opal_dirname(const char* filename)
{
#if defined(HAVE_DIRNAME)
    char* safe_tmp = strdup(filename), *result;
    result = strdup(dirname(safe_tmp));
    free(safe_tmp);
    return result;
#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) */
}