#include #include "tool.h" /* Execute une commande et recueille le r‚sultat dans bufres Si le r‚sultat contient un \n a la fin, il est elimin‚ Retourne -1 si la commande ˆtre ex‚cut‚ */ export int command_saveexec (const char *cmd, char *bufres, int len) { FILE *fin = popen (cmd,"r"); int ret = -1; if (fin != NULL){ ret = fread (bufres,1,len-1,fin); pclose (fin); if (ret > 0 && bufres[ret-1] == '\n') ret--; bufres[ret] = '\0'; } return (ret); } #ifdef TEST static void tst (const char *cmd) { char buf[1000]; int len = command_saveexec (cmd,buf,1000); printf ("Ex‚cute :%s:\n",cmd); if (len != -1){ printf ("len = %d -> :%s:\n",len,buf); }else{ printf ("erreur\n"); } } int main (void) { tst ("pwd"); tst ("echo *.c"); return (0); } #endif