ReactOS 0.4.15-dev-8419-g7f0e8a3
redir.c
Go to the documentation of this file.
1/*
2 * REDIR.C - redirection handling.
3 *
4 *
5 * History:
6 *
7 * 12/15/95 (Tim Norman)
8 * started.
9 *
10 * 12 Jul 98 (Hans B Pufal)
11 * Rewrote to make more efficient and to conform to new command.c
12 * and batch.c processing.
13 *
14 * 27-Jul-1998 (John P Price <linux-guru@gcfl.net>)
15 * Added config.h include
16 *
17 * 22-Jan-1999 (Eric Kohl)
18 * Unicode safe!
19 * Added new error redirection "2>" and "2>>".
20 *
21 * 26-Jan-1999 (Eric Kohl)
22 * Added new error AND output redirection "&>" and "&>>".
23 *
24 * 24-Jun-2005 (Brandon Turner <turnerb7@msu.edu>)
25 * simple check to fix > and | bug with 'rem'
26 */
27
28#include "precomp.h"
29
30#ifdef FEATURE_REDIRECTION
31
32/*
33 * CMD allows redirection of handles numbered 3-9 even though
34 * these don't correspond to any STD_ constant.
35 */
36static const PCON_STREAM StdStreams[] = { StdIn, StdOut, StdErr };
37static HANDLE ExtraHandles[10 - 3]; // 3 == ARRAYSIZE(StdStreams)
38
40{
41 if (Number < 3)
43 // return GetStdHandle(STD_INPUT_HANDLE - Number);
44 else if (Number < ARRAYSIZE(ExtraHandles) + 3)
45 return ExtraHandles[Number - 3];
46 else
48}
49
51{
52 if (Number < 3)
53 {
55 /* Synchronize the associated Win32 handle */
57 }
58 else if (Number < ARRAYSIZE(ExtraHandles) + 3)
60}
61
62BOOL
64{
65 REDIRECTION *Redir;
67 HANDLE hNew;
68 UINT DupNumber;
69
70 static SECURITY_ATTRIBUTES SecAttr = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE };
71
72 /* Some parameters used for read, write, and append, respectively */
73 static struct REDIR_PARAMS
74 {
75 DWORD dwDesiredAccess;
76 DWORD dwShareMode;
77 DWORD dwCreationDisposition;
78 } RedirParams[] =
79 {
82 {GENERIC_WRITE, FILE_SHARE_READ , OPEN_ALWAYS } // REDIR_APPEND
83 };
84
85 for (Redir = RedirList; Redir; Redir = Redir->Next)
86 {
88 if (!Filename)
89 goto redir_error;
91
92 if (*Filename == _T('&'))
93 {
94 DupNumber = Filename[1] - _T('0');
95 if (DupNumber >= 10 ||
97 GetHandle(DupNumber),
99 &hNew,
100 0,
101 TRUE,
103 {
105 }
106 }
107 else
108 {
109 hNew = CreateFile(Filename,
110 RedirParams[Redir->Mode].dwDesiredAccess,
111 RedirParams[Redir->Mode].dwShareMode,
112 &SecAttr,
113 RedirParams[Redir->Mode].dwCreationDisposition,
114 0,
115 NULL);
116 }
117
118 if (hNew == INVALID_HANDLE_VALUE)
119 {
120 /* TODO: Print a more detailed message */
122 Filename);
124redir_error:
125 /* Undo all the redirections before this one */
126 UndoRedirection(RedirList, Redir);
127 return FALSE;
128 }
129
130 if (Redir->Mode == REDIR_APPEND)
131 SetFilePointer(hNew, 0, NULL, FILE_END);
132 Redir->OldHandle = GetHandle(Redir->Number);
133 SetHandle(Redir->Number, hNew);
134
135 TRACE("%d redirected to: %s\n", Redir->Number, debugstr_aw(Filename));
137 }
138 return TRUE;
139}
140
141VOID
143{
144 for (; Redir != End; Redir = Redir->Next)
145 {
147 SetHandle(Redir->Number, Redir->OldHandle);
149 }
150}
151
152VOID
154{
155 REDIRECTION *Next;
156 for (; Redir; Redir = Next)
157 {
158 Next = Redir->Next;
160 cmd_free(Redir);
161 }
162}
163
164#endif /* FEATURE_REDIRECTION */
#define StdOut
Definition: fc.c:14
#define StdErr
Definition: fc.c:15
PTSTR DoDelayedExpansion(IN PCTSTR Line)
Definition: cmd.c:1640
@ REDIR_READ
Definition: cmd.h:445
@ REDIR_APPEND
Definition: cmd.h:447
#define ConErrResPrintf(uID,...)
Definition: console.h:50
#define debugstr_aw
Definition: precomp.h:44
#define STRING_CMD_ERROR3
Definition: resource.h:36
#define STRING_CMD_ERROR1
Definition: resource.h:34
static VOID StripQuotes(LPSTR in)
Definition: cmdcons.c:116
#define cmd_free(ptr)
Definition: cmddbg.h:31
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define ARRAYSIZE(array)
Definition: filtermapper.c:47
#define CloseHandle
Definition: compat.h:739
struct _SECURITY_ATTRIBUTES SECURITY_ATTRIBUTES
#define OPEN_EXISTING
Definition: compat.h:775
#define SetFilePointer
Definition: compat.h:743
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
#define GetCurrentProcess()
Definition: compat.h:759
#define GENERIC_READ
Definition: compat.h:135
#define FILE_SHARE_READ
Definition: compat.h:136
BOOL WINAPI DECLSPEC_HOTPATCH SetStdHandle(DWORD nStdHandle, HANDLE hHandle)
Definition: console.c:1213
BOOL WINAPI DuplicateHandle(IN HANDLE hSourceProcessHandle, IN HANDLE hSourceHandle, IN HANDLE hTargetProcessHandle, OUT LPHANDLE lpTargetHandle, IN DWORD dwDesiredAccess, IN BOOL bInheritHandle, IN DWORD dwOptions)
Definition: handle.c:149
IN PVCB IN PBCB OUT PDIRENT IN USHORT IN POEM_STRING Filename
Definition: fatprocs.h:939
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
ULONG Handle
Definition: gdb_input.c:15
#define ASSERT(a)
Definition: mode.c:44
#define CREATE_ALWAYS
Definition: disk.h:72
#define OPEN_ALWAYS
Definition: disk.h:70
unsigned int UINT
Definition: ndis.h:50
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
#define GENERIC_WRITE
Definition: nt_native.h:90
_In_opt_ PENTER_STATE_SYSTEM_HANDLER _In_opt_ PVOID _In_ LONG _In_opt_ LONG volatile * Number
Definition: ntpoapi.h:207
static HANDLE ExtraHandles[10 - 3]
Definition: redir.c:37
static const PCON_STREAM StdStreams[]
Definition: redir.c:36
VOID SetHandle(UINT Number, HANDLE Handle)
Definition: redir.c:50
VOID UndoRedirection(REDIRECTION *Redir, REDIRECTION *End)
Definition: redir.c:142
VOID FreeRedirection(REDIRECTION *Redir)
Definition: redir.c:153
BOOL PerformRedirection(REDIRECTION *RedirList)
Definition: redir.c:63
BOOL ConStreamSetOSHandle(IN PCON_STREAM Stream, IN HANDLE Handle)
Definition: stream.c:263
HANDLE ConStreamGetOSHandle(IN PCON_STREAM Stream)
Definition: stream.c:240
#define StdIn
Definition: stream.h:81
#define TRACE(s)
Definition: solgame.cpp:4
REDIR_MODE Mode
Definition: cmd.h:454
HANDLE OldHandle
Definition: cmd.h:452
BYTE Number
Definition: cmd.h:453
TCHAR Filename[]
Definition: cmd.h:455
struct _REDIRECTION * Next
Definition: cmd.h:451
#define GetHandle(h)
Definition: treelist.c:116
#define _T(x)
Definition: vfdio.h:22
#define FILE_END
Definition: winbase.h:114
#define STD_INPUT_HANDLE
Definition: winbase.h:267
#define CreateFile
Definition: winbase.h:3749
#define DUPLICATE_SAME_ACCESS
CHAR * LPTSTR
Definition: xmlstorage.h:192