routine update, no changes

This commit is contained in:
anihilis 2026-01-20 09:17:31 -08:00
parent f458b73fd5
commit 0d4dc438e6

View file

@ -1,28 +1,15 @@
Perfect — lets switch hats from *“just making it work”* to *“learning C properly”*.
Ill explain **how to read and think about this file**, not just what it does.
Ill reference the structure you now have in the file and explain the *C concepts* behind each part.
---
## 1⃣ The mindset shift: C vs C++
## The mindset shift: C vs C++
In C:
* ❌ No classes, no RAII, no `std::string`
* ✅ **You control memory explicitly**
* ✅ Functions are small, focused, and composable
* ✅ Portability comes from *conditional compilation*
- ❌ No classes, no RAII, no `std::string`
- ✅ **You control memory explicitly**
- ✅ Functions are small, focused, and composable
- ✅ Portability comes from *conditional compilation*
Your program follows a **very idiomatic C pattern**:
- “Each function returns ownership of dynamically allocated data, and the caller frees it.”*
> *“Each function returns ownership of dynamically allocated data, and the caller frees it.”*
Thats a **huge** C concept to internalize.
---
## 2⃣ Feature macros (`_POSIX_C_SOURCE`)
## Feature macros (`_POSIX_C_SOURCE`)
```c
#define _POSIX_C_SOURCE 200809L
@ -32,21 +19,14 @@ Thats a **huge** C concept to internalize.
C standard libraries hide some functions unless you *opt in*.
This line tells the compiler:
> “I want modern POSIX features like `strdup`, `gethostname`, `sysconf`.”
Without it:
* Some systems wont expose those functions
* Youll get mysterious warnings or missing symbols
**Lesson:**
👉 In C, *what functions exist* depends on feature macros.
---
## 3 Headers: what C teaches you here
## Headers: what C teaches you here
```c
#include <stdio.h> // printf, FILE
@ -66,7 +46,7 @@ In C:
---
## 4 Conditional compilation (`#if defined(...)`)
## Conditional compilation (`#if defined(...)`)
Example:
@ -92,7 +72,7 @@ This is how *real* portable C software works (git, curl, openssh).
---
## 5 The most important function in the file
## The most important function in the file
```c
static char *dup_or_unknown(const char *s)
@ -121,12 +101,9 @@ free(user);
No special cases.
No defensive `if (ptr)` checks.
**Lesson:**
👉 In C, helper functions enforce invariants so callers stay clean.
---
## 6 Dynamic allocation patterns (CRITICAL)
## Dynamic allocation patterns (CRITICAL)
Example:
@ -151,12 +128,9 @@ Contrast this with C++:
* No smart pointers
* No safety net
**Lesson:**
👉 In C, *ownership is a social contract*, not a language feature.
---
## 7 Why no global buffers?
## Why no global buffers?
You *could* have done:
@ -176,12 +150,9 @@ Dynamic allocation makes your functions:
* Testable
* Library-quality
**Lesson:**
👉 Globals are easy — dynamic ownership is correct.
---
## 8 Reading system information in C
## Reading system information in C
Each platform teaches a lesson:
@ -206,12 +177,9 @@ sysctl()
* Buffer-size negotiation
* Integer & struct-based data
**Lesson:**
👉 C doesnt abstract the OS — it exposes it.
---
## 9 Time handling (classic C pain point)
## Time handling (classic C pain point)
```c
time_t now = time(NULL);
@ -224,12 +192,9 @@ Why this matters:
* You *never* subtract times directly
* `difftime()` handles portability
**Lesson:**
👉 In C, use the standard library *exactly as intended*.
---
## 🔟 The `main()` function: clean by design
## The `main()` function: clean by design
```c
char *user = get_user();
@ -246,39 +211,3 @@ Notice:
All complexity lives *outside* `main()`.
**Lesson:**
👉 In C, `main()` should read like a script.
---
## 1⃣1⃣ What this code teaches you overall
Youve learned:
* ✅ How C handles memory *explicitly*
* ✅ How portability really works
* ✅ How to design safe APIs in unsafe languages
* ✅ How Unix exposes system information
* ✅ How real-world C programs are structured
This is **not beginner C** — this is *professional-grade foundational C*.
---
## If you want to go deeper next
From a learning perspective, great next steps would be:
* 🔹 Splitting into `.h` / `.c`
* 🔹 Writing your own `string_builder`
* 🔹 Adding error-return variants (`int get_x(char **out)`)
* 🔹 Making this a reusable library
* 🔹 Running it under `valgrind` and analyzing output
If you want, I can:
* Rewrite one function *line by line* explaining *every character*
* Or turn this into a **guided C learning project**
Just tell me how deep you want to go — youre asking exactly the right questions 👏