xref: /reactos/base/shell/cmd/path.c (revision c2c66aff)
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     if (!_tcsncmp (param, _T("/?"), 2))
39     {
40         ConOutResPaging(TRUE,STRING_PATH_HELP1);
41         return 0;
42     }
43 
44     nErrorLevel = 0;
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         dwBuffer = GetEnvironmentVariable (_T("PATH"), pszBuffer, ENV_BUFFER_SIZE);
54         if (dwBuffer == 0)
55         {
56             cmd_free(pszBuffer);
57             ConOutResPrintf(STRING_VOL_HELP2, _T("PATH"));
58             return 0;
59         }
60         else if (dwBuffer > ENV_BUFFER_SIZE)
61         {
62             LPTSTR pszOldBuffer = pszBuffer;
63             pszBuffer = (LPTSTR)cmd_realloc (pszBuffer, dwBuffer * sizeof (TCHAR));
64             if (pszBuffer == NULL)
65             {
66                 cmd_free(pszOldBuffer);
67                 return 1;
68             }
69             GetEnvironmentVariable (_T("PATH"), pszBuffer, dwBuffer);
70         }
71 
72         ConOutPrintf(_T("PATH=%s\n"), pszBuffer);
73         cmd_free (pszBuffer);
74 
75         return 0;
76     }
77 
78     /* skip leading '=' */
79     if (*param == _T('='))
80         param++;
81 
82     /* set PATH environment variable */
83     if (!SetEnvironmentVariable (_T("PATH"), param))
84     {
85         nErrorLevel = 1;
86         return 1;
87     }
88 
89     return 0;
90 }
91 
92 #endif
93 
94 /* EOF */
95