ReactOS 0.4.15-dev-8241-g63935f8
redir.c File Reference
#include "precomp.h"
Include dependency graph for redir.c:

Go to the source code of this file.

Functions

HANDLE GetHandle (UINT Number)
 
VOID SetHandle (UINT Number, HANDLE Handle)
 
BOOL PerformRedirection (REDIRECTION *RedirList)
 
VOID UndoRedirection (REDIRECTION *Redir, REDIRECTION *End)
 
VOID FreeRedirection (REDIRECTION *Redir)
 

Variables

static const PCON_STREAM StdStreams [] = { StdIn, StdOut, StdErr }
 
static HANDLE ExtraHandles [10 - 3]
 

Function Documentation

◆ FreeRedirection()

VOID FreeRedirection ( REDIRECTION Redir)

Definition at line 153 of file redir.c.

154{
155 REDIRECTION *Next;
156 for (; Redir; Redir = Next)
157 {
158 Next = Redir->Next;
160 cmd_free(Redir);
161 }
162}
#define cmd_free(ptr)
Definition: cmddbg.h:31
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
#define ASSERT(a)
Definition: mode.c:44
HANDLE OldHandle
Definition: cmd.h:452
struct _REDIRECTION * Next
Definition: cmd.h:451

Referenced by ExitBatch(), FreeCommand(), ParsePrimary(), and ParseRedirection().

◆ GetHandle()

HANDLE GetHandle ( UINT  Number)

Definition at line 39 of file redir.c.

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}
#define ARRAYSIZE(array)
Definition: filtermapper.c:47
_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
HANDLE ConStreamGetOSHandle(IN PCON_STREAM Stream)
Definition: stream.c:240

◆ PerformRedirection()

BOOL PerformRedirection ( REDIRECTION RedirList)

Definition at line 63 of file redir.c.

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}
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:43
#define STRING_CMD_ERROR3
Definition: resource.h:29
#define STRING_CMD_ERROR1
Definition: resource.h:27
static VOID StripQuotes(LPSTR in)
Definition: cmdcons.c:116
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
struct _SECURITY_ATTRIBUTES SECURITY_ATTRIBUTES
#define OPEN_EXISTING
Definition: compat.h:775
#define SetFilePointer
Definition: compat.h:743
#define GetCurrentProcess()
Definition: compat.h:759
#define GENERIC_READ
Definition: compat.h:135
#define FILE_SHARE_READ
Definition: compat.h:136
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 long DWORD
Definition: ntddk_ex.h:95
#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
VOID SetHandle(UINT Number, HANDLE Handle)
Definition: redir.c:50
VOID UndoRedirection(REDIRECTION *Redir, REDIRECTION *End)
Definition: redir.c:142
#define TRACE(s)
Definition: solgame.cpp:4
REDIR_MODE Mode
Definition: cmd.h:454
BYTE Number
Definition: cmd.h:453
TCHAR Filename[]
Definition: cmd.h:455
#define GetHandle(h)
Definition: treelist.c:116
#define _T(x)
Definition: vfdio.h:22
#define FILE_END
Definition: winbase.h:114
#define CreateFile
Definition: winbase.h:3749
#define DUPLICATE_SAME_ACCESS
CHAR * LPTSTR
Definition: xmlstorage.h:192

Referenced by ExecuteCommand().

◆ SetHandle()

VOID SetHandle ( UINT  Number,
HANDLE  Handle 
)

Definition at line 50 of file redir.c.

51{
52 if (Number < 3)
53 {
55 /* Synchronize the associated Win32 handle */
57 }
58 else if (Number < ARRAYSIZE(ExtraHandles) + 3)
60}
BOOL WINAPI DECLSPEC_HOTPATCH SetStdHandle(DWORD nStdHandle, HANDLE hHandle)
Definition: console.c:1213
ULONG Handle
Definition: gdb_input.c:15
BOOL ConStreamSetOSHandle(IN PCON_STREAM Stream, IN HANDLE Handle)
Definition: stream.c:263
#define STD_INPUT_HANDLE
Definition: winbase.h:267

Referenced by cmd_ctty(), PerformRedirection(), and UndoRedirection().

◆ UndoRedirection()

VOID UndoRedirection ( REDIRECTION Redir,
REDIRECTION End 
)

Definition at line 142 of file redir.c.

143{
144 for (; Redir != End; Redir = Redir->Next)
145 {
147 SetHandle(Redir->Number, Redir->OldHandle);
149 }
150}
#define CloseHandle
Definition: compat.h:739

Referenced by ExecuteCommand(), ExitBatch(), and PerformRedirection().

Variable Documentation

◆ ExtraHandles

HANDLE ExtraHandles[10 - 3]
static

Definition at line 37 of file redir.c.

Referenced by GetHandle(), and SetHandle().

◆ StdStreams

const PCON_STREAM StdStreams[] = { StdIn, StdOut, StdErr }
static

Definition at line 36 of file redir.c.

Referenced by GetHandle(), and SetHandle().