запуск bin-программ с помощью fork и exec

я кодирую микрошелл, который будет запускать программы из bin-файла с помощью fork и exec, и он отлично работает, но всегда получает ввод в следующей строке без командной строки, и я не знаю, как с этим справиться

#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#define MAX 4096
#define BLUE "\x1b[94m"
#define CLEAR "\x1b[0m"
int main()
{
    int pyk = 1;
    while (pyk == 1)
    {
        struct stat buf;
        const char *space = " ";
        const char *enter = "\n";
        char *token;
        char input[MAX];
        char *arg;
        char *cwd = getcwd(NULL, 0);
        printf(BLUE "[{%s}] $ ", cwd);
        printf(CLEAR);

        fgets(input, MAX, stdin);
        token = strtok(input, space);
        arg = strtok(NULL, enter);

        if (!strncmp(token, "/bin/", 5))
        {
            if (!stat(token, &buf))
            {
                if (fork() == 0)
                {

                    pyk = 0;

                    execl(token, arg, NULL);

                    continue;
                }
                else
                {
                    continue;
                }
            }
        }
    }
    return 0;
}
[{/home/maks/lab/vsc}] $ /bin/ls ls
[{/home/maks/lab/vsc}] $ a.out  messenger  pliczek.cm  shel  shel.c  shel.o  test  test.c  -W

[{/home/maks/lab/vsc}] $ 

пустое место - это место, где он получает ввод сразу после ls, и все, что я хочу сделать, это поместить туда командную строку имейте в виду, что это всего лишь часть моего кода


person mxsgd    schedule 22.01.2021    source источник


Ответы (1)


Вы хотите продолжить только после завершения дочернего процесса, поэтому вам нужно подождать, пока он это сделает:

#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h> 
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#define MAX 4096
#define BLUE "\x1b[94m"
#define CLEAR "\x1b[0m"
int main() 
{  
  int pyk=1;
   while(pyk==1){
             struct stat buf;
              const char *space=" ";
            const char *enter="\n"; 
            char *token;
            char input[MAX];
            char *arg;
            char * cwd = getcwd(NULL, 0);
            int child;
              printf(BLUE "[{%s}] $ ", cwd);
               printf(CLEAR);
                fflush(stdout);
              fgets(input, MAX, stdin);
              token = strtok(input, space);
              arg = strtok(NULL, enter);      
             
               if (!strncmp(token,"/bin/",5))
                {
                 
                    if (!stat(token,&buf))
                    {
                       child = fork();

                        if(child==0)
                        {  
                            pyk=0;
                              execl(token, arg, NULL);
                                    continue;               
                        }
                            else
                            {      
                                    wait(NULL);           
                            }  
                }
   }
   }
 return 0;
}   
person dratenik    schedule 22.01.2021