-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
158 lines (121 loc) · 4.46 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#if SYSTEM2_USE_SOURCE
#define SYSTEM2_DECLARATION_ONLY 1
#endif
//This bypasses inheriting memory from parent process on linux (glibc 2.24) but removes the ability to use RunDirectory.
//See https://github.com/Neko-Box-Coder/System2/issues/3
//#define SYSTEM2_POSIX_SPAWN 1
//#define SYSTEM2_DECLARATION_ONLY 1
//#define SYSTEM2_IMPLEMENTATION_ONLY 1
#include "System2.h"
#include <stdio.h>
#if SYSTEM2_TEST_MEMORY
#include <stdlib.h>
#endif
#define EXIT_IF_FAILED(result) \
if(result != SYSTEM2_RESULT_SUCCESS) \
{\
printf("Error at %d: %d", __LINE__, result);\
exit(-1);\
}
System2CommandInfo RedirectIOExample(void)
{
System2CommandInfo commandInfo;
memset(&commandInfo, 0, sizeof(System2CommandInfo));
commandInfo.RedirectInput = true;
commandInfo.RedirectOutput = true;
SYSTEM2_RESULT result;
#if defined(__unix__) || defined(__APPLE__)
result = System2Run("sleep 1; read testVar && echo testVar is \\\"$testVar\\\"", &commandInfo);
#endif
#if defined(_WIN32)
result = System2Run("ping localhost -n 2 > nul & 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);
return commandInfo;
}
void BlockedCommandExample(void)
{
System2CommandInfo commandInfo;
memset(&commandInfo, 0, sizeof(System2CommandInfo));
SYSTEM2_RESULT result;
#if defined(__unix__) || defined(__APPLE__)
result = System2Run("sleep 2; echo Hello", &commandInfo);
#endif
#if defined(_WIN32)
result = System2Run("ping localhost -n 3 > nul & echo Hello", &commandInfo);
#endif
EXIT_IF_FAILED(result);
int returnCode = -1;
result = System2GetCommandReturnValueSync(&commandInfo, &returnCode, false);
EXIT_IF_FAILED(result);
}
void StdinStdoutExample(void)
{
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, false);
EXIT_IF_FAILED(result);
printf("%s: %d\n", "Command has executed with return value", returnCode);
}
int main(int argc, char** argv)
{
(void)argc;
(void)argv;
#if SYSTEM2_TEST_MEMORY
void* testMem = malloc(50 * 1024 * 1024); //Test 50 MB
memset(testMem, 1, 50 * 1024 * 1024);
#endif
//Execute the first command
System2CommandInfo commandInfo = RedirectIOExample();
//We can execute other commands while the previous one is still running
//Output: Hello
BlockedCommandExample();
//The first command should be finish by now.
//Output: testVar is "test content"
//Output: 1st command has finished with return value: : 0
{
int returnCode = -1;
//True to perform manual cleanup
SYSTEM2_RESULT result = System2GetCommandReturnValueAsync(&commandInfo, &returnCode, true);
if(result == SYSTEM2_RESULT_SUCCESS)
{
char outputBuffer[1024];
do
{
uint32_t bytesRead = 0;
result = System2ReadFromOutput(&commandInfo, outputBuffer, 1023, &bytesRead);
outputBuffer[bytesRead] = 0;
printf("%s", outputBuffer);
}
while(result == SYSTEM2_RESULT_READ_NOT_FINISHED);
printf("%s: %d\n", "1st command has finished with return value", returnCode);
}
else if(result == SYSTEM2_RESULT_COMMAND_NOT_FINISHED)
printf("1st command not yet finished");
else
EXIT_IF_FAILED(result);
result = System2CleanupCommand(&commandInfo);
EXIT_IF_FAILED(result);
}
printf("\nUsing stdin now, enter the value of testVar: ");
fflush(stdout);
//If you don't do redirect, it will directly use stdin and stdout instead
StdinStdoutExample();
#if SYSTEM2_TEST_MEMORY
free(testMem);
#endif
return 0;
}