mirror of
https://github.com/ebrandi/FDD-book.git
synced 2026-12-06 00:23:30 +00:00
Cleanup to reorganize the example files under examples/ directory
This commit is contained in:
parent
68843d0e40
commit
92ac73a6fb
34 changed files with 0 additions and 720 deletions
|
|
@ -1,6 +0,0 @@
|
||||||
# Makefile for hello_world kernel module
|
|
||||||
|
|
||||||
KMOD= hello_world
|
|
||||||
SRCS= hello_world.c
|
|
||||||
|
|
||||||
.include <bsd.kmod.mk>
|
|
||||||
|
|
@ -1,48 +0,0 @@
|
||||||
/*
|
|
||||||
* hello_world.c - Simple FreeBSD kernel module
|
|
||||||
* Prints messages when loaded and unloaded
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <sys/param.h>
|
|
||||||
#include <sys/kernel.h>
|
|
||||||
#include <sys/module.h>
|
|
||||||
#include <sys/systm.h>
|
|
||||||
|
|
||||||
/*
|
|
||||||
* 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);
|
|
||||||
|
|
@ -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"
|
|
||||||
|
|
@ -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
|
|
||||||
|
|
@ -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
|
|
||||||
|
|
@ -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"
|
|
||||||
|
|
@ -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)"
|
|
||||||
|
|
@ -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
|
|
||||||
|
|
@ -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"
|
|
||||||
|
|
@ -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"
|
|
||||||
|
|
@ -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 {} +
|
|
||||||
|
|
@ -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"
|
|
||||||
|
|
@ -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"
|
|
||||||
|
|
@ -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
|
|
||||||
|
|
@ -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 $?"
|
|
||||||
|
|
@ -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")"
|
|
||||||
|
|
@ -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"
|
|
||||||
|
|
@ -1,21 +0,0 @@
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
|
|
@ -1,4 +0,0 @@
|
||||||
# Makefile for hello.c
|
|
||||||
|
|
||||||
hello: hello.c
|
|
||||||
cc -o hello hello.c
|
|
||||||
|
|
@ -1,6 +0,0 @@
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
int main(void) {
|
|
||||||
printf("Hello, World!\n");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
@ -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
|
|
||||||
|
|
@ -1,33 +0,0 @@
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
/*
|
|
||||||
* 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);
|
|
||||||
}
|
|
||||||
|
|
@ -1,23 +0,0 @@
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
/*
|
|
||||||
* '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;
|
|
||||||
}
|
|
||||||
|
|
@ -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
|
|
||||||
|
|
@ -1,9 +0,0 @@
|
||||||
#include <stdio.h>
|
|
||||||
#include "mathutils.h"
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
printf("Sum: %d\n", add(3, 4));
|
|
||||||
printf("Difference: %d\n", subtract(10, 4));
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
@ -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;
|
|
||||||
}
|
|
||||||
|
|
@ -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
|
|
||||||
|
|
@ -1,9 +0,0 @@
|
||||||
#include "mydevice.h"
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
mydevice_probe();
|
|
||||||
mydevice_attach();
|
|
||||||
mydevice_detach();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
@ -1,18 +0,0 @@
|
||||||
#include <stdio.h>
|
|
||||||
#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");
|
|
||||||
}
|
|
||||||
|
|
@ -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
|
|
||||||
|
|
@ -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 <bsd.kmod.mk>
|
|
||||||
|
|
@ -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 <sys/param.h>
|
|
||||||
#include <sys/kernel.h>
|
|
||||||
#include <sys/module.h>
|
|
||||||
#include <sys/bus.h>
|
|
||||||
#include <sys/systm.h> // 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);
|
|
||||||
}
|
|
||||||
|
|
@ -1,14 +0,0 @@
|
||||||
#ifndef _MYDRIVER_H_
|
|
||||||
#define _MYDRIVER_H_
|
|
||||||
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/bus.h>
|
|
||||||
|
|
||||||
// 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_ */
|
|
||||||
|
|
@ -1,20 +0,0 @@
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
Loading…
Reference in a new issue