Add function scan directory

This commit is contained in:
Fabio Scotto di Santolo
2026-07-31 20:09:37 +02:00
parent af56ec524d
commit 078b95dc4b
7 changed files with 224 additions and 0 deletions

1
.gitignore vendored
View File

@@ -35,6 +35,7 @@
*.i*86
*.x86_64
*.hex
build/
# Debug files
*.dSYM/

39
Makefile Normal file
View File

@@ -0,0 +1,39 @@
CC := cc
CPPFLAGS := -Iinclude -MMD -MP
CFLAGS := -std=c11 -Wall -Wextra -Wpedantic -Wconversion
LDFLAGS :=
LDLIBS :=
BUILD_DIR := build
TARGET := $(BUILD_DIR)/fsnap
SOURCES := \
src/main.c \
src/walk.c \
src/scan.c
OBJECTS := $(SOURCES:%.c=$(BUILD_DIR)/%.o)
DEPENDENCIES := $(OBJECTS:.o=.d)
.PHONY: all clean run
all: $(TARGET)
$(TARGET): $(OBJECTS) | $(BUILD_DIR)
$(CC) $(LDFLAGS) $(OBJECTS) $(LDLIBS) -o $@
$(BUILD_DIR)/%.o: %.c
@mkdir -p $(dir $@)
$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@
$(BUILD_DIR):
mkdir -p $@
run: $(TARGET)
./$(TARGET)
clean:
rm -rf $(BUILD_DIR)
-include $(DEPENDENCIES)

6
include/scan.h Normal file
View File

@@ -0,0 +1,6 @@
#ifndef SCAN_H
#define SCAN_H
int scan(const char *);
#endif

8
include/walk.h Normal file
View File

@@ -0,0 +1,8 @@
#ifndef WALK_H
#define WALK_H
typedef int(*operation)(const char *);
int walk(const char *directory, operation op);
#endif

29
src/main.c Normal file
View File

@@ -0,0 +1,29 @@
#include "scan.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "command not found [scan]\n");
return EXIT_FAILURE;
}
// I check the argument command exists
errno = 0;
const char *cmd = argv[1];
if (strcmp(cmd, "scan") == 0) {
const char *directory = argv[2];
if (scan(directory) == -1) {
const int err = errno;
fprintf(stderr, "scan failed: %s\n", strerror(err));
return EXIT_FAILURE;
}
} else {
fprintf(stderr, "command %s is not valid\n", cmd);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}

31
src/scan.c Normal file
View File

@@ -0,0 +1,31 @@
#define _XOPEN_SOURCE 500
#include "scan.h"
#include "walk.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef PATH_MAX
static long pathmax = PATH_MAX;
#else
static long pathmax = 1024;
#endif
int print_file(const char *file) {
printf("[%s]\n", file);
return 0;
}
/* Scan recursively directory, printing all entry */
int scan(const char *directory) {
if (directory == NULL || strlen(directory) == 0)
return -1;
char path[pathmax];
const char *dir = NULL;
if ((dir = realpath(directory, path)) == NULL) {
perror("realpath");
return -1;
}
return walk(dir, print_file);
}

110
src/walk.c Normal file
View File

@@ -0,0 +1,110 @@
#define _DEFAULT_SOURCE
#include "walk.h"
#include <dirent.h>
#include <errno.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
static char *blacklist[] = {".", ".."};
char *strconcat(const char *src, int n, ...) {
va_list ap;
/* First pass: total length. strcat never reallocates, so the buffer has to
be sized up front. */
size_t len = strlen(src);
va_start(ap, n);
for (int i = 0; i < n; i++) {
len += strlen(va_arg(ap, char *));
}
va_end(ap);
char *path = malloc(len + 1);
if (path == NULL)
return NULL;
size_t off = strlen(src);
memcpy(path, src, off);
va_start(ap, n);
for (int i = 0; i < n; i++) {
const char *s = va_arg(ap, char *);
size_t l = strlen(s);
memcpy(path + off, s, l);
off += l;
}
va_end(ap);
path[off] = '\0';
return path;
}
int ignore_file(const char *name) {
for (long unsigned int i = 0; i < sizeof(blacklist) / sizeof(char *); i++) {
if (strcmp(name, blacklist[i]) == 0)
return 1;
}
return 0;
}
static int handle_unknown(const char *path, operation op) {
struct stat st;
if (lstat(path, &st) == -1) {
return -1;
}
if (S_ISDIR(st.st_mode)) {
return walk(path, op);
}
op(path);
return 0;
}
int walk(const char *directory, operation op) {
DIR *dirp = opendir(directory);
if (dirp == NULL) {
return -1;
}
errno = 0;
char *curr = NULL;
struct dirent *direntp = NULL;
while ((direntp = readdir(dirp)) != NULL) {
curr = direntp->d_name;
if (ignore_file(curr))
continue;
char *path = strconcat(directory, 2, "/", curr);
if (path == NULL)
return -1;
switch (direntp->d_type) {
case DT_UNKNOWN:
// operating system or filesystem do not support dirent->d_type.
// In this case use alternative algorithm for walk directories.
if (handle_unknown(path, op) == -1) {
return -1;
}
break;
case DT_DIR:
if (walk(path, op) == -1) {
return -1;
}
break;
default:
op(path);
break;
}
if (path != NULL)
free(path);
}
if (errno != 0) {
return -1;
}
return closedir(dirp);
}