static char * common_string (char **argv, int argc, char seperator) { char *res, *p, *q, *last_seperator_start; int i; res = 0; for (i = 1; i < argc; i++) { if (!skip_arg (argv, &i)) { if (res) { last_seperator_start = res - 1; for (p = argv[i], q = res; *p && *p == *q; p++, q++) { if (*q == seperator) last_seperator_start = q; } if (*q) last_seperator_start[1] = 0; } else res = strdup (argv[i]); } } return res; }
int main(void) { STARTUPINFO stup; PROCESS_INFORMATION pinfo; LONGLONG ct, et, kt, ut, elapse; SYSTEMTIME sct, set; char *call; call = GetCommandLine(); // string com a linha de comando call = skip_arg(call); // salta o 1.o argumento GetStartupInfo(&stup); // necessário para a criação de um novo processo CreateProcess(NULL, call, NULL, NULL, TRUE, NORMAL_PRIORITY_CLASS, NULL, NULL,&stup, &pinfo); // cria novo processo com parâmetros de chamada WaitForSingleObject(pinfo.hProcess, INFINITE); // espera que termine GetProcessTimes(pinfo.hProcess, (FILETIME *)&ct, (FILETIME *)&et,(FILETIME *)&kt, (FILETIME *)&ut); CloseHandle(pinfo.hProcess); // fecha handle do processo terminado CloseHandle(pinfo.hThread); // fecha handle do thread terminado elapse = et - ct; FileTimeToSystemTime((FILETIME *)&ct, &sct); FileTimeToSystemTime((FILETIME *)&et, &set); printf("\n\nStart time: %02d:%02d:%02d.%03d\n", sct.wHour, sct.wMinute,sct.wSecond, sct.wMilliseconds); printf("End time: %02d:%02d:%02d.%03d\n\n", set.wHour, set.wMinute,set.wSecond, set.wMilliseconds); printf("ELAPSED: %.3f ms (%.3f s)\n", (double)elapse/10000, (double)elapse/1E7); printf(" Kernel: %.3f ms (%.3f s)\n", (double)kt/10000, (double)kt/1E7); printf(" User: %.3f ms (%.3f s)\n", (double)ut/10000, (double)ut/1E7); return 0; }
static int count_argc(const char *str) { int count = 0; while (*str) { str = skip_sep(str); if (*str) { count++; str = skip_arg(str); } } return count; }
static char * common_suffix (char **argv, int argc) { char **my_argv, *res; int i, my_argc; my_argv = (char **) malloc (sizeof (char *) * argc); for (i = 1, my_argc = 1; i < argc; i++) { if (!skip_arg (argv, &i)) my_argv[my_argc++] = reverse_string (argv[i]); } res = common_string (my_argv, my_argc, '.'); for (i = 1; i < my_argc; i++) free (my_argv[i]); free (my_argv); return res; }
//int _tmain( int argc, TCHAR* argv[] ) int main( void ) { STARTUPINFO si; PROCESS_INFORMATION pi; TCHAR* cmd; BOOL option; BOOL opt_m; BOOL installed; HMODULE ansi; int rc = 0; int argc; LPWSTR* argv = CommandLineToArgvW( GetCommandLine(), &argc ); if (argc > 1) { if (lstrcmp( argv[1], L"--help" ) == 0 || (argv[1][0] == '-' && (argv[1][1] == '?' || argv[1][1] == 'h')) || (argv[1][0] == '/' && argv[1][1] == '?')) { help(); return rc; } if (lstrcmp( argv[1], L"--version" ) == 0) { _putws( L"ANSICON (" BITS L"-bit) version " PVERS L" (" PDATE L")." ); return rc; } } #if (MYDEBUG > 1) DEBUGSTR( NULL ); // create a new file #endif option = (argc > 1 && argv[1][0] == '-'); if (option && (towlower( argv[1][1] ) == 'i' || towlower( argv[1][1] ) == 'u')) { process_autorun( argv[1][1] ); argv[1][1] = 'p'; } get_original_attr(); opt_m = FALSE; if (option && argv[1][1] == 'm') { WORD attr = 7; if (iswxdigit( argv[1][2] )) { attr = iswdigit( argv[1][2] ) ? argv[1][2] - '0' : (argv[1][2] | 0x20) - 'a' + 10; if (iswxdigit( argv[1][3])) { attr <<= 4; attr |= iswdigit( argv[1][3] ) ? argv[1][3] - '0' : (argv[1][3] | 0x20) - 'a' + 10; } } SetConsoleTextAttribute( hConOut, attr ); opt_m = TRUE; ++argv; --argc; option = (argc > 1 && argv[1][0] == '-'); } installed = (GetEnvironmentVariable( L"ANSICON", NULL, 0 ) != 0); if (option && argv[1][1] == 'p') { // If it's already installed, there's no need to do anything. if (installed) ; else if (GetParentProcessInfo( &pi )) { pi.hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, pi.dwProcessId ); pi.hThread = OpenThread( THREAD_ALL_ACCESS, FALSE, pi.dwThreadId ); SuspendThread( pi.hThread ); if (!Inject( &pi )) { _putws( L"ANSICON: parent process type is not supported." ); rc = 1; } ResumeThread( pi.hThread ); CloseHandle( pi.hThread ); CloseHandle( pi.hProcess ); } else { _putws( L"ANSICON: could not obtain the parent process." ); rc = 1; } } else { ansi = 0; if (!installed) { ansi = LoadLibrary( L"ANSI" BITS L".dll" ); if (!ansi) { fputws( L"ANSICON: failed to load ANSI" BITS L".dll.\n", stderr ); rc = 1; } } if (option && (argv[1][1] == 't' || argv[1][1] == 'T')) { BOOL title = (argv[1][1] == 'T'); if (argc == 2) { argv[2] = L"-"; ++argc; } for (; argc > 2; ++argv, --argc) { if (title) wprintf( L"==> %s <==\n", argv[2] ); display( argv[2], title ); if (title) putwchar( '\n' ); } } else { // Retrieve the original command line, skipping our name and the option. cmd = skip_spaces( skip_arg( skip_spaces( GetCommandLine() ) ) ); if (opt_m) cmd = skip_spaces( skip_arg( cmd ) ); if (cmd[0] == '-' && (cmd[1] == 'e' || cmd[1] == 'E')) { fputws( cmd + 3, stdout ); if (cmd[1] == 'e') putwchar( '\n' ); } else if (!_isatty( 0 ) && *cmd == '\0') { display( L"-", FALSE ); } else { if (*cmd == '\0') { cmd = _wgetenv( L"ComSpec" ); if (cmd == NULL) cmd = L"cmd"; } ZeroMemory( &si, sizeof(si) ); si.cb = sizeof(si); if (CreateProcess( NULL, cmd, NULL,NULL, TRUE, 0, NULL,NULL, &si, &pi )) { BOOL console = FALSE; TCHAR name[MAX_PATH]; DWORD rc; CoInitialize( NULL ); do { // When I first tried doing this, it took a little while to // succeed. Testing again shows it works immediately - perhaps the // CoInitialize introduces enough of a delay. Still, play it safe // and keep trying. And if you're wondering why I do it at all, // ProcessType may detect GUI, even for a console process. That's // fine after injection (including -p), but not here. We *need* to // suspend our own execution whilst running the child, otherwise // bad things happen (besides which, I want to restore the original // attributes when the child exits). if (GetModuleFileNameEx( pi.hProcess, NULL, name, lenof(name) )) { DWORD_PTR info; info = SHGetFileInfo( name, 0, NULL, 0, SHGFI_EXETYPE ); if (info == 0x00004550) // console PE console = TRUE; DEBUGSTR( L"%s", name ); DEBUGSTR( L" %s (%p)", (console) ? L"Console" : L"Not console", info ); break; } Sleep( 10 ); } while (GetExitCodeProcess( pi.hProcess, &rc ) && rc == STILL_ACTIVE); CoUninitialize(); if (console) { SetConsoleCtrlHandler( (PHANDLER_ROUTINE)CtrlHandler, TRUE ); WaitForSingleObject( pi.hProcess, INFINITE ); } CloseHandle( pi.hProcess ); CloseHandle( pi.hThread ); } else { *skip_arg( cmd ) = '\0'; wprintf( L"ANSICON: '%s' could not be executed.\n", cmd ); rc = 1; } } } if (ansi) FreeLibrary( ansi ); } set_original_attr(); return rc; }
//int _tmain( int argc, TCHAR* argv[] ) int main( void ) { STARTUPINFO si; PROCESS_INFORMATION pi; TCHAR* cmd; BOOL option; BOOL opt_m; BOOL installed; HMODULE ansi; int rc = 0; int argc; LPWSTR* argv = CommandLineToArgvW( GetCommandLineW(), &argc ); if (argc > 1) { if (lstrcmp( argv[1], TEXT("--help") ) == 0 || (argv[1][0] == '-' && (argv[1][1] == '?' || argv[1][1] == 'h')) || (argv[1][0] == '/' && argv[1][1] == '?')) { help(); return rc; } if (lstrcmp( argv[1], TEXT("--version") ) == 0) { _putts( TEXT("ANSICON (" BITS "-bit) version " PVERS " (" PDATE ").") ); return rc; } } option = (argc > 1 && argv[1][0] == '-'); if (option && (_totlower( argv[1][1] ) == 'i' || _totlower( argv[1][1] ) == 'u')) { process_autorun( argv[1][1] ); return rc; } get_original_attr(); opt_m = FALSE; if (option && argv[1][1] == 'm') { WORD attr = 7; if (_istxdigit( argv[1][2] )) { attr = _istdigit( argv[1][2] ) ? argv[1][2] - '0' : (argv[1][2] | 0x20) - 'a' + 10; if (_istxdigit( argv[1][3])) { attr <<= 4; attr |= _istdigit( argv[1][3] ) ? argv[1][3] - '0' : (argv[1][3] | 0x20) - 'a' + 10; } } SetConsoleTextAttribute( hConOut, attr ); opt_m = TRUE; ++argv; --argc; option = (argc > 1 && argv[1][0] == '-'); } installed = (GetEnvironmentVariable( TEXT("ANSICON"), NULL, 0 ) != 0); if (option && argv[1][1] == 'p') { // If it's already installed, there's no need to do anything. if (installed) ; else if (GetParentProcessInfo( &pi )) { pi.hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, pi.dwProcessId ); pi.hThread = OpenThread( THREAD_ALL_ACCESS, FALSE, pi.dwThreadId ); SuspendThread( pi.hThread ); Inject( &pi ); ResumeThread( pi.hThread ); CloseHandle( pi.hThread ); CloseHandle( pi.hProcess ); } else { _putts( TEXT("ANSICON: could not obtain the parent process.") ); rc = 1; } } else { ansi = 0; if (!installed) ansi = LoadLibrary( TEXT("ANSI" BITS ".dll") ); if (option && (argv[1][1] == 't' || argv[1][1] == 'T')) { BOOL title = (argv[1][1] == 'T'); if (argc == 2) { argv[2] = L"-"; ++argc; } for (; argc > 2; ++argv, --argc) { if (title) _tprintf( TEXT("==> %s <==\n"), argv[2] ); display( argv[2], title ); if (title) _puttchar( '\n' ); } } else { // Retrieve the original command line, skipping our name and the option. cmd = skip_spaces( skip_arg( skip_spaces( GetCommandLine() ) ) ); if (opt_m) cmd = skip_spaces( skip_arg( cmd ) ); if (cmd[0] == '-' && (cmd[1] == 'e' || cmd[1] == 'E')) { _fputts( cmd + 3, stdout ); if (cmd[1] == 'e') _puttchar( '\n' ); } else if (!isatty( 0 ) && *cmd == '\0') { display( TEXT("-"), FALSE ); } else { if (*cmd == '\0') { cmd = _tgetenv( TEXT("ComSpec") ); if (cmd == NULL) cmd = TEXT("cmd"); } ZeroMemory( &si, sizeof(si) ); si.cb = sizeof(si); if (CreateProcess( NULL, cmd, NULL,NULL, TRUE, 0, NULL,NULL, &si, &pi )) { SetConsoleCtrlHandler( (PHANDLER_ROUTINE)CtrlHandler, TRUE ); WaitForSingleObject( pi.hProcess, INFINITE ); } else { *skip_arg( cmd ) = '\0'; _tprintf( TEXT("ANSICON: '%s' could not be executed.\n"), cmd ); rc = 1; } } } if (ansi) FreeLibrary( ansi ); } set_original_attr(); return rc; }
int CDaemonConsole::Run(int argc, char* argv[]) { /* Read command line parameters */ int cur_arg=1; std::string sAction; std::string sConfigFile; std::string sPrefix; eMonitorOption MonitorOption = MO_UNDEFINED; bool bRestartedOnCrash = false; int nPid = 0; CDaemon daemon; m_pDaemon = &daemon; //MessageBoxA(NULL, "", "", 0); while(args_left()>0) { if(cmp_arg("--help")) { if(args_left()!=1) { m_sErrorMsg = "Too many parameters for --help command."; goto exit; } sAction += "help"; break; } else if(cmp_arg("--test-crash")) { int* p = NULL; *p = 13; } else if(cmp_arg("-v")) { if(args_left()!=1) { m_sErrorMsg = "Too many parameters for -v command."; goto exit; } sAction += "version"; break; } else if(cmp_arg("--start")) { sAction += "start"; } else if(cmp_arg("--stop")) { sAction += "stop"; } #ifdef _WIN32 else if(cmp_arg("--install")) { sAction += "install"; } else if(cmp_arg("--remove")) { sAction += "remove"; } else if(cmp_arg("--restart")) { sAction += "restart"; } else if(cmp_arg("--run")) { sAction += "run"; } #endif else if(cmp_arg("-c") || cmp_arg("--config")) { skip_arg(); char* szParam = get_arg(); if(szParam==NULL) { m_sErrorMsg = "Exptected configuration file name."; goto exit; } sConfigFile = szParam; } else if(cmp_arg("--run-as-monitor")) { skip_arg(); char* szParam = get_arg(); if(szParam==NULL) { m_sErrorMsg = "Exptected PID."; goto exit; } nPid = atoi(szParam); MonitorOption = MO_IS_MONITOR; } else if(cmp_arg("--restart-on-crash")) { MonitorOption = MO_NO_MONITOR; bRestartedOnCrash = TRUE; } else { char* szParam = get_arg(); m_sErrorMsg += "Unexpected parameter: "; m_sErrorMsg += szParam; goto exit; } skip_arg(); } if(sAction=="help") { PrintUsage(); goto exit; } else if(sAction=="version") { PrintVersion(); goto exit; } #ifdef _WIN32 else if(sAction=="install") { if(!sConfigFile.empty()) daemon.SetConfigFile(sConfigFile); return daemon.Install(); } else if(sAction=="remove") { if(!sConfigFile.empty()) daemon.SetConfigFile(sConfigFile); return daemon.Remove(); } else if(sAction=="run") { if(!sConfigFile.empty()) daemon.SetConfigFile(sConfigFile); daemon.SetMonitorOption(MonitorOption, nPid); daemon.SetErrorRestartFlag(bRestartedOnCrash); m_pDaemon = &daemon; daemon.Run(); return 0; } #endif else if(sAction=="start") { if(!sConfigFile.empty()) daemon.SetConfigFile(sConfigFile); daemon.SetMonitorOption(MonitorOption, nPid); daemon.SetErrorRestartFlag(bRestartedOnCrash); m_pDaemon = &daemon; return daemon.Start(); } else if(sAction=="restart") { if(!sConfigFile.empty()) daemon.SetConfigFile(sConfigFile); if(!sPrefix.empty()) daemon.SetPrefix(sPrefix); m_pDaemon = &daemon; return daemon.Restart(); } else if(sAction=="stop") { if(!sConfigFile.empty()) daemon.SetConfigFile(sConfigFile); if(!sPrefix.empty()) daemon.SetPrefix(sPrefix); return daemon.Stop(); } else { m_sErrorMsg = "Invalid combination of commands."; #ifdef _WIN32 m_pDaemon = &daemon; daemon.EnterServiceMain(); #endif goto exit; } exit: m_pDaemon = NULL; return m_sErrorMsg.empty()?0:1; }
int main (int argc, char **argv) { int i, pid, status, res; int time_out, max_decisions; char *name; pretty_print = 0; multiple_files = 0; time_out = 3600; max_decisions = -1; /* no bound is default */ #ifdef SAT2002FMT verbose = 1; #else verbose = 0; #endif limmat = 0; sparse = 0; name = 0; for (i = 1; i < argc; i++) { if (!strcmp (argv[i], "-h") || !strcmp (argv[i], "--help")) { usage (); exit (0); } else if (!strcmp (argv[i], "--clauses")) { assignment_as_clauses = 1; } else if (!strcmp (argv[i], "--version")) { printf ("%s\n", version_Limmat ()); exit (0); } else if (!strcmp (argv[i], "--options")) { printf ("%s\n", options_Limmat ()); exit (0); } else if (!strcmp (argv[i], "--copyright")) { printf ("%s\n", copyright_Limmat ()); exit (0); } else if (!strcmp (argv[i], "-v") || !strcmp (argv[i], "--verbose")) { verbose++; } else if (!strcmp (argv[i], "-s") || !strcmp (argv[i], "--sparse")) { sparse++; } else if (!strcmp (argv[i], "-p") || !strcmp (argv[i], "--pretty-print")) { pretty_print = 1; } else if (argv[i][0] == '-' && argv[i][1] == 'm') { if (argv[i][2]) { if (isdigit ((int) argv[i][2])) { max_decisions = atoi (argv[i] + 2); } else goto EXPECTED_DIGIT_FOR_MAX_DECISIONS_ARGUMENT; } else if (++i < argc && isdigit ((int) argv[i][0])) { max_decisions = atoi (argv[i]); } else goto EXPECTED_DIGIT_FOR_MAX_DECISIONS_ARGUMENT; } else if (argv[i][0] == '-' && argv[i][1] == '-' && argv[i][2] == 't' && argv[i][3] == 'i' && argv[i][4] == 'm' && argv[i][5] == 'e' && argv[i][6] == '-' && argv[i][7] == 'o' && argv[i][8] == 'u' && argv[i][9] == 't' && argv[i][10] == '=') { if (isdigit ((int) argv[i][11])) { max_decisions = atoi (argv[i] + 11); } else { EXPECTED_DIGIT_FOR_MAX_DECISIONS_ARGUMENT: fprintf (stderr, "*** limmat: expected digit for max decisions argument\n"); exit (1); } } else if (argv[i][0] == '-' && argv[i][1] == 't') { if (argv[i][2]) { if (isdigit ((int) argv[i][2])) { time_out = atoi (argv[i] + 2); } else goto EXPECTED_DIGIT_FOR_TIME_OUT_ARGUMENT; } else if (++i < argc && isdigit ((int) argv[i][0])) { time_out = atoi (argv[i]); } else goto EXPECTED_DIGIT_FOR_TIME_OUT_ARGUMENT; } else if (argv[i][0] == '-' && argv[i][1] == '-' && argv[i][2] == 't' && argv[i][3] == 'i' && argv[i][4] == 'm' && argv[i][5] == 'e' && argv[i][6] == '-' && argv[i][7] == 'o' && argv[i][8] == 'u' && argv[i][9] == 't' && argv[i][10] == '=') { if (isdigit ((int) argv[i][11])) { time_out = atoi (argv[i] + 11); } else { EXPECTED_DIGIT_FOR_TIME_OUT_ARGUMENT: fprintf (stderr, "*** limmat: expected digit for time out argument\n"); exit (1); } } else if (argv[i][0] == '-') { fprintf (stderr, "*** limmat: unknown command line option '%s' (try '-h')\n", argv[i]); exit (1); } else if (name) { multiple_files = 1; } else name = argv[i]; } if (multiple_files) { prefix = common_prefix (argv, argc); suffix = common_suffix (argv, argc); len_prefix = strlen (prefix); len_suffix = strlen (suffix); sparse = 1; } if (verbose) sparse = 0; if (pretty_print) { if (multiple_files) { fprintf (stderr, "*** limmat: can not pretty print multiple files\n"); exit (1); } limmat = new_Limmat (0); parse (name); print_Limmat (limmat, stdout); delete_Limmat (limmat); res = ABNORMAL_EXIT_VALUE; } else { res = 0; if (multiple_files) { for (i = 1; !res && i < argc; i++) { if (!skip_arg (argv, &i)) { if ((pid = fork ())) { wait (&status); if (WIFSIGNALED (status) || WEXITSTATUS (status) == ABNORMAL_EXIT_VALUE) { res = ABNORMAL_EXIT_VALUE; } } else child (argv[i], time_out, max_decisions); } } } else child (name, time_out, max_decisions); } if (prefix) free (prefix); if (suffix) free (suffix); exit (res); return res; }