/** * * @File : exo_06.cxx * * @Authors : A. B. Dragut * * @Date : 2011 * * @Synopsis : emulation de la fonction systeme * * **/ #include #include #include #include // atoi() #include // strcpy(), strtok(), strlen() #include // system() #include #include "CExc.h" #include "nsSysteme.h" // Fork(), Signal() using namespace std; using namespace nsSysteme; // Fork(), Signal() namespace{ int CstMaxArg = 10; void System (const char * const LigneCommande){ if (::pid_t PidFils = Fork ()) //pere Waitpid (PidFils); else //fils { char ChaineADecomposer [strlen (LigneCommande) + 1]; strcpy (ChaineADecomposer,LigneCommande); char * NewArgv [CstMaxArg + 1]; // pour mettre le pointeur nul int NewArgc = 0; char Separateur[]=" "; NewArgv [NewArgc] = strtok (ChaineADecomposer, Separateur); while( NewArgc < CstMaxArg ) NewArgv [++NewArgc] = strtok (0,Separateur); // strtok renvoie un pointeur sur le lexeme suivant // Si le dernier pointeur est non nul ==> plus de // CstMaxArg arguments dans la commande if (NewArgv [NewArgc]) { std::ostringstream Str; Str << "Commande trop longue : " << LigneCommande << "; plus de " << CstMaxArg << " arguments"; throw CExc("System()",Str.str()); } ::execvp (NewArgv [0], NewArgv); throw CExc("execv ()",NewArgv [0]); } } // System() } // namespace anonyme int main(int argc, char * argv []) { try { if (2!= argc) throw CExc("main()",string ("Usage : ") + argv [0] + " "); System(argv [1]); return 0; } catch (const CExc & Exc) { cerr <