-1

I am coding a C program with termcaps, and I need to return a value by executing like :

more `./program arg1 arg2 arg3 arg4`

And my function get_winsize have a condition if my screen is too small.

int     *get_winsize()
{
    struct winsize  w;
    int             *ret;

    ret = malloc(sizeof(int) * 2);
    ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
    ret[0] = w.ws_row;
    ret[1] = w.ws_col;
    if (ret[0] <= 5 || ret[1] <= 5)
    {
        printf("screen too small : size %d x %d\n", ret[0], ret[1]);
        exit(0);
    }
    return (ret);
} 

When i start my program without the back-quote, I don't have problem. but with the back-quote i have :

size: No such file or directory
screen: No such file or directory
0: No such file or directory
x: No such file or directory
0: No such file or directory

Any idea why ? Maybe back-quoting duplicate a environment without a screen ?!

Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67
albttx
  • 3,444
  • 4
  • 23
  • 42
  • What happens when you try and print the value of w.ws_row and w.ws_col? If they aren't what you'd expect then the problem might lie in the ioctl() function or how you're using it in this case – jess Jan 18 '16 at 17:14
  • i edited, it print my word line by line. – albttx Jan 18 '16 at 17:18

2 Answers2

1

First: you do not check return value of your ioctl, which means when it fails results in w structure are undefined.
Second: when using backticks you disconnect your program from the terminal, hence your ioctl will fail.
Third: redesign your program to write your results using standard error, not standard output. Then run it like this:

tmpfile=`mktemp`
./program arg1 arg2 arg3 arg4 2> $tmpfile
more `cat $tmpfile`
rm $tmpfile
nsilent22
  • 2,763
  • 10
  • 14
0

I found the solution !

First i can get the size with the termcaps function like

tgetnum("co"); //column
tgetnum("li"); //rows

Then, for handle the backcote, i'm printing everything on /dev/tty !

Problem Solve !

albttx
  • 3,444
  • 4
  • 23
  • 42