1 /* Copyright (C) 1999 Transarc Corporation - All rights reserved.
15 #include <WINNT/afsreg.h>
19 /* Some install/uninstall related utilities. */
22 #define NETWORK_PROVIDER_ORDER_KEY "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\NetworkProvider\\Order"
23 #define PROVIDER_ORDER_VALUE_NAME "ProviderOrder"
25 #define ENVIRONMENT_KEY "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment"
27 #define AUTOEXEC_PATH "c:\\autoexec.bat"
28 #define AUTOEXEC_TMP_PATH "c:\\TaAfsAutoexec.tmp"
31 static BOOL ReadRegEnv(char **ppszEnvValue, char *pszRegValueName);
32 static BOOL WriteRegEnv(char *pszEnvValue, char *pszEnvName);
33 static BOOL ReadAutoExec(char **ppszEnvValue, char *pszEnvName);
34 static BOOL WriteAutoExec(char *pszEnvValue, char *pszEnvName);
35 static BOOL FindSubString(const char *s1, const char *s2);
39 /* ------------------ exported functions ---------------------- */
41 BOOL InNetworkProviderOrder(char *pszNetworkProvider, BOOL *pbIn)
46 char *pszProviderOrder = 0;
49 bResult = FALSE; // Assume failure
51 if (RegOpenKeyAlt(AFSREG_NULL_KEY, NETWORK_PROVIDER_ORDER_KEY, KEY_READ, FALSE, &hKey, 0) == ERROR_SUCCESS) {
52 if (RegQueryValueAlt(hKey, PROVIDER_ORDER_VALUE_NAME, &dwType, &pszProviderOrder, &dwSize) == ERROR_SUCCESS) {
53 *pbIn = strstr(pszProviderOrder, pszNetworkProvider) != 0;
55 free(pszProviderOrder);
65 * AddToProviderOrder() -- add entry to network provider order
67 BOOL AddToProviderOrder(char *pszWhatToAdd)
75 BOOL bAlreadyAdded = FALSE;
77 /* Open the key, creating it if necessary (but should always be there). */
78 result = RegOpenKeyAlt(AFSREG_NULL_KEY,
79 NETWORK_PROVIDER_ORDER_KEY,
80 KEY_SET_VALUE | KEY_ALL_ACCESS, TRUE, &hKey, 0);
81 if (result != ERROR_SUCCESS)
84 /* Get the old value */
85 result = RegQueryValueAlt(hKey,
86 PROVIDER_ORDER_VALUE_NAME,
87 &dwType, &pszValue, &nLen);
88 if (result != ERROR_SUCCESS) {
92 pszNewValue = malloc(nLen + strlen(pszWhatToAdd) + 1);/* Add 1 for comma */
95 /* Add the new value */
96 if (result == ERROR_SUCCESS) {
97 if (strstr(pszValue, pszWhatToAdd) != 0)
100 if (pszValue && *pszValue) {
101 strcpy(pszNewValue, pszValue);
102 strcat(pszNewValue, ",");
104 strcat(pszNewValue, pszWhatToAdd);
107 } else if (result == ERROR_FILE_NOT_FOUND)
108 strcpy(pszNewValue, pszWhatToAdd);
110 /* Set the new value in the registry */
111 if (((result == ERROR_SUCCESS) ||
112 (result == ERROR_FILE_NOT_FOUND)) && !bAlreadyAdded)
113 result = RegSetValueEx(hKey, PROVIDER_ORDER_VALUE_NAME, 0,
114 REG_SZ, pszNewValue, strlen(pszNewValue) + 1);
120 return (result == ERROR_SUCCESS);
125 * RemoveFromProviderOrder() -- remove entry from network provider order
127 BOOL RemoveFromProviderOrder(char *pszWhatToDel)
135 BOOL bAlreadyRemoved = FALSE;
137 /* Open the key, creating if necessary (but should always be there). */
138 result = RegOpenKeyAlt(AFSREG_NULL_KEY, NETWORK_PROVIDER_ORDER_KEY,
139 KEY_SET_VALUE | KEY_ALL_ACCESS, TRUE, &hKey, 0);
140 if (result != ERROR_SUCCESS)
143 /* Get the old value */
144 result = RegQueryValueAlt(hKey, PROVIDER_ORDER_VALUE_NAME,
145 &dwType, &pszValue, &nLen);
147 if (result == ERROR_SUCCESS) {
148 pszNewValue = malloc(nLen); /* bigger than we need, but that's ok */
151 if (strstr(pszValue, pszWhatToDel) == 0)
152 bAlreadyRemoved = TRUE;
156 pszCur = strtok(pszValue, ",");
158 if (strcmp(pszCur, pszWhatToDel) != 0) {
160 strcat(pszNewValue, ",");
161 strcat(pszNewValue, pszCur);
163 pszCur = strtok(0, ",");
167 /* Set the new value in the registry */
168 if (!bAlreadyRemoved)
169 result = RegSetValueEx(hKey, PROVIDER_ORDER_VALUE_NAME, 0, REG_SZ,
170 pszNewValue, strlen(pszNewValue) + 1);
176 return (result == ERROR_SUCCESS);
181 * ReadSystemEnv() -- read system environment variable
183 BOOL ReadSystemEnv(char **ppszEnvValue, char *pszEnvName)
186 return ReadRegEnv(ppszEnvValue, pszEnvName);
188 return ReadAutoExec(ppszEnvValue, pszEnvName);
193 * WriteSystemEnv() -- write system environment variable
195 BOOL WriteSystemEnv(char *pszEnvValue, char *pszEnvName)
198 return WriteRegEnv(pszEnvValue, pszEnvName);
200 return WriteAutoExec(pszEnvValue, pszEnvName);
205 * AddToSystemPath() -- add specified entry to system PATH variable.
207 BOOL AddToSystemPath(char *pszPath)
209 char *pszCurPath = 0;
210 char *pszNewPath = 0;
213 if (!ReadSystemEnv(&pszCurPath, "Path"))
216 /* Do we need to add it? */
217 if (!pszCurPath || !FindSubString(pszCurPath, pszPath)) {
219 /* Old path + a semicolon + the new path entry + a null */
220 pszNewPath = malloc((pszCurPath ? strlen(pszCurPath) + 1 : 0) +
221 strlen(pszPath) + 1);
222 if (pszNewPath == 0) {
231 strcpy(pszNewPath, pszCurPath);
232 strcat(pszNewPath, ";");
235 strcat(pszNewPath, pszPath);
236 bStatus = WriteSystemEnv(pszNewPath, "Path");
249 * RemoveFromSystemPath() -- remove specified entry from system PATH variable.
251 BOOL RemoveFromSystemPath(char *pszPath)
259 if (!ReadSystemEnv(&pszCurNls, "Path"))
262 /* Is it already not in the path? */
263 if (!pszCurNls || !FindSubString(pszCurNls, pszPath)) {
269 pszNewNls = (char *)malloc(strlen(pszCurNls) + 1);
270 if (pszNewNls == 0) {
277 pCurPath = pszCurNls;
280 pSemi = strchr(pCurPath, ';');
284 if (_stricmp(pCurPath, pszPath) != 0) {
285 if (pszNewNls[0] != 0)
286 strcat(pszNewNls, ";");
287 strcat(pszNewNls, pCurPath);
297 bStatus = WriteSystemEnv(pszNewNls, "Path");
307 * IsWinNT() -- determine if system is NT or other (95/98).
311 DWORD dwVersion = GetVersion();
313 return (dwVersion < 0x80000000);
321 /* ------------------ utility functions ---------------------- */
325 * ReadRegEnv() -- read system enviornment variable from registry (NT only).
327 static BOOL ReadRegEnv(char **ppszEnvValue, char *pszRegValueName)
334 result = RegOpenKeyAlt(AFSREG_NULL_KEY, ENVIRONMENT_KEY,
335 KEY_SET_VALUE | KEY_ALL_ACCESS, FALSE, &hKey, 0);
336 if (result != ERROR_SUCCESS)
345 *ppszEnvValue = (char *)malloc(nLen);
346 if (*ppszEnvValue == 0) {
351 /* If function fails to open the value and the error code says that
352 * the value doesn't exist, then we will attempt to make it.
354 result = RegQueryValueEx(hKey, pszRegValueName, 0,
355 &dwType, *ppszEnvValue, &nLen);
357 if (result == ERROR_FILE_NOT_FOUND) {
358 result = RegSetValueEx(hKey, pszRegValueName, 0,
359 REG_EXPAND_SZ, "", 0);
360 **ppszEnvValue = '\0'; /* zero length string "read" */
362 } while (result == ERROR_MORE_DATA);
366 if (result != ERROR_SUCCESS || strlen(*ppszEnvValue) == 0) {
367 /* Don't return empty strings; instead set buffer pointer to 0. */
371 return (result == ERROR_SUCCESS);
376 * WriteRegEnv() -- write system environment variable to registry (NT only).
378 static BOOL WriteRegEnv(char *pszEnvValue, char *pszEnvName)
383 result = RegOpenKeyAlt(AFSREG_NULL_KEY, ENVIRONMENT_KEY,
384 KEY_ALL_ACCESS, FALSE, &hKey, 0);
385 if (result != ERROR_SUCCESS)
388 result = RegSetValueEx(hKey, pszEnvName, 0, REG_EXPAND_SZ,
389 pszEnvValue, strlen(pszEnvValue) + 1);
392 return (result == ERROR_SUCCESS);
397 * ReadAutoExec() -- read environment variable from autoexec.bat (95/98).
399 static BOOL ReadAutoExec(char **ppszEnvValue, char *pszEnvName)
407 fp = fopen(AUTOEXEC_PATH, "rt");
411 /* Create the string we are looking for */
412 sprintf(szSetCmd, "SET %s", pszEnvName);
414 /* Now read each line and look for our SetCmd string */
417 fgets(szLine, sizeof(szLine), fp);
422 /* Strip off the trailing newline */
423 nLineLen = strlen(szLine);
424 if (szLine[nLineLen - 1] == '\n') {
426 szLine[nLineLen] = 0;
429 if (_strnicmp(szSetCmd, szLine, strlen(szSetCmd)) == 0) {
430 char *value = strchr(szLine, '=');
432 *ppszEnvValue = _strdup(++value);
437 /* Don't return empty strings; instead set buffer to 0. */
438 if (*ppszEnvValue && (strlen(*ppszEnvValue) == 0)) {
450 * WriteAutoExec() -- write environment variable to autoexec.bat (95/98).
452 static BOOL WriteAutoExec(char *pszEnvValue, char *pszEnvName)
456 BOOL bValueWritten = FALSE;
460 fpOut = fopen(AUTOEXEC_TMP_PATH, "wt");
464 sprintf(szSetCmd, "SET %s", pszEnvName);
466 fpIn = fopen(AUTOEXEC_PATH, "rt");
468 /* Now read each line and look for our SetCmd string */
470 fgets(szLine, sizeof(szLine), fpIn);
474 if (!bValueWritten &&
475 (_strnicmp(szSetCmd, szLine, strlen(szSetCmd)) == 0)) {
476 fprintf(fpOut, "%s=%s\n", szSetCmd, pszEnvValue);
477 bValueWritten = TRUE;
479 fputs(szLine, fpOut);
485 /* If the value didn't previously exist, then add it to the end */
487 fprintf(fpOut, "%s=%s\n", szSetCmd, pszEnvValue);
491 bResult = CopyFile(AUTOEXEC_TMP_PATH, AUTOEXEC_PATH, FALSE);
493 /* Try to delete this even if the copy fails. Tie the return code
494 * to the copy and not the delete.
496 DeleteFile(AUTOEXEC_TMP_PATH);
503 * FindSubString() -- basically a case-insensitive strstr().
505 static BOOL FindSubString(const char *s1, const char *s2)
520 bFound = strstr(_strlwr(ls1), _strlwr(ls2)) != 0;