- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试实现 ls
命令并陷入 -i
使用 stat()
选项列出 inode 编号的函数。
格式为./myls [options] [list of files]
如果我运行./myls -i .
我得到正确的输出(这会打印当前目录中的文件)
./myls -i
1446018 myls.c
1441809 myls
1445497 something
如果我运行 ./myls ..
我得到正确的输出(打印父目录中的文件)
./myls ..
Assignment2
Assignment3
Assignment4
Assignment1
但是如果将它们与父目录合并并运行 ./myls -i ..
我收到错误消息
/myls -i ..
Error finding file: No such file or directory
“错误查找文件”是我在 ls
中打印的错误消息功能。
//printf("%s", name);
if (stat(name, fileStat) != 0) {
error("Error finding file");
}
如果我取消注释 printf
在此错误消息之前声明,我得到以下内容
/myls -i ..
Assignment2
Error finding file: No such file or directory
因此它正在获取父目录的第一个文件的名称,但 stat 函数失败。有人知道我该如何解决这个问题吗?
我在某处读到它可能会失败,因为它只传递文件的名称而不是其路径。但这适用于当前目录,因此这可能不是问题。如果是,那么我将如何传递路径而不仅仅是文件名?
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int i = 0, R = 0, l = 0;
int anyFiles = 0;
void error(char *msg) {
perror(msg);
exit(0);
}
void ls(char *path) {
DIR *d;
struct dirent *dir;
d = opendir(path);
if (!d || d == NULL)
error("Unable to read directory");
char name[256]; //to store file name of each file in directory
while ((dir = readdir(d)) != NULL) {
strcpy(name, dir -> d_name);
if (name[0] == '.' || strncmp(name, "..", 2) == 0)
continue;
if (i || R || l) {
struct stat *fileStat;
//printf("%s\n",name);
if (stat(name, fileStat) != 0) {
error("Error finding file");
}
if (i) {
printf("%lu\t\t%s\n", fileStat->st_ino, name);
continue;
}
/*else if (R) {
continue;
}
else if (l) {
continue;
}*/
}
printf("%s\n", name);
}
closedir(d);
}
void process(int args, char *argsList[]) {
int j;
for (j = 1; j < args; j++) {
if (argsList[j][0] == '-') {
int k;
for (k = 1; k < (strlen(argsList[j])); k++) {
if (argsList[j][k] == 'i')
i = 1;
else if (argsList[j][k] == 'R')
R = 1;
else if (argsList[j][k] == 'l')
l = 1;
else
error("option not supported");
}
}
else if (argsList[j][0] != '-') {
ls(argsList[j]);
anyFiles = 1;
}
}
if (anyFiles == 0)
ls(".");
}
int main(int argc, char *argv[]) {
if (argc == 1)
ls(".");
else if (argc > 1) {
process(argc, argv);
}
return 0;
}
最佳答案
传递给 stat(name, fileStat)
中的 stat
的名称是相对于打开的目录的。您必须从 path
和 dir->d_name
构造实际名称,或者使用 fstatat()
。它适用于 "."
因为相对路径恰好是相对于当前目录的。
此外,stat
结构应该声明为自动存储,而不是作为未初始化的指针。
请注意,ls
实用程序在枚举目录内容和命令行参数时不使用 stat
,而是使用 lstat
返回符号值链接而不是解析它们,除非它们有尾部斜杠并解析为目录。
这是一个修改后的版本,修复了许多其他问题:
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int flag_a, flag_i, flag_R, flag_l;
typedef struct entry_t {
struct entry_t *next;
char name[];
} entry_t;
typedef struct list_t {
struct entry_t *head;
struct entry_t *tail;
} list_t;
void free_entries(list_t *list) {
while (list->head) {
entry_t *ep = list->head;
list->head = ep->next;
free(ep);
}
list->tail = NULL;
}
void add_entry(list_t *list, const char *name) {
entry_t *ep = malloc(sizeof(*ep) + strlen(name) + 1);
if (ep == NULL) {
fprintf(stderr, "out of memory\n");
exit(1);
}
strcpy(ep->name, name);
/* simplistic insertion sort */
if (!list->head || strcmp(name, list->head->name) < 0) {
ep->next = list->head;
list->head = ep;
if (list->tail == NULL)
list->tail = ep;
} else {
entry_t *p = list->head;
while (p->next && strcmp(name, p->next->name) > 0)
p = p->next;
ep->next = p->next;
p->next = ep;
}
if (list->tail->next)
list->tail = list->tail->next;
}
int isdir(const char *name) {
struct stat fileStat;
return !lstat(name, &fileStat) && S_ISDIR(fileStat.st_mode);
}
int ls_files(list_t *list, list_t *dirs, const char *path) {
int status = 0;
for (entry_t *ep = list->head; ep; ep = ep->next) {
if (flag_i + flag_l + flag_R) {
char buf[1024]; //to store the constructed path name
struct stat fileStat;
char *name = ep->name;
if (path) {
snprintf(name = buf, sizeof buf, "%s/%s", path, ep->name);
}
if (lstat(name, &fileStat) != 0) {
fprintf(stderr, "cannot stat file %s: %s\n",
name, strerror(errno));
status |= 1;
continue;
}
if (flag_R && S_ISDIR(fileStat.st_mode) &&
strcmp(ep->name, ".") && strcmp(ep->name, "..")) {
add_entry(dirs, name);
}
if (flag_i) {
printf("%lu\t", (unsigned long)fileStat.st_ino);
}
if (flag_l) {
printf("%lu\t", (unsigned long)fileStat.st_size);
/* should also output mode and date */
}
}
printf("%s\n", ep->name);
}
return status;
}
int ls_dir(const char *path, list_t *subdirs) {
DIR *d = opendir(path);
if (d == NULL) {
fprintf(stderr, "cannot open directory %s: %s\n",
path, strerror(errno));
return 1;
}
struct dirent *dir;
list_t files = { NULL, NULL };
/* enumerate directory entries and store the names in a sorted list */
while ((dir = readdir(d)) != NULL) {
if (*dir->d_name == '.' && !flag_a)
continue;
add_entry(&files, dir->d_name);
}
closedir(d);
int status = ls_files(&files, subdirs, path);
free_entries(&files);
return status;
}
int ls_dirs(list_t *dirs, int header) {
int status = 0;
list_t subdirs = { NULL, NULL };
for (entry_t *ep = dirs->head; ep; ep = ep->next) {
if (header) {
if (header > 1)
printf("\n");
printf("%s:\n", ep->name);
}
ls_dir(ep->name, &subdirs);
header = 2;
if (subdirs.head) {
/* insert the sorted list of subdirectories */
subdirs.tail->next = ep->next;
ep->next = subdirs.head;
subdirs.head = subdirs.tail = NULL;
}
}
return status;
}
int process(int args, char *argsList[]) {
int status = 0;
list_t files = { NULL, NULL };
list_t dirs = { NULL, NULL };
for (int j = 1; j < args; j++) {
char *arg = argsList[j];
if (*arg == '-') {
for (int k = 1; arg[k] != '\0'; k++) {
switch (arg[k]) {
case 'a': flag_a = 1; continue;
case 'i': flag_i = 1; continue;
case 'R': flag_R = 1; continue;
case 'l': flag_l = 1; continue;
default: fprintf(stderr, "option not supported: -%c\n", arg[k]);
return 1;
}
}
} else {
if (isdir(arg))
add_entry(&dirs, arg);
else
add_entry(&files, arg);
}
}
if (!dirs.head && !files.head) {
add_entry(&dirs, ".");
}
int header = 0;
if (files.head) {
status |= ls_files(&files, &dirs, NULL);
header = 2;
}
if (dirs.head) {
if (!header && dirs.head->next)
header = 1;
status |= ls_dirs(&dirs, header);
}
free_entries(&files);
free_entries(&dirs);
return status;
}
int main(int argc, char *argv[]) {
return process(argc, argv);
}
关于c - 传递不同目录的文件名时 Stat 函数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73221219/
所以,我正在尝试创建一种 ls 函数。这是我对每个文件的描述的代码 struct stat fileStat; struct dirent **files; num_entries = scandir
我最近一直在尝试实现我自己的 linux ls 命令版本。一切都很好,但是当我尝试使用 ls -l 功能时,struct stat 的某些字段未初始化 - 我得到 NULL 指针或垃圾值,尽管它似乎只
我在 Yii 中遇到 STAT 关系问题。我不确定我正在寻找的东西是否可以通过本地 Yii 关系实现。我会尽力描述我的问题,如果不清楚,请询问任何具体细节。 我有三个表,因此有三个模型 | table
我正在为一个严重依赖 scipy.stats.stats(scipy 版本 0.9.0)的包创建一个 django-powered (1.3) 接口(interface),称为 ovl 。在早期开发阶
为了安全起见,我喜欢显式初始化我的变量(当您编写大量代码时,它通常会使它更安全,因为您的代码最终不会崩溃那么多。) 对于大多数类型,无论是结构还是整数等基本 C++ 类型,我都可以编写以下内容: ti
我一直在使用 stat() 检查文件是否存在,据我所知,这比尝试打开文件更好。但是,stat() 不适用于包含其他语言的 unicode 字符的文件名。是否有 stat() 的宽字符版本或我可以使用的
错误: File "/usr/lib/python2.7/dist-packages/statsmodels/regression/linear_model.py", line 36, in
下面是我要运行的脚本。我不能在 awk 中使用 stat。 cat /etc/passwd | awk 'BEGIN{FS=":"}{print $6 }' | (stat $6 | sed -n '
我正在尝试拟合 xlog 线性回归。我使用 Seaborn regplot 来绘制拟合,看起来很合适(绿线)。然后,因为 regplot 不提供系数。我使用 stats.linregress 来查找系
我正在尝试使用共享库 (libscplugin.so) 中包含的方法。 我已经满足了库的所有要求: libc.so 带有指向 libc.so.6 的符号链接(symbolic link) libz.s
嘿,感谢阅读。 我正在制作一个程序,它接受 1 个参数(目录)并使用 opendir()/readdir() 读取目录中的所有文件,并使用 stat 显示文件类型(reg、链接、目录等)。当我在 sh
简单问题:在 Linux 中,我 stat() 一个不是设备的文件。 st_rdev 字段的期望值是多少?我可以运行 major(stat.st_rdev) 和 minor(stat.st_rdev)
我正在尝试为我的 Angular 6 应用程序生成 stats.json 文件。下面的事情我已经尝试过,但根本没有生成文件。我的系统需要有 “npm 运行”在每个 angular cli 命令之前。
我正在尝试使用返回的 stat 结构中的 st_mode,该结构是我通过以下方式从 stat() 调用获得的; char *fn = "test.c" struct s
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我有一个程序,是我通过修改原始暗网(深度学习图像识别,Yolov2)的许多地方而制作的。几个月前我一直在使用它,但是今天当我编译它时,它给了我一个错误: gcc -DSAVE_LAYER_INPUT
我预计 scipy.stats.mstats.pearsonr 对于屏蔽数组输入的结果将与 scipy.stats.pearsonr 对于输入数据的 unmasked 值给出相同的结果,但它不会't:
给定 tmp.c: #include #include #include int main(int argc, const char *argv[]) { struct stat st;
In [15]: a = np.array([0.5, 0.5, 0, 0, 0]) In [16]: b = np.array([1, 0, 0, 0, 0]) In [17]: entropy(a
当我们运行 stat filename我们得到 Access: 2021-06-25 15:40:18.532621916 +0530 Modify: 2020-08-13 15:57:30.0000
我是一名优秀的程序员,十分优秀!