2 * MORE.C - external command.
4 * clone from 4nt more command
6 * 26 Sep 1999 - Paolo Pantaleo <paolopan@freemail.it>
8 * Oct 2003 - Timothy Schepens <tischepe at fastmail dot fm>
9 * use window size instead of buffer size.
18 LPTSTR msg
= _T("--- continue ---");
21 /*handle for file and console*/
29 GetScreenSize (PSHORT maxx
, PSHORT maxy
)
31 CONSOLE_SCREEN_BUFFER_INFO csbi
;
33 GetConsoleScreenBufferInfo (hStdOut
, &csbi
);
34 *maxx
= (csbi
.srWindow
.Right
- csbi
.srWindow
.Left
) + 1;
35 *maxy
= (csbi
.srWindow
.Bottom
- csbi
.srWindow
.Top
) - 4;
41 VOID
ConOutPuts (LPTSTR szText
)
45 WriteFile (GetStdHandle (STD_OUTPUT_HANDLE
), szText
, _tcslen(szText
), &dwWritten
, NULL
);
46 WriteFile (GetStdHandle (STD_OUTPUT_HANDLE
), "\n", 1, &dwWritten
, NULL
);
58 ReadConsoleInput (hKeyboard
, &ir
, 1, &dwRead
);
59 if ((ir
.EventType
== KEY_EVENT
) &&
60 (ir
.Event
.KeyEvent
.bKeyDown
== TRUE
))
72 WriteFile (hStdErr
,msg
, len
, &dwWritten
, NULL
);
76 WriteFile (hStdErr
, _T("\n"), 1, &dwWritten
, NULL
);
78 // FlushConsoleInputBuffer (hConsoleIn);
82 //INT CommandMore (LPTSTR cmd, LPTSTR param)
83 int main (int argc
, char **argv
)
86 SHORT line_count
=0,ch_count
=0;
88 HANDLE hFile
= INVALID_HANDLE_VALUE
;
89 TCHAR szFullPath
[MAX_PATH
];
91 /*reading/writing buffer*/
94 /*bytes written by WriteFile and ReadFile*/
95 DWORD dwRead
,dwWritten
;
97 /*ReadFile() return value*/
101 hStdIn
= GetStdHandle(STD_INPUT_HANDLE
);
102 hStdOut
= GetStdHandle(STD_OUTPUT_HANDLE
);
103 hStdErr
= GetStdHandle(STD_ERROR_HANDLE
);
105 if (argc
> 1 && _tcsncmp (argv
[1], _T("/?"), 2) == 0)
107 ConOutPuts(_T("Help text still missing!!"));
111 hKeyboard
= CreateFile (_T("CONIN$"), GENERIC_READ
,
112 0,NULL
,OPEN_ALWAYS
,0,0);
114 GetScreenSize(&maxx
,&maxy
);
118 FlushConsoleInputBuffer (hKeyboard
);
122 GetFullPathName(argv
[1], MAX_PATH
, szFullPath
, NULL
);
123 hFile
= CreateFile (szFullPath
, GENERIC_READ
,
124 0,NULL
,OPEN_ALWAYS
,0,0);
129 if(hFile
!= INVALID_HANDLE_VALUE
)
131 bRet
= ReadFile(hFile
,buff
,4096,&dwRead
,NULL
);
135 bRet
= ReadFile(hStdIn
,buff
,4096,&dwRead
,NULL
);
138 for(last
=i
=0;i
<dwRead
&& bRet
;i
++)
141 if(buff
[i
] == _T('\n') || ch_count
== maxx
)
145 if (line_count
== maxy
)
148 WriteFile(hStdOut
,&buff
[last
], i
-last
+1, &dwWritten
, NULL
);
150 FlushFileBuffers (hStdOut
);
155 if (last
<dwRead
&& bRet
)
156 WriteFile(hStdOut
,&buff
[last
], dwRead
-last
, &dwWritten
, NULL
);
159 while(dwRead
>0 && bRet
);
162 CloseHandle (hKeyboard
);