已解决
我正在尝试创建一个程序,它的功能类似于使用C的默认ls命令。目前,我在其中一个属性(-l)和格式化方面遇到了一些问题。对于-l,它并没有运行应该使其成为一个包含inode和名称(argv2)的简单列表的代码行,而是运行与-f相同的功能。
#include <stdio.h>
#include <sys/types.h>
#include <sys/dir.h>
#include <sys/stat.h>
struct direct **dirs;
int main (int argc, char* argv[])
{
struct stat statb;
int nent, i;
int x = 0;
int y;
char *F = argv[1];
char *l = argv[2];
char *others = argv[3];
nent = scandir (".", &dirs, NULL, NULL);
for (i = 0; i < nent; i ++) {
if(argv[1]) {
stat(dirs[i]->d_name, &statb);
if (y == 0) {
printf("\n%-16s",dirs[i]->d_name);
switch (statb.st_mode & S_IFMT) {
case S_IFDIR :
printf("/\t ");
break;
case S_IFREG :
printf("*\t ");
break;
}
}
else {
printf ("%-16s",dirs[i]->d_name);
switch (statb.st_mode & S_IFMT) {
case S_IFDIR :
printf("/\t ");
break;
case S_IFREG :
printf("*\t ");
break;
}
}
x++;
y = x%5;
}
else if (argv[2]) {
printf("I-node = %d name = %s\n", dirs[i]->d_ino, dirs[i]->d_name);
}
else {
if (y == 0) {
printf("\n%-16s\t ",dirs[i]->d_name);
}
else {
printf("%-16s\t ",dirs[i]->d_name);
}
x++;
y = x%5;
}
}
printf("\n");
}发布于 2017-03-24 06:25:44
您将永远不会输入else子句。如果argv[1]为NULL,则argv[2]将为NULL。您只需解析参数即可。
https://stackoverflow.com/questions/42942737
复制相似问题