Lesson 20 of 23
ls
The ls Command
ls lists the contents of a directory, sorted alphabetically. By default it skips hidden files — those whose names begin with a dot:
$ ls
Makefile README.md src tests
Files like .git and .env are present on disk but not shown without the -a flag.
Your Implementation
Write void my_ls(const char *entries) that takes a newline-separated list of filenames (unsorted, may contain hidden files), skips any that start with ., sorts the rest, and prints them one per line.
This combines two operations you have already built:
- Filter — skip entries whose first character is
. - Sort — print the remaining entries in alphabetical order
void my_ls(const char *entries) {
char lines[64][256];
int count = 0;
// Parse lines, skipping hidden files
while (*entries) {
char *out = lines[count];
const char *start = entries;
while (*entries && *entries != '\n') { *out++ = *entries++; }
*out = '\0';
if (*entries == '\n') entries++;
if (start[0] != '.') count++; // keep only non-hidden
}
// Bubble sort
...
for (int i = 0; i < count; i++) printf("%s\n", lines[i]);
}
Your Task
Implement my_ls that filters hidden entries and prints the rest sorted alphabetically.
TCC compiler loading...
Loading...
Click "Run" to execute your code.