Llamada al sistema Execlp()

Llamada al sistema Execlp()

He creado mi propio shell en Linux. Toma la ruta del archivo y el comando. Opera comandos simples como grep ls, etc. Utiliza la llamada al sistema execlp().

por ejemplo, /bin/ls ls < este comando se ejecuta correctamente.

ls <¡este comando no se ejecuta!

Sólo quiero saber qué estoy haciendo mal aquí.

#include <iostream>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <cstring>
#include <sys/types.h>
using namespace std;

int main (int argc, char * argv[])
{
    while (true){
    char * input;
    string insert;
    char * token;
    char * parsed[5];
    int count=0;

    cout<<"My Shell $";
    getline(cin,insert);
    input= new char [insert.size()+1];
    strcpy(input, insert.c_str());

    for (int i=0; i<5; i++)
        parsed[i]=NULL;

    token=strtok(input, " ");
    while (token!=NULL)
    {
        parsed[count] = new char[strlen(token) + 1];
        strcpy(parsed[count++],token);
        token=strtok(NULL, " ");
    }
    delete input;
    delete token;


    pid_t mypid=fork();


    if (mypid==0)
    {

            execlp (parsed[0],parsed[1],parsed[2],parsed[3],parsed[4], (char*) NULL); 

    }
    else if (mypid>0)
    {
        wait(NULL);
    }

    } //end of while
}

Respuesta1

El problema es que te falta un parámetro execlp; debe especificar el comando a ejecutar y luego todos sus argumentos, incluido el primer argumento, que es el nombre del comando:

execlp(parsed[0], parsed[0], parsed[1], parsed[2], parsed[3], parsed[4], (char*) NULL);

El primero parsed[0]se toma como execlpparámetro pathy el segundo es el primero arg, que termina argv[0]dentro del comando ejecutado.

información relacionada