remove whitespace from end of lines
[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 ConOutResPuts(STRING_SET_HELP);
55 return 0;
56 }
57
58 /* if no parameters, show the environment */
59 if (param[0] == _T('\0'))
60 {
61 LPTSTR lpEnv;
62 LPTSTR lpOutput;
63 INT len;
64
65 lpEnv = (LPTSTR)GetEnvironmentStrings ();
66 if (lpEnv)
67 {
68 lpOutput = lpEnv;
69 while (*lpOutput)
70 {
71 len = _tcslen(lpOutput);
72 if (len)
73 {
74 if (*lpOutput != _T('='))
75 ConOutPuts (lpOutput);
76 lpOutput += (len + 1);
77 }
78 }
79 FreeEnvironmentStrings (lpEnv);
80 }
81
82 return 0;
83 }
84
85 p = _tcschr (param, _T('='));
86 if (p)
87 {
88 /* set or remove environment variable */
89 *p = _T('\0');
90 p++;
91 if (*p == _T('\0'))
92 {
93 p = NULL;
94 }
95 SetEnvironmentVariable (param, p);
96 }
97 else
98 {
99 /* display environment variable */
100 LPTSTR pszBuffer;
101 DWORD dwBuffer;
102
103 pszBuffer = (LPTSTR)malloc (ENV_BUFFER_SIZE * sizeof(TCHAR));
104 dwBuffer = GetEnvironmentVariable (param, pszBuffer, ENV_BUFFER_SIZE);
105 if (dwBuffer == 0)
106 {
107 LoadString(CMD_ModuleHandle, STRING_PATH_ERROR, szMsg, RC_STRING_MAX_SIZE);
108 ConErrPrintf (szMsg, 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