📄 main.c
字号:
lstrcat(cMsgBoxMsg, "Command explanations:\n"); lstrcat(cMsgBoxMsg, " add <path>\n"); lstrcat(cMsgBoxMsg, " Adding the path to the system's PATH environment variable\n"); lstrcat(cMsgBoxMsg, " remove <path>,\n"); lstrcat(cMsgBoxMsg, " Removing the path from the system's PATH environment "); lstrcat(cMsgBoxMsg, "variable\n\n"); lstrcat(cMsgBoxMsg, " * On the Windows 9x variations, the Autoexec.bat file are "); lstrcat(cMsgBoxMsg, "edited\n"); lstrcat(cMsgBoxMsg, " * On the Windows NT variations, the registry are edited. The "); lstrcat(cMsgBoxMsg, "program tries\n"); lstrcat(cMsgBoxMsg, " to edit the Environment in HKLM first. If that fails, then "); lstrcat(cMsgBoxMsg, "the Environment\n in HKCU are used.\n\n"); lstrcat(cMsgBoxMsg, " -h, --help: Print help (this page)\n\n"); lstrcat(cMsgBoxMsg, "Notes:\n"); lstrcat(cMsgBoxMsg, " * For playing safe: -Make sure that the given path allways is "); lstrcat(cMsgBoxMsg, "quoted between\n"); lstrcat(cMsgBoxMsg, " two \"'s wherewer the path contains spaces or not\n"); MessageBox(0,cMsgBoxMsg, cMsgBoxCaption , lMsgBoxFlag); return 0;}/*** svn_read_regval ***//* * Reading a registry value */intsvn_read_regval (HKEY hKey, char cValue[10], char cKey[BUFSIZE], char *pcPathCur[BUFSIZE], DWORD *lpType){ long lRet; DWORD dwBufLen; dwBufLen=BUFSIZE; /* Get the key value and put in pcPathCur */ lRet = RegOpenKeyExA(hKey, cKey, 0, KEY_READ, &hKey ); lRet = RegQueryValueExA(hKey, cValue, NULL, &*lpType, (LPBYTE) &**pcPathCur, &dwBufLen); RegCloseKey(hKey); if (lRet != 0) { return (1); } else { return (0); }}/*** svn_remove9x ***//* * Removing the path from the %PATH% environment in Autoexec.bat for Win-9x */intsvn_remove9x (char cPath[255]){ char cPathTmp[255]; FILE *FH_AUBAT, *FH_AUSVN; char cLineBuffer[255]; char cSvnLineBuffer[255]; int iCounter=0; int iAutoBatRo=0; lstrcpy (cPathTmp, cPath); if (! svn_svnpath_exists(cPathTmp)) { exit(1); } /* Make a backup of Autoexec.bat to Autoexec.svn if it exists, write the * svn stuff to Autoexec.bat */ if(_access(g_AuExBatFile, 0) != -1) { /* The file exists, so we make sure that we have write permission * before we continue*/ if((_access(g_AuExBatFile, 2 )) == -1) { _chmod(g_AuExBatFile, _S_IWRITE); iAutoBatRo=1; } /* Make the backup */ CopyFileA(g_AuExBatFile, g_AuExSvnFile, FALSE); } /* Open Autoexec.svn and parse it line by line. Save the new contents * to Autoexec.bat */ FH_AUSVN=fopen(g_AuExSvnFile, "rt"); FH_AUBAT=fopen(g_AuExBatFile, "wt"); /* Give cSvnLineBuffer the first line to remove from Autoexec.bat */ svn_set_auexlines(cPath); lstrcpy (cSvnLineBuffer, g_cSvnLineRem1); while(fgets(cLineBuffer, 255, FH_AUSVN) != NULL) { if (strstr (cLineBuffer, cSvnLineBuffer) == NULL) { fputs(cLineBuffer, FH_AUBAT); } else { iCounter++; switch (iCounter) { case 1: lstrcpy (cSvnLineBuffer, g_cSvnLineRem2); break; case 2: lstrcpy (cSvnLineBuffer, g_cSvnLinePath); break; } } } fclose(FH_AUSVN); fclose(FH_AUBAT); /* Turn back to Read only if that was the original state */ if (iAutoBatRo) { _chmod(g_AuExBatFile, _S_IREAD); } return 0;}/*** svn_removent ***//* * Removing the path from the %PATH% environment in the registry on Win-NT's */intsvn_removent (char cPathSvn[255]){ long lRet; char cPathTmp[BUFSIZE]; HKEY hKey; char cKey[BUFSIZE], cPathNew[BUFSIZE], cPathCur[BUFSIZE]; DWORD dwBufLen, lpType; char *pcPathCur[BUFSIZE]; char * pcSubPath; *pcPathCur=cPathCur; dwBufLen=BUFSIZE; lstrcpy (cPathTmp, cPathSvn); if (! svn_svnpath_exists(cPathTmp)) { exit (1); } lstrcpy(cKey, "SYSTEM\\CurrentControlSet\\"); lstrcat(cKey, "Control\\Session Manager\\Environment"); /* Get value, value type and current path from HKLM and try to append * the svnpath to it */ lRet = svn_read_regval(HKEY_LOCAL_MACHINE, "Path", cKey, &*pcPathCur, &lpType); /* Reopen the key for writing */ lRet = RegCreateKeyEx( HKEY_LOCAL_MACHINE, cKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dwBufLen); /* Remove the Subversion path from the system path and put the new path * on cPathNew*/ pcSubPath = strtok (cPathCur,";"); strcpy(cPathNew, ""); while (pcSubPath != NULL) { if (strcmp(pcSubPath, cPathSvn)) { if (strlen(cPathNew)==0) { lstrcpy(cPathNew, pcSubPath); } else { lstrcat(cPathNew, ";"); lstrcat(cPathNew, pcSubPath); } } pcSubPath = strtok (NULL, ";"); } lRet = RegSetValueExA(hKey, "Path", 0, lpType, (BYTE*)cPathNew, strlen(cPathNew)+1); RegCloseKey(hKey); /* If it went wrong to do it with HKLM, then try HKCU */ if (lRet != 0) { strcpy(cPathCur, ""); lRet = svn_read_regval(HKEY_CURRENT_USER, "Path", "Environment", &*pcPathCur, &lpType); pcSubPath = strtok (cPathCur,";"); strcpy(cPathNew, ""); while (pcSubPath != NULL) { if (strcmp(pcSubPath, cPathSvn)) { if (strlen(cPathNew)==0) { lstrcpy(cPathNew, pcSubPath); } else { lstrcat(cPathNew, ";"); lstrcat(cPathNew, pcSubPath); } } pcSubPath = strtok (NULL, ";"); } /* Reopen the key for writing */ lRet = RegCreateKeyEx( HKEY_CURRENT_USER, "Environment", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dwBufLen); lRet = RegSetValueExA(hKey, "Path", 0, lpType, (LPBYTE)cPathNew, strlen(cPathNew)+1); if (lRet != 0) { return (1); } RegCloseKey(hKey); } if (lRet != 0) { return (lRet); } else { /* Tell the system about the new path */ SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM) "Environment", SMTO_ABORTIFHUNG, 5000, &lRet); } return (0);}/*** svn_run_cmd ***//* * Running the ordinary command line when adding/removing a path */intsvn_run_cmd (char cAction[10], char cPath[255]){ int iRetVal=1; if (svn_os_is_nt()) { if (! strcmp(cAction, "add")) { iRetVal=svn_addnt(cPath); } else if (! strcmp(cAction, "remove")) { iRetVal=svn_removent(cPath); } } else { if (! strcmp(cAction, "add")) { iRetVal=svn_add9x(cPath); } else if (! strcmp(cAction, "remove")) { iRetVal=svn_remove9x(cPath); } } return (iRetVal);}/*** svn_set_auexlines ***//* * Filling the g_cSvnLine* variables with the svn contents of Autoexec.bat */intsvn_set_auexlines (char cPath[255]){ lstrcpy (g_cSvnLineRem1, "REM *** For Subversion: "); lstrcat (g_cSvnLineRem1, "Don't touch this and the two next lines ***\n"); lstrcpy (g_cSvnLineRem2, "REM *** They will be removed when Subversion is "); lstrcat (g_cSvnLineRem2, "uninstalled ***\n"); lstrcat (g_cSvnLinePath, "PATH=%PATH%;\""); lstrcat (g_cSvnLinePath, cPath); lstrcat (g_cSvnLinePath, "\"\n"); return 0;}/*** svn_svnpath_exists ***//* * Checking if the svn path is in the system's PATH. Returns 0 if not and 1 if * it already exists */intsvn_svnpath_exists (char cPath[255]){ char cSysPath[1024]; DWORD dwLenPath; int iRetVal=0; char * pcSubPath; dwLenPath = GetEnvironmentVariable("PATH", cSysPath, 1024); /* Split %PATH% to it's sub paths and compare each of them with cPath. */ if (dwLenPath) { pcSubPath = strtok (cSysPath,";"); while (pcSubPath != NULL) { if (! strcmp(strupr(pcSubPath), strupr(cPath)) && strlen(pcSubPath) == strlen(cPath)) { iRetVal = 1; break; } pcSubPath = strtok (NULL, ";"); } } else { exit (1); } return iRetVal;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -