Advertisement
aiguyGaryMiller

C Function Reading Cursor Position

Jan 25th, 2020
526
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.45 KB | None | 0 0
  1. void get_cursor_position(int *col, int *rows)
  2. {
  3.     int a = 0;
  4.     int i = 0;
  5.     char buf[4]
  6.  
  7.     write(1, "\033[6n", 4); // string asking for the cursor position
  8.     read(1, buf, 4);
  9.  
  10.     while (buf[i])
  11.     {
  12.         if (buf[i] >= 48 && buf[i] <= 57)
  13.         {
  14.             if (a == 0)
  15.                 *rows = atoi(&buf[i]) - 1;
  16.             else
  17.                 *col = atoi(&(buf[i]) - 1;
  18.             a++;
  19.         }
  20.         i++;
  21.     }
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement