22#include <stdio.h>
33#include <stdlib.h>
44#include <string.h>
5+
6+ // Additional for windows support
7+ #ifdef _WIN32
8+ #include <windows.h>
9+ #define popen _popen
10+ #define pclose _pclose
11+ #else
512#include <sys/wait.h>
13+ #endif
614
715// Pointers for returning errors
816#define FILE_ERROR ((char*)1)
@@ -29,7 +37,11 @@ char* runJudge(const char* usersCode, const char* programmingLanguage, const cha
2937 }
3038
3139 // Call the compiler for the correct language and check for errors
40+ #ifdef _WIN32
41+ int compilation = callCompiler (fileName , "gcc %s -o UserProgramm.exe 2> NUL" );
42+ #else
3243 int compilation = callCompiler (fileName , "gcc %s -o UserProgramm 2> /dev/null" );
44+ #endif
3345 if (compilation == SYSTEM_ERROR ) {
3446 free (fileName );
3547 return "ERROR: Calling system() didn't work (Code 3)" ;
@@ -42,7 +54,11 @@ char* runJudge(const char* usersCode, const char* programmingLanguage, const cha
4254 }
4355
4456 // Run and correct the programm
45- result = runProgramAndCalculateTheScore (solution , "./UserProgramm" );
57+ #ifdef _WIN32
58+ result = runProgramAndCalculateTheScore ((char * )solution , "UserProgramm.exe" );
59+ #else
60+ result = runProgramAndCalculateTheScore ((char * )solution , "./UserProgramm" );
61+ #endif
4662 } else if (strcmp (programmingLanguage , ".go" ) == 0 ) {
4763 // Write the users code into a file with the correct ending of the language he used
4864 fileName = writeFile (usersCode , programmingLanguage );
@@ -65,7 +81,11 @@ char* runJudge(const char* usersCode, const char* programmingLanguage, const cha
6581 }
6682
6783 // Run and correct the programm
68- result = runProgramAndCalculateTheScore (solution , "./UserProgramm" );
84+ #ifdef _WIN32
85+ result = runProgramAndCalculateTheScore ((char * )solution , "UserProgramm.exe" );
86+ #else
87+ result = runProgramAndCalculateTheScore ((char * )solution , "./UserProgramm" );
88+ #endif
6989 } else if (strcmp (programmingLanguage , ".py" ) == 0 ) {
7090 // Write the users code into a file with the correct ending of the language he used
7191 fileName = writeFile (usersCode , programmingLanguage );
@@ -75,7 +95,11 @@ char* runJudge(const char* usersCode, const char* programmingLanguage, const cha
7595 }
7696
7797 // Check at least whether the syntax is fine or not
98+ #ifdef _WIN32
99+ int interpretation = callCompiler (fileName , "python -m py_compile %s 2> NUL" );
100+ #else
78101 int interpretation = callCompiler (fileName , "python3 -m py_compile %s 2> /dev/null" );
102+ #endif
79103 if (interpretation == SYSTEM_ERROR ) {
80104 free (fileName );
81105 return "ERROR: Calling system() didn't work (Code 3)" ;
@@ -88,7 +112,11 @@ char* runJudge(const char* usersCode, const char* programmingLanguage, const cha
88112 }
89113
90114 // Run and correct the programm
91- result = runProgramAndCalculateTheScore (solution , "python3 UserCode.py" );
115+ #ifdef _WIN32
116+ result = runProgramAndCalculateTheScore ((char * )solution , "python UserCode.py" );
117+ #else
118+ result = runProgramAndCalculateTheScore ((char * )solution , "python3 UserCode.py" );
119+ #endif
92120 } else if (strcmp (programmingLanguage , ".cpp" ) == 0 ) {
93121 // Write the users code into a file with the correct ending of the language he used
94122 fileName = writeFile (usersCode , programmingLanguage );
@@ -98,7 +126,11 @@ char* runJudge(const char* usersCode, const char* programmingLanguage, const cha
98126 }
99127
100128 // Call the compiler for the correct language and check for errors
129+ #ifdef _WIN32
130+ int compilation = callCompiler (fileName , "g++ %s -o UserProgramm.exe 2> NUL" );
131+ #else
101132 int compilation = callCompiler (fileName , "g++ %s -o UserProgramm 2> /dev/null" );
133+ #endif
102134 if (compilation == SYSTEM_ERROR ) {
103135 free (fileName );
104136 return "ERROR: Calling system() didn't work (Code 3)" ;
@@ -111,7 +143,11 @@ char* runJudge(const char* usersCode, const char* programmingLanguage, const cha
111143 }
112144
113145 // Run and correct the programm
114- result = runProgramAndCalculateTheScore (solution , "./UserProgramm" );
146+ #ifdef _WIN32
147+ result = runProgramAndCalculateTheScore ((char * )solution , "UserProgramm.exe" );
148+ #else
149+ result = runProgramAndCalculateTheScore ((char * )solution , "./UserProgramm" );
150+ #endif
115151 } else if (strcmp (programmingLanguage , ".rs" ) == 0 ) {
116152 // Write the users code into a file with the correct ending of the language he used
117153 fileName = writeFile (usersCode , programmingLanguage );
@@ -121,7 +157,11 @@ char* runJudge(const char* usersCode, const char* programmingLanguage, const cha
121157 }
122158
123159 // Call the compiler for the correct language and check for errors
160+ #ifdef _WIN32
161+ int compilation = callCompiler (fileName , "rustc %s -o UserProgramm.exe" );
162+ #else
124163 int compilation = callCompiler (fileName , "rustc %s -o UserProgramm" );
164+ #endif
125165 if (compilation == SYSTEM_ERROR ) {
126166 free (fileName );
127167 return "ERROR: Calling system() didn't work (Code 3)" ;
@@ -134,7 +174,11 @@ char* runJudge(const char* usersCode, const char* programmingLanguage, const cha
134174 }
135175
136176 // Run and correct the programm
137- result = runProgramAndCalculateTheScore (solution , "./UserProgramm" );
177+ #ifdef _WIN32
178+ result = runProgramAndCalculateTheScore ((char * )solution , "UserProgramm.exe" );
179+ #else
180+ result = runProgramAndCalculateTheScore ((char * )solution , "./UserProgramm" );
181+ #endif
138182 } else if (strcmp (programmingLanguage , ".rb" ) == 0 ) {
139183 // Write the users code into a file with the correct ending of the language he used
140184 fileName = writeFile (usersCode , programmingLanguage );
@@ -157,7 +201,7 @@ char* runJudge(const char* usersCode, const char* programmingLanguage, const cha
157201 }
158202
159203 // Run and correct the programm
160- result = runProgramAndCalculateTheScore (solution , "ruby UserCode.rb" );
204+ result = runProgramAndCalculateTheScore (( char * ) solution , "ruby UserCode.rb" );
161205 } else if (strcmp (programmingLanguage , ".js" ) == 0 ) {
162206 // Write the users code into a file with the correct ending of the language he used
163207 fileName = writeFile (usersCode , programmingLanguage );
@@ -180,7 +224,7 @@ char* runJudge(const char* usersCode, const char* programmingLanguage, const cha
180224 }
181225
182226 // Run and correct the programm
183- result = runProgramAndCalculateTheScore (solution , "node UserCode.js" );
227+ result = runProgramAndCalculateTheScore (( char * ) solution , "node UserCode.js" );
184228 } else if (strcmp (programmingLanguage , ".php" ) == 0 ) {
185229 // Write the users code into a file with the correct ending of the language he used
186230 fileName = writeFile (usersCode , programmingLanguage );
@@ -203,7 +247,7 @@ char* runJudge(const char* usersCode, const char* programmingLanguage, const cha
203247 }
204248
205249 // Run and correct the programm
206- result = runProgramAndCalculateTheScore (solution , "php UserCode.php" );
250+ result = runProgramAndCalculateTheScore (( char * ) solution , "php UserCode.php" );
207251 } else {
208252 printf ("ERROR: Undefined language (Code 1)\n" );
209253 return "ERROR: Undefined language (Code 1)" ;
@@ -244,14 +288,22 @@ int callCompiler(char* fileName, char* instruction){
244288 snprintf (cmd , sizeof (cmd ), instruction , fileName );
245289
246290 int status = system (cmd );
247- int exitCode = WEXITSTATUS (status );
248291
249292 // Check whether the system() works
250293 if (status == -1 ) {
251294 printf ("ERROR: Calling system() didn't work (Code 3)\n" );
252295 return SYSTEM_ERROR ;
253296 }
254297
298+ #ifdef _WIN32
299+ // On Windows we do not distinguish missing compiler vs. other compilation errors reliably
300+ if (status != 0 ) {
301+ printf ("ERROR: Error while compiling the code (Code 2)\n" );
302+ return COMPILATION_ERROR ;
303+ }
304+ #else
305+ int exitCode = WEXITSTATUS (status );
306+
255307 // Check for errors and whether the compiler is installed or not
256308 if (exitCode == 127 ) {
257309 printf ("ERROR: No compiler found (Code -2)\n" );
@@ -260,6 +312,7 @@ int callCompiler(char* fileName, char* instruction){
260312 printf ("ERROR: Error while compiling the code (Code 2)\n" );
261313 return COMPILATION_ERROR ;
262314 }
315+ #endif
263316
264317 return 0 ;
265318}
@@ -293,13 +346,23 @@ char* runProgramAndCalculateTheScore(char* correctSolution, char* instruction) {
293346 int status = pclose (pipe );
294347
295348 // Check whether the programm crashed by checking the exit codes
349+ #ifdef _WIN32
350+ if (status != 0 ) {
351+ printf ("ERROR: The programm crashed (Code -3)\n" );
352+ crashed = TRUE;
353+ } else {
354+ // If everything is fine return the correct Output
355+ crashed = FALSE;
356+ }
357+ #else
296358 if (WIFSIGNALED (status )) {
297359 printf ("ERROR: The programm crashed (Code -3)\n" );
298360 crashed = TRUE;
299361 } else {
300362 // If everything is fine return the correct Output
301363 crashed = FALSE;
302364 }
365+ #endif
303366
304367 // Calculate the score
305368 trimNewline (result );
@@ -332,3 +395,5 @@ void trimNewline(char* s) {
332395 len -- ;
333396 }
334397}
398+
399+ // #ifdef _WIN32 ... else ... endif makes sure it runs on windows as well as on linux
0 commit comments