ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

redir.c
Go to the documentation of this file.
00001 /*
00002  *  REDIR.C - redirection handling.
00003  *
00004  *
00005  *  History:
00006  *
00007  *    12/15/95 (Tim Norman)
00008  *        started.
00009  *
00010  *    12 Jul 98 (Hans B Pufal)
00011  *        Rewrote to make more efficient and to conform to new command.c
00012  *        and batch.c processing.
00013  *
00014  *    27-Jul-1998 (John P Price <linux-guru@gcfl.net>)
00015  *        Added config.h include
00016  *
00017  *    22-Jan-1999 (Eric Kohl)
00018  *        Unicode safe!
00019  *        Added new error redirection "2>" and "2>>".
00020  *
00021  *    26-Jan-1999 (Eric Kohl)
00022  *        Added new error AND output redirection "&>" and "&>>".
00023  *
00024  *    24-Jun-2005 (Brandon Turner <turnerb7@msu.edu>)
00025  *        simple check to fix > and | bug with 'rem'
00026  */
00027 
00028 #include <precomp.h>
00029 
00030 #ifdef FEATURE_REDIRECTION
00031 
00032 
00033 /* cmd allows redirection of handles numbered 3-9 even though these don't
00034  * correspond to any STD_ constant. */
00035 static HANDLE ExtraHandles[10 - 3];
00036 
00037 static HANDLE GetHandle(UINT Number)
00038 {
00039     if (Number < 3)
00040         return GetStdHandle(STD_INPUT_HANDLE - Number);
00041     else
00042         return ExtraHandles[Number - 3];
00043 }
00044 
00045 static VOID SetHandle(UINT Number, HANDLE Handle)
00046 {
00047     if (Number < 3)
00048         SetStdHandle(STD_INPUT_HANDLE - Number, Handle);
00049     else
00050         ExtraHandles[Number - 3] = Handle;
00051 }
00052 
00053 BOOL
00054 PerformRedirection(REDIRECTION *RedirList)
00055 {
00056     REDIRECTION *Redir;
00057     LPTSTR Filename;
00058     HANDLE hNew;
00059     UINT DupNumber;
00060     static SECURITY_ATTRIBUTES SecAttr = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE };
00061 
00062     /* Some parameters used for read, write, and append, respectively */
00063     static const DWORD dwAccess[] = {
00064         GENERIC_READ, 
00065         GENERIC_WRITE,
00066         GENERIC_WRITE
00067     };
00068     static const DWORD dwShareMode[] = {
00069         FILE_SHARE_READ | FILE_SHARE_WRITE,
00070         FILE_SHARE_READ,
00071         FILE_SHARE_READ
00072     };
00073     static const DWORD dwCreationDisposition[] = {
00074         OPEN_EXISTING,
00075         CREATE_ALWAYS,
00076         OPEN_ALWAYS
00077     };
00078 
00079     for (Redir = RedirList; Redir; Redir = Redir->Next)
00080     {
00081         Filename = DoDelayedExpansion(Redir->Filename);
00082         if (!Filename)
00083             goto redir_error;
00084         StripQuotes(Filename);
00085 
00086         if (*Filename == _T('&'))
00087         {
00088             DupNumber = Filename[1] - _T('0');
00089             if (DupNumber >= 10 ||
00090                 !DuplicateHandle(GetCurrentProcess(),
00091                                  GetHandle(DupNumber),
00092                                  GetCurrentProcess(),
00093                                  &hNew,
00094                                  0,
00095                                  TRUE,
00096                                  DUPLICATE_SAME_ACCESS))
00097             {
00098                 hNew = INVALID_HANDLE_VALUE;
00099             }
00100         }
00101         else
00102         {
00103             hNew = CreateFile(Filename,
00104                               dwAccess[Redir->Type],
00105                               dwShareMode[Redir->Type],
00106                               &SecAttr,
00107                               dwCreationDisposition[Redir->Type],
00108                               0,
00109                               NULL);
00110         }
00111 
00112         if (hNew == INVALID_HANDLE_VALUE)
00113         {
00114             ConErrResPrintf(Redir->Type == REDIR_READ ? STRING_CMD_ERROR1 : STRING_CMD_ERROR3,
00115                             Filename);
00116             cmd_free(Filename);
00117 redir_error:
00118             /* Undo all the redirections before this one */
00119             UndoRedirection(RedirList, Redir);
00120             return FALSE;
00121         }
00122 
00123         if (Redir->Type == REDIR_APPEND)
00124             SetFilePointer(hNew, 0, NULL, FILE_END);
00125         Redir->OldHandle = GetHandle(Redir->Number);
00126         SetHandle(Redir->Number, hNew);
00127 
00128         TRACE("%d redirected to: %s\n", Redir->Number, debugstr_aw(Filename));
00129         cmd_free(Filename);
00130     }
00131     return TRUE;
00132 }
00133 
00134 VOID
00135 UndoRedirection(REDIRECTION *Redir, REDIRECTION *End)
00136 {
00137     for (; Redir != End; Redir = Redir->Next)
00138     {
00139         CloseHandle(GetHandle(Redir->Number));
00140         SetHandle(Redir->Number, Redir->OldHandle);
00141         Redir->OldHandle = INVALID_HANDLE_VALUE;
00142     }
00143 }
00144 
00145 VOID
00146 FreeRedirection(REDIRECTION *Redir)
00147 {
00148     REDIRECTION *Next;
00149     for (; Redir; Redir = Next)
00150     {
00151         Next = Redir->Next;
00152         ASSERT(Redir->OldHandle == INVALID_HANDLE_VALUE);
00153         cmd_free(Redir);
00154     }
00155 }
00156 
00157 #endif /* FEATURE_REDIRECTION */

Generated on Fri May 25 2012 04:16:32 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.