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

subst.c
Go to the documentation of this file.
00001 /* PROJECT:         ReactOS Kernel
00002  * LICENSE:         GPL - See COPYING in the top level directory
00003  * FILE:            base/system/subst/subst.c
00004  * PURPOSE:         Associates a path with a drive letter
00005  * PROGRAMMERS:     Sam Arun Raj
00006  */
00007 
00008 /* INCLUDES *****************************************************************/
00009 
00010 #define LEAN_AND_MEAN
00011 #include <stdio.h>
00012 #include <stdlib.h>
00013 #include <windows.h>
00014 #include <tchar.h>
00015 #define NDEBUG
00016 #include <debug.h>
00017 #include "resource.h"
00018 
00019 /* FUNCTIONS ****************************************************************/
00020 
00021 void PrintError(DWORD ErrCode)
00022 {
00023     TCHAR szFmtString[RC_STRING_MAX_SIZE] = {0};
00024     TCHAR *buffer = (TCHAR*) calloc(2048,
00025                                     sizeof(TCHAR));
00026     TCHAR *msg = NULL;
00027 
00028     if (buffer)
00029     {
00030         FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
00031                       NULL,
00032                       ErrCode,
00033                       MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
00034                       (TCHAR*)&msg,
00035                       0,
00036                       NULL);
00037         LoadString(GetModuleHandle(NULL),
00038                    IDS_FAILED_WITH_ERROCODE,
00039                    szFmtString,
00040                    sizeof(szFmtString) / sizeof(szFmtString[0]));
00041         _sntprintf(buffer,
00042                    2048,
00043                    szFmtString,
00044                    ErrCode,
00045                    msg);
00046         _tprintf(_T("%s"),
00047                  buffer);
00048         if (msg)
00049             LocalFree(msg);
00050         free(buffer);
00051     }
00052 }
00053 
00054 void DisplaySubstUsage(void)
00055 {
00056     TCHAR szHelp[RC_STRING_MAX_SIZE] = {0};
00057 
00058     LoadString(GetModuleHandle(NULL),
00059                 IDS_USAGE,
00060                 szHelp,
00061                 sizeof(szHelp) / sizeof(szHelp[0]));
00062     _tprintf(_T("%s"), szHelp);
00063 }
00064 
00065 BOOLEAN IsSubstedDrive(TCHAR *Drive)
00066 {
00067     BOOLEAN Result = FALSE;
00068     LPTSTR lpTargetPath = NULL;
00069     DWORD CharCount, dwSize;
00070 
00071     if (_tcslen(Drive) > 2)
00072         return FALSE;
00073 
00074     dwSize = sizeof(TCHAR) * MAX_PATH;
00075     lpTargetPath = (LPTSTR) malloc(sizeof(TCHAR) * MAX_PATH);
00076     if ( lpTargetPath)
00077     {
00078         CharCount = QueryDosDevice(Drive,
00079                                    lpTargetPath,
00080                                    dwSize / sizeof(TCHAR));
00081         while (! CharCount &&
00082                GetLastError() == ERROR_INSUFFICIENT_BUFFER)
00083         {
00084             free(lpTargetPath);
00085             dwSize *= 2;
00086             lpTargetPath = (LPTSTR) malloc(dwSize);
00087             if (lpTargetPath)
00088             {
00089                 CharCount = QueryDosDevice(Drive,
00090                                            lpTargetPath,
00091                                            dwSize / sizeof(TCHAR));
00092             }
00093         }
00094 
00095         if (CharCount)
00096         {
00097             if ( _tcsncmp(lpTargetPath, _T("\\??\\"), 4) == 0 &&
00098                 ( (lpTargetPath[4] >= _T('A') &&
00099                 lpTargetPath[4] <= _T('Z')) ||
00100                  (lpTargetPath[4] >= _T('a') &&
00101                 lpTargetPath[4] <= _T('z')) ) )
00102             {
00103                 Result = TRUE;
00104             }
00105         }
00106         free(lpTargetPath);
00107     }
00108     return Result;
00109 }
00110 
00111 void DumpSubstedDrives(void)
00112 {
00113     TCHAR Drive[3] = _T("A:");
00114     LPTSTR lpTargetPath = NULL;
00115     DWORD CharCount, dwSize;
00116     INT i = 0;
00117 
00118     dwSize = sizeof(TCHAR) * MAX_PATH;
00119     lpTargetPath = (LPTSTR) malloc(sizeof(TCHAR) * MAX_PATH);
00120     if (! lpTargetPath)
00121         return;
00122 
00123     while (i < 26)
00124     {
00125         Drive[0] = _T('A') + i;
00126         CharCount = QueryDosDevice(Drive,
00127                                    lpTargetPath,
00128                                    dwSize / sizeof(TCHAR));
00129         while (! CharCount &&
00130                GetLastError() == ERROR_INSUFFICIENT_BUFFER)
00131         {
00132             free(lpTargetPath);
00133             dwSize *= 2;
00134             lpTargetPath = (LPTSTR) malloc(dwSize);
00135             if (lpTargetPath)
00136             {
00137                 CharCount = QueryDosDevice(Drive,
00138                                            lpTargetPath,
00139                                            dwSize / sizeof(TCHAR));
00140             }
00141         }
00142 
00143         if (! CharCount)
00144         {
00145             i++;
00146             continue;
00147         }
00148         else
00149         {
00150             if ( _tcsncmp(lpTargetPath, _T("\\??\\"), 4) == 0 &&
00151                 ( (lpTargetPath[4] >= _T('A') &&
00152                 lpTargetPath[4] <= _T('Z')) ||
00153                  (lpTargetPath[4] >= _T('a') &&
00154                 lpTargetPath[4] <= _T('z')) ) )
00155             {
00156                 _tprintf(_T("%s\\: => %s\n"),
00157                          Drive,
00158                          lpTargetPath + 4);
00159             }
00160         }
00161         i++;
00162     }
00163     free(lpTargetPath);
00164 }
00165 
00166 int DeleteSubst(TCHAR* Drive)
00167 {
00168     BOOL Result;
00169     TCHAR szFmtString[RC_STRING_MAX_SIZE] = {0};
00170 
00171     LoadString(GetModuleHandle(NULL),
00172                 IDS_INVALID_PARAMETER2,
00173                 szFmtString,
00174                 sizeof(szFmtString) / sizeof(szFmtString[0]));
00175 
00176     if (_tcslen(Drive) > 2)
00177     {
00178         _tprintf(szFmtString,
00179                  Drive);
00180         return 1;
00181     }
00182 
00183     if (! IsSubstedDrive(Drive))
00184     {
00185         _tprintf(szFmtString,
00186                 Drive);
00187         return 1;
00188     }
00189 
00190     Result = DefineDosDevice(DDD_REMOVE_DEFINITION,
00191                              Drive,
00192                              NULL);
00193     if (! Result)
00194     {
00195         PrintError(GetLastError());
00196         return 1;
00197     }
00198     return 0;
00199 }
00200 
00201 int AddSubst(TCHAR* Drive, TCHAR *Path)
00202 {
00203     BOOL Result;
00204     TCHAR szFmtString[RC_STRING_MAX_SIZE] = {0};
00205 
00206     LoadString(GetModuleHandle(NULL),
00207                 IDS_INVALID_PARAMETER2,
00208                 szFmtString,
00209                 sizeof(szFmtString) / sizeof(szFmtString[0]));
00210     if (_tcslen(Drive) != 2)
00211     {
00212         _tprintf(szFmtString,
00213                  Drive);
00214         return 1;
00215     }
00216 
00217     if (Drive[1] != _T(':'))
00218     {
00219         _tprintf(szFmtString,
00220                  Drive);
00221         return 1;
00222     }
00223 
00224     if (IsSubstedDrive(Drive))
00225     {
00226         LoadString(GetModuleHandle(NULL),
00227                    IDS_DRIVE_ALREAD_SUBSTED,
00228                    szFmtString,
00229                    sizeof(szFmtString) / sizeof(szFmtString[0]));
00230         _tprintf(szFmtString);
00231         return 1;
00232     }
00233 
00234     Result = DefineDosDevice(0,
00235                              Drive,
00236                              Path);
00237     if (! Result)
00238     {
00239         PrintError(GetLastError());
00240         return 1;
00241     }
00242     return 0;
00243 }
00244 
00245 int _tmain(int argc, TCHAR* argv[])
00246 {
00247     INT i;
00248     TCHAR szFmtString[RC_STRING_MAX_SIZE] = {0};
00249 
00250     for (i = 0; i < argc; i++)
00251     {
00252         if (!_tcsicmp(argv[i], _T("/?")))
00253         {
00254             DisplaySubstUsage();
00255             return 0;
00256         }
00257     }
00258 
00259     if (argc < 3)
00260     {
00261         if (argc >= 2)
00262         {
00263             LoadString(GetModuleHandle(NULL),
00264                        IDS_INVALID_PARAMETER,
00265                        szFmtString,
00266                        sizeof(szFmtString) / sizeof(szFmtString[0]));
00267             _tprintf(szFmtString,
00268                      argv[1]);
00269             return 1;
00270         }
00271         DumpSubstedDrives();
00272         return 0;
00273     }
00274 
00275     if (argc > 3)
00276     {
00277         LoadString(GetModuleHandle(NULL),
00278                    IDS_INCORRECT_PARAMETER_COUNT,
00279                    szFmtString,
00280                    sizeof(szFmtString) / sizeof(szFmtString[0]));
00281         _tprintf(szFmtString,
00282                  argv[3]);
00283         return 1;
00284     }
00285 
00286     if (! _tcsicmp(argv[1], _T("/D")))
00287         return DeleteSubst(argv[2]);
00288     if (! _tcsicmp(argv[2], _T("/D")))
00289         return DeleteSubst(argv[1]);
00290     return AddSubst(argv[1], argv[2]);
00291 }
00292 
00293 /* EOF */

Generated on Fri May 25 2012 04:17:03 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.