Skip to content

Commit

Permalink
Add lecture 5 program of a poor shell
Browse files Browse the repository at this point in the history
  • Loading branch information
unmeshvrije committed Dec 30, 2024
1 parent 2b23821 commit 4ac9fb2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*.exe
ashwathama
bash_gitignore.sh
garibsh
ior
pid
sync
37 changes: 37 additions & 0 deletions command_executor.c
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);
}
3 changes: 3 additions & 0 deletions fork.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

int main(int argc, char *argv[]) {
if (fork() == 0) {
/* child */
execl("mycopy", "mycopy", argv[1], argv[2], 0);
}

/* parent */
wait((int*)0);
printf("copy done\n");
//exit
Expand Down

0 comments on commit 4ac9fb2

Please sign in to comment.