I want to print only file names without printing Directory Names. So, I implement this function
void list_file(char* directory){
DIR *d;
struct dirent *dir;
d = opendir(directory);
if (d)
{
while ((dir = readdir(d)) != NULL)
{
printf("%c", dir->d_name[(int)strlen(dir->d_name)]);
if(dir->d_name[(int)strlen(dir->d_name)-2] != '/')
printf("%sn", dir->d_name);
}
closedir(d);
}
}
I checked that Directory names ends with ‘/’ character. So, I checked that if there are ‘/’ character at the end of name, don’t print that name but when I run the function, all of them is printed in selected directory?
Can you lead me that how can I check the end of Directory name?
Advertisement
Answer
What you are looking for is stat
or one of its variants. Specifically look at the st_mode
field of struct stat
. The macro you are interested in is S_ISDIR(x)
.
Find below your modified code that demonstrates what you want:
void list_file(char* directory) {
DIR *d;
struct dirent *dir;
int dir_len = strlen(directory);
char* path = malloc(dir_len + NAME_MAX + 2); // +2, 1 for '/' and 1 for ''
if(path == NULL) {
fprintf(stderr, "malloc failedn");
return;
}
strcpy(path, directory);
if(path[dir_len-1] != '/') {
path[dir_len] = '/';
dir_len++;
}
d = opendir(directory);
if (d) {
while ((dir = readdir(d)) != NULL)
{
struct stat buf;
strcpy(&path[dir_len], dir->d_name);
if(stat(path, &buf) < 0) {
fprintf(stderr, "errorn");
}
else {if(!S_ISDIR(buf.st_mode)) {
printf("%sn", dir->d_name);
}
}
}
closedir(d);
}
free(path);
}
I have removed your first print as it was printing the null terminating character of the string.
Update:
As pointed out in the comments since we are dealing with Linux you can use the d_type
field in struct dirent
(which is not part of POSIX but is part of Linux).
With that said the code would be the following.
void list_file(char* directory){
DIR *d;
struct dirent *dir;
d = opendir(directory);
if (d) {
while ((dir = readdir(d)) != NULL)
{
struct stat buf;
if(dir->d_type == DT_DIR) {
printf("%sn", dir->d_name);
}
}
closedir(d);
}
}
It is a lot cleaner, no need for malloc
.