+
+#ifdef USE_SEPARATE_SCREEN
+ cur_screen = CreateConsoleScreenBuffer (GENERIC_READ | GENERIC_WRITE,
+ 0, NULL,
+ CONSOLE_TEXTMODE_BUFFER,
+ NULL);
+
+ if (cur_screen == INVALID_HANDLE_VALUE)
+ {
+ printf ("CreateConsoleScreenBuffer failed in ResetTerm\n");
+ printf ("LastError = 0x%lx\n", GetLastError ());
+ fflush (stdout);
+ exit (0);
+ }
+#else
+ cur_screen = prev_screen;
+ GetConsoleCursorInfo (prev_screen, &prev_console_cursor);
+#endif
+
+ /* Respect setting of LINES and COLUMNS environment variables. */
+ {
+ char * lines = getenv("LINES");
+ char * columns = getenv("COLUMNS");
+
+ if (lines != NULL && columns != NULL)
+ {
+ SMALL_RECT new_win_dims;
+ COORD new_size;
+
+ new_size.X = atoi (columns);
+ new_size.Y = atoi (lines);
+
+ GetConsoleScreenBufferInfo (cur_screen, &info);
+
+ /* Shrink the window first, so the buffer dimensions can be
+ reduced if necessary. */
+ new_win_dims.Top = 0;
+ new_win_dims.Left = 0;
+ new_win_dims.Bottom = min (new_size.Y, info.dwSize.Y) - 1;
+ new_win_dims.Right = min (new_size.X, info.dwSize.X) - 1;
+ SetConsoleWindowInfo (cur_screen, TRUE, &new_win_dims);
+
+ SetConsoleScreenBufferSize (cur_screen, new_size);
+
+ /* Set the window size to match the buffer dimension. */
+ new_win_dims.Top = 0;
+ new_win_dims.Left = 0;
+ new_win_dims.Bottom = new_size.Y - 1;
+ new_win_dims.Right = new_size.X - 1;
+ SetConsoleWindowInfo (cur_screen, TRUE, &new_win_dims);
+ }
+ }
+