I have this program which uses
ioctl(file_descriptor_of_stdout, TIOCGWINSZ, &w);
to get the terminal width (used in printing arrays). This works fine if I run my program directly on a terminal; however, I often pipe output through less, for paging and searching. For the sake of discussion assume stderr is also piped, so I can't "cheat" by expecting that to actually go to the terminal.
Now, less itself does take account of the terminal's width - for breaking lines or chopping them off. Does it not communicate this information further somehow to whatever is being piped through it?