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 static TCHAR szCont
[128];
20 static DWORD szContLength
;
21 static HINSTANCE hApp
;
23 /*handle for file and console*/
31 GetScreenSize (PSHORT maxx
, PSHORT maxy
)
33 CONSOLE_SCREEN_BUFFER_INFO csbi
;
35 GetConsoleScreenBufferInfo (hStdOut
, &csbi
);
36 *maxx
= (csbi
.srWindow
.Right
- csbi
.srWindow
.Left
) + 1;
37 *maxy
= (csbi
.srWindow
.Bottom
- csbi
.srWindow
.Top
) - 4;
43 VOID
ConOutPuts (LPTSTR szText
)
47 WriteFile (GetStdHandle (STD_OUTPUT_HANDLE
), szText
, _tcslen(szText
), &dwWritten
, NULL
);
48 WriteFile (GetStdHandle (STD_OUTPUT_HANDLE
), "\n", 1, &dwWritten
, NULL
);
60 ReadConsoleInput (hKeyboard
, &ir
, 1, &dwRead
);
61 if ((ir
.EventType
== KEY_EVENT
) &&
62 (ir
.Event
.KeyEvent
.bKeyDown
== TRUE
))
74 WriteFile (hStdErr
, szCont
, szContLength
, &dwWritten
, NULL
);
78 WriteFile (hStdErr
, _T("\n"), 1, &dwWritten
, NULL
);
80 // FlushConsoleInputBuffer (hConsoleIn);
84 //INT CommandMore (LPTSTR cmd, LPTSTR param)
85 int main (int argc
, char **argv
)
88 SHORT line_count
=0,ch_count
=0;
90 HANDLE hFile
= INVALID_HANDLE_VALUE
;
91 TCHAR szFullPath
[MAX_PATH
];
93 /*reading/writing buffer*/
96 /*bytes written by WriteFile and ReadFile*/
97 DWORD dwRead
,dwWritten
;
99 /*ReadFile() return value*/
103 hStdIn
= GetStdHandle(STD_INPUT_HANDLE
);
104 hStdOut
= GetStdHandle(STD_OUTPUT_HANDLE
);
105 hStdErr
= GetStdHandle(STD_ERROR_HANDLE
);
106 hApp
= GetModuleHandle(NULL
);
111 ConOutPuts(_T("Error: no memory"));
115 if (argc
> 1 && _tcsncmp (argv
[1], _T("/?"), 2) == 0)
117 if (LoadString(hApp
, IDS_USAGE
, buff
, 4096 / sizeof(TCHAR
)) < (int)(4096 / sizeof(TCHAR
)))
119 CharToOem(buff
, buff
);
127 hKeyboard
= CreateFile (_T("CONIN$"), GENERIC_READ
|GENERIC_WRITE
,
128 0,NULL
,OPEN_ALWAYS
,0,0);
130 GetScreenSize(&maxx
,&maxy
);
134 FlushConsoleInputBuffer (hKeyboard
);
138 GetFullPathNameA(argv
[1], MAX_PATH
, szFullPath
, NULL
);
139 hFile
= CreateFile (szFullPath
,
146 if (hFile
== INVALID_HANDLE_VALUE
)
148 if (LoadString(hApp
, IDS_FILE_ACCESS
, szMsg
, sizeof(szMsg
) / sizeof(TCHAR
)) < (int)(sizeof(szMsg
) / sizeof(TCHAR
)))
150 _stprintf(buff
, szMsg
, szFullPath
);
151 CharToOem(buff
, buff
);
164 if (!LoadString(hApp
, IDS_CONTINUE
, szCont
, sizeof(szCont
) / sizeof(TCHAR
)))
166 /* Shouldn't happen, so exit */
169 szContLength
= _tcslen(szCont
);
175 bRet
= ReadFile(hFile
,buff
,4096,&dwRead
,NULL
);
177 for(last
=i
=0;i
<dwRead
&& bRet
;i
++)
180 if(buff
[i
] == _T('\n') || ch_count
== maxx
)
184 if (line_count
== maxy
)
187 WriteFile(hStdOut
,&buff
[last
], i
-last
+1, &dwWritten
, NULL
);
189 FlushFileBuffers (hStdOut
);
194 if (last
<dwRead
&& bRet
)
195 WriteFile(hStdOut
,&buff
[last
], dwRead
-last
, &dwWritten
, NULL
);
198 while(dwRead
>0 && bRet
);
201 CloseHandle (hKeyboard
);