Skip to content

Instantly share code, notes, and snippets.

@MylesBorins
Created January 8, 2018 16:21
Show Gist options
  • Save MylesBorins/6470e321365e6364f009da36f9c71eed to your computer and use it in GitHub Desktop.
Save MylesBorins/6470e321365e6364f009da36f9c71eed to your computer and use it in GitHub Desktop.
example of super basic ls implementation in c to build `gcc ls.c`
#include <stdio.h>
#include <dirent.h>
int main (int argc, char *argv[ ])
{
DIR *dp;
struct dirent *dirp;
if (argc != 2) {
fprintf(stderr, "usage: ls directory_name\n");
return 1;
}
if ((dp = opendir(argv[1])) == NULL) {
perror("can't copen");
return 2;
}
while((dirp = readdir(dp)) != NULL)
printf("%s\n", dirp->d_name);
closedir(dp);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment