srch.c
April 3, 2026 ยท View on GitHub
Purpose
Recursive file/content search with metadata filtering and optional result collection.
Callback Contract
- search callback receives:
(search, entry, NULL)for a found match(search, NULL, message)for an operational error
- callback return meaning for matches:
> 0: keep the entry and append it tofileArray0: do not store the entry< 0: interrupt the search
API Reference
Entry helpers
void XSearch_InitEntry(xsearch_entry_t *pEntry)
- Arguments:
- entry object to clear.
- Does:
- zeroes textual fields and metadata.
- Returns:
- no return value.
void XSearch_CreateEntry(xsearch_entry_t *pEntry, const char *pName, const char *pPath, xstat_t *pStat)
- Arguments:
- destination entry, optional file name, optional path and optional stat struct.
- Does:
- initializes the entry and copies name/path/stat-derived metadata.
- on non-Windows symlinks, also fills
sLinkandpRealPath.
- Returns:
- no return value.
xsearch_entry_t *XSearch_AllocEntry(void)
xsearch_entry_t *XSearch_NewEntry(const char *pName, const char *pPath, xstat_t *pStat)
- Arguments:
- optional entry metadata.
- Does:
- heap-allocates a new entry and optionally populates it.
- Returns:
- allocated entry or
NULL.
- allocated entry or
void XSearch_FreeEntry(xsearch_entry_t *pEntry)
- Arguments:
- heap-allocated entry.
- Does:
- frees
pRealPathwhen present and then frees the entry itself.
- frees
- Returns:
- no return value.
xsearch_entry_t *XSearch_GetEntry(xsearch_t *pSearch, int nIndex)
- Arguments:
- search context and result index.
- Does:
- returns a stored result entry from
fileArray.
- returns a stored result entry from
- Returns:
- entry pointer or
NULLwhen the index is out of range.
- entry pointer or
Search lifecycle
void XSearch_Init(xsearch_t *pSearch, const char *pFileName)
- Arguments:
- search context.
- file-name pattern string, optionally
;-separated.
- Does:
- resets search flags and filter fields.
- initializes
nameTokensandfileArray. - tokenizes multi-name patterns into
nameTokens. - stores the raw pattern in
sName.
- Returns:
- no return value.
void XSearch_Destroy(xsearch_t *pSearch)
- Arguments:
- search context.
- Does:
- marks the search as interrupted.
- destroys the stored result array and token array.
- Returns:
- no return value.
int XSearch(xsearch_t *pSearch, const char *pDirectory)
- Arguments:
- search context.
- directory path, or
NULLwhen searching stdin withbReadStdin.
- Does:
- applies filename, permission, size, link-count and file-type filters.
- optionally searches file contents for
sText. - optionally recurses into subdirectories.
- for stdin mode, searches the input buffer instead of traversing directories.
- Returns:
XSTDOKon successful completion.XSTDNONfor some no-match content-search paths.XSTDERRon hard failure or user interruption.
- Important nuance:
- some operational filesystem errors are only reported through the callback and do not force a final
XSTDERRunless the callback interrupts the search.
- some operational filesystem errors are only reported through the callback and do not force a final
Content Search Behavior
bInsensitivelowercases both search strings and loaded data before matching.bSearchLinesreports every matching line separately withnLineNumandsLine.- without
bSearchLines, the code tries to report the nearest line around the first match position. - binary/non-line-friendly matches are reported as:
"Binary file matches""Binary input matches"
Important Notes
XSearch_Init()treats;inpFileNameas a multi-pattern separator.- Caller-owned result arrays inside
xsearch_tare destroyed byXSearch_Destroy(). - Permission filtering compares chmod-style numeric permissions.
- Type filtering can include execute permission (
XF_EXEC) as a separate bit.