[英]My c code for implementing ls, doesn't work on mac
I'm facing a strange problem, last night I was doing my homework for implementing ls
on my linux
, the code works fine there, now when I test it on imac, it doesn't work. 我正面临一个奇怪的问题,昨晚我正在做我的功课,在我的linux
上实现ls
,代码在那里工作正常,现在当我在imac上测试它时,它不起作用。 It only reads the current directory, it doesn't include subdirectories. 它只读取当前目录,不包含子目录。
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#define GREEN "\x1b[32m"
#define BLUE "\x1b[34m"
#define WHITE "\x1b[37m"
void Usage() {
fprintf(stderr, "\nUsage: exec [OPTION]... [DIR]...\n");
fprintf(stderr, "List DIR's (directory) contents\n");
fprintf(stderr, "\nOptions\n-R\tlist subdirectories recursively\n");
return;
}
void RecDir(char * path, int flag) {
DIR * dp = opendir(path);
if(!dp) {
perror(path);
return;
}
struct dirent * ep;
char newdir[512];
printf(BLUE "\n%s :\n" WHITE, path);
while((ep = readdir(dp)))
if(strncmp(ep->d_name, ".", 1))
printf(GREEN "\t%s" WHITE, ep->d_name);
closedir(dp);
dp = opendir(path);
while((ep = readdir(dp))) if(strncmp(ep->d_name, ".", 1)) {
if(flag && ep->d_type == 4) {
sprintf(newdir, "%s/%s", path, ep->d_name);
RecDir(newdir, 1);
}
}
closedir(dp);
}
int main(int argc, char ** argv) {
switch(argc) {
case 2:
if(strcmp(argv[1], "-R") == 0)
Usage();
else
RecDir(argv[1], 0);
break;
case 3:
if(strcmp(argv[1], "-R") == 0)
RecDir(argv[2], 1);
else
Usage();
break;
default:
Usage();
}
return 0;
}
The flag variable was always 0
so i changed it while calling the RecDir()
function so that it will go into the if block: 标志变量总是0
所以我在调用RecDir()
函数时更改它,以便它进入if块:
if(flag && ep->d_type == 4) {
sprintf(newdir, "%s/%s", path, ep->d_name);
printf("here: %s\n",ep->d_name);
RecDir(newdir, 1);
}
and be able to call RecDir()
function recursively. 并能够递归调用RecDir()
函数。
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#define GREEN "\x1b[32m"
#define BLUE "\x1b[34m"
#define WHITE "\x1b[37m"
void Usage() {
fprintf(stderr, "\nUsage: exec [OPTION]... [DIR]...\n");
fprintf(stderr, "List DIR's (directory) contents\n");
fprintf(stderr, "\nOptions\n-R\tlist subdirectories recursively\n");
return;
}
void RecDir(char * path, int flag) {
DIR * dp = opendir(path);
if(!dp) {
perror(path);
return;
}
struct dirent * ep;
char newdir[512];
printf(BLUE "\n%s :\n" WHITE, path);
while((ep = readdir(dp)))
if(strncmp(ep->d_name, ".", 1))
printf(GREEN "\t%s" WHITE, ep->d_name);
closedir(dp);
dp = opendir(path);
while((ep = readdir(dp))) if(strncmp(ep->d_name, ".", 1)) {
//printf("In While %d\n",flag);
if(flag && ep->d_type == 4) {
sprintf(newdir, "%s/%s", path, ep->d_name);
printf("here: %s\n",ep->d_name);
RecDir(newdir, 1);
}
}
closedir(dp);
}
int main(int argc, char ** argv) {
switch(argc) {
case 2:
if(strcmp(argv[1], "-R") == 0)
Usage();
else
//supplied flag=1 instead of 0, explained above.
RecDir(argv[1], 1);
break;
case 3:
if(strcmp(argv[1], "-R") == 0) {
RecDir(argv[2], 1);
}
else
Usage();
break;
default:
Usage();
}
return 0;
}
Given that it's another operating system, you first need to confirm that d_type
holds the value that you think it does. 鉴于它是另一个操作系统,您首先需要确认d_type
包含您认为它的值。 In the first loop, as debug, print out d_type
as well as the name. 在第一个循环中,作为调试,打印出d_type
以及名称。
Note that in the second loop, you go through an awful lot of trouble scanning the directory a second time to recurse into subdirectories - and then at the last second decide not to if
flag
is0
. 请注意,在第二个循环中,您第二次扫描目录以进入子目录时会遇到很多麻烦 - 然后在最后一秒决定不返回flag
是否为0
。 Why not pre-testflag
for0
and simply not do the whole open-the-directory-again thing? 为什么不预先测试0
flag
,而不是完全打开目录?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.