I am writing a pspg pager. There I have to solve the following problem. After reading from stdin I need to reassign stdin from the previous read from the pipe to read from the terminal.
I used
freopen("/dev/tty", "r", stdin)
But this does not work when the pager was used from a command that was not executed directly
su - someuser -c 'export PAGER=pspg psql somedb'
In this case, I received an error message: There is no such device or address .
I found a workaround - now the code looks like this:
if (freopen("/dev/tty", "r", stdin) == NULL) { if (freopen(ttyname(fileno(stdout)), "r", stdin) == NULL) { fprintf(stderr, "cannot to reopen stdin: %s\n", strerror(errno)); exit(1); } }
What is the correct way to detect the assigned terminal device in this case?
But this workaround is wrong. He fixed one question, but the next one goes. When someuser is different from the current user, reopening fails with a Permission denied error. Therefore, this workaround cannot be used for my purposes.
source share