3 * MORE.C - external command.
5 * clone from 4nt more command
7 * 26 Sep 1999 - Paolo Pantaleo <paolopan@freemail.it>
9 * Oct 2003 - Timothy Schepens <tischepe at fastmail dot fm>
10 * use window size instead of buffer size.
19 LPTSTR msg
= _T("--- continue ---");
22 /*handle for file and console*/
30 GetScreenSize (PSHORT maxx
, PSHORT maxy
)
32 CONSOLE_SCREEN_BUFFER_INFO csbi
;
34 GetConsoleScreenBufferInfo (hStdOut
, &csbi
);
35 *maxx
= (csbi
.srWindow
.Right
- csbi
.srWindow
.Left
) + 1;
36 *maxy
= (csbi
.srWindow
.Bottom
- csbi
.srWindow
.Top
) - 4;
42 VOID
ConOutPuts (LPTSTR szText
)
46 WriteFile (GetStdHandle (STD_OUTPUT_HANDLE
), szText
, _tcslen(szText
), &dwWritten
, NULL
);
47 WriteFile (GetStdHandle (STD_OUTPUT_HANDLE
), "\n", 1, &dwWritten
, NULL
);
59 ReadConsoleInput (hKeyboard
, &ir
, 1, &dwRead
);
60 if ((ir
.EventType
== KEY_EVENT
) &&
61 (ir
.Event
.KeyEvent
.bKeyDown
== TRUE
))
73 WriteFile (hStdErr
,msg
, len
, &dwWritten
, NULL
);
77 WriteFile (hStdErr
, _T("\n"), 1, &dwWritten
, NULL
);
79 // FlushConsoleInputBuffer (hConsoleIn);
83 //INT CommandMore (LPTSTR cmd, LPTSTR param)
84 int main (int argc
, char **argv
)
87 SHORT line_count
=0,ch_count
=0;
89 HANDLE hFile
= INVALID_HANDLE_VALUE
;
90 TCHAR szFullPath
[MAX_PATH
];
92 /*reading/writing buffer*/
95 /*bytes written by WriteFile and ReadFile*/
96 DWORD dwRead
,dwWritten
;
98 /*ReadFile() return value*/
102 hStdIn
= GetStdHandle(STD_INPUT_HANDLE
);
103 hStdOut
= GetStdHandle(STD_OUTPUT_HANDLE
);
104 hStdErr
= GetStdHandle(STD_ERROR_HANDLE
);
106 if (argc
> 1 && _tcsncmp (argv
[1], _T("/?"), 2) == 0)
108 ConOutPuts(_T("Help text still missing!!"));
112 hKeyboard
= CreateFile (_T("CONIN$"), GENERIC_READ
,
113 0,NULL
,OPEN_ALWAYS
,0,0);
115 GetScreenSize(&maxx
,&maxy
);
119 FlushConsoleInputBuffer (hKeyboard
);
123 GetFullPathName(argv
[1], MAX_PATH
, szFullPath
, NULL
);
124 hFile
= CreateFile (szFullPath
, GENERIC_READ
,
125 0,NULL
,OPEN_ALWAYS
,0,0);
130 if(hFile
!= INVALID_HANDLE_VALUE
)
132 bRet
= ReadFile(hFile
,buff
,4096,&dwRead
,NULL
);
136 bRet
= ReadFile(hStdIn
,buff
,4096,&dwRead
,NULL
);
139 for(last
=i
=0;i
<dwRead
&& bRet
;i
++)
142 if(buff
[i
] == _T('\n') || ch_count
== maxx
)
146 if (line_count
== maxy
)
149 WriteFile(hStdOut
,&buff
[last
], i
-last
+1, &dwWritten
, NULL
);
151 FlushFileBuffers (hStdOut
);
156 if (last
<dwRead
&& bRet
)
157 WriteFile(hStdOut
,&buff
[last
], dwRead
-last
, &dwWritten
, NULL
);
160 while(dwRead
>0 && bRet
);
163 CloseHandle (hKeyboard
);