0

my program is meant to fork a process, read from a file line by line within the parent and shove these lines down a pipe to be passed to bc which is called using execve.

The file I am using is a text file that simply contains 5 + 10.

Parent process reads 'em in like this:

while(fgets(newWord, sizeof newWord, coolFile) != NULL)
{
    write(stdin_pipe_fds[1], newWord, (strlen(newWord)+1));
}

Child calls bc like this:

execve("/usr/bin/bc", argv, NULL);

Running the program gives me this error message:

 scg3q@system64:~/CS/project4$ (standard_in) 1: syntax error
 (standard_in) 1: illegal character: ^@
 (standard_in) 1: syntax error

This happens when I try to execve bc. Any ideas?

EDIT: making sure the null terminator is not passed to bc via the pipe fixes one error message(Illegal character), but the other two remain. Hoping someone has an idea of the problem could be!

jww
  • 97,681
  • 90
  • 411
  • 885

2 Answers2

2

Don't add 1 to strlen(newWord). That's causing you to write the null terminator to the pipe, but bc doesn't expect a null byte in its input.

write(stdin_pipe_fds[1], newWord, (strlen(newWord)));

To fix the other syntax error, make sure that newWord ends with a newline character, because bc requires its input to be complete lines.

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

Running the program gives me this error message:

scg3q@system64:~/CS/project4$ (standard_in) 1: syntax error
(standard_in) 1: illegal character: ^@
(standard_in) 1: syntax error

Those messages are from bc. They are complaining that the data you are feeding to its standard input are syntactically incorrect on account of an illegal character, ^@, which is the null terminator that you are erroneously careful to send down the stream. That's not part of the input you read, and it should not be sent to bc.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • Yes,thank you, making sure the null terminator is not sent down the pipe fixes the illegal character message, but the other two remain. Any ideas? – OldIslander Jun 27 '19 at 18:04
  • @OldIslander, nothing else presented in the question explains any errors, but it is suspicious that you are passing the program's argument vector (`argv`) on to `bc` as its own. Alternatively, the issue might be something in the input file, so try feeding that directly to `bc`, via a command along the lines of `bc – John Bollinger Jun 27 '19 at 18:12