more.c:
[reactos.git] / rosapps / cmdutils / more.c
index a13609d..d1bd8c5 100644 (file)
@@ -5,6 +5,8 @@
  *
  * 26 Sep 1999 - Paolo Pantaleo <paolopan@freemail.it>
  *     started 
+ * Oct 2003 - Timothy Schepens <tischepe at fastmail dot fm>
+ *     use window size instead of buffer size.
  */
 
 #include <windows.h>
@@ -29,13 +31,22 @@ GetScreenSize (PSHORT maxx, PSHORT maxy)
        CONSOLE_SCREEN_BUFFER_INFO csbi;
 
        GetConsoleScreenBufferInfo (hStdOut, &csbi);
+               *maxx = csbi.srWindow.Right;
+               *maxy = csbi.srWindow.Bottom;
 
-       if (maxx)
-               *maxx = csbi.dwSize.X;
-       if (maxy)
-               *maxy = csbi.dwSize.Y;
 }
 
+
+static
+VOID ConOutPuts (LPTSTR szText)
+{
+       DWORD dwWritten;
+
+       WriteFile (GetStdHandle (STD_OUTPUT_HANDLE), szText, _tcslen(szText), &dwWritten, NULL);
+       WriteFile (GetStdHandle (STD_OUTPUT_HANDLE), "\n", 1, &dwWritten, NULL);
+}
+
+
 static VOID
 ConInKey (VOID)
 {
@@ -73,7 +84,7 @@ int main (int argc, char **argv)
 {
        SHORT maxx,maxy;
        SHORT line_count=0,ch_count=0;
-       INT i;
+       INT i, last;
 
        /*reading/writing buffer*/
        TCHAR *buff;
@@ -86,39 +97,48 @@ int main (int argc, char **argv)
 
        len = _tcslen (msg);
        hStdIn = GetStdHandle(STD_INPUT_HANDLE);
-       hKeyboard = CreateFile ("CONIN$", GENERIC_READ,
-                               0,NULL,OPEN_ALWAYS,0,0);
        hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
        hStdErr = GetStdHandle(STD_ERROR_HANDLE);
 
+       if (argc > 1 && _tcsncmp (argv[1], _T("/?"), 2) == 0)
+       {
+               ConOutPuts(_T("Help text still missing!!"));
+               return 0;
+       }
+
+       hKeyboard = CreateFile ("CONIN$", GENERIC_READ,
+                               0,NULL,OPEN_ALWAYS,0,0);
+
        GetScreenSize(&maxx,&maxy);
 
-       buff=malloc(maxx);
+       buff=malloc(4096);
 
        FlushConsoleInputBuffer (hKeyboard);
 
        do
        {
-               bRet = ReadFile(hStdIn,buff,1,&dwRead,NULL);
+               bRet = ReadFile(hStdIn,buff,4096,&dwRead,NULL);
 
-               if (dwRead>0 && bRet)
-                       WriteFile(hStdOut,buff,dwRead,&dwWritten,NULL);
-
-               for(i=0;i<dwRead;i++)
+               for(last=i=0;i<dwRead && bRet;i++)
                {
                        ch_count++;
-                       if(buff[i] == _T('\x0a') || ch_count == maxx)
+                       if(buff[i] == _T('\n') || ch_count == maxx)
                        {
                                ch_count=0;
                                line_count++;
-                               if (line_count == maxy-1)
+                               if (line_count == maxy)
                                {
                                        line_count = 0;
+                                       WriteFile(hStdOut,&buff[last], i-last+1, &dwWritten, NULL);
+                                       last=i+1;
                                        FlushFileBuffers (hStdOut);
                                        WaitForKey ();
                                }
                        }
                }
+               if (last<dwRead && bRet)
+                       WriteFile(hStdOut,&buff[last], dwRead-last, &dwWritten, NULL);
+
        }
        while(dwRead>0 && bRet);
 
@@ -128,4 +148,4 @@ int main (int argc, char **argv)
        return 0;
 }
 
-/* EOF */
\ No newline at end of file
+/* EOF */