3

Full disclosure, this is an assignment for my intro to computer security class.

We are creating a guessing game (see questioner.c below) that asks the user for their name, and then a guess of the magic number. Upon telling the user whether their guess was too high, too low, or correct, the program exits, simple as that. The second part to the assignment is creating a "guesser" program (see guesser.c below) that plays our guessing game, and this is where my problems lie. The output for questioner is fed into the input for guesser, and the output for guesser is fed into the input for questioner.

I've tried "./questioner | ./guesser" on the terminal but the programs don't seem to be aware of each other and aren't using the stdin or stdout together like I was hoping. I feel like I'm missing something frustratingly basic but I'm at a loss and would appreciate any help than can be given.

The questioner.c file:

int main(){

  int magic = 2936;
  char name[30];
  char temp[30];
  int answer;

  fputs("What is your name?\n", stdout);
  fgets(name, 30, stdin);

  fputs("What is the magic number, test?\n", stdout);
  fgets(temp, 10, stdin);

  answer = strtol(temp, NULL, 0);

  if(answer < magic){
    fputs("TOO LOW\n", stdout);
    return -1;
  }
  if(answer > magic){
    fputs("TOO HIGH\n", stdout);
    return -1;
  }
  if(answer == magic){
    fputs("SUCCESS\n", stdout);
    return 0;
  }
}

The guesser.c file:

int main(){
  char input[50];

  fgets(input, 50, stdin);

  if(strcmp(input, "What is your name?") == 0){
    fputs("AndyG\0\n", stdout);
  }
  else
    fputs("???\0\n", stdout);

  fgets(input, 50, stdin);

  if(strcmp(input, "What is the magic number, AndyG?")){
    fputs("2936\0\n", stdout);
  }
  else
    fputs("???\0\n", stdout);

  return 0;
}
Black Dahlia1147
  • 243
  • 1
  • 3
  • 9
  • `\0` *terminates* the string, so `AndyG` is not going to be followed by a newline. That, and the fact that the pipes are unidirectional, but you need bidirectional communication... – Antti Haapala -- Слава Україні Jan 14 '18 at 21:02
  • You'd need to look into expect and such – Antti Haapala -- Слава Україні Jan 14 '18 at 21:02
  • expect and such? also, @AnttiHaapala how would I begin to implement bidirectional communication? – Black Dahlia1147 Jan 14 '18 at 21:05
  • Forgive me, but while that seems like the right track, I don't think we are supposed to install any programs to make this happen, it should be doable from linux as is, yes? – Black Dahlia1147 Jan 14 '18 at 21:11
  • I'm not clear exactly what you're up to, but creating two pipes and juggling the plumbing allows the two processes to communicate bidirectionally, with one pipe going from questioner to guesser and one from guesser to questioner. That much is easy. I'm not clear whether the human user is going to be typing any information, and if so, which program will be reading it; nor is it clear whether any information will be displayed on the terminal, and if so, which program will be doing it. I can envisage setups where each of the programs could be doing either or both. _[…continued…]_ – Jonathan Leffler Jan 15 '18 at 09:07
  • _[…continuation…]_ The name plus one guess plus exit is odd, too; a very unpleasant interface for a human. You'd normally expect both programs to hang around semi-indefinitely. If they won't, then the process that the user kicks off at the terminal will hang around, repeatedly launching the other. The user won't be typing connectivity at the shell; that isn't a user's job. It isn't clear why the questioner expects the name other than to annoy the user. With a bit more clarity on what you want doing, this can be done with two pipes (and without expect). But more clarity is needed. – Jonathan Leffler Jan 15 '18 at 09:11

1 Answers1

1

Bidirectional piping in Linux is a bit tricky. The easiest way may be to use a FIFO, which is a pipe that has a filename. You can still use the | pipe for one direction:

mkfifo my_fifo
./questioner < my_fifo | ./guesser > my_fifo
user253751
  • 57,427
  • 7
  • 48
  • 90