Verificando se um dir. entrada retornada por readdir é um diretório, link ou arquivo
Estou criando um programa que é executado em um shell Linux e aceita um argumento (um diretório) e exibe todos os arquivos no diretório, juntamente com seu tipo.
A saída deve ser assim:
<< ./Program testDirectory
Dir directory1
lnk linkprogram.c
reg file.txt
Se nenhum argumento for apresentado, ele usará o diretório atual. Aqui está o meu código:
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
int main(int argc, char *argv[])
{
struct stat info;
DIR *dirp;
struct dirent* dent;
//If no args
if (argc == 1)
{
argv[1] = ".";
dirp = opendir(argv[1]); // specify directory here: "." is the "current directory"
do
{
dent = readdir(dirp);
if (dent)
{
printf("%c ", dent->d_type);
printf("%s \n", dent->d_name);
/* if (!stat(dent->d_name, &info))
{
//printf("%u bytes\n", (unsigned int)info.st_size);
}*/
}
} while (dent);
closedir(dirp);
}
//If specified directory
if (argc > 1)
{
dirp = opendir(argv[1]); // specify directory here: "." is the "current directory"
do
{
dent = readdir(dirp);
if (dent)
{
printf("%c ", dent->d_type);
printf("%s \n", dent->d_name);
/* if (!stat(dent->d_name, &info))
{
printf("%u bytes\n", (unsigned int)info.st_size);
}*/
}
} while (dent);
closedir(dirp);
}
return 0;
}
Por algum motivodent->d_type
não está exibindo o tipo de arquivo. Não tenho muita certeza do que fazer, alguma sugestão?