Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <sys/wait.h>
- #define RED "\x1B[31m"
- #define RESET "\x1B[0m"
- char* varNames[1000];
- char* varValues[1000];
- int varCount = 0;
- int varIndex = 0;
- void register_child_signal(){
- }
- void execute_command(char** args){
- int pid;
- int status;
- pid = fork();
- if(pid == 0){
- execvp(args[0], args);
- printf("Error! Command not found\n");
- exit(0);
- }
- else if(pid < 0){
- printf("Error forking!\n");
- }
- else{
- do{
- waitpid(pid, &status, 0);
- }
- while(!WIFEXITED(status) && !WIFSIGNALED(status));
- }
- }
- void execute_shell_builtin(char** command_array){
- if(!strcmp("cd", command_array[0])){
- if(command_array[1] == NULL || !strcmp("~", command_array[1])){
- chdir(getenv("HOME"));
- }
- else if(!strcmp("..", command_array[1])){
- chdir("..");
- }
- else{
- chdir(command_array[1]);
- if (chdir(command_array[1]) == -1){
- printf("Error! Directory not found\n");
- }
- }
- }
- else if(!strcmp("echo", command_array[0])){
- if(command_array[1] == NULL){
- printf("\n");
- }
- else{
- int j = 1;
- while(command_array[j] != NULL){
- if(command_array[j][0] == '$'){
- char** x = varNames;
- char* varName = command_array[1] + 1;
- for(int i = 0; i < varIndex; i++){
- if(!strcmp(varName, varNames[i])){
- printf("%s\n", varValues[i]);
- return;
- }
- }
- printf("Error! Variable not found\n");
- }
- else{
- for(int i = 1; command_array[i] != NULL; i++){
- printf("%s ", command_array[i]);
- }
- printf("\n");
- }
- j++;
- }
- }
- }
- else if(!strcmp("export", command_array[0])){
- char* token = strtok(command_array[1], "=");
- varNames[varIndex] = token;
- token = strtok(NULL, "=");
- varValues[varIndex] = token;
- varIndex++;
- }
- int len = sizeof(varNames) / sizeof(varNames[0]);
- for (int i = 0; i < len ; i++) {
- printf("%s\n", varNames[i]);
- }
- for (int i = 0; i < len ; i++) {
- printf("%s\n", varValues[i]);
- }
- }
- int evaluate_command(char** command_array){
- if(!strcmp("cd", command_array[0]) || !strcmp("echo", command_array[0]) || !strcmp("export", command_array[0])){
- return 0;
- }
- else if(!strcmp("exit", command_array[0])){
- return -1;
- }
- else if(!strcmp(" ", command_array[0]) || !strcmp("", command_array[0])){
- return 10;
- }
- else{
- return 1;
- }
- }
- char** parse_command(char input[]){
- static char* strings[100];
- int index = 0;
- char* token = strtok(input, " \n"); // split string by space and newline
- while (token != NULL){
- strings[index++] = token ;
- token = strtok(NULL, " \n");
- }
- strings[index] = NULL;
- return strings;
- }
- void shell_loop(){
- int status = 1;
- do {
- if(!strcmp(getenv("HOME"), getcwd(NULL, 0))){
- printf(RED "%s", "ubuntu@Armia-PC:");
- printf( "%s", "~$ " RESET);
- }
- else{
- printf(RED "%s", "ubuntu@Armia-PC:");
- printf("%s", getcwd(NULL, 0));
- printf("%s", "$ " RESET);
- }
- char input_command[100] ;
- fgets(input_command, 100, stdin);
- char** command_array = parse_command(input_command); // parse array of char to array of strings seperated by space and ended with null
- int type = evaluate_command(command_array); // 0: shell builtin, 1: executable or error, -1: exit
- switch (type) {
- case 0:
- execute_shell_builtin(command_array);
- break;
- case 1:
- execute_command(command_array);
- break;
- case -1:
- status = 0;
- break;
- default:
- break;
- }
- }
- while(status);
- }
- void setup_environment(){
- chdir(getenv("HOME"));
- }
- void on_child_exit(){
- /*reap_child_zombie()
- write_to_log_file("Child terminated")*/
- }
- int main() {
- setup_environment();
- shell_loop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement