This document details the application startup, execution, and shutdown lifecycle of fastfetch, focusing on the entry points, initialization sequence, and the global state management through the FFinstance singleton. This covers the journey from when the user executes the fastfetch binary to when the program exits and cleans up resources.
For information about configuration parsing and JSON schema validation, see Configuration and Schema System. For details about the module execution system, see Module System Architecture.
Fastfetch provides two primary entry points in separate executables:
The primary entry point is the main() function in src/fastfetch.c This function implements a complete command-line interface with extensive argument parsing, configuration loading, and module execution based on user-provided structure definitions.
Key responsibilities:
parseCommand() and parseOption() functions--help, --version, --list-*, --print-*, --gen-config)optionParseConfigFile()An alternative entry point exists in src/flashfetch.c7-142 which provides a hardcoded execution path designed to replicate neofetch's default behavior. Unlike the main fastfetch binary, flashfetch:
Sources: src/fastfetch.c src/flashfetch.c1-143
The entire application state is managed through a single global variable instance of type FFinstance, defined in src/common/init.c20 This singleton pattern centralizes all configuration and runtime state.
Structure breakdown:
| Component | Type | Purpose | Defined In |
|---|---|---|---|
instance.config.logo | FFOptionsLogo | Logo rendering configuration | src/options/logo.h |
instance.config.display | FFOptionsDisplay | Output formatting options | src/options/display.h |
instance.config.general | FFOptionsGeneral | General behavior settings | src/options/general.h |
instance.state.platform | FFPlatform | OS-specific paths and data | src/util/platform/FFPlatform.h |
instance.state.configDoc | yyjson_doc* | Parsed JSON configuration | src/fastfetch.h45 |
instance.state.resultDoc | yyjson_mut_doc* | JSON output accumulator | src/fastfetch.h46 |
Sources: src/fastfetch.h29-58 src/common/init.c20
Sources: src/fastfetch.c src/common/init.c
The ffInitInstance() function in src/common/init.c49-61 performs the initial application setup:
Key operations:
setlocale() to configure character encoding (UTF-8 on Windows, time locale on Unix)ffPlatformInit() to detect OS type, config directories, data directories, and executable pathffDetectTerminalTheme()Sources: src/common/init.c49-61 src/common/init.c22-47
Configuration happens in two stages within the main() function:
The parseCommand() function src/fastfetch.c572-680 handles special commands that cause immediate program exit:
| Command Pattern | Handler | Action |
|---|---|---|
-h, --help | printCommandHelp() | Display help and exit |
-v, --version | printVersion() | Display version and exit |
--print-* | Various print functions | Print built-in data and exit |
--list-* | Various list functions | List available options and exit |
--gen-config* | generateConfigFile() | Generate config file and continue |
-c, --config | optionParseConfigFile() | Load configuration file |
-j, --json | enableJsonOutput() | Enable JSON output mode |
After parseCommand(), the parseOption() function src/fastfetch.c682-824 processes module-specific and display options.
Sources: src/fastfetch.c572-824
The optionParseConfigFile() function src/fastfetch.c461-544 implements a cascading search strategy:
Search paths (in order):
--config -)platform.dataDirs + fastfetch/presets/presets/The parseJsoncFile() function src/fastfetch.c381-431 uses the yyjson library to parse JSON/JSONC/JSON5 formats based on file extension:
.json: Strict JSON (no comments or trailing commas).jsonc: JSON with comments and trailing commas.json5: Full JSON5 supportSources: src/fastfetch.c461-544 src/fastfetch.c381-431
The ffStart() function src/common/init.c97-134 prepares the terminal for output:
Console State Management:
Signal handlers:
SIGINT, SIGTERM, SIGQUIT → call resetConsole() and exitSIGCHLD → no-op handler to interrupt blocking syscallsConsole modifications:
\033<FileRef file-url="https://github.com/fastfetch-cli/fastfetch/blob/f0759acd/?25l) to prevent flicker during output\n- Disable line wrapping (\\033[?7l) to control output layout\n- Enable Windows virtual terminal processing for ANSI escape codes\n\nSources#LNaN-LNaN" NaN file-path="?25l) to prevent flicker during output\n- Disable line wrapping (\033[?7l`) to control output layout\n- Enable Windows virtual terminal processing for ANSI escape codes\n\nSources">Hii src/common/init.c63-95Module execution is handled by the main loop in src/fastfetch.c826-949 This phase is documented in detail in Module System Architecture.
Sources: src/fastfetch.c826-949
The ffFinish() function src/common/init.c136-142 performs cleanup:
Operations:
config.logo.printRemaining is trueSources: src/common/init.c136-142 src/common/init.c66-77
The ffDestroyInstance() function src/common/init.c159-163 deallocates all resources:
Memory deallocation:
instance is a global variable, its memory is reclaimed by the OS on process exitSources: src/common/init.c159-163 src/common/init.c144-157
Fastfetch installs signal handlers to ensure proper terminal cleanup even when interrupted:
Platform-specific implementation:
| Platform | Mechanism | Signals Handled |
|---|---|---|
| Windows | SetConsoleCtrlHandler() | CTRL_C_EVENT, CTRL_BREAK_EVENT, etc. |
| Unix | sigaction() | SIGINT, SIGTERM, SIGQUIT, SIGCHLD |
Handler behavior:
resetConsole() to restore terminal state, then call exit(0)poll()) when child processes exitSources: src/common/init.c79-95 src/common/init.c116-121
The resetConsole() function src/common/init.c66-77 ensures the terminal is left in a usable state:
State restoration:
This function is called in three scenarios:
ffFinish() src/common/init.c141atexit() registration (not currently used, but pattern supports it)Sources: src/common/init.c66-77 src/common/init.c97-121
The simplified flashfetch entry point demonstrates the minimal lifecycle:
Key differences from main fastfetch:
__attribute__((cleanup(ffDestroyXxxOptions)))Sources: src/flashfetch.c7-142
Refresh this wiki