BOOL PerformRegAction(REGEDIT_ACTION action, LPSTR s) { TCHAR filename[MAX_PATH]; TCHAR reg_key_name[KEY_MAX_LEN]; switch (action) { case ACTION_ADD: get_file_name(&s, filename, MAX_PATH); if (!filename[0]) { printf("No file name is specified\n%s", usage); return FALSE; //exit(1); } while (filename[0]) { if (!import_registry_file(filename)) { perror(""); printf("Can't open file \"%s\"\n", filename); return FALSE; //exit(1); } get_file_name(&s, filename, MAX_PATH); } break; case ACTION_DELETE: get_file_name(&s, reg_key_name, KEY_MAX_LEN); if (!reg_key_name[0]) { printf("No registry key is specified for removal\n%s", usage); return FALSE; //exit(1); } delete_registry_key(reg_key_name); break; case ACTION_EXPORT: filename[0] = '\0'; get_file_name(&s, filename, MAX_PATH); if (!filename[0]) { printf("No file name is specified\n%s", usage); return FALSE; //exit(1); } if (s[0]) { get_file_name(&s, reg_key_name, KEY_MAX_LEN); export_registry_key(filename, reg_key_name); } else { export_registry_key(filename, NULL); } break; default: printf("Unhandled action!\n"); return FALSE; } return TRUE; }
/****************************************************************************** * This function receives the currently read entry and performs the * corresponding action. * isUnicode affects parsing of REG_MULTI_SZ values */ static void processRegEntry(WCHAR* stdInput, BOOL isUnicode) { /* * We encountered the end of the file, make sure we * close the opened key and exit */ if (stdInput == NULL) { closeKey(); return; } if ( stdInput[0] == '[') /* We are reading a new key */ { WCHAR* keyEnd; closeKey(); /* Close the previous key */ /* Get rid of the square brackets */ stdInput++; keyEnd = strrchrW(stdInput, ']'); if (keyEnd) *keyEnd='\0'; /* delete the key if we encounter '-' at the start of reg key */ if ( stdInput[0] == '-') { delete_registry_key(stdInput + 1); } else if ( openKeyW(stdInput) != ERROR_SUCCESS ) { char* stdInputA = GetMultiByteString(stdInput); fprintf(stderr,"%s: setValue failed to open key %s\n", getAppName(), stdInputA); HeapFree(GetProcessHeap(), 0, stdInputA); } } else if( currentKeyHandle && (( stdInput[0] == '@') || /* reading a default @=data pair */ ( stdInput[0] == '\"'))) /* reading a new value=data pair */ { processSetValue(stdInput, isUnicode); } else { /* Since we are assuming that the file format is valid we must be * reading a blank line which indicates the end of this key processing */ closeKey(); } }
/****************************************************************************** * This function receives the currently read entry and performs the * corresponding action. * isUnicode affects parsing of REG_MULTI_SZ values */ static void processRegEntry(WCHAR* stdInput, BOOL isUnicode) { /* * We encountered the end of the file, make sure we * close the opened key and exit */ if (stdInput == NULL) { closeKey(); return; } if ( stdInput[0] == '[') /* We are reading a new key */ { WCHAR* keyEnd; closeKey(); /* Close the previous key */ /* Get rid of the square brackets */ stdInput++; keyEnd = strrchrW(stdInput, ']'); if (keyEnd) *keyEnd='\0'; /* delete the key if we encounter '-' at the start of reg key */ if (stdInput[0] == '-') delete_registry_key(stdInput + 1); else if (openKeyW(stdInput) != ERROR_SUCCESS) output_message(STRING_OPEN_KEY_FAILED, stdInput); } else if( currentKeyHandle && (( stdInput[0] == '@') || /* reading a default @=data pair */ ( stdInput[0] == '\"'))) /* reading a new value=data pair */ { processSetValue(stdInput, isUnicode); } else { /* Since we are assuming that the file format is valid we must be * reading a blank line which indicates the end of this key processing */ closeKey(); } }
static BOOL PerformRegAction(REGEDIT_ACTION action, LPSTR s) { switch (action) { case ACTION_ADD: { CHAR filename[MAX_PATH]; FILE *reg_file; get_file_name(&s, filename); if (!filename[0]) { fprintf(stderr,"%s: No file name was specified\n", getAppName()); fprintf(stderr,usage); exit(1); } while(filename[0]) { char* realname = NULL; if (strcmp(filename, "-") == 0) { reg_file=stdin; } else { int size; size=SearchPath(NULL, filename, NULL,0, NULL, NULL); if (size>0) { realname=HeapAlloc(GetProcessHeap(), 0, size); size=SearchPath(NULL, filename, NULL, size, realname, NULL); } if (size==0) { fprintf(stderr, "%s: File not found \"%s\" (%d)\n", getAppName(), filename, GetLastError()); exit(1); } reg_file = fopen(realname, "r"); if (reg_file==NULL) { perror(""); fprintf(stderr,"%s: Can't open file \"%s\"\n", getAppName(), filename); exit(1); } } import_registry_file(reg_file); if (realname) { HeapFree(GetProcessHeap(),0,realname); fclose(reg_file); } get_file_name(&s, filename); } break; } case ACTION_DELETE: { CHAR reg_key_name[KEY_MAX_LEN]; get_file_name(&s, reg_key_name); if (!reg_key_name[0]) { fprintf(stderr,"%s: No registry key was specified for removal\n", getAppName()); fprintf(stderr,usage); exit(1); } else { WCHAR* reg_key_nameW = GetWideString(reg_key_name); delete_registry_key(reg_key_nameW); HeapFree(GetProcessHeap(), 0, reg_key_nameW); } break; } case ACTION_EXPORT: { CHAR filename[MAX_PATH]; WCHAR* filenameW; filename[0] = '\0'; get_file_name(&s, filename); if (!filename[0]) { fprintf(stderr,"%s: No file name was specified\n", getAppName()); fprintf(stderr,usage); exit(1); } filenameW = GetWideString(filename); if (s[0]) { CHAR reg_key_name[KEY_MAX_LEN]; WCHAR* reg_key_nameW; get_file_name(&s, reg_key_name); reg_key_nameW = GetWideString(reg_key_name); export_registry_key(filenameW, reg_key_nameW, REG_FORMAT_4); HeapFree(GetProcessHeap(), 0, reg_key_nameW); } else { export_registry_key(filenameW, NULL, REG_FORMAT_4); } HeapFree(GetProcessHeap(), 0, filenameW); break; } default: fprintf(stderr,"%s: Unhandled action!\n", getAppName()); exit(1); break; } return TRUE; }
BOOL PerformRegAction(REGEDIT_ACTION action, LPWSTR s, BOOL silent) { switch (action) { case ACTION_ADD: { WCHAR szTitle[512], szText[512]; WCHAR filename[MAX_PATH]; FILE *fp; get_file_name(&s, filename); if (!filename[0]) { InfoMessageBox(NULL, MB_OK | MB_ICONINFORMATION, NULL, L"No file name is specified."); InfoMessageBox(NULL, MB_OK | MB_ICONINFORMATION, NULL, usage); exit(4); } LoadStringW(hInst, IDS_APP_TITLE, szTitle, COUNT_OF(szTitle)); while (filename[0]) { /* Request import confirmation */ if (!silent) { int choice; LoadStringW(hInst, IDS_IMPORT_PROMPT, szText, COUNT_OF(szText)); choice = InfoMessageBox(NULL, MB_YESNOCANCEL | MB_ICONWARNING, szTitle, szText, filename); switch (choice) { case IDNO: goto cont; case IDCANCEL: /* The cancel case is useful if the user is importing more than one registry file at a time, and wants to back out anytime during the import process. This way, the user doesn't have to resort to ending the regedit process abruptly just to cancel the operation. */ return TRUE; default: break; } } /* Open the file */ fp = _wfopen(filename, L"r"); /* Import it */ if (fp == NULL || !import_registry_file(fp)) { /* Error opening the file */ if (!silent) { LoadStringW(hInst, IDS_IMPORT_ERROR, szText, COUNT_OF(szText)); InfoMessageBox(NULL, MB_OK | MB_ICONERROR, szTitle, szText, filename); } } else { /* Show successful import */ if (!silent) { LoadStringW(hInst, IDS_IMPORT_OK, szText, COUNT_OF(szText)); InfoMessageBox(NULL, MB_OK | MB_ICONINFORMATION, szTitle, szText, filename); } } /* Close the file */ if (fp) fclose(fp); cont: get_file_name(&s, filename); } break; } case ACTION_DELETE: { WCHAR reg_key_name[KEY_MAX_LEN]; get_file_name(&s, reg_key_name); if (!reg_key_name[0]) { InfoMessageBox(NULL, MB_OK | MB_ICONINFORMATION, NULL, L"No registry key is specified for removal."); InfoMessageBox(NULL, MB_OK | MB_ICONINFORMATION, NULL, usage); exit(6); } delete_registry_key(reg_key_name); break; } case ACTION_EXPORT: { WCHAR filename[MAX_PATH]; filename[0] = L'\0'; get_file_name(&s, filename); if (!filename[0]) { InfoMessageBox(NULL, MB_OK | MB_ICONINFORMATION, NULL, L"No file name is specified."); InfoMessageBox(NULL, MB_OK | MB_ICONINFORMATION, NULL, usage); exit(7); } if (s[0]) { WCHAR reg_key_name[KEY_MAX_LEN]; get_file_name(&s, reg_key_name); export_registry_key(filename, reg_key_name, REG_FORMAT_4); } else { export_registry_key(filename, NULL, REG_FORMAT_4); } break; } default: fwprintf(stderr, L"%s: Unhandled action!\n", getAppName()); exit(8); break; } return TRUE; }
/** * Main entry for program. * * @param argc Number of command line arguments * @param argv Command line arguments * @return 0 on success */ int main (int argc, char* argv[]) { char *option; int result; char *sparam1=NULL, *sparam2, *sparam3, *sparam4; char *sparam5, *sparam6, *sparam7, *sparam8, *sparam9; #ifdef _WIN32 char path[MAX_PATH]; #endif int iparam1, iparam2; long lparam1; /**************************************************************************** * Show program help ****************************************************************************/ if (get_option (argv, argc, "--help", NULL) == 0) { usage (stdout); exit (0); } /**************************************************************************** * Read command line options from file ****************************************************************************/ if (get_option (argv, argc, "-file", &option) == 0) { result = read_file_options (option); free (option); if (result == -1) fail ("Failed to read options file: %s.", option); } /**************************************************************************** * Enable logging ****************************************************************************/ if (get_option (argv, argc, "-log", &option) == 0) { logFile = fopen (option, "a"); if (logFile == NULL) fail ("Failed to write log file: %s", option); free (option); } /**************************************************************************** * Wait for process to terminate ****************************************************************************/ if (get_option (argv, argc, "-pid", &option) == 0) { iparam1 = -1; lparam1 = atoi (option); free (option); if (get_option (argv, argc, "-wait", &option) == 0) { iparam1 = atoi (option); } if (wait_for_pid (lparam1, iparam1) != 0) fail ("Timed out waiting for process to terminate."); free (option); } /**************************************************************************** * Remove directories ****************************************************************************/ if (get_option (argv, argc, "-removeDir", &option) == 0) { result = delete_directories (option, 0); if (result != 0) fail ("Failed to delete directories: %s", option); else log_message ("-removeDir %s", option); free (option); } /**************************************************************************** * Remove directories if they are empty ****************************************************************************/ if (get_option (argv, argc, "-removeEmptyDir", &option) == 0) { result = delete_directories (option, 1); if (result != 0) fail ("Failed to delete empty directories: %s", option); else log_message ("-removeEmptyDir %s", option); free (option); } /**************************************************************************** * Set registry value ****************************************************************************/ if (get_option (argv, argc, "-regSetValue", &option) == 0) { #ifdef _WIN32 sparam1 = get_argument (option); sparam2 = get_argument (NULL); sparam3 = get_argument (NULL); sparam4 = get_argument (NULL); result = set_registry_value (sparam1, sparam2, sparam3, sparam4); if (result != 0) fail ("Failed to set registry value."); else log_message ("-regSetValue %s, %s, %s, %s", sparam1, sparam2, sparam3, sparam4); free (option); #else fail ("-regSetValue is only supported on Windows."); #endif } /**************************************************************************** * Get registry value ****************************************************************************/ if (get_option (argv, argc, "-regGetValue", &option) == 0) { #ifdef _WIN32 sparam1 = get_argument (option); sparam2 = get_argument (NULL); sparam3 = get_registry_value (sparam1, sparam2); if (sparam3) puts (sparam3); else fail ("Failed to get registry value %s, %s.", sparam1, sparam2); free (option); #else fail ("-regSetValue is only supported on Windows."); #endif } /**************************************************************************** * Delete registry value ****************************************************************************/ if (get_option (argv, argc, "-regDeleteValue", &option) == 0) { #ifdef _WIN32 sparam1 = get_argument (option); sparam2 = get_argument (NULL); result = delete_registry_value (sparam1, sparam2); if (result != 0) fail ("Failed to delete registry value."); else log_message ("-regDeleteValue %s, %s", sparam1, sparam2); free (option); #else fail ("-regDeleteValue is only supported on Windows."); #endif } /**************************************************************************** * Delete registry key ****************************************************************************/ if (get_option (argv, argc, "-regDeleteKey", &option) == 0) { #ifdef _WIN32 result = delete_registry_key (option); if (result != 0) fail ("Failed to delete registry key."); else log_message ("-regDeleteKey %s", option); free (option); #else fail ("-regDeleteKey is only supported on Windows."); #endif } /**************************************************************************** * Run as administrator ****************************************************************************/ if (get_option (argv, argc, "-runAdmin", &option) == 0) { #ifdef _WIN32 sparam1 = get_argument (option); sparam2 = get_argument (NULL); result = -1; if ((sparam1 != NULL) && (sparam2 != NULL)) { replace_chars (sparam2, '\'', '"'); result = run_admin (sparam1, sparam2); } if (result != 0) fail ("Failed to run program."); else log_message ("-runAdmin %s,%s", sparam1, sparam2); free (option); #else fail ("-runAdmin is currently only supported on Windows."); #endif } /**************************************************************************** * Create short-cut ****************************************************************************/ if (get_option (argv, argc, "-createShortcut", &option) == 0) { sparam1 = get_argument (option); sparam2 = get_argument (NULL); sparam3 = get_argument (NULL); sparam4 = get_argument (NULL); sparam5 = get_argument (NULL); sparam6 = get_argument (NULL); sparam7 = get_argument (NULL); sparam8 = get_argument (NULL); sparam9 = get_argument (NULL); if (strlen (sparam6) != 0) iparam1 = atoi (sparam6); else iparam1 = 0; if (strlen (sparam9) != 0) iparam2 = atoi (sparam9); else iparam2 = 0; result = create_shortcut (sparam1, sparam2, sparam3, sparam4, sparam5, iparam1, sparam7, sparam8, iparam2); if (result != 0) fail ("Failed to create shortcut."); else log_message ("-createShortcut %s, %s, %s, %s, %s, %s, %s, %s %s", sparam1, sparam2, sparam3, sparam4, sparam5, sparam6, sparam7, sparam8, sparam9); free (option); } /**************************************************************************** * Get special folder ****************************************************************************/ if (get_option (argv, argc, "-getSpecialFolder", &option) == 0) { #ifdef _WIN32 sparam1 = get_argument (option); iparam1 = get_clsid (sparam1); result = get_special_folder (iparam1, path); free (option); if (result != 0) fail ("Failed to get special folder: %s", sparam1); else puts(path); #else fail ("-getSpecialFolder is only supported on Windows."); #endif } /**************************************************************************** * Get property ****************************************************************************/ if (get_option (argv, argc, "-getOsProperty", &option) == 0) { sparam1 = get_argument (option); print_os_property (sparam1); free (option); } if (logFile != NULL) fclose (logFile); if (fargv != NULL) free (fargv); return EXIT_SUCCESS; }