I'm writing a basic program which takes in the name of a directory and opens it. Once I've opened it, I would like to classify files and subdirectories. I found code from a while ago which did something similar - but this is the output I'm getting:
[.] -> [0]
[..] -> [0]
[a.txt] -> [0]
[subDir] -> [0]
Where a.txt is a text fille and subDir is a directory, but both those values are returning 0. Not sure where I'm going wrong.
Full code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
void handleDirectory(DIR *dir);
int isFile(const char *path);
int main(int argc, char **argv)
{
if (argc != 2){
printf("Not enough arguments!\n");
return 0;
}
DIR *dir = opendir(argv[1]);
if (dir == NULL){
return 0;
}
handleDirectory(dir);
} // end of main
void handleDirectory(DIR *dir){
struct dirent *temp;
while ((temp = readdir(dir)) != NULL) {
printf("[%s] -> [%d]\n", temp->d_name, isFile(temp->d_name));
}
} // end of handle directory method
int isFile(const char *path)
{
struct stat path_stat;
lstat(path, &path_stat);
return S_ISREG(path_stat.st_mode);
}