Added Eric Kohl's port of freedos command
[reactos.git] / reactos / apps / utils / 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
31 #define WIN32_LEAN_AND_MEAN
32
33 #include "config.h"
34
35 #ifdef INCLUDE_CMD_SET
36
37 #include <windows.h>
38 #include <tchar.h>
39 #include <string.h>
40 #include <stdlib.h>
41
42 #include "cmd.h"
43
44
45 /* size of environment variable buffer */
46 #define ENV_BUFFER_SIZE 1024
47
48
49 INT cmd_set (LPTSTR cmd, LPTSTR param)
50 {
51 LPTSTR p;
52
53 if (!_tcsncmp (param, _T("/?"), 2))
54 {
55 ConOutPuts (_T("Displays, sets, or removes environment variables.\n\n"
56 "SET [variable[=][string]]\n\n"
57 " variable Specifies the environment-variable name.\n"
58 " string Specifies a series of characters to assign to the variable.\n\n"
59 "Type SET without parameters to display the current environment variables.\n"));
60 return 0;
61 }
62
63 /* if no parameters, show the environment */
64 if (param[0] == _T('\0'))
65 {
66 LPTSTR lpEnv;
67 LPTSTR lpOutput;
68 INT len;
69
70 lpEnv = (LPTSTR)GetEnvironmentStrings ();
71 if (lpEnv)
72 {
73 lpOutput = lpEnv;
74 while (*lpOutput)
75 {
76 len = _tcslen(lpOutput);
77 if (len)
78 {
79 if (*lpOutput != _T('='))
80 ConOutPuts (lpOutput);
81 lpOutput += (len + 1);
82 }
83 }
84 FreeEnvironmentStrings (lpEnv);
85 }
86
87 return 0;
88 }
89
90 p = _tcschr (param, _T('='));
91 if (p)
92 {
93 /* set or remove environment variable */
94 *p = _T('\0');
95 p++;
96
97 SetEnvironmentVariable (param, p);
98 }
99 else
100 {
101 /* display environment variable */
102 LPTSTR pszBuffer;
103 DWORD dwBuffer;
104
105 pszBuffer = (LPTSTR)malloc (ENV_BUFFER_SIZE * sizeof(TCHAR));
106 dwBuffer = GetEnvironmentVariable (param, pszBuffer, ENV_BUFFER_SIZE);
107 if (dwBuffer == 0)
108 {
109 ConErrPrintf ("CMD: Not in environment \"%s\"\n", param);
110 return 0;
111 }
112 else if (dwBuffer > ENV_BUFFER_SIZE)
113 {
114 pszBuffer = (LPTSTR)realloc (pszBuffer, dwBuffer * sizeof (TCHAR));
115 GetEnvironmentVariable (param, pszBuffer, ENV_BUFFER_SIZE);
116 }
117
118 ConOutPrintf ("%s\n", pszBuffer);
119 free (pszBuffer);
120
121 return 0;
122 }
123
124 return 0;
125 }
126
127 #endif