update for HEAD-2003091401
[reactos.git] / subsys / system / cmd / path.c
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 <ekohl@abo.rhein-zeitung.de>)
14  *        Added help text ("/?").
15  *
16  *    18-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
17  *        Unicode ready!
18  *
19  *    18-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
20  *        Redirection safe!
21  *
22  *    24-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
23  *        Fixed Win32 environment handling.
24  */
25
26 #include "config.h"
27
28 #ifdef INCLUDE_CMD_PATH
29
30 #include <windows.h>
31 #include <tchar.h>
32 #include <string.h>
33 #include <stdlib.h>
34
35 #include "cmd.h"
36
37
38 /* size of environment variable buffer */
39 #define ENV_BUFFER_SIZE 1024
40
41
42 INT cmd_path (LPTSTR cmd, LPTSTR param)
43 {
44         if (!_tcsncmp (param, _T("/?"), 2))
45         {
46                 ConOutPuts (_T("Displays or sets a search path for executable files.\n\n"
47                                    "PATH [[drive:]path[;...]]\nPATH ;\n\n"
48                                    "Type PATH ; to clear all search-path settings and direct the command shell\n"
49                                    "to search only in the current directory.\n"
50                                    "Type PATH without parameters to display the current path.\n"));
51                 return 0;
52         }
53
54         /* if param is empty, display the PATH environment variable */
55         if (!param || !*param)
56         {
57                 DWORD  dwBuffer;
58                 LPTSTR pszBuffer;
59
60                 pszBuffer = (LPTSTR)malloc (ENV_BUFFER_SIZE * sizeof(TCHAR));
61                 dwBuffer = GetEnvironmentVariable (_T("PATH"), pszBuffer, ENV_BUFFER_SIZE);
62                 if (dwBuffer == 0)
63                 {
64                         ConErrPrintf (_T("CMD: Not in environment \"PATH\"\n"));
65                         return 0;
66                 }
67                 else if (dwBuffer > ENV_BUFFER_SIZE)
68                 {
69                         pszBuffer = (LPTSTR)realloc (pszBuffer, dwBuffer * sizeof (TCHAR));
70                         GetEnvironmentVariable (_T("PATH"), pszBuffer, ENV_BUFFER_SIZE);
71                 }
72
73                 ConOutPrintf (_T("PATH=%s\n"), pszBuffer);
74                 free (pszBuffer);
75
76                 return 0;
77         }
78
79         /* skip leading '=' */
80         if (*param == _T('='))
81                 param++;
82
83         /* set PATH environment variable */
84         if (!SetEnvironmentVariable (_T("PATH"), param))
85                 return 1;
86
87         return 0;
88 }
89
90 #endif