将 Unix 移植到 Windows-pwd.h 的用法 [英] Porting Unix to Windows- usage of pwd.h

查看:219
本文介绍了将 Unix 移植到 Windows-pwd.h 的用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 MinGW 编译 libUnihan 代码,但遇到了一个需要移植.该函数的目的是获得规范的路径表示.它使用 pwd.h(这是 POSIX,而 MinGW 不是),因此它可以通过检索 passwd 来解释使用~"来表示主目录结构体,其中包含 pw_dir.我确实在这里找到了一些信息,还有一个realpath 这里,但我仍然完全不知道如何处理有了这个.使用 MinGW,我仍然有一个由 ~ 表示的主目录,位于 /home/nate,但由于它不是 POSIX,我没有 pwd.h 帮助我找到这个主目录的位置.

I'm trying to compile libUnihan code with MinGW, but have run into a function which requires porting. The purpose of the function is to get a canonical path representation. It uses pwd.h (which is POSIX, and MinGW isn't) so it can account for the use of '~' to mean the home directory by retrieving a passwd struct, which contains pw_dir. I did find a little information here, and a port of realpath here, but I am still entirely at a loss as to how to deal with this. With MinGW, I still have a home directory represented by ~ and located at /home/nate, but since it isn't POSIX, I don't have pwd.h to help me find where this home directory is.

问:如何将下面的函数移植到 MinGW 上正常工作?

Q: How can I port the function below to work properly with MinGW?

/**
 * Return the canonicalized absolute pathname.
 *
 * It works exactly the same with realpath(3), except this function can handle the path with ~,
 * where realpath cannot.
 *
 * @param path The path to be resolved.
 * @param resolved_path Buffer for holding the resolved_path.
 * @return resolved path, NULL is the resolution is not sucessful.
 */
gchar*
truepath(const gchar *path, gchar *resolved_path){
    gchar workingPath[PATH_MAX];
    gchar fullPath[PATH_MAX];
    gchar *result=NULL;
    g_strlcpy(workingPath,path,PATH_MAX);

//     printf("*** path=%s \n",path);

    if ( workingPath[0] != '~' ){
        result = realpath(workingPath, resolved_path);
    }else{
        gchar *firstSlash, *suffix, *homeDirStr;
        struct passwd *pw;

        // initialize variables
        firstSlash = suffix = homeDirStr = NULL;

    firstSlash = strchr(workingPath, DIRECTORY_SEPARATOR);
        if (firstSlash == NULL)
            suffix = "";
        else
        {
            *firstSlash = 0;    // so userName is null terminated
            suffix = firstSlash + 1;
        }

        if (workingPath[1] == '\0')
            pw = getpwuid( getuid() );
        else
            pw = getpwnam( &workingPath[1] );

        if (pw != NULL)
            homeDirStr = pw->pw_dir;

        if (homeDirStr != NULL){
        gint ret=g_sprintf(fullPath, "%s%c%s", homeDirStr, DIRECTORY_SEPARATOR, suffix);
        if (ret>0){
        result = realpath(fullPath, resolved_path);
        }

    }
    }
    return result;
}

推荐答案

目的是实现~[username]/重映射逻辑.这类代码在 Linux/UNIX 环境中是有意义的,但最常见的用途只是引用用户自己的主目录.

The purpose is to implement ~[username]/ remapping logic. This sort of code makes sense in Linux/UNIX environments, but the most common use is just to refer to the user's own home directory.

为方便起见,我只添加对常见情况的支持 - ~/ - 即当前用户,而不是支持更一般的情况 - 让它失败并出现明显错误案例.

For expediency, I'd just add support for the common case - ~/ - i.e. the current user, and not bother supporting the more general case - have it fail with an obvious error in that case.

获取当前用户家目录的函数是SHGetFolderPath.

The function to get the current user's home directory is SHGetFolderPath.

#include <windows.h>

char homeDirStr[MAX_PATH];
if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, 0, homeDirStr))) {
    // Do something with the path
} else {
    // Do something else
}

在用户查找失败的情况下,您粘贴的代码不会尝试替换该字符串,而只是返回 NULL,因此您可以进行模拟.

In the case of a failed lookup of the user, the code you pasted does not try to replace that string, but simply returns NULL, so you could emulate that.

这篇关于将 Unix 移植到 Windows-pwd.h 的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆