Merge 14551:14980 from trunk
[reactos.git] / reactos / 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 * 30-Apr-2005 (Magnus Olsen) <magnus@greatlord.com>)
34 * Remove all hardcode string to En.rc
35 */
36
37 #include "precomp.h"
38 #include "resource.h"
39
40 #ifdef INCLUDE_CMD_SET
41
42
43 /* initial size of environment variable buffer */
44 #define ENV_BUFFER_SIZE 1024
45
46
47 INT cmd_set (LPTSTR cmd, LPTSTR param)
48 {
49 TCHAR szMsg[RC_STRING_MAX_SIZE];
50 LPTSTR p;
51
52 if (!_tcsncmp (param, _T("/?"), 2))
53 {
54 LoadString(GetModuleHandle(NULL), STRING_SET_HELP, szMsg, RC_STRING_MAX_SIZE);
55 ConOutPuts(szMsg);
56 return 0;
57 }
58
59 /* if no parameters, show the environment */
60 if (param[0] == _T('\0'))
61 {
62 LPTSTR lpEnv;
63 LPTSTR lpOutput;
64 INT len;
65
66 lpEnv = (LPTSTR)GetEnvironmentStrings ();
67 if (lpEnv)
68 {
69 lpOutput = lpEnv;
70 while (*lpOutput)
71 {
72 len = _tcslen(lpOutput);
73 if (len)
74 {
75 if (*lpOutput != _T('='))
76 ConOutPuts (lpOutput);
77 lpOutput += (len + 1);
78 }
79 }
80 FreeEnvironmentStrings (lpEnv);
81 }
82
83 return 0;
84 }
85
86 p = _tcschr (param, _T('='));
87 if (p)
88 {
89 /* set or remove environment variable */
90 *p = _T('\0');
91 p++;
92 if (*p == _T('\0'))
93 {
94 p = NULL;
95 }
96 SetEnvironmentVariable (param, p);
97 }
98 else
99 {
100 /* display environment variable */
101 LPTSTR pszBuffer;
102 DWORD dwBuffer;
103
104 pszBuffer = (LPTSTR)malloc (ENV_BUFFER_SIZE * sizeof(TCHAR));
105 dwBuffer = GetEnvironmentVariable (param, pszBuffer, ENV_BUFFER_SIZE);
106 if (dwBuffer == 0)
107 {
108 ConErrPrintf (_T("CMD: Not in environment \"%s\"\n"), param);
109 return 0;
110 }
111 else if (dwBuffer > ENV_BUFFER_SIZE)
112 {
113 pszBuffer = (LPTSTR)realloc (pszBuffer, dwBuffer * sizeof (TCHAR));
114 GetEnvironmentVariable (param, pszBuffer, dwBuffer);
115 }
116 ConOutPrintf (_T("%s\n"), pszBuffer);
117
118 free (pszBuffer);
119
120 return 0;
121 }
122
123 return 0;
124 }
125
126 #endif