ReactOS  0.4.14-dev-593-g1793dcc
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  */
36 static const PCON_STREAM StdStreams[] = { StdIn, StdOut, StdErr };
37 static 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
47  return INVALID_HANDLE_VALUE;
48 }
49 
51 {
52  if (Number < 3)
53  {
55  /* Synchronize the associated Win32 handle */
57  }
58  else if (Number < ARRAYSIZE(ExtraHandles) + 3)
59  ExtraHandles[Number - 3] = Handle;
60 }
61 
62 BOOL
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  {
81  {GENERIC_WRITE, FILE_SHARE_READ , CREATE_ALWAYS}, // REDIR_WRITE
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  {
104  hNew = INVALID_HANDLE_VALUE;
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);
124 redir_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 
141 VOID
143 {
144  for (; Redir != End; Redir = Redir->Next)
145  {
146  CloseHandle(GetHandle(Redir->Number));
147  SetHandle(Redir->Number, Redir->OldHandle);
149  }
150 }
151 
152 VOID
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 STRING_CMD_ERROR1
Definition: resource.h:27
#define TRUE
Definition: types.h:120
#define CloseHandle
Definition: compat.h:406
#define STRING_CMD_ERROR3
Definition: resource.h:29
#define debugstr_aw
Definition: precomp.h:43
#define ARRAYSIZE(array)
Definition: filtermapper.c:47
#define INVALID_HANDLE_VALUE
Definition: compat.h:399
BOOL PerformRedirection(REDIRECTION *RedirList)
Definition: redir.c:63
VOID FreeRedirection(REDIRECTION *Redir)
Definition: redir.c:153
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
static VOID StripQuotes(LPSTR in)
Definition: cmdcons.c:116
CHAR * LPTSTR
Definition: xmlstorage.h:192
IN PVCB IN PBCB OUT PDIRENT IN USHORT IN POEM_STRING Filename
Definition: fatprocs.h:925
#define FILE_SHARE_READ
Definition: compat.h:125
DWORD WINAPI DECLSPEC_HOTPATCH SetFilePointer(HANDLE hFile, LONG lDistanceToMove, PLONG lpDistanceToMoveHigh, DWORD dwMoveMethod)
Definition: fileinfo.c:204
REDIR_MODE Mode
Definition: cmd.h:351
HANDLE OldHandle
Definition: cmd.h:349
#define DUPLICATE_SAME_ACCESS
unsigned int BOOL
Definition: ntddk_ex.h:94
#define ConErrResPrintf(uID,...)
Definition: console.h:51
#define GENERIC_WRITE
Definition: nt_native.h:90
HANDLE GetHandle(UINT Number)
Definition: redir.c:39
smooth NULL
Definition: ftsmooth.c:416
#define STD_INPUT_HANDLE
Definition: winbase.h:264
#define OPEN_EXISTING
Definition: compat.h:434
HANDLE ConStreamGetOSHandle(IN PCON_STREAM Stream)
Definition: stream.c:240
BYTE Number
Definition: cmd.h:350
_In_ HANDLE Handle
Definition: extypes.h:390
#define _T(x)
Definition: vfdio.h:22
#define TRACE(s)
Definition: solgame.cpp:4
#define FILE_END
Definition: winbase.h:114
static HANDLE ExtraHandles[10 - 3]
Definition: redir.c:37
HANDLE WINAPI GetCurrentProcess(VOID)
Definition: proc.c:1138
TCHAR Filename[]
Definition: cmd.h:352
LPTSTR DoDelayedExpansion(LPTSTR Line)
Definition: cmd.c:1408
unsigned long DWORD
Definition: ntddk_ex.h:95
static const PCON_STREAM StdStreams[]
Definition: redir.c:36
#define OPEN_ALWAYS
Definition: disk.h:70
#define StdErr
Definition: stream.h:77
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
BOOL WINAPI DECLSPEC_HOTPATCH SetStdHandle(DWORD nStdHandle, HANDLE hHandle)
Definition: console.c:1213
BOOL ConStreamSetOSHandle(IN PCON_STREAM Stream, IN HANDLE Handle)
Definition: stream.c:263
#define GENERIC_READ
Definition: compat.h:124
_In_opt_ PENTER_STATE_SYSTEM_HANDLER _In_opt_ PVOID _In_ LONG _In_opt_ LONG volatile * Number
Definition: ntpoapi.h:204
#define CREATE_ALWAYS
Definition: disk.h:72
#define cmd_free(ptr)
Definition: cmddbg.h:31
IN OUT PVCB OUT PDIRENT OUT PBCB IN BOOLEAN CreateFile
Definition: fatprocs.h:904
VOID SetHandle(UINT Number, HANDLE Handle)
Definition: redir.c:50
unsigned int UINT
Definition: ndis.h:50
struct _REDIRECTION * Next
Definition: cmd.h:348
#define StdOut
Definition: stream.h:76
VOID UndoRedirection(REDIRECTION *Redir, REDIRECTION *End)
Definition: redir.c:142
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
#define StdIn
Definition: stream.h:75
struct _SECURITY_ATTRIBUTES SECURITY_ATTRIBUTES