- GetConsoleScreenBufferInfo(console, &sbi);
- attr = plain_attr = sbi.wAttributes;
- negative = 0;
+ /* get OS handle of the stream */
+ hcon = (HANDLE) _get_osfhandle(_fileno(stream));
+ if (hcon == INVALID_HANDLE_VALUE)
+ return 0;
+
+ /* check if its a handle to a console output screen buffer */
+ if (!GetConsoleScreenBufferInfo(hcon, &sbi))
+ return 0;
+
+ if (!initialized) {
+ attr = plain_attr = sbi.wAttributes;
+ negative = 0;
+ initialized = 1;
+ }