加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 在路径中解决“../”和“./”的“realpath”的替代方法

发布时间:2020-12-16 05:48:54 所属栏目:百科 来源:网络整理
导读:realpath做我需要的,但只有在路径中的文件实际存在的情况下才起作用. 我需要一个从字符串返回归一化路径的函数(例如../some/./directory/a/b/c/../d到some / directory / a / b / d),而不管目录/文件实际存在 基本上等同于Windows上的PathCanonicalize. 这样
realpath做我需要的,但只有在路径中的文件实际存在的情况下才起作用.

我需要一个从字符串返回归一化路径的函数(例如../some/./directory/a/b/c/../d到some / directory / a / b / d),而不管目录/文件实际存在

基本上等同于Windows上的PathCanonicalize.

这样的功能是否已经存在?

解决方法

我不认为有任何标准库函数可用于此.

您可以在Apache httpd源代码文件server / util.c中使用函数ap_getparents().我相信它的确是你想要的:https://github.com/apache/httpd/blob/trunk/server/util.c#L500

#ifdef WIN32
#define IS_SLASH(s) ((s == '/') || (s == ''))
#else
#define IS_SLASH(s) (s == '/')
#endif

void ap_getparents(char *name)
{
    char *next;
    int l,w,first_dot;

    /* Four paseses,as per RFC 1808 */
    /* a) remove ./ path segments */
    for (next = name; *next && (*next != '.'); next++) {
    }

    l = w = first_dot = next - name;
    while (name[l] != '') {
        if (name[l] == '.' && IS_SLASH(name[l + 1])
            && (l == 0 || IS_SLASH(name[l - 1])))
            l += 2;
        else
            name[w++] = name[l++];
    }

    /* b) remove trailing . path,segment */
    if (w == 1 && name[0] == '.')
        w--;
    else if (w > 1 && name[w - 1] == '.' && IS_SLASH(name[w - 2]))
        w--;
    name[w] = '';

    /* c) remove all xx/../ segments. (including leading ../ and /../) */
    l = first_dot;

    while (name[l] != '') {
        if (name[l] == '.' && name[l + 1] == '.' && IS_SLASH(name[l + 2])
            && (l == 0 || IS_SLASH(name[l - 1]))) {
            int m = l + 3,n;

            l = l - 2;
            if (l >= 0) {
                while (l >= 0 && !IS_SLASH(name[l]))
                    l--;
                l++;
            }
            else
                l = 0;
            n = l;
            while ((name[n] = name[m]))
                (++n,++m);
        }
        else
            ++l;
    }

    /* d) remove trailing xx/.. segment. */
    if (l == 2 && name[0] == '.' && name[1] == '.')
        name[0] = '';
    else if (l > 2 && name[l - 1] == '.' && name[l - 2] == '.'
             && IS_SLASH(name[l - 3])) {
        l = l - 4;
        if (l >= 0) {
            while (l >= 0 && !IS_SLASH(name[l]))
                l--;
            l++;
        }
        else
            l = 0;
        name[l] = '';
    }
}

(这是假设在您的项目中重新使用Apache许可代码是可以接受的.)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读