Skip to content

Commit ee9ca77

Browse files
committed
added c code execution
1 parent 939410f commit ee9ca77

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

server/api/services/code.service.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ const ROOT_DIR = `${process.cwd()}`;
77
const SOURCE_DIR = path.join(ROOT_DIR, 'executor');
88
const TARGET_DIR = `/app/codes`;
99
const IMAGE_NAME = 'executor:1.0';
10-
//const VOL_NAME = `my_vol`;
11-
const VOL_NAME = SOURCE_DIR;
10+
const VOL_NAME = `my_vol`;
11+
//const VOL_NAME = SOURCE_DIR;
1212

1313
class CodeService {
1414
async execute(code, input, lang, id) {
@@ -77,6 +77,10 @@ class CodeService {
7777
fileName += '.java';
7878
break;
7979
}
80+
case 'c': {
81+
fileName += '.c';
82+
break;
83+
}
8084
default: {
8185
throw { message: 'Invalid language' };
8286
}
@@ -114,6 +118,10 @@ class CodeService {
114118
command = `cd ${TARGET_DIR} && javac ${file} && java Input < ${input}`;
115119
break;
116120
}
121+
case 'c': {
122+
command = `cd ${TARGET_DIR} && gcc -o ${id} ${file} && ./${id} < ${input}`;
123+
break;
124+
}
117125
default: {
118126
throw { message: 'Invalid language' };
119127
}
@@ -157,9 +165,9 @@ class CodeService {
157165
if (err) throw { message: err };
158166
});
159167
}
160-
if (lang == 'c++') {
168+
if (lang == 'c++' || lang == 'c') {
161169
fs.unlinkSync(path.join(SOURCE_DIR, id), err => {
162-
if (err) throw { message: err };
170+
if (err) throw err;
163171
});
164172
}
165173
if (lang == 'java') {

server/api/services/validation.service.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ class ValidationService {
4545
isValid: true
4646
};
4747
}
48+
case 'c': {
49+
return {
50+
isValid: true
51+
};
52+
}
4853
default: {
4954
return {
5055
isValid: false,

0 commit comments

Comments
 (0)