如何正确打印文件的路径?

时间:2018-11-20 21:55:46

标签: c linux shell pwd

试图为c shell创建一个密码。这是我在网站上找到的,并且想要了解更多有关它的信息。 我已经在程序中一直使用调试printf语句,并且它返回“。”。而不是实际的目录名。我想念什么?为什么会这样?

#include <dirent.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main()
{
    struct stat stat_buf;   
    struct dirent *file_info;
    ino_t itself_ino;       /* holds current folder inode */
    ino_t parent_ino;       /* holds parent folder inode */
    char Current[PATH_MAX];  /* folder name */
    char Path[PATH_MAX];     /* holds the full path */
    char Slash[PATH_MAX];    /* add / before the folder name */ 
    DIR *dir;

    while (1)
    {   
        dir = opendir(".");
        if(dir == NULL) { 
            fprintf(stderr, "cannot get current directory.\n");
            exit(-1);
        }
        /* read the information about the current folder */
        file_info = readdir(dir);

        lstat(file_info->d_name, &stat_buf);
        itself_ino = stat_buf.st_ino;
        closedir(dir);

        chdir("..");    /* go to parent directory */
        dir = opendir(".");

        file_info = readdir(dir);
        lstat(file_info->d_name, &stat_buf);
        parent_ino = stat_buf.st_ino;

        if(itself_ino == parent_ino) {
            /*closedir(dir);*/
            break;
        } else {
            strcpy(Slash, "/");
            strcpy(Current, file_info->d_name);
            strcat(Slash, Current);  /* add "/" as the first */ 
            strcat(Slash, Path);     /* charcter of the directory */ 

            /* check the length of the pathname */
            if(strlen(Slash)  >= PATH_MAX) {
                fprintf(stderr, "Error! Path too long!\n");
                exit(0);
            }           
            /* save the full pathname */       
            strcpy(Path, Slash);
        }
        closedir(dir);
    }

    /* print the full path of the current working directory */
    printf("%s\n", Path);
    return 0;
}

1 个答案:

答案 0 :(得分:0)

它只是realpath

if (realpath(".", &Path) == NULL) {
   // handle error
}

但是您可能瞄准getcwdget_current_dir_name

printf("%s\n", get_current_dir_name());