title: How to get terminal window width?
body: <p>I am working on embedded system using <code>C</code> and <code>linux</code>. User can connect to the device via SSH or a console serial cable. He can do this through PuTTY or Tera Term. My question is, after he is connected, how can I know his window's width? I have tried different approaches, they work if I simulate my system on a linux pc, but none of them work on the device:</p>
  
  <ol>
  <li><p><code>ioctl()</code></p>
  
  <pre><code>struct winsize ws;
  ioctl(..., TIOCGWINSZ, &amp;ws); 
  </code></pre>
  
  <p>This method works on pc, but it always returns 0 on the device.</p></li>
  <li><p><code>tgetnum()</code></p>
  
  <pre><code>setupterm((char *)0, 1, (int *)0);
  CGR_INT columns = tgetnum("co");
  </code></pre>
  
  <p>This method works on pc, but it always returns 80x24 on the device.</p></li>
  <li><p><code>getmaxyx()</code></p>
  
  <pre><code>CGR_INT xdim;
  CGR_INT ydim;
  initscr();
  refresh();
  getmaxyx(stdscr, ydim, xdim);
  </code></pre>
  
  <p>This method works on pc, but it always returns 0 on the device</p></li>
  </ol>