process_common.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * SPDX-License-Identifier: Apache-2.0.
  4. */
  5. #include <aws/common/process.h>
  6. #include <aws/common/string.h>
  7. #include <stdio.h>
  8. #include <sys/types.h>
  9. #define MAX_BUFFER_SIZE (2048)
  10. int aws_run_command_result_init(struct aws_allocator *allocator, struct aws_run_command_result *result) {
  11. if (!allocator || !result) {
  12. return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
  13. }
  14. AWS_ZERO_STRUCT(*result);
  15. return AWS_OP_SUCCESS;
  16. }
  17. void aws_run_command_result_cleanup(struct aws_run_command_result *result) {
  18. if (!result) {
  19. return;
  20. }
  21. aws_string_destroy_secure(result->std_out);
  22. aws_string_destroy_secure(result->std_err);
  23. }
  24. #if defined(AWS_OS_WINDOWS) && !defined(AWS_OS_WINDOWS_DESKTOP)
  25. int aws_run_command(
  26. struct aws_allocator *allocator,
  27. struct aws_run_command_options *options,
  28. struct aws_run_command_result *result) {
  29. (void)allocator;
  30. (void)options;
  31. (void)result;
  32. return aws_raise_error(AWS_ERROR_UNSUPPORTED_OPERATION);
  33. }
  34. #else
  35. int aws_run_command(
  36. struct aws_allocator *allocator,
  37. struct aws_run_command_options *options,
  38. struct aws_run_command_result *result) {
  39. AWS_FATAL_ASSERT(allocator);
  40. AWS_FATAL_ASSERT(options);
  41. AWS_FATAL_ASSERT(result);
  42. FILE *output_stream;
  43. char output_buffer[MAX_BUFFER_SIZE];
  44. struct aws_byte_buf result_buffer;
  45. int ret = AWS_OP_ERR;
  46. if (aws_byte_buf_init(&result_buffer, allocator, MAX_BUFFER_SIZE)) {
  47. goto on_finish;
  48. }
  49. # if defined(AWS_OS_WINDOWS)
  50. output_stream = _popen(options->command, "r");
  51. # else
  52. output_stream = popen(options->command, "r");
  53. # endif
  54. if (output_stream) {
  55. while (!feof(output_stream)) {
  56. if (fgets(output_buffer, MAX_BUFFER_SIZE, output_stream) != NULL) {
  57. struct aws_byte_cursor cursor = aws_byte_cursor_from_c_str(output_buffer);
  58. if (aws_byte_buf_append_dynamic(&result_buffer, &cursor)) {
  59. goto on_finish;
  60. }
  61. }
  62. }
  63. # if defined(AWS_OS_WINDOWS)
  64. result->ret_code = _pclose(output_stream);
  65. # else
  66. result->ret_code = pclose(output_stream);
  67. # endif
  68. }
  69. struct aws_byte_cursor trim_cursor = aws_byte_cursor_from_buf(&result_buffer);
  70. struct aws_byte_cursor trimmed_cursor = aws_byte_cursor_trim_pred(&trim_cursor, aws_char_is_space);
  71. if (trimmed_cursor.len) {
  72. result->std_out = aws_string_new_from_array(allocator, trimmed_cursor.ptr, trimmed_cursor.len);
  73. if (!result->std_out) {
  74. goto on_finish;
  75. }
  76. }
  77. ret = AWS_OP_SUCCESS;
  78. on_finish:
  79. aws_byte_buf_clean_up_secure(&result_buffer);
  80. return ret;
  81. }
  82. #endif /* !AWS_OS_WINDOWS */