-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add lecture 5 program of a poor shell
- Loading branch information
1 parent
2b23821
commit 4ac9fb2
Showing
3 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
*.exe | ||
ashwathama | ||
bash_gitignore.sh | ||
garibsh | ||
ior | ||
pid | ||
sync |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include<stdio.h> | ||
#include<unistd.h> | ||
#include<stdlib.h> | ||
#include<string.h> | ||
#include<sys/wait.h> | ||
|
||
#define MAXLINE 1000 | ||
|
||
int main(int argc, char* argv[]) { | ||
|
||
char buf[MAXLINE]; | ||
pid_t pid; | ||
int status; | ||
|
||
while (fgets(buf, MAXLINE, stdin) != NULL) { | ||
if (buf[strlen(buf) - 1] == '\n') { | ||
buf[strlen(buf) - 1] = 0; /*replace newline with null*/ | ||
} | ||
|
||
if ((pid = fork()) < 0) { | ||
printf("cannot fork\n"); | ||
exit(1); | ||
} else if (pid == 0) { | ||
/* child */ | ||
execlp(buf, buf, (char*) 0); | ||
printf("could not execute %s", buf); | ||
exit(127); | ||
} | ||
|
||
/* parent */ | ||
if ((pid = waitpid(pid, &status, 0)) < 0) { | ||
printf("could not wait\n"); | ||
} | ||
} | ||
|
||
exit(0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters