| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chromecast/base/process_utils.h" | 5 #include "chromecast/base/process_utils.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <stdio.h> | 8 #include <stdio.h> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/safe_strerror_posix.h" | 11 #include "base/posix/safe_strerror.h" |
| 12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 13 | 13 |
| 14 namespace chromecast { | 14 namespace chromecast { |
| 15 | 15 |
| 16 bool GetAppOutput(const std::vector<std::string>& argv, std::string* output) { | 16 bool GetAppOutput(const std::vector<std::string>& argv, std::string* output) { |
| 17 DCHECK(output); | 17 DCHECK(output); |
| 18 | 18 |
| 19 // Join the args into one string, creating the command. | 19 // Join the args into one string, creating the command. |
| 20 std::string command = JoinString(argv, ' '); | 20 std::string command = JoinString(argv, ' '); |
| 21 | 21 |
| 22 // Open the process. | 22 // Open the process. |
| 23 FILE* fp = popen(command.c_str(), "r"); | 23 FILE* fp = popen(command.c_str(), "r"); |
| 24 if (!fp) { | 24 if (!fp) { |
| 25 LOG(ERROR) << "popen (" << command << ") failed: " << safe_strerror(errno); | 25 LOG(ERROR) << "popen (" << command << ") failed: " |
| 26 << base::safe_strerror(errno); |
| 26 return false; | 27 return false; |
| 27 } | 28 } |
| 28 | 29 |
| 29 // Fill |output| with the stdout from the process. | 30 // Fill |output| with the stdout from the process. |
| 30 output->clear(); | 31 output->clear(); |
| 31 while (!feof(fp)) { | 32 while (!feof(fp)) { |
| 32 char buffer[256]; | 33 char buffer[256]; |
| 33 size_t bytes_read = fread(buffer, 1, sizeof(buffer), fp); | 34 size_t bytes_read = fread(buffer, 1, sizeof(buffer), fp); |
| 34 if (bytes_read <= 0) | 35 if (bytes_read <= 0) |
| 35 break; | 36 break; |
| 36 output->append(buffer, bytes_read); | 37 output->append(buffer, bytes_read); |
| 37 } | 38 } |
| 38 | 39 |
| 39 // pclose() function waits for the associated process to terminate and returns | 40 // pclose() function waits for the associated process to terminate and returns |
| 40 // the exit status. | 41 // the exit status. |
| 41 return (pclose(fp) == 0); | 42 return (pclose(fp) == 0); |
| 42 } | 43 } |
| 43 | 44 |
| 44 } // namespace chromecast | 45 } // namespace chromecast |
| OLD | NEW |