I created a simple linux shell. It reads line by line with getline () until ctrl + d (eof / -1) is entered into standard input.
When entering stdin string using string code:
ls -al & ls -a -l
My shell works very well.
I tried to run the script through my shell, but it does not work. When I execute the script, my shell is automatically executed (1st line), but the shell does not interpret other lines.
What can cause this? I have to say that I am very new to Linux, and the teacher said nothing about all this. Just homework. I did some research, but all I found.
Here is the code of my Shell. I added shell path to etc / shells but it still doesn't work
#include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <stdbool.h> int main() { ssize_t bufer_size = 0; char* line = NULL; int line_size; while ((line_size = getline(&line, &bufer_size, stdin)) != -1) // while end of file { char** words_array; words_array = (char**)malloc(200 * sizeof(char*)); int words_count = 0; int i; int j = 0; int words_length = 0; char word[100]; for (i = 0; i < line_size; i++) { if (line[i] == ' ' || line[i] == '\n') { words_array[words_count] = (char*)malloc(words_length * sizeof(char)); int b; for (b = 0; b < words_length; b++) { words_array[words_count][b] = word[b]; } j = 0; words_count++; words_length = 0; } else { word[j] = line[i]; j++; words_length++; } } bool run_in_background = false; if (words_array[words_count - 1][0] == '&') { run_in_background = true; words_array[words_count - 1] = NULL; } int a = fork(); if (a == 0) // child process { execvp(words_array[0], words_array); } else // parent process { if (run_in_background == true) { printf("\n ---- running in background. \n"); } else { printf("\n ---- running normal \n"); wait(NULL); } } } return 0; }
c linux shell
Patryk
source share