xref: /reactos/base/shell/cmd/path.c (revision 2b933529)
1 /*
2  *  PATH.C - path internal command.
3  *
4  *
5  *  History:
6  *
7  *    17 Jul 1998 (John P Price)
8  *        Separated commands into individual files.
9  *
10  *    27-Jul-1998 (John P Price <linux-guru@gcfl.net>)
11  *        added config.h include
12  *
13  *    09-Dec-1998 (Eric Kohl)
14  *        Added help text ("/?").
15  *
16  *    18-Jan-1999 (Eric Kohl)
17  *        Unicode ready!
18  *
19  *    18-Jan-1999 (Eric Kohl)
20  *        Redirection safe!
21  *
22  *    24-Jan-1999 (Eric Kohl)
23  *        Fixed Win32 environment handling.
24  *
25  *    30-Apr-2005 (Magnus Olsen <magnus@greatlord.com>)
26  *        Remove all hardcoded strings in En.rc
27  */
28 #include "precomp.h"
29 
30 #ifdef INCLUDE_CMD_PATH
31 
32 /* Size of environment variable buffer */
33 #define ENV_BUFFER_SIZE 1024
34 
35 
36 INT cmd_path(LPTSTR param)
37 {
38     INT retval = 0;
39 
40     if (!_tcsncmp(param, _T("/?"), 2))
41     {
42         ConOutResPaging(TRUE, STRING_PATH_HELP1);
43         return 0;
44     }
45 
46     /* If param is empty, display the PATH environment variable */
47     if (!param || !*param)
48     {
49         DWORD  dwBuffer;
50         LPTSTR pszBuffer;
51 
52         pszBuffer = (LPTSTR)cmd_alloc(ENV_BUFFER_SIZE * sizeof(TCHAR));
53         if (!pszBuffer)
54         {
55             WARN("Cannot allocate memory for pszBuffer!\n");
56             error_out_of_memory();
57             retval = 1;
58             goto Quit;
59         }
60 
61         dwBuffer = GetEnvironmentVariable(_T("PATH"), pszBuffer, ENV_BUFFER_SIZE);
62         if (dwBuffer == 0)
63         {
64             cmd_free(pszBuffer);
65             ConErrResPrintf(STRING_SET_ENV_ERROR, _T("PATH"));
66             retval = 0;
67             goto Quit;
68         }
69         else if (dwBuffer > ENV_BUFFER_SIZE)
70         {
71             LPTSTR pszOldBuffer = pszBuffer;
72             pszBuffer = (LPTSTR)cmd_realloc(pszBuffer, dwBuffer * sizeof (TCHAR));
73             if (!pszBuffer)
74             {
75                 WARN("Cannot reallocate memory for pszBuffer!\n");
76                 error_out_of_memory();
77                 cmd_free(pszOldBuffer);
78                 retval = 1;
79                 goto Quit;
80             }
81             GetEnvironmentVariable(_T("PATH"), pszBuffer, dwBuffer);
82         }
83 
84         ConOutPrintf(_T("PATH=%s\n"), pszBuffer);
85         cmd_free(pszBuffer);
86 
87         retval = 0;
88         goto Quit;
89     }
90 
91     /* Skip leading '=' */
92     if (*param == _T('='))
93         param++;
94 
95     /* Set PATH environment variable */
96     if (!SetEnvironmentVariable(_T("PATH"), param))
97     {
98         retval = 1;
99     }
100 
101 Quit:
102     if (BatType != CMD_TYPE)
103     {
104         if (retval != 0)
105             nErrorLevel = retval;
106     }
107     else
108     {
109         nErrorLevel = retval;
110     }
111 
112     return retval;
113 }
114 
115 #endif
116 
117 /* EOF */
118