Reputation: 16081
At a high level, how would you use the execve() function to write a duplicate of the ls program in UNIX? I am doing an exercise to familiarize myself with the exec() family of functions, command-line arguments, and environment variables. I am not familiar with using these concepts, however I know what they do.
Upvotes: 1
Views: 2912
Reputation: 2459
The code below can excute ls
command. Do you mean this?
#include <stdio.h>
int main(void)
{
system("ls");
return 0;
}
And I wrote a simple ls
demo for you.
my_ls.c
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
int main(int argc, char *argv[])
{
if (argc != 2) {
return 0;
}
DIR *dir = opendir(argv[1]);
if (dir) {
struct dirent *s_dir;
while((s_dir = readdir(dir))) {
printf("%s ", s_dir->d_name);
}
printf("\n");
}
return 0;
}
Usage:
gcc my_ls.c -o my_ls
./my_ls .
Upvotes: 3