From 92ac73a6fb2961b4e5ebdf4bea6d628e2ad5a70c Mon Sep 17 00:00:00 2001 From: Edson Brandi Date: Wed, 22 Apr 2026 18:48:19 +0000 Subject: [PATCH] Cleanup to reorganize the example files under examples/ directory --- src/chapters/2/hello_world/Makefile | 6 - src/chapters/2/hello_world/hello_world.c | 48 -------- src/chapters/3/backup/backup.sh | 41 ------- src/chapters/3/case/case.sh | 20 --- src/chapters/3/debug_demo/debug_demo.sh | 11 -- src/chapters/3/functions/functions.sh | 40 ------ src/chapters/3/hello/hello.sh | 9 -- src/chapters/3/ifs/ifs.sh | 24 ---- src/chapters/3/list_files/list_files.sh | 16 --- .../3/list_files_bash/list_files_bash.sh | 14 --- .../3/list_files_exec/list_files_exec.sh | 12 -- .../3/list_files_posix/list_files_posix.sh | 23 ---- src/chapters/3/loops/loops.sh | 15 --- .../organize_downloads/organize_downloads.sh | 65 ---------- src/chapters/3/status/status.sh | 11 -- src/chapters/3/tmp_demo/tmp_demo.sh | 15 --- src/chapters/3/vars/vars.sh | 13 -- src/chapters/4/driver_sim/driver_sim.c | 21 ---- src/chapters/4/hello_world/Makefile | 4 - src/chapters/4/hello_world/hello.c | 6 - src/chapters/4/linkage_demo/Makefile | 14 --- src/chapters/4/linkage_demo/foo.c | 33 ----- src/chapters/4/linkage_demo/main.c | 23 ---- src/chapters/4/mathutils/Makefile | 14 --- src/chapters/4/mathutils/main.c | 9 -- src/chapters/4/mathutils/mathutils.c | 11 -- src/chapters/4/mathutils/mathutils.h | 7 -- src/chapters/4/mydevice/main.c | 9 -- src/chapters/4/mydevice/mydevice.c | 18 --- src/chapters/4/mydevice/mydevice.h | 9 -- src/chapters/4/mydriver/Makefile | 9 -- src/chapters/4/mydriver/mydriver.c | 116 ------------------ src/chapters/4/mydriver/mydriver.h | 14 --- .../4/null_terminator/null_terminator.c | 20 --- 34 files changed, 720 deletions(-) delete mode 100644 src/chapters/2/hello_world/Makefile delete mode 100644 src/chapters/2/hello_world/hello_world.c delete mode 100755 src/chapters/3/backup/backup.sh delete mode 100755 src/chapters/3/case/case.sh delete mode 100755 src/chapters/3/debug_demo/debug_demo.sh delete mode 100755 src/chapters/3/functions/functions.sh delete mode 100755 src/chapters/3/hello/hello.sh delete mode 100755 src/chapters/3/ifs/ifs.sh delete mode 100755 src/chapters/3/list_files/list_files.sh delete mode 100755 src/chapters/3/list_files_bash/list_files_bash.sh delete mode 100755 src/chapters/3/list_files_exec/list_files_exec.sh delete mode 100755 src/chapters/3/list_files_posix/list_files_posix.sh delete mode 100755 src/chapters/3/loops/loops.sh delete mode 100755 src/chapters/3/organize_downloads/organize_downloads.sh delete mode 100755 src/chapters/3/status/status.sh delete mode 100755 src/chapters/3/tmp_demo/tmp_demo.sh delete mode 100755 src/chapters/3/vars/vars.sh delete mode 100644 src/chapters/4/driver_sim/driver_sim.c delete mode 100644 src/chapters/4/hello_world/Makefile delete mode 100644 src/chapters/4/hello_world/hello.c delete mode 100644 src/chapters/4/linkage_demo/Makefile delete mode 100644 src/chapters/4/linkage_demo/foo.c delete mode 100644 src/chapters/4/linkage_demo/main.c delete mode 100644 src/chapters/4/mathutils/Makefile delete mode 100644 src/chapters/4/mathutils/main.c delete mode 100644 src/chapters/4/mathutils/mathutils.c delete mode 100644 src/chapters/4/mathutils/mathutils.h delete mode 100644 src/chapters/4/mydevice/main.c delete mode 100644 src/chapters/4/mydevice/mydevice.c delete mode 100644 src/chapters/4/mydevice/mydevice.h delete mode 100644 src/chapters/4/mydriver/Makefile delete mode 100644 src/chapters/4/mydriver/mydriver.c delete mode 100644 src/chapters/4/mydriver/mydriver.h delete mode 100644 src/chapters/4/null_terminator/null_terminator.c diff --git a/src/chapters/2/hello_world/Makefile b/src/chapters/2/hello_world/Makefile deleted file mode 100644 index 3c3701e..0000000 --- a/src/chapters/2/hello_world/Makefile +++ /dev/null @@ -1,6 +0,0 @@ -# Makefile for hello_world kernel module - -KMOD= hello_world -SRCS= hello_world.c - -.include diff --git a/src/chapters/2/hello_world/hello_world.c b/src/chapters/2/hello_world/hello_world.c deleted file mode 100644 index 00dbb64..0000000 --- a/src/chapters/2/hello_world/hello_world.c +++ /dev/null @@ -1,48 +0,0 @@ -/* - * hello_world.c - Simple FreeBSD kernel module - * Prints messages when loaded and unloaded - */ - -#include -#include -#include -#include - -/* - * Load handler - called when the module is loaded - */ -static int -hello_world_load(module_t mod, int cmd, void *arg) -{ - int error = 0; - - switch (cmd) { - case MOD_LOAD: - printf("Hello World! Kernel module loaded.\n"); - break; - case MOD_UNLOAD: - printf("Goodbye World! Kernel module unloaded.\n"); - break; - default: - error = EOPNOTSUPP; - break; - } - - return (error); -} - -/* - * Module declaration - */ -static moduledata_t hello_world_mod = { - "hello_world", /* module name */ - hello_world_load, /* event handler */ - NULL /* extra data */ -}; - -/* - * Register the module with the kernel - * DECLARE_MODULE(name, data, sub-system, order) - */ -DECLARE_MODULE(hello_world, hello_world_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE); -MODULE_VERSION(hello_world, 1); diff --git a/src/chapters/3/backup/backup.sh b/src/chapters/3/backup/backup.sh deleted file mode 100755 index 93e77e3..0000000 --- a/src/chapters/3/backup/backup.sh +++ /dev/null @@ -1,41 +0,0 @@ -#!/bin/sh -# backup.sh create a timestamped tar archive of a directory -# Usage: ./backup.sh /path/to/source -# Notes: -# - Uses /bin/sh so it runs on a clean FreeBSD 14.x install. -# - Creates ~/backups if it does not exist. -# - Names the archive sourcebasename-YYYYMMDD-HHMMSS.tar.gz - -set -eu -# set -e: exit immediately if any command fails -# set -u: treat use of unset variables as an error - -# Validate input -if [ $# -ne 1 ]; then - echo "Usage: $0 /path/to/source" >&2 - exit 2 -fi - -src="$1" - -# Verify that source is a directory -if [ ! -d "$src" ]; then - echo "Error: $src is not a directory" >&2 - exit 3 -fi - -# Prepare destination directory -dest="${HOME}/backups" -mkdir -p "$dest" - -# Build a safe archive name using only the last path component -base="$(basename "$src")" -stamp="$(date +%Y%m%d-%H%M%S)" -archive="${dest}/${base}-${stamp}.tar.gz" - -# Create the archive -# tar(1) is in the base system. The flags mean: -# - c: create - z: gzip - f: file name - C: change to directory -tar -czf "$archive" -C "$(dirname "$src")" "$base" - -echo "Backup created: $archive" diff --git a/src/chapters/3/case/case.sh b/src/chapters/3/case/case.sh deleted file mode 100755 index 555e25b..0000000 --- a/src/chapters/3/case/case.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/sh -# case.sh handle options with a case statement - -action="$1" # first command line argument - -case "$action" in - start) - echo "Starting service" - ;; - stop) - echo "Stopping service" - ;; - restart) - echo "Restarting service" - ;; - *) - echo "Usage: $0 {start|stop|restart}" >&2 - exit 2 - ;; -esac diff --git a/src/chapters/3/debug_demo/debug_demo.sh b/src/chapters/3/debug_demo/debug_demo.sh deleted file mode 100755 index 24461da..0000000 --- a/src/chapters/3/debug_demo/debug_demo.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/sh -# debug_demo.sh show simple tracing - -# set -x comment to disable verbose trace: -set -x - -echo "Step 1" -ls /etc >/dev/null - -echo "Step 2" -date diff --git a/src/chapters/3/functions/functions.sh b/src/chapters/3/functions/functions.sh deleted file mode 100755 index 85db6b9..0000000 --- a/src/chapters/3/functions/functions.sh +++ /dev/null @@ -1,40 +0,0 @@ -#!/bin/sh -# functions.sh - Demonstrates using functions and command-line arguments in a shell script. -# -# Usage: -# ./functions.sh NUM1 NUM2 -# Example: -# ./functions.sh 5 7 -# This will output: "[INFO] 5 + 7 = 12" - -# A simple function to print informational messages -say() { - # "$1" represents the first argument passed to the function - echo "[INFO] $1" -} - -# A function to sum two integers -sum() { - # "$1" and "$2" are the first and second arguments - local a="$1" - local b="$2" - - # Perform arithmetic expansion to add them - echo $((a + b)) -} - -# --- Main script execution starts here --- - -# Make sure the user provided two arguments -if [ $# -ne 2 ]; then - echo "Usage: $0 NUM1 NUM2" - exit 1 -fi - -say "Beginning work" - -# Call the sum() function with the provided arguments -result="$(sum "$1" "$2")" - -# Print the result in a nice format -say "$1 + $2 = $result" diff --git a/src/chapters/3/hello/hello.sh b/src/chapters/3/hello/hello.sh deleted file mode 100755 index d5a7479..0000000 --- a/src/chapters/3/hello/hello.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/sh -# hello.sh a first shell script using FreeBSD's native /bin/sh -# Print a friendly message with the current date and the active user. - -# 'date' prints the current date and time -# 'whoami' prints the current user -echo "Hello from FreeBSD!" -echo "Date: $(date)" -echo "User: $(whoami)" diff --git a/src/chapters/3/ifs/ifs.sh b/src/chapters/3/ifs/ifs.sh deleted file mode 100755 index 6a5bc8e..0000000 --- a/src/chapters/3/ifs/ifs.sh +++ /dev/null @@ -1,24 +0,0 @@ -#!/bin/sh -# ifs.sh demonstrate file and numeric tests - -file="/etc/rc.conf" - -# -f tests if a regular file exists -if [ -f "$file" ]; then - echo "$file exists" -else - echo "$file does not exist" -fi - -num=5 -if [ "$num" -gt 3 ]; then - echo "$num is greater than 3" -fi - -# String tests -user="$(whoami)" -if [ "$user" = "root" ]; then - echo "You are root" -else - echo "You are $user" -fi diff --git a/src/chapters/3/list_files/list_files.sh b/src/chapters/3/list_files/list_files.sh deleted file mode 100755 index 7cc2218..0000000 --- a/src/chapters/3/list_files/list_files.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/sh -# list_files.sh - count files in home directory - -set -eu -cd "${HOME}" - -count=0 -while IFS= read -r f; do - fname=${f#./} - echo "File found: '$fname'" - count=$((count + 1)) -done << EOF -$(find . -maxdepth 1 -type f ! -name ".*" -print) -EOF - -echo "Total files found: $count" diff --git a/src/chapters/3/list_files_bash/list_files_bash.sh b/src/chapters/3/list_files_bash/list_files_bash.sh deleted file mode 100755 index fae39ca..0000000 --- a/src/chapters/3/list_files_bash/list_files_bash.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/local/bin/bash -# list_files_bash.sh - correctly handle unusual filenames with bash - -set -eu -cd "${HOME}" - -count=0 -while IFS= read -r -d '' f; do - fname=${f#./} - echo "File found: '$fname'" - count=$((count + 1)) -done < <(find . -maxdepth 1 -type f ! -name ".*" -print0) - -echo "Total files found: $count" diff --git a/src/chapters/3/list_files_exec/list_files_exec.sh b/src/chapters/3/list_files_exec/list_files_exec.sh deleted file mode 100755 index 2968874..0000000 --- a/src/chapters/3/list_files_exec/list_files_exec.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/sh -# list_files_exec.sh - handle all filenames using find -exec - -set -eu -cd "${HOME}" - -find . -maxdepth 1 -type f ! -name ".*" -exec sh -c ' - for f; do - fname=${f#./} - printf "File found: '\''%s'\''\n" "$fname" - done -' sh {} + diff --git a/src/chapters/3/list_files_posix/list_files_posix.sh b/src/chapters/3/list_files_posix/list_files_posix.sh deleted file mode 100755 index 6e7eea0..0000000 --- a/src/chapters/3/list_files_posix/list_files_posix.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/bin/sh -# list_files_posix.sh - POSIX-compliant file listing - -set -eu -cd "${HOME}" - -# Use a temporary file instead of a pipe -tmpfile=$(mktemp) -trap 'rm -f "$tmpfile"' EXIT - -# Store find results in temporary file -find . -maxdepth 1 -type f ! -name ".*" > "$tmpfile" - -count=0 -while IFS= read -r f; do - fname=${f#./} - [ -z "$fname" ] && continue - - echo "File found: '$fname'" - count=$((count + 1)) -done < "$tmpfile" - -echo "Total files found: $count" diff --git a/src/chapters/3/loops/loops.sh b/src/chapters/3/loops/loops.sh deleted file mode 100755 index 829295e..0000000 --- a/src/chapters/3/loops/loops.sh +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/sh -# loops.sh for and while loops in /bin/sh - -# A 'for' loop over pathnames. Always quote expansions to handle spaces safely. -for f in /etc/*.conf; do - echo "Found conf file: $f" -done - -# A 'while' loop to read lines from a file safely. -# The 'IFS=' and 'read -r' avoid trimming spaces and backslash escapes. -count=0 -while IFS= read -r line; do - count=$((count + 1)) -done < /etc/hosts -echo "The /etc/hosts file has $count lines" diff --git a/src/chapters/3/organize_downloads/organize_downloads.sh b/src/chapters/3/organize_downloads/organize_downloads.sh deleted file mode 100755 index 604f254..0000000 --- a/src/chapters/3/organize_downloads/organize_downloads.sh +++ /dev/null @@ -1,65 +0,0 @@ -#!/bin/sh -# organize_downloads.sh - Tidy ~/Downloads by file extension -# -# Usage: -# ./organize_downloads.sh -# -# Creates subdirectories like Documents, Images, Audio, Video, Archives, Other -# and moves matched files into them safely. - -set -eu - -downloads="${HOME}/Downloads" - -# Create a temporary file to store the list of files -tmpfile=$(mktemp) - -# Remove temporary file when script exits (normal or error) -trap 'rm -f "$tmpfile"' EXIT - -# Ensure the Downloads directory exists -if [ ! -d "$downloads" ]; then - echo "Downloads directory not found at $downloads" >&2 - exit 1 -fi - -cd "$downloads" - -# Create target folders if missing -mkdir -p Documents Images Audio Video Archives Other - -# Find all regular files in current directory (non-recursive, excluding hidden files) -# -maxdepth 1: don't search in subdirectories -# -type f: only regular files (not directories or symlinks) -# ! -name ".*": exclude hidden files (those starting with a dot) -count=0 -find . -maxdepth 1 -type f ! -name ".*" > "$tmpfile" -while IFS= read -r f; do - # Strip leading "./" from path - fname=${f#./} - - # Skip if filename is empty (shouldn't happen, but safety check) - [ -z "$fname" ] && continue - - # Convert filename extension to lowercase for matching - lower=$(printf '%s' "$fname" | tr '[:upper:]' '[:lower:]') - - case "$lower" in - *.pdf|*.txt|*.md|*.doc|*.docx) dest="Documents" ;; - *.png|*.jpg|*.jpeg|*.gif|*.bmp) dest="Images" ;; - *.mp3|*.wav|*.flac) dest="Audio" ;; - *.mp4|*.mkv|*.mov|*.avi) dest="Video" ;; - *.zip|*.tar|*.gz|*.tgz|*.bz2) dest="Archives" ;; - *) dest="Other" ;; - esac - - echo "Moving '$fname' -> $dest/" - mv -n -- "$fname" "$dest/" # -n prevents overwriting existing files - count=$((count + 1)) # Increment the counter -done < "$tmpfile" # Feed the temporary file into the while loop - -if [ $count -eq 0 ]; then - echo "No files to organize." -else - echo "Done. Organized $count file(s)." -fi diff --git a/src/chapters/3/status/status.sh b/src/chapters/3/status/status.sh deleted file mode 100755 index d1c7b0f..0000000 --- a/src/chapters/3/status/status.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/sh -# status.sh show exit codes and conditional chaining - -# Try to list a directory that exists. 'ls' should return 0. -ls /etc && echo "Listing /etc succeeded" - -# Try something that fails. 'false' always returns nonzero. -false || echo "Previous command failed, so this message appears" - -# You can test the last status explicitly using $? -echo "Last status was $?" diff --git a/src/chapters/3/tmp_demo/tmp_demo.sh b/src/chapters/3/tmp_demo/tmp_demo.sh deleted file mode 100755 index 06c92aa..0000000 --- a/src/chapters/3/tmp_demo/tmp_demo.sh +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/sh -# tmp_demo.sh create and clean a temporary file safely - -set -eu - -tmpfile="$(mktemp -t myscript)" -# Arrange cleanup on exit for success or error -cleanup() { - [ -f "$tmpfile" ] && rm -f "$tmpfile" -} -trap cleanup EXIT - -echo "Temporary file is $tmpfile" -echo "Hello temp" > "$tmpfile" -echo "Contents: $(cat "$tmpfile")" diff --git a/src/chapters/3/vars/vars.sh b/src/chapters/3/vars/vars.sh deleted file mode 100755 index bb96ad2..0000000 --- a/src/chapters/3/vars/vars.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/sh -# vars.sh demonstrate variables and proper quoting - -name="dev" -greeting="Welcome" -# Double quotes preserve spaces and expand variables. -echo "$greeting, $name" -# Single quotes prevent expansion. This prints the literal characters. -echo '$greeting, $name' - -# Command substitution captures output of a command. -today="$(date +%Y-%m-%d)" -echo "Today is $today" diff --git a/src/chapters/4/driver_sim/driver_sim.c b/src/chapters/4/driver_sim/driver_sim.c deleted file mode 100644 index 0ac90db..0000000 --- a/src/chapters/4/driver_sim/driver_sim.c +++ /dev/null @@ -1,21 +0,0 @@ -#include - -// Function definition -unsigned int read_register(unsigned int address) -{ - // Simulated register value based on address - unsigned int value = address * 2; - printf("[driver] Reading register at address 0x%X...\n", address); - return value; -} - -int main(void) -{ - unsigned int reg_addr = 0x10; // pretend this is a hardware register address - unsigned int data; - - data = read_register(reg_addr); // Call the function - printf("[driver] Value read: 0x%X\n", data); - - return 0; -} diff --git a/src/chapters/4/hello_world/Makefile b/src/chapters/4/hello_world/Makefile deleted file mode 100644 index b62c8b9..0000000 --- a/src/chapters/4/hello_world/Makefile +++ /dev/null @@ -1,4 +0,0 @@ -# Makefile for hello.c - -hello: hello.c - cc -o hello hello.c diff --git a/src/chapters/4/hello_world/hello.c b/src/chapters/4/hello_world/hello.c deleted file mode 100644 index 288609c..0000000 --- a/src/chapters/4/hello_world/hello.c +++ /dev/null @@ -1,6 +0,0 @@ -#include - -int main(void) { - printf("Hello, World!\n"); - return 0; -} diff --git a/src/chapters/4/linkage_demo/Makefile b/src/chapters/4/linkage_demo/Makefile deleted file mode 100644 index 1004103..0000000 --- a/src/chapters/4/linkage_demo/Makefile +++ /dev/null @@ -1,14 +0,0 @@ -CC = cc -CFLAGS = -Wall -Wextra -std=c99 - -linkage_demo: main.o foo.o - $(CC) $(CFLAGS) -o linkage_demo main.o foo.o - -main.o: main.c - $(CC) $(CFLAGS) -c main.c - -foo.o: foo.c - $(CC) $(CFLAGS) -c foo.c - -clean: - rm -f *.o linkage_demo diff --git a/src/chapters/4/linkage_demo/foo.c b/src/chapters/4/linkage_demo/foo.c deleted file mode 100644 index ca14c02..0000000 --- a/src/chapters/4/linkage_demo/foo.c +++ /dev/null @@ -1,33 +0,0 @@ -#include - -/* - * Global variable with external linkage: - * - Visible to other files (translation units) in the program. - * - No 'static' keyword means it has external linkage by default. - */ -int shared_counter = 0; - -/* - * File-private variable with internal linkage: - * - The 'static' keyword at file scope means this name - * is only visible inside foo.c. - */ -static int internal_flag = 1; - -/* - * Function with external linkage by default: - * - Can be called from other files if they declare its prototype. - */ -void increment(void) { - /* - * Local variable with no linkage: - * - Exists only during this function call. - * - Cannot be accessed from anywhere else. - */ - int step = 1; - - if (internal_flag) // Only code in foo.c can see internal_flag - shared_counter += step; // Modifies the global shared_counter - - printf("Counter: %d\n", shared_counter); -} diff --git a/src/chapters/4/linkage_demo/main.c b/src/chapters/4/linkage_demo/main.c deleted file mode 100644 index bea4f27..0000000 --- a/src/chapters/4/linkage_demo/main.c +++ /dev/null @@ -1,23 +0,0 @@ -#include - -/* - * 'extern' tells the compiler: - * - This variable exists in another file (foo.c). - * - Do not allocate storage for it here. - */ -extern int shared_counter; - -/* - * Forward declaration for the function defined in foo.c: - * - Lets us call increment() from this file. - */ -void increment(void); - -int main(void) { - increment(); // Calls increment() from foo.c - shared_counter += 10; // Legal: shared_counter has external linkage - increment(); - - // internal_flag = 0; // ERROR: not visible here (internal linkage in foo.c) - return 0; -} diff --git a/src/chapters/4/mathutils/Makefile b/src/chapters/4/mathutils/Makefile deleted file mode 100644 index 5100d9f..0000000 --- a/src/chapters/4/mathutils/Makefile +++ /dev/null @@ -1,14 +0,0 @@ -CC = cc -CFLAGS = -Wall -Wextra -std=c99 - -mathprog: main.o mathutils.o - $(CC) $(CFLAGS) -o mathprog main.o mathutils.o - -main.o: main.c mathutils.h - $(CC) $(CFLAGS) -c main.c - -mathutils.o: mathutils.c mathutils.h - $(CC) $(CFLAGS) -c mathutils.c - -clean: - rm -f *.o mathprog diff --git a/src/chapters/4/mathutils/main.c b/src/chapters/4/mathutils/main.c deleted file mode 100644 index 0d5cd8e..0000000 --- a/src/chapters/4/mathutils/main.c +++ /dev/null @@ -1,9 +0,0 @@ -#include -#include "mathutils.h" - -int main(void) -{ - printf("Sum: %d\n", add(3, 4)); - printf("Difference: %d\n", subtract(10, 4)); - return 0; -} diff --git a/src/chapters/4/mathutils/mathutils.c b/src/chapters/4/mathutils/mathutils.c deleted file mode 100644 index e4ccfea..0000000 --- a/src/chapters/4/mathutils/mathutils.c +++ /dev/null @@ -1,11 +0,0 @@ -#include "mathutils.h" - -int add(int a, int b) -{ - return a + b; -} - -int subtract(int a, int b) -{ - return a - b; -} diff --git a/src/chapters/4/mathutils/mathutils.h b/src/chapters/4/mathutils/mathutils.h deleted file mode 100644 index f4203ad..0000000 --- a/src/chapters/4/mathutils/mathutils.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef MATHUTILS_H -#define MATHUTILS_H - -int add(int a, int b); -int subtract(int a, int b); - -#endif // MATHUTILS_H diff --git a/src/chapters/4/mydevice/main.c b/src/chapters/4/mydevice/main.c deleted file mode 100644 index ea8b00d..0000000 --- a/src/chapters/4/mydevice/main.c +++ /dev/null @@ -1,9 +0,0 @@ -#include "mydevice.h" - -int main(void) -{ - mydevice_probe(); - mydevice_attach(); - mydevice_detach(); - return 0; -} diff --git a/src/chapters/4/mydevice/mydevice.c b/src/chapters/4/mydevice/mydevice.c deleted file mode 100644 index 89547ff..0000000 --- a/src/chapters/4/mydevice/mydevice.c +++ /dev/null @@ -1,18 +0,0 @@ -#include -#include "mydevice.h" - -// Function definitions -void mydevice_probe(void) -{ - printf("[mydevice] Probing hardware... done.\n"); -} - -void mydevice_attach(void) -{ - printf("[mydevice] Attaching device and initializing resources...\n"); -} - -void mydevice_detach(void) -{ - printf("[mydevice] Detaching device and cleaning up.\n"); -} diff --git a/src/chapters/4/mydevice/mydevice.h b/src/chapters/4/mydevice/mydevice.h deleted file mode 100644 index 37b6f2a..0000000 --- a/src/chapters/4/mydevice/mydevice.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef MYDEVICE_H -#define MYDEVICE_H - -// Function declarations (prototypes) -void mydevice_probe(void); -void mydevice_attach(void); -void mydevice_detach(void); - -#endif // MYDEVICE_H diff --git a/src/chapters/4/mydriver/Makefile b/src/chapters/4/mydriver/Makefile deleted file mode 100644 index 58cadc3..0000000 --- a/src/chapters/4/mydriver/Makefile +++ /dev/null @@ -1,9 +0,0 @@ -# Makefile for hello_world kernel module - -KMOD= mydriver -SRCS= mydriver.c - -# Required for Newbus drivers - generates device_if.h and bus_if.h -SRCS+= device_if.h bus_if.h - -.include diff --git a/src/chapters/4/mydriver/mydriver.c b/src/chapters/4/mydriver/mydriver.c deleted file mode 100644 index dd9e1fa..0000000 --- a/src/chapters/4/mydriver/mydriver.c +++ /dev/null @@ -1,116 +0,0 @@ -/* - * FreeBSD device driver lifecycle (quick map) - * - * 1) Kernel enumerates devices on a bus - * The bus framework walks hardware and creates device_t objects. - * - * 2) probe() - * The kernel asks your driver if it supports a given device. - * You inspect IDs or capabilities and return a score. - * - Return ENXIO if this driver does not match. - * - Return BUS_PROBE_DEFAULT or a better score if it matches. - * - * 3) attach() - * Called after a successful probe to bring the device online. - * Typical work: - * - Allocate resources (memory, IRQ) with bus_alloc_resource_any() - * - Map registers and set up bus_space - * - Initialize hardware to a known state - * - Set up interrupts and handlers - * Return 0 on success, or an errno if something fails. - * - * 4) Runtime - * Your driver services requests. This may include: - * - Interrupt handlers - * - I/O paths invoked by upper layers or devfs interfaces - * - Periodic tasks, callouts, or taskqueues - * - * 5) detach() - * Called when the device is being removed or the module unloads. - * Cleanup tasks: - * - Quiesce hardware, stop DMA, disable interrupts - * - Tear down handlers and timers - * - Unmap registers and release resources with bus_release_resource() - * Return 0 on success, or an errno if detach must be denied. - * - * 6) Optional lifecycle events - * - suspend() and resume() during power management - * - shutdown() during system shutdown - * - * Files to remember - * - mydriver.h declares the entry points that the kernel and bus code will call - * - mydriver.c defines those functions and contains the implementation details - */ -#include -#include -#include -#include -#include // device_printf -#include "mydriver.h" - -/* - * mydriver_probe() - * Called early during device enumeration. - * Purpose: decide if this driver matches the hardware represented by dev. - * Return: BUS_PROBE_DEFAULT for a normal match, a better score for a strong match, - * or ENXIO if the device is not supported. - */ -int -mydriver_probe(device_t dev) -{ - device_printf(dev, "Probing device...\n"); - - /* - * Here you would usually check vendor and device IDs or use bus-specific - * helper routines. If the device is not supported, return (ENXIO). - */ - - return (BUS_PROBE_DEFAULT); -} - -/* - * mydriver_attach() - * Called after a successful probe when the kernel is ready to attach the device. - * Purpose: allocate resources, map registers, initialise hardware, register interrupts, - * and make the device ready for use. - * Return: 0 on success, or an errno value (like ENOMEM or EIO) on failure. - */ -int -mydriver_attach(device_t dev) -{ - device_printf(dev, "Attaching device and initializing resources...\n"); - - /* - * Typical steps you will add here: - * 1) Allocate device resources (I/O memory, IRQs) with bus_alloc_resource_any(). - * 2) Map register space and set up bus_space tags and handles. - * 3) Initialise hardware registers to a known state. - * 4) Set up interrupt handlers if needed. - * 5) Create device nodes or child devices if this driver exposes them. - * On any failure, release what you allocated and return an errno. - */ - - return (0); -} - -/* - * mydriver_detach() - * Called when the device is being detached or the module is unloading. - * Purpose: stop the hardware, free resources, and leave the system clean. - * Return: 0 on success, or an errno value if detach must be refused. - */ -int -mydriver_detach(device_t dev) -{ - device_printf(dev, "Detaching device and cleaning up...\n"); - - /* - * Typical steps you will add here: - * 1) Disable interrupts and stop DMA or timers. - * 2) Tear down interrupt handlers. - * 3) Unmap register space and free bus resources with bus_release_resource(). - * 4) Destroy any device nodes or sysctl entries created at attach time. - */ - - return (0); -} diff --git a/src/chapters/4/mydriver/mydriver.h b/src/chapters/4/mydriver/mydriver.h deleted file mode 100644 index ca39986..0000000 --- a/src/chapters/4/mydriver/mydriver.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef _MYDRIVER_H_ -#define _MYDRIVER_H_ - -#include -#include - -// Public entry points: declared here so bus/framework code can call them -// These are the entry points the kernel will call during the device's lifecycle -// Function prototypes (declarations) -int mydriver_probe(device_t dev); -int mydriver_attach(device_t dev); -int mydriver_detach(device_t dev); - -#endif /* _MYDRIVER_H_ */ diff --git a/src/chapters/4/null_terminator/null_terminator.c b/src/chapters/4/null_terminator/null_terminator.c deleted file mode 100644 index 12bd9e1..0000000 --- a/src/chapters/4/null_terminator/null_terminator.c +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include - -int main() { - // Fill stack with non-zero garbage - char garbage[100]; - memset(garbage, 'Z', sizeof(garbage)); - - // Deliberately forget the null terminator - char broken[5] = {'B', 'S', 'D', '!', 'X'}; - - // Print as if it were a string - will likely show garbage now - printf("Broken string: %s\n", broken); - - // Now with proper termination - char fixed[6] = {'B', 'S', 'D', '!', 'X', '\0'}; - printf("Fixed string: %s\n", fixed); - - return 0; -}