exec
This commit is contained in:
parent
c6cab81a01
commit
5120f27b59
2 changed files with 34 additions and 17 deletions
BIN
shell2
BIN
shell2
Binary file not shown.
51
shell2.c
51
shell2.c
|
|
@ -1,33 +1,34 @@
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <readline/readline.h>
|
#include <unistd.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
#include <readline/history.h>
|
#include <readline/history.h>
|
||||||
|
#include <readline/readline.h>
|
||||||
|
|
||||||
#define clear() printf("\033[H\033[J")
|
#define clear() printf("\033[H\033[J")
|
||||||
|
|
||||||
void init_shell() {
|
void init_shell() {
|
||||||
clear();
|
clear();
|
||||||
|
|
||||||
printf("\n\n\n\n******************************************");
|
printf("\n\n\n\n******************************************");
|
||||||
printf("\n\n\n\t****MY SHELL****");
|
printf("\n\n\n\t****MY SHELL****");
|
||||||
printf("\n\n\t-USE AT YOUR OWN RISK-");
|
printf("\n\n\t-USE AT YOUR OWN RISK-");
|
||||||
printf("\n\n\n******************************************");
|
printf("\n\n\n******************************************");
|
||||||
|
|
||||||
char* username = getenv("USER");
|
char *username = getenv("USER");
|
||||||
printf("\n\nUSER is: @%s\n", username);
|
printf("\n\nUSER is: @%s\n", username);
|
||||||
sleep(1);
|
sleep(1);
|
||||||
|
|
||||||
clear();
|
clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
int take_input(char* str) {
|
int take_input(char *str) {
|
||||||
char* buf;
|
char *buf;
|
||||||
|
|
||||||
buf = readline("\n>>> ");
|
buf = readline("\n>>> ");
|
||||||
if(strlen(buf) != 0) {
|
if (strlen(buf) != 0) {
|
||||||
add_history(buf);
|
add_history(buf);
|
||||||
strcpy(str, buf);
|
strcpy(str, buf);
|
||||||
return 0;
|
return 0;
|
||||||
|
|
@ -42,17 +43,33 @@ void print_dir() {
|
||||||
printf("\nDir: %s", cwd);
|
printf("\nDir: %s", cwd);
|
||||||
}
|
}
|
||||||
|
|
||||||
def execute(char** parsed) {
|
void execute_args(char **parsed) {
|
||||||
pid_t pid = fork();
|
pid_t pid = fork();
|
||||||
|
|
||||||
if (pid == -1) {
|
if (pid == -1) {
|
||||||
printf("\nFailed forking child..");
|
printf("\nFailed forking child..");
|
||||||
return;
|
return;
|
||||||
} else if (pid == 0) {
|
} else if (pid == 0) {
|
||||||
|
if (execvp(parsed[0], parsed) < 0) {
|
||||||
|
printf("\nCould not execute command..");
|
||||||
|
}
|
||||||
|
exit(0);
|
||||||
|
} else {
|
||||||
|
wait(NULL);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void open_help() {
|
||||||
|
puts("\n***Welcome to my shell help"
|
||||||
|
"\n-Use the shell at your own risk..."
|
||||||
|
"\nList of commands support:"
|
||||||
|
"\n>cd"
|
||||||
|
"\n>ls"
|
||||||
|
"\n>exit"
|
||||||
|
"\n>all other general commands available in UNIX shell");
|
||||||
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
init_shell();
|
init_shell();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue