Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

more.c

Go to the documentation of this file.
00001 /* $Id: more.c 38171 2008-12-18 12:32:32Z cwittich $
00002  * 
00003  * MORE.C - external command.
00004  *
00005  * clone from 4nt more command
00006  *
00007  * 26 Sep 1999 - Paolo Pantaleo <paolopan@freemail.it>
00008  *     started
00009  * Oct 2003 - Timothy Schepens <tischepe at fastmail dot fm>
00010  *     use window size instead of buffer size.
00011  */
00012 
00013 #include <windows.h>
00014 #include <malloc.h>
00015 #include <tchar.h>
00016 #include <stdio.h>
00017 #include "resource.h"
00018 
00019 static TCHAR szCont[128];
00020 static DWORD szContLength;
00021 static HINSTANCE hApp;
00022 
00023 /*handle for file and console*/
00024 HANDLE hStdIn;
00025 HANDLE hStdOut;
00026 HANDLE hStdErr;
00027 HANDLE hKeyboard;
00028 
00029 
00030 static VOID
00031 GetScreenSize (PSHORT maxx, PSHORT maxy)
00032 {
00033    CONSOLE_SCREEN_BUFFER_INFO csbi;
00034 
00035    GetConsoleScreenBufferInfo (hStdOut, &csbi);
00036       *maxx = (csbi.srWindow.Right - csbi.srWindow.Left) + 1;
00037       *maxy = (csbi.srWindow.Bottom  - csbi.srWindow.Top) - 4;
00038 
00039 }
00040 
00041 
00042 static
00043 VOID ConOutPuts (LPTSTR szText)
00044 {
00045    DWORD dwWritten;
00046    
00047    WriteFile (GetStdHandle (STD_OUTPUT_HANDLE), szText, _tcslen(szText), &dwWritten, NULL);
00048    WriteFile (GetStdHandle (STD_OUTPUT_HANDLE), "\n", 1, &dwWritten, NULL);
00049 }
00050 
00051 
00052 static VOID
00053 ConInKey (VOID)
00054 {
00055    INPUT_RECORD ir;
00056    DWORD dwRead;
00057 
00058    do
00059    {
00060       ReadConsoleInput (hKeyboard, &ir, 1, &dwRead);
00061       if ((ir.EventType == KEY_EVENT) &&
00062          (ir.Event.KeyEvent.bKeyDown == TRUE))
00063          return;
00064    }
00065    while (TRUE);
00066 }
00067 
00068 
00069 static VOID
00070 WaitForKey (VOID)
00071 {
00072    DWORD dwWritten;
00073 
00074     WriteFile (hStdErr, szCont , szContLength, &dwWritten, NULL);
00075 
00076    ConInKey();
00077 
00078    WriteFile (hStdErr, _T("\n"), 1, &dwWritten, NULL);
00079 
00080 //  FlushConsoleInputBuffer (hConsoleIn);
00081 }
00082 
00083 
00084 //INT CommandMore (LPTSTR cmd, LPTSTR param)
00085 int main (int argc, char **argv)
00086 {
00087    SHORT maxx,maxy;
00088    SHORT line_count=0,ch_count=0;
00089    DWORD i, last;
00090    HANDLE hFile = INVALID_HANDLE_VALUE;
00091    TCHAR szFullPath[MAX_PATH];
00092     TCHAR szMsg[1024];
00093    /*reading/writing buffer*/
00094    TCHAR *buff;
00095 
00096    /*bytes written by WriteFile and ReadFile*/
00097    DWORD dwRead,dwWritten;
00098 
00099    /*ReadFile() return value*/
00100    BOOL bRet;
00101 
00102 
00103    hStdIn = GetStdHandle(STD_INPUT_HANDLE);
00104    hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
00105    hStdErr = GetStdHandle(STD_ERROR_HANDLE);
00106     hApp = GetModuleHandle(NULL);
00107 
00108    buff=malloc(4096);
00109    if (!buff)
00110    {
00111       ConOutPuts(_T("Error: no memory"));
00112       return 1;
00113    }
00114 
00115    if (argc > 1 && _tcsncmp (argv[1], _T("/?"), 2) == 0)
00116    {
00117       if (LoadString(hApp, IDS_USAGE, buff, 4096 / sizeof(TCHAR)) < (int)(4096 / sizeof(TCHAR)))
00118       {
00119          CharToOem(buff, buff);
00120          ConOutPuts(buff);
00121       }
00122 
00123       free(buff);
00124       return 0;
00125    }
00126 
00127    hKeyboard = CreateFile (_T("CONIN$"), GENERIC_READ|GENERIC_WRITE,
00128                            0,NULL,OPEN_ALWAYS,0,0);
00129 
00130    GetScreenSize(&maxx,&maxy);
00131 
00132 
00133 
00134    FlushConsoleInputBuffer (hKeyboard);
00135 
00136    if(argc > 1)
00137    {
00138       GetFullPathNameA(argv[1], MAX_PATH, szFullPath, NULL);
00139       hFile = CreateFile (szFullPath, 
00140                             GENERIC_READ,
00141                            0,
00142                             NULL,
00143                             OPEN_EXISTING,
00144                             0,
00145                             0);
00146       if (hFile == INVALID_HANDLE_VALUE)
00147       {
00148          if (LoadString(hApp, IDS_FILE_ACCESS, szMsg, sizeof(szMsg) / sizeof(TCHAR)) < (int)(sizeof(szMsg) / sizeof(TCHAR)))
00149          {
00150             _stprintf(buff, szMsg, szFullPath);
00151             CharToOem(buff, buff);
00152             ConOutPuts(buff);
00153          }
00154 
00155          free(buff);
00156          return 0;
00157       }
00158    }
00159    else
00160    {
00161       hFile = hStdIn;
00162    }
00163 
00164     if (!LoadString(hApp, IDS_CONTINUE, szCont, sizeof(szCont) / sizeof(TCHAR)))
00165     {
00166         /* Shouldn't happen, so exit */
00167         return 1;
00168     }
00169     szContLength = _tcslen(szCont);
00170 
00171 
00172 
00173    do
00174    {
00175       bRet = ReadFile(hFile,buff,4096,&dwRead,NULL);
00176 
00177       for(last=i=0;i<dwRead && bRet;i++)
00178       {
00179          ch_count++;
00180          if(buff[i] == _T('\n') || ch_count == maxx)
00181          {
00182             ch_count=0;
00183             line_count++;
00184             if (line_count == maxy)
00185             {
00186                line_count = 0;
00187                WriteFile(hStdOut,&buff[last], i-last+1, &dwWritten, NULL);
00188                last=i+1;
00189                FlushFileBuffers (hStdOut);
00190                WaitForKey ();
00191             }
00192          }
00193       }
00194       if (last<dwRead && bRet)
00195          WriteFile(hStdOut,&buff[last], dwRead-last, &dwWritten, NULL);
00196 
00197    }
00198    while(dwRead>0 && bRet);
00199 
00200    free (buff);
00201    CloseHandle (hKeyboard);
00202    if (hFile != hStdIn)
00203       CloseHandle (hFile);
00204 
00205    return 0;
00206 }
00207 
00208 /* EOF */

Generated on Thu Feb 9 04:39:01 2012 for ReactOS by doxygen 1.6.3

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.