mirror of
https://github.com/BlyDoesCoding/ls-clone.git
synced 2025-12-31 04:23:08 +00:00
added main
This commit is contained in:
commit
50857ded20
1 changed files with 45 additions and 0 deletions
45
main.c
Normal file
45
main.c
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
#include <stdio.h> /* for printf and stderr */
|
||||
#include <string.h> /* for strerror */
|
||||
#include <dirent.h> /* for DIR, opendir, readdir, and dirent */
|
||||
#include <errno.h> /* for errno */
|
||||
#include <sys/stat.h> /* for stat */
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
DIR* FD; /* represent the directory */
|
||||
struct dirent* in_file; /* represent the file */
|
||||
char* target_dir = "."; /* current directory */
|
||||
|
||||
/* Scanning the target directory */
|
||||
FD = opendir(target_dir);
|
||||
if (FD == NULL)
|
||||
{
|
||||
fprintf(stderr, "Error: Failed to open input directory - %s\n", strerror(errno));
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Reading object (files, directories ...) from the folder */
|
||||
while ((in_file = readdir(FD)))
|
||||
{
|
||||
struct stat buffer;
|
||||
int status;
|
||||
|
||||
status = stat(in_file->d_name, &buffer);
|
||||
/* check status */
|
||||
if (status == -1)
|
||||
{
|
||||
fprintf(stderr, "Error: Failed to stat item - %s\n", strerror(errno));
|
||||
return 1;
|
||||
}
|
||||
/* check result */
|
||||
if ( buffer.st_mode & S_IFREG )
|
||||
{
|
||||
printf("%s is file \n", in_file->d_name);
|
||||
}
|
||||
}
|
||||
|
||||
/* Close the directory */
|
||||
closedir(FD);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue