ls: Use command-line arguments
This commit is contained in:
parent
e1f58c0163
commit
2512acc716
@ -1,22 +1,31 @@
|
||||
#include <dirent.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main()
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
DIR* dp = opendir("/bin");
|
||||
const char* pathname;
|
||||
if (argc == 1) pathname = "/";
|
||||
else
|
||||
pathname = argv[1];
|
||||
DIR* dp = opendir(pathname);
|
||||
if (!dp)
|
||||
{
|
||||
perror("opendir");
|
||||
return 1;
|
||||
}
|
||||
bool first_ent = true;
|
||||
do {
|
||||
struct dirent* ent = readdir(dp);
|
||||
if (!ent) break;
|
||||
printf("%s\n", ent->d_name);
|
||||
printf(first_ent ? "%s" : " %s", ent->d_name);
|
||||
first_ent = false;
|
||||
} while (1);
|
||||
|
||||
printf("\n");
|
||||
|
||||
closedir(dp);
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user