When you type `*.txt`, the shell - not the program - turns it into the list of matching filenames. Today you implement that filename expansion for the `*` and `?` wildcards.
Expand a word containing `*` or `?` into the sorted list of matching filenames in the current directory.
Here is a surprise for most people: when you run rm *.txt, the rm program
never sees the *. The shell expands *.txt into the actual list of matching
filenames first, then hands rm that list. This is globbing, and it means
every program gets wildcard support for free without knowing wildcards exist.
Two wildcards today. * matches any run of characters (including none), and ?
matches exactly one character. To expand a pattern, scan the current directory and
keep every filename the pattern matches, then sort the results - shells return
glob matches in sorted order so output is predictable. One conventional rule:
files whose names begin with . are hidden and are not matched by a leading
* or ?, which is why * does not sweep up .git. A small recursive matcher
handles both wildcards cleanly.
// match(pattern, name): '*' matches any run, '?' matches one char// then scan the directory and keep names that match, sortedfor each entry in readdir(".")if (entry[0] != '.' && match(pattern, entry)) // skip dotfilesadd(results, entry);sort(results);