Solved exercise 2 - create a simple tree program clone

This commit is contained in:
Fabio Scotto di Santolo
2025-06-30 22:25:31 +02:00
parent 7bc220f92c
commit a629f9de01
6 changed files with 162 additions and 0 deletions

27
exercises/tree/Makefile Normal file
View File

@@ -0,0 +1,27 @@
CC = gcc
CFLAGS = -Wall -Wextra -std=c11 -g
SRC = main.c tree.c
OBJ = $(SRC:.c=.o)
EXEC = tree
# Main target
all: $(EXEC)
# Build executable
$(EXEC): $(OBJ)
$(CC) $(CFLAGS) -o $@ $^
# Run test suite
test: all
@cd test && ./run_tests.sh
# Clean object files and executable
clean:
rm -f $(OBJ) $(EXEC)
# Full cleanup (also test artifacts)
distclean: clean
cd test && rm -f actual_output.txt valgrind.log expected_output.txt && rm -rf testdir
.PHONY: all test clean distclean

15
exercises/tree/main.c Normal file
View File

@@ -0,0 +1,15 @@
#include <stdio.h>
#include <stdlib.h>
#include "tree.h"
int main(int argc, char *argv[]) {
const char *path = (argc > 1) ? argv[1] : ".";
if (print_tree(path, 0) == -1) {
perror("Error traversing directory");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}

View File

@@ -0,0 +1,68 @@
#!/bin/bash
TREE_EXEC="../tree"
TEST_DIR="testdir"
REF_OUTPUT="expected_output.txt"
OUT_FILE="actual_output.txt"
VALGRIND_LOG="valgrind.log"
# Preparazione struttura di test
setup() {
rm -rf "$TEST_DIR"
mkdir -p "$TEST_DIR/sub1"
mkdir -p "$TEST_DIR/sub2"
touch "$TEST_DIR/file1.txt"
touch "$TEST_DIR/sub1/file2.txt"
touch "$TEST_DIR/sub2/file3.txt"
cat > "$REF_OUTPUT" <<EOF
|-- file1.txt
|-- sub1
|-- file2.txt
|-- sub2
|-- file3.txt
EOF
}
# Esecuzione tree e confronto output
run_test() {
echo "[*] Running functional test..."
"$TREE_EXEC" "$TEST_DIR" | grep -v "^\\." > "$OUT_FILE"
if diff -q "$OUT_FILE" "$REF_OUTPUT"; then
echo "[✓] Output matches expected output."
else
echo "[✗] Output differs!"
diff "$OUT_FILE" "$REF_OUTPUT"
return 1
fi
}
# Valgrind check
run_valgrind() {
echo "[*] Running Valgrind..."
valgrind --leak-check=full --log-file="$VALGRIND_LOG" "$TREE_EXEC" "$TEST_DIR" > /dev/null
if grep -q "no leaks are possible" "$VALGRIND_LOG"; then
echo "[✓] Valgrind: No memory leaks."
else
echo "[✗] Valgrind detected memory issues:"
cat "$VALGRIND_LOG"
return 1
fi
}
cleanup() {
rm -rf "$TEST_DIR" "$OUT_FILE" "$VALGRIND_LOG" "$REF_OUTPUT"
}
main() {
setup
run_test || exit 1
run_valgrind || exit 1
cleanup
echo "[✓] All tests passed."
}
main

View File

@@ -0,0 +1,10 @@
#!/bin/bash
# Crea una struttura di directory per test
mkdir -p testdir/{sub1,sub2}
touch testdir/file1.txt
touch testdir/sub1/file2.txt
touch testdir/sub2/file3.txt
echo "Directory structure created."

35
exercises/tree/tree.c Normal file
View File

@@ -0,0 +1,35 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include "tree.h"
int print_tree(const char *path, int depth) {
DIR *dir = opendir(path);
if (!dir) return -1;
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
// Skip . and ..
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
for (int i = 0; i < depth; i++) printf(" ");
printf("|-- %s\n", entry->d_name);
// Construct full path
char full_path[4096];
snprintf(full_path, sizeof(full_path), "%s/%s", path, entry->d_name);
struct stat st;
if (stat(full_path, &st) == 0 && S_ISDIR(st.st_mode)) {
print_tree(full_path, depth + 1);
}
}
closedir(dir);
return 0;
}

7
exercises/tree/tree.h Normal file
View File

@@ -0,0 +1,7 @@
#ifndef TREE_H
#define TREE_H
int print_tree(const char *path, int depth);
#endif