#include <sys/types.h>
#include <dirent.h>
int main(int argc, char* argv[]){
DIR *dp;
struct dirent *dirp;
if(argc != 2) {
printf("usage: ls directory_name");
exit(0);
}
if((dp = opendir(argv[1]))==NULL){
printf("can't open %s", argv[1]);
exit(0);
}
else {
while((dirp=readdir(dp)) != NULL )
printf("%s\n", dirp->d_name);
}
closedir(dp);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main(void){
char buf[256];
pid_t pid;
int status;
printf("%% ");
while(fgets(buf, 256, stdin) != 0){
if(buf[strlen(buf)-1] == '\n')
buf[strlen(buf)-1]=0;
if((pid=fork()) <0) {
printf("fork error");
}
else if(pid==0){
execlp(buf, buf, (char*)0);
exit(0);
}
if((pid=waitpid(pid, &status, 0))<0)
printf("waitpid error");
printf("%% ");
}
return 0;
}