-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
88 lines (66 loc) · 2.75 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#if USE_SYSTEM2_SOURCE
#define SYSTEM2_DECLARATION_ONLY 1
#endif
#include "System2.h"
#include <stdio.h>
#define EXIT_IF_FAILED(result) \
if(result != SYSTEM2_RESULT_SUCCESS) \
{\
printf("Error at %d: %d", __LINE__, result);\
return -1;\
}
int main(int argc, char** argv)
{
{
System2CommandInfo commandInfo;
memset(&commandInfo, 0, sizeof(System2CommandInfo));
commandInfo.RedirectInput = true;
commandInfo.RedirectOutput = true;
SYSTEM2_RESULT result;
#if defined(__unix__) || defined(__APPLE__)
result = System2Run("read testVar && echo testVar is \\\"$testVar\\\"", &commandInfo);
#endif
#if defined(_WIN32)
result = System2Run("set /p testVar= && echo testVar is \"!testVar!\"", &commandInfo);
#endif
EXIT_IF_FAILED(result);
char input[] = "test content\n";
result = System2WriteToInput(&commandInfo, input, sizeof(input));
EXIT_IF_FAILED(result);
//Waiting here simulates the child process has "finished" and we read the output of it
//Sleep(2000);
char outputBuffer[1024];
uint32_t bytesRead = 0;
//System2ReadFromOutput can also return SYSTEM2_RESULT_READ_NOT_FINISHED if we have more to read
//In which case can use a do while loop to keep getting the output
result = System2ReadFromOutput(&commandInfo, outputBuffer, 1023, &bytesRead);
EXIT_IF_FAILED(result);
outputBuffer[bytesRead] = 0;
int returnCode = -1;
result = System2GetCommandReturnValueSync(&commandInfo, &returnCode);
EXIT_IF_FAILED(result);
printf("%s", outputBuffer);
printf("%s: %d\n", "Command has executed with return value", returnCode);
}
//Output: Command has executed with return value: : 0
//Output: testVar is "test content"
printf("\nUsing stdin now, enter the value of testVar: ");
fflush(stdout);
{
System2CommandInfo commandInfo;
memset(&commandInfo, 0, sizeof(System2CommandInfo));
SYSTEM2_RESULT result;
#if defined(__unix__) || defined(__APPLE__)
result = System2Run("read testVar && echo testVar is \\\"$testVar\\\"", &commandInfo);
#endif
#if defined(_WIN32)
result = System2Run("set /p testVar= && echo testVar is \"!testVar!\"", &commandInfo);
#endif
EXIT_IF_FAILED(result);
int returnCode = -1;
result = System2GetCommandReturnValueSync(&commandInfo, &returnCode);
EXIT_IF_FAILED(result);
printf("%s: %d\n", "Command has executed with return value", returnCode);
}
return 0;
}