【发布时间】:2021-10-18 12:25:01
【问题描述】:
我写了一个函数,如果你输入-link,它必须在最后返回链接指向的文件的名称,如果你不写该命令,则不返回它。问题是我不知道如何使用函数“readlink()”获取文件的名称,因为它返回文件的大小而不是名称,并在“https://pubs.opengroup.org/”中搜索onlinepubs/000095399/functions/readlink.html" 我无法理解从文件中获取名称的方法。
printf("%s %ld (%lu) %s %s ", time, info.st_nlink, info.st_ino, pw->pw_name, gr->gr_name); //prints a bunch of information about the file
printf( (S_ISDIR(info.st_mode)) ? "d" : "-");
printf( (info.st_mode & S_IRUSR) ? "r" : "-");
printf( (info.st_mode & S_IWUSR) ? "w" : "-");
printf( (info.st_mode & S_IXUSR) ? "x" : "-");
printf( (info.st_mode & S_IRGRP) ? "r" : "-");
printf( (info.st_mode & S_IWGRP) ? "w" : "-");
printf( (info.st_mode & S_IXGRP) ? "x" : "-");
printf( (info.st_mode & S_IROTH) ? "r" : "-");
printf( (info.st_mode & S_IWOTH) ? "w" : "-");
printf( (info.st_mode & S_IXOTH) ? "x" : "-");
if(!opts.link){ //If the -link command wasnt written
printf(" %ld %s\n", info.st_size, tokens[2]);
} else{
//Here I need to print the same as in the last line but adding the name of the file the link points to like this:
printf(" %ld %s -> %s\n", info.st_size, tokens[2], name);
}
【问题讨论】: