2

I am trying to compile and run an C file stored locally in my computer using php i used the following piece of code

$dir = '/home/User/Desktop';
 if ( !file_exists($dir) ) {
  mkdir ($dir, 0744);
 }

 file_put_contents ($dir.'/test.c', $code);

$path = "/home/User/Desktop/test.c";
$Command = "gcc $path 2&>1";
exec($Command,$return_val,$error);

i have set all the permissions to the file using chmod and chgrp but on executing it just echos "Succesful" on my screen but not my $output value This is the sample C program i typed in my

#include <stdio.h>
int main()
{
printf("Hello world");

return 0;

}

but the following program runs fine when i have executed it using GCC i am currently using Ubuntu 14.04.2 LTS

i tried using

$Command = "gcc $path 2&>1"; $output = exec($Command); $output = exec(./a.out);

but still i cant get desired output

I tried to use

$output = exec($Command,$return_val,$error);

now when i echo $error it gives me "2"

I tried using system

$last_line = system('gcc $path', $retval);
echo '
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;

i get the following output

Last line of the output: 
Return value: 4

sh: 1: cannot create 1: Permission denied
gcc: error: 2: No such file or directory

but i given permission to the file using

 sudo chmod g+w /home/User/Desktop/test.c

can anyone help me out with this issue?

13
  • 2
    C files are not executable. GCC compiles, links, then runs the executable that it creates from the C file. Why do you need to run an external executable? Commented Jun 26, 2015 at 17:39
  • You have to specify the created executable file in your $path variable, not the source code. The issue that your script outputs "Successful" is simply a result of the fact that you do not do any error checking at all. Without, how should your script know that something is wrong? Commented Jun 26, 2015 at 17:40
  • $Command = "gcc $path 2&>1"; $output = exec($Command); $output = exec(./a.out); thanks @foley but i tried even using GCC but still i cant get the $output value Commented Jun 26, 2015 at 17:41
  • Sorry, but I still can see no error handling there. Commented Jun 26, 2015 at 17:45
  • 1
    Even if we ignore that strange effect: I can't see where some shell command should try to "create" something. In the code above you create a directory, but from within php, not some shell. Something is missing here. Anyway: one issue might be that you do not specify an output file in your gcc call. So the compiler will try to create a file in its working directory. But it is unclear which that is. Either specify an output path in the gcc call (see its optional argument list) or switch the directory to /home/User/Desktop prior to running gcc. Commented Jun 27, 2015 at 8:31

1 Answer 1

1

It's not 2&>1, it's 2>&1.

sh: 1: cannot create 1: Permission denied
gcc: error: 2: No such file or directory

The first error you see here is the shell complaining about not being able to create a file named "1" to redirect stdout.
The second error is gcc complaining about the lack of an input file called "2".

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot @aib that worked sorry for that silly mistake

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.