0

For an exercise about termcaps, I have to write a program that will display a list of items and let the user to select some of them. The program will then output the selected items separated with a space. The program can be called like this:

$> ./my_program foo1 foo2 foo3

or like this:

$> ls `my_program foo1 foo2 foo3`

My program works well in the first case, but in the second case, the stdout of my_program is intercepted by the backticks and will not show up on the terminal. I know that the program is running well since I got the expected result from ls if I select some items blindly.

In my program, I can't use any built-in functions excepted the following: • ioctl • tcsetattr • tcgetattr • tgetent • tgoto • tputs • tgetstr • tparm • open • close • write • malloc • free • read • exit • signal

How can I display my_program's stdout to the user when called like in the second case ?

EDIT: The only option I found is to output everything to stderr and output the selected items to stdout...

Quentin Hayot
  • 7,786
  • 6
  • 45
  • 62
  • 1
    A program that uses the terminal display is not designed for use in back-quotes (or, better, `$(...)` notation). At the least, it probably varies its actions based on whether its standard output is a terminal or not. Since the exercise is about using terminal display, you may have to get inventive. Perhaps you should arrange to use `/dev/tty` for the I/O operations with the user, leaving stdout strictly for the non-interactive stuff. But the requisite design is...sub-optimal, non-standard, nasty. Remind me not to have to use the program. (Oh, on second thoughts, you won't have to remind me...) – Jonathan Leffler Nov 22 '12 at 05:41
  • Writing to /dev/tty works great ! Thanks ! It's just for learning purposes, this code will not be used outside this scope ;) – Quentin Hayot Nov 22 '12 at 06:02

1 Answers1

3

You can open and write to /dev/tty in my_program and it will be only to the terminal not to the redirected stdout.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134