first tests with lstat

This commit is contained in:
root committed 2026-06-25 22:19:30 +02:00
1 parent 0455ebaa40
commit b5811ecd39
1 file changed
+44 -5
+44 -5
View File
@@ -1,17 +1,22 @@
#define _XOPEN_SOURCE 700
// external dependencies
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <argp.h>
#include <dirent.h>
#include <errno.h>
#include <linux/limits.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
// internal dependencies
#include "log.h"
#include "arguments.h"
// program logic
int main( int argc, char** argv )
{
@@ -36,14 +41,48 @@ int main( int argc, char** argv )
while ( NULL != ( entry = readdir( directory ) ) )
{
char full_path[ PATH_MAX ]; // TODO: assemble full path
struct stat status = { 0 };
char full_path[ PATH_MAX ];
if ( !args.show_all && ( !strncmp( entry->d_name, ".", 1) || !strncmp( entry->d_name, "..", 2 ) ) )
{
continue;
}
// if ( lstat( ) ) // TODO: get file stats
log_info( "%s: %c", entry->d_name, entry->d_type );
size_t path_length = strlen( args.path );
char* seperator = args.path[ path_length - 1 ] == '/' ? "" : "/";
snprintf( full_path, PATH_MAX, "%s%s%s", args.path, seperator, entry->d_name );
struct stat file_stat = { 0 };
if ( lstat( full_path, &file_stat ) )
{
log_error( "failed to get file stats for [ %s ]", full_path );
return -EIO;
}
switch ( file_stat.st_mode & S_IFMT )
{
case S_IFBLK:
log_info( "%s is a [ block device ]", full_path );
break;
case S_IFCHR:
log_info( "%s is a [ character device ]", full_path );
break;
case S_IFDIR:
log_info( "%s is a [ directory ]", full_path );
break;
case S_IFIFO:
log_info( "%s is a [ fifo device ]", full_path );
break;
case S_IFLNK:
log_info( "%s is a [ link ]", full_path );
break;
case S_IFREG:
log_info( "%s is a [ file ]", full_path );
break;
case S_IFSOCK:
log_info( "%s is a [ socket ]", full_path );
break;
}
}
closedir( directory );