Hello. I'm having some problems with this code, that shall become some kind of a "locate" copy (it's a college assignment).
I'm guessing that the issue resides in the file type testing macros (S_ISLNK(), S_ISDIR(), S_ISREG()). To run do program you shall type a directory (full path, ex. /usr) as the first (and only) argument, then, it is supposed to list all the directories, regular files and links but it lists some links as directories and other directories aren't even being listed.
Here's the code.
#include "sys/types.h"
#include "sys/stat.h"
#include "stdlib.h"
#include "string.h"
#include "unistd.h"
#include "dirent.h"
#include "stdio.h"
#define MAX_SIZE 2048
int main(int argc, char* argv[]) {
DIR* teste;
char* actual = (char*) malloc(MAX_SIZE);
struct stat file;
struct dirent* info = (struct dirent*) malloc(MAX_SIZE);
teste = opendir(argv[1]);
info = readdir(teste);
while (readdir(teste)!=NULL) {
stat(actual,&file);
info = readdir(teste);
actual = info->d_name;
if (strcmp(actual,"..")==0 ||
strcmp(actual,".")==0) continue;
if (S_ISLNK(file.st_mode)) {
printf("%s is a link\n",actual);
continue;
}
else if (S_ISREG(file.st_mode)) {
printf("%s is a file\n",actual);
}
else if (S_ISDIR(file.st_mode)) {
printf("%s is a folder\n",actual);
}
}
closedir(teste);
return 0;
}
Running example:
(...)$ ./ldir /usr
only prints:
lib is a folder
sbin is a folder
in spite of /usr containing seven other folders.
Another running example:
(...)$ ./ldir /
prints
initrd.img.old is a folder
usr is a folder
dev is a folder
opt is a folder
etc is a folder
lib is a folder
tmp is a folder
selinux is a folder
boot is a folder
sys is a folder
in spite of initrd.img.old being a link.
I can't see what's wrong with it (the code) so, if anyone could help I'd be very thankful.
Question
takecare
Hello. I'm having some problems with this code, that shall become some kind of a "locate" copy (it's a college assignment).
I'm guessing that the issue resides in the file type testing macros (S_ISLNK(), S_ISDIR(), S_ISREG()). To run do program you shall type a directory (full path, ex. /usr) as the first (and only) argument, then, it is supposed to list all the directories, regular files and links but it lists some links as directories and other directories aren't even being listed.
Here's the code.
#include "sys/types.h" #include "sys/stat.h" #include "stdlib.h" #include "string.h" #include "unistd.h" #include "dirent.h" #include "stdio.h" #define MAX_SIZE 2048 int main(int argc, char* argv[]) { DIR* teste; char* actual = (char*) malloc(MAX_SIZE); struct stat file; struct dirent* info = (struct dirent*) malloc(MAX_SIZE); teste = opendir(argv[1]); info = readdir(teste); while (readdir(teste)!=NULL) { stat(actual,&file); info = readdir(teste); actual = info->d_name; if (strcmp(actual,"..")==0 || strcmp(actual,".")==0) continue; if (S_ISLNK(file.st_mode)) { printf("%s is a link\n",actual); continue; } else if (S_ISREG(file.st_mode)) { printf("%s is a file\n",actual); } else if (S_ISDIR(file.st_mode)) { printf("%s is a folder\n",actual); } } closedir(teste); return 0; }Running example:
only prints:
in spite of /usr containing seven other folders.
Another running example:
prints
in spite of initrd.img.old being a link.
I can't see what's wrong with it (the code) so, if anyone could help I'd be very thankful.
Link to comment
Share on other sites
2 answers to this question
Recommended Posts