ReactOS  0.4.15-dev-1152-g6c94e4f
path.c
Go to the documentation of this file.
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 
37 {
38  INT retval = 0;
39 
40  if (!_tcsncmp(param, _T("/?"), 2))
41  {
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");
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);
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");
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 */
INT nErrorLevel
Definition: cmd.c:158
#define TRUE
Definition: types.h:120
#define WARN(fmt,...)
Definition: debug.h:112
#define ConOutPrintf(szStr,...)
Definition: console.h:42
VOID ConOutResPaging(BOOL StartPaging, UINT resID)
Definition: console.c:182
int32_t INT
Definition: typedefs.h:58
CHAR * LPTSTR
Definition: xmlstorage.h:192
#define STRING_SET_ENV_ERROR
Definition: resource.h:55
#define ConErrResPrintf(uID,...)
Definition: console.h:51
VOID error_out_of_memory(VOID)
Definition: error.c:138
INT cmd_path(LPTSTR param)
Definition: path.c:36
Definition: batch.h:19
char TCHAR
Definition: xmlstorage.h:189
#define _T(x)
Definition: vfdio.h:22
BATCH_TYPE BatType
Definition: batch.c:66
GLfloat param
Definition: glext.h:5796
unsigned long DWORD
Definition: ntddk_ex.h:95
#define cmd_realloc(ptr, size)
Definition: cmddbg.h:30
#define SetEnvironmentVariable
Definition: winbase.h:3748
#define cmd_alloc(size)
Definition: cmddbg.h:29
#define cmd_free(ptr)
Definition: cmddbg.h:31
#define GetEnvironmentVariable
Definition: winbase.h:3654
#define ENV_BUFFER_SIZE
Definition: path.c:33
#define STRING_PATH_HELP1
Definition: resource.h:151
int _tcsncmp(const _TCHAR *s1, const _TCHAR *s2, size_t n)
Definition: tcsncmp.h:9