Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A00227290_tarea1 #61

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions labs/03/myTerminal.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Noé Benjamín Rosales Medina
A00227290
Tarea 1.5 Simple Terminal (fork/exec)
19/10/2023
*/

#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <string.h>

int main(){
// Variables declaration
char command[100];
char stop[] = "stop";
char ls[] = "ls";
char date[] = "date";
char pwd[] = "pwd";
pid_t pid;

while(strcmp(command, stop) != 0){
printf("nbrm> ");
scanf("%s", command);
if(strcmp(command, date) == 0 || strcmp(command, ls) == 0 || strcmp(command, pwd) == 0){
// Fork a child process
pid = fork();
if(pid < 0){
// Error ocurred
fprintf(stderr, "Fork Failed");
exit(-1);
}
else if (pid == 0 && strcmp(command, ls) == 0){
// Child process for ls
execlp("/bin/ls","ls",NULL);
}
else if (pid == 0 && strcmp(command, date) == 0){
// Child process for date
execlp("/bin/date","date",NULL);
}
else if (pid == 0 && strcmp(command, pwd) == 0){
// Child process for pwd
execlp("/bin/pwd","pwd",NULL);
}
else{
// Parent waits for the child to complete its process
wait(NULL);
}
}
else if (strcmp(command, stop) != 0){
// Any other command
printf("nbrm> Wrong command, stop to exit the terminal\n");
}
}
return 0;
}