我是我大学系统编程课的助教。最近,学生们正在完成一项涉及复制该程序的作业pwd
。
一些学生注意到了似乎不一致的地方。当他们从 readdir 条目中读取 ino 时,它会给出与统计同一目录时不同的 inode。他们中的许多人都在问为什么。目录项的索引节点不应该指向目标目录所在的索引节点吗?如果是这样,为什么 stat 给出不同的 inode?
这是一些示例代码来演示:
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#define DIR_NAME "~redacted~"
int getReaddirInode(char* entName)
{
DIR* directory;
struct dirent* entry;
ino_t result;
if ((directory = opendir(".")) == NULL)
{
perror("opendir");
exit(1);
}
while ((entry = readdir(directory)) != NULL)
{
if (strcmp(entName, entry->d_name) == 0)
{
result = entry->d_ino;
break;
}
}
if (entry == NULL)
{
fprintf(stderr, "No such directory: %s.\n", …
Run Code Online (Sandbox Code Playgroud) 我有以下代码模仿ls
:
#include <dirent.h>
#include <stdio.h>
char* dirent_type_to_str(unsigned char dirent_type) {
switch (dirent_type) {
case DT_DIR:
return "Dir ";
case DT_REG:
return "File";
}
printf("DEBUG: Unknown type %x\n", dirent_type);
return "Unk ";
}
int main(int argc, char** argv) {
char* dir_path = argc > 1 ? argv[1] : ".";
DIR* dir_stream = opendir(dir_path);
struct dirent* dirent_ptr;
while (dirent_ptr = readdir(dir_stream)) {
printf("%s %s\n", dirent_type_to_str(dirent_ptr->d_type), dirent_ptr->d_name);
}
closedir(dir_stream);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
出于某种原因,当我运行该程序时,它表示d_type
与之关联.
并且..
未知:
james.ko@cslab1-20:~/Desktop/systems-11-02$ ./main …
Run Code Online (Sandbox Code Playgroud) 在一个终端我可以打电话ls -d */
.现在我想要一个c程序为我这样做,像这样:
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <unistd.h>
int main( void )
{
int status;
char *args[] = { "/bin/ls", "-l", NULL };
if ( fork() == 0 )
execv( args[0], args );
else
wait( &status );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这将是ls -l
一切.但是,当我尝试时:
char *args[] = { "/bin/ls", "-d", "*/", NULL };
Run Code Online (Sandbox Code Playgroud)
我会得到一个运行时错误:
ls:*/:没有这样的文件或目录
我试图使用以下代码列出共享驱动器上某个目录中的所有文件:
#include <iostream>
#include <string>
#include "dirent.h"
int main ()
{
DIR *directoryHandle = opendir("./temp/");
if (NULL != directoryHandle)
{
dirent *entry = readdir(directoryHandle);
while (NULL != entry)
{
//skip directories and select only files (hopefully)
if ((DT_DIR != entry->d_type) && (DT_REG == entry->d_type || DT_UNKNOWN == entry->d_type))
{
std::cout << "Name: " << entry->d_name << " Type:" << std::to_string(entry->d_type) << std::endl;
}
//go to next entry
entry = readdir(directoryHandle);
}
closedir(directoryHandle);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
问题是entry-> d_type包含目录的DT_UNKNOWN以及目录中的./temp/
文件. …
所以我有一个项目,我需要构建一个小的简单文本shell,可以运行,编辑和读取目录中的文件.我有一个应该工作的小原型,除了我编译时,我收到一个错误,关于在dirent.h头文件中使用的struct dirent中找不到d_type.
d = opendir( "." );
c = 0;
while ((de = readdir(d))){
if ((de->de_type) & DT_DIR)
printf( " ( %d Directory: %s ) \n", c++, de->de_name);
}
Run Code Online (Sandbox Code Playgroud)
变量"de"的类型为struct dirent*,并且正在检查它的类型,我得到错误:'struct dirent'没有名为'de_type'的成员
这里我真的很困惑和困惑:我在两个窗口(使用开发C++)和Ubuntu(使用gcc)上编译了这段代码.我在两个操作系统上都收到了相同的错误,当我检查使用的库时,我相信这是正常的gnu C库,有一个名为d_type的变量:
https://www.gnu.org/software/libc/manual/html_node/Directory-Entries.html
我找到了对dirent.h文件的其他引用,因为一个文件不在同一个库中,如果是这样,我该如何加载该库以便编译代码?
对不起,很长的帖子,非常感谢所有回答!