• [^] # Re: execle(...);

    Posté par . En réponse au message Lancer un programme, comment ?. Évalué à 1.

    j'ai pas réussi avec ton truc, mais j'ai réussi à faire quelque chose qui marche à peu près
    int my_system (std::string commande, std::string separator) {
    std::vector<std::string> liste;
    std::string cmd;
    char **tableau;
    int pid, position=0, last_find = 0,i, retour = -1;
    cmd = commande + separator;
    for (position = 0 ; position < cmd.size() ; position ++) {
    if (cmd.substr(position, separator.size()) == separator) {
    liste.push_back(cmd.substr(last_find, position-last_find));
    last_find = position + separator.size();
    }
    }
    tableau = (char **)malloc((liste.size()+1)*sizeof(char *));
    for (i=0 ; i < liste.size() ; i++) {
    std::cout << liste[i] << std::endl;
    tableau[i] = (char *)liste[i].c_str();
    }
    tableau[i+1] = NULL;
    if ((pid=fork()) > 0) {
    execvp(liste[0].c_str(), tableau);
    } else if (pid==0) {
    wait4 (pid, &retour, 0, NULL);
    if (tableau) free(tableau);
    return retour;
    } else if (pid<0) {
    std::cerr << "Le fork n'a pas marché !" << std::endl;
    }
    if (tableau) free(tableau);
    return -1;
    }

    Ce code est crade, je sais
    La commande ifconfig eth0 se lance par :
    my_system("ifconfig||eth0","||");
    Si quelqu'un arrive à résoudre le besoin du séparateur.
    C'est du au fait que si la commande contient un argument comme : nom fichier.tar.gz, en un argument, ça marchera pas avec " " comme séparateur.