Advertisement
paulogp

Linha de comando

Jul 13th, 2011
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.90 KB | None | 0 0
  1. /* Este exemplo usa o comando popen que permite correr comandos de terminal. Neste caso, lista a directoria (ls -l) */
  2. // Apple Xcode
  3.  
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. #define PATH_MAX 500
  9.  
  10. int main (int argc, const char * argv[])
  11. {
  12.     // linha de comandos
  13.     FILE *the_file;
  14.     int the_status;
  15.     char the_path[PATH_MAX];
  16.  
  17.     // initiate pipe streams to or from a process
  18.     the_file = popen("ls -l", "r");
  19.  
  20.     if (the_file == NULL)
  21.     {
  22.         printf("erro no comando");
  23.     }
  24.  
  25.     // output
  26.     while (fgets(the_path, PATH_MAX, the_file) != NULL)
  27.     {
  28.         printf("%s", the_path);
  29.     }
  30.  
  31.     // status
  32.     the_status = pclose(the_file);
  33.     if (the_status == -1)
  34.     {
  35.         // error reported by pclose()
  36.         printf("close");
  37.     }
  38.     else
  39.     {
  40.         /* Use macros described under wait() to inspect `status' in order
  41.         to determine success/failure of command executed by popen() */
  42.         printf("%i\n", the_status);
  43.     }
  44.  
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement