Advertisement
Guest User

Untitled

a guest
May 27th, 2015
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. /* executes a command and returns result */
  2. char* doCommand(char* cmd) {
  3. FILE* pipe = popen(cmd, "r");
  4. if (!pipe) {
  5. printf("Error opening pipe ");
  6. return "ERROR";
  7. }
  8.  
  9. //fflush(stdout);
  10. char buffer[128];
  11. char* result;
  12. int started = 0;
  13. char* hold;
  14. while(fgets(buffer, 128, pipe) != NULL) {
  15.  
  16. if (started != 0) {
  17. hold = mallocCat(result,buffer);
  18. free(result);
  19. result = hold;
  20. }else{
  21. result = malloc(sizeof(buffer));
  22. strcpy(result,buffer);
  23. started = 1;
  24. }
  25. }
  26.  
  27. if (pclose(pipe) == -256) {
  28.  
  29. }
  30. return result;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement