update for HEAD-2003091401
[reactos.git] / subsys / system / cmd / set.c
1 /*
2  *  SET.C - set internal command.
3  *
4  *
5  *  History:
6  *
7  *    06/14/97 (Tim Norman)
8  *        changed static var in set() to a malloc'd space to pass to putenv.
9  *        need to find a better way to do this, since it seems it is wasting
10  *        memory when variables are redefined.
11  *
12  *    07/08/1998 (John P. Price)
13  *        removed call to show_environment in set command.
14  *        moved test for syntax before allocating memory in set command.
15  *        misc clean up and optimization.
16  *
17  *    27-Jul-1998 (John P Price <linux-guru@gcfl.net>)
18  *        added config.h include
19  *
20  *    28-Jul-1998 (John P Price <linux-guru@gcfl.net>)
21  *        added set_env function to set env. variable without needing set command
22  *
23  *    09-Dec-1998 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
24  *        Added help text ("/?").
25  *
26  *    24-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
27  *        Fixed Win32 environment handling.
28  *        Unicode and redirection safe!
29  *
30  *    25-Feb-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
31  *        Fixed little bug.
32  */
33
34 #include "config.h"
35
36 #ifdef INCLUDE_CMD_SET
37
38 #include <windows.h>
39 #include <tchar.h>
40 #include <string.h>
41 #include <stdlib.h>
42
43 #include "cmd.h"
44
45
46 /* initial size of environment variable buffer */
47 #define ENV_BUFFER_SIZE  1024
48
49
50 INT cmd_set (LPTSTR cmd, LPTSTR param)
51 {
52         LPTSTR p;
53
54         if (!_tcsncmp (param, _T("/?"), 2))
55         {
56                 ConOutPuts (_T("Displays, sets, or removes environment variables.\n\n"
57                                            "SET [variable[=][string]]\n\n"
58                                            "  variable  Specifies the environment-variable name.\n"
59                                            "  string    Specifies a series of characters to assign to the variable.\n\n"
60                                            "Type SET without parameters to display the current environment variables.\n"));
61                 return 0;
62         }
63
64         /* if no parameters, show the environment */
65         if (param[0] == _T('\0'))
66         {
67                 LPTSTR lpEnv;
68                 LPTSTR lpOutput;
69                 INT len;
70
71                 lpEnv = (LPTSTR)GetEnvironmentStrings ();
72                 if (lpEnv)
73                 {
74                         lpOutput = lpEnv;
75                         while (*lpOutput)
76                         {
77                                 len = _tcslen(lpOutput);
78                                 if (len)
79                                 {
80                                         if (*lpOutput != _T('='))
81                                                 ConOutPuts (lpOutput);
82                                         lpOutput += (len + 1);
83                                 }
84                         }
85                         FreeEnvironmentStrings (lpEnv);
86                 }
87
88                 return 0;
89         }
90
91         p = _tcschr (param, _T('='));
92         if (p)
93         {
94                 /* set or remove environment variable */
95                 *p = _T('\0');
96                 p++;
97
98                 SetEnvironmentVariable (param, p);
99         }
100         else
101         {
102                 /* display environment variable */
103                 LPTSTR pszBuffer;
104                 DWORD dwBuffer;
105
106                 pszBuffer = (LPTSTR)malloc (ENV_BUFFER_SIZE * sizeof(TCHAR));
107                 dwBuffer = GetEnvironmentVariable (param, pszBuffer, ENV_BUFFER_SIZE);
108                 if (dwBuffer == 0)
109                 {
110                         ConErrPrintf (_T("CMD: Not in environment \"%s\"\n"), param);
111                         return 0;
112                 }
113                 else if (dwBuffer > ENV_BUFFER_SIZE)
114                 {
115                         pszBuffer = (LPTSTR)realloc (pszBuffer, dwBuffer * sizeof (TCHAR));
116                         GetEnvironmentVariable (param, pszBuffer, dwBuffer);
117                 }
118                 ConOutPrintf (_T("%s\n"), pszBuffer);
119
120                 free (pszBuffer);
121
122                 return 0;
123         }
124
125         return 0;
126 }
127
128 #endif