From ee9ca77003302d23877af18bfa5be334943bb616 Mon Sep 17 00:00:00 2001 From: tomarviii88 Date: Mon, 4 Jan 2021 19:22:15 +0530 Subject: [PATCH] added c code execution --- server/api/services/code.service.js | 16 ++++++++++++---- server/api/services/validation.service.js | 5 +++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/server/api/services/code.service.js b/server/api/services/code.service.js index 3e12e1f..3bb9e29 100644 --- a/server/api/services/code.service.js +++ b/server/api/services/code.service.js @@ -7,8 +7,8 @@ const ROOT_DIR = `${process.cwd()}`; const SOURCE_DIR = path.join(ROOT_DIR, 'executor'); const TARGET_DIR = `/app/codes`; const IMAGE_NAME = 'executor:1.0'; -//const VOL_NAME = `my_vol`; -const VOL_NAME = SOURCE_DIR; +const VOL_NAME = `my_vol`; +//const VOL_NAME = SOURCE_DIR; class CodeService { async execute(code, input, lang, id) { @@ -77,6 +77,10 @@ class CodeService { fileName += '.java'; break; } + case 'c': { + fileName += '.c'; + break; + } default: { throw { message: 'Invalid language' }; } @@ -114,6 +118,10 @@ class CodeService { command = `cd ${TARGET_DIR} && javac ${file} && java Input < ${input}`; break; } + case 'c': { + command = `cd ${TARGET_DIR} && gcc -o ${id} ${file} && ./${id} < ${input}`; + break; + } default: { throw { message: 'Invalid language' }; } @@ -157,9 +165,9 @@ class CodeService { if (err) throw { message: err }; }); } - if (lang == 'c++') { + if (lang == 'c++' || lang == 'c') { fs.unlinkSync(path.join(SOURCE_DIR, id), err => { - if (err) throw { message: err }; + if (err) throw err; }); } if (lang == 'java') { diff --git a/server/api/services/validation.service.js b/server/api/services/validation.service.js index 96ab2cc..b6dc249 100644 --- a/server/api/services/validation.service.js +++ b/server/api/services/validation.service.js @@ -45,6 +45,11 @@ class ValidationService { isValid: true }; } + case 'c': { + return { + isValid: true + }; + } default: { return { isValid: false,