int main(int argc, const char* argv[]) { printf("[BEGIN] crt-vars-libSystem\n"); bool success = true; if ( _NSGetArgv() != &NXArgv ) { printf("[FAIL] crt-libSystem: _NSGetArgv() != &NXArgv (%p!=%p) for %s", _NSGetArgv(), &NXArgv, argv[0]); success = false; } if ( _NSGetArgc() != &NXArgc ) { printf("[FAIL] crt-libSystem: _NSGetArgc() != &NXArgc (%p!=%p) for %s", _NSGetArgc(), &NXArgc, argv[0]); success = false; } if ( _NSGetEnviron() != &environ ) { printf("[FAIL] crt-libSystem: _NSGetEnviron() != &environv (%p!=%p) for %s", _NSGetEnviron(), &environ, argv[0]); success = false; } if ( _NSGetProgname() != &__progname ) { printf("[FAIL] crt-libSystem: _NSGetProgname() != &__progname (%p!=%p) for %s", _NSGetProgname(), &__progname, argv[0]); success = false; } if ( _NSGetMachExecuteHeader() != &_mh_execute_header ) { printf("[FAIL] crt-libSystem: _NSGetMachExecuteHeader() != &_mh_execute_headerv (%p!=%p) for %s", _NSGetMachExecuteHeader(), &_mh_execute_header, argv[0]); success = false; } if ( sVars->NXArgvPtr != &NXArgv ) { printf("[FAIL] crt-libSystem: sVars->NXArgvPtr != &NXArg (%p!=%p) for %s", sVars->NXArgvPtr, &NXArgv, argv[0]); success = false; } if ( sVars->NXArgcPtr != &NXArgc ) { printf("[FAIL] crt-libSystem: sVars->NXArgcPtr != &NXArgc (%p!=%p) for %s", sVars->NXArgcPtr, &NXArgc, argv[0]); success = false; } if ( sVars->environPtr != &environ ) { printf("[FAIL] crt-libSystem: sVars->environPtr != &environ (%p!=%p) for %s", sVars->environPtr, &environ, argv[0]); success = false; } if ( sVars->__prognamePtr != &__progname ) { printf("[FAIL] crt-libSystem: sVars->__prognamePtr != &__progname (%p!=%p) for %s", sVars->__prognamePtr, &__progname, argv[0]); success = false; } if ( sVars->mh != &_mh_execute_header ) { printf("[FAIL] crt-libSystem: sVars->mh != &_mh_execute_header (%p!=%p) for %s", sVars->mh, &_mh_execute_header, argv[0]); success = false; } if ( success ) printf("[PASS] crt-vars-libSystem\n"); return 0; }
int _CFArgc(void) { #if defined(__MACH__) return *_NSGetArgc(); #else return 0; #endif }
/* static warning: this method is to be called very early at time when VProcess and VCppMem is not initialized yet. Don't create any VObject here nor call any other xbox method. */ bool VProcess::_GetCommandLineArgumentAsLongStatic( const char* inArgumentName, sLONG* outValue) { bool isFound = false; *outValue = 0; #if VERSIONWIN wchar_t wideArgumentName[256]; size_t len = strlen(inArgumentName); if (len > 255) len = 255; std::copy( inArgumentName, inArgumentName + len + 1, &wideArgumentName[0]); int argc = 0; LPWSTR *argv = ::CommandLineToArgvW( ::GetCommandLineW(), &argc); for( int i = 0 ; i < argc ; ++i) { if (wcscmp(argv[i], wideArgumentName) == 0) { if (++i < argc) { *outValue = ::_wtol(argv[i]); isFound = true; break; } } } LocalFree( argv); #else #if VERSIONMAC int *argc_p = _NSGetArgc(); char ***argv_p = _NSGetArgv(); int argc = (argc_p == NULL) ? 0 : *argc_p; char **argv = (argv_p == NULL) ? NULL : *argv_p; #elif VERSION_LINUX // Postponed Linux Implementation ! //extern int __libc_argc; jmo - Theese symbols are not available for us ! //extern char ** __libc_argv; int argc = 0; char **argv = NULL; #endif for( int i = 0 ; i < argc ; ++i) { if (strcmp(argv[i], inArgumentName) == 0) { if (++i < argc) { *outValue = (sLONG) ::atol(argv[i]); isFound = true; break; } } } #endif return isFound; }
/* ZGCMDL -- Get the host system command line used to invoke this process. * There does not seem to be any straightforward way to do this for UNIX, * but the argc,argv info is evidently pushed on the stack immediately before * the environment list, so we can locate the ARGV array by searching back * up the stack a bit. This is very host OS dependent. */ int ZGCMDL ( PKCHAR *cmd, /* receives the command line */ XINT *maxch, /* maxch chars out */ XINT *status ) { register char *ip, *op; unsigned int *ep; register int n, narg; char **argv; #ifdef MACOSX argv = *_NSGetArgv(); xargc = *_NSGetArgc(); xargv = argv; #else if (!(argv = xargv)) { /* Locate the ARGV array. This assumes that argc,argv are * stored in memory immediately preceeding the environment * list, i.e., * * argc * argv[0] * argv[1] * ... * argv[argc-1] * NULL * env[0] <- environ * env[1] * ... * * !! NOTE !! - This is very system dependent! */ ep = ((unsigned int *) *environ) - 1; for (narg=0; *(ep-1) != (unsigned int)narg; narg++) --ep; xargc = narg; argv = (char **)ep; } #endif /* Reconstruct the argument list. */ for (op=(char *)cmd, n = *maxch, argv++; n >= 0 && *argv; argv++) { if (op > (char *)cmd && --n >= 0) *op++ = ' '; for (ip = *argv; --n >= 0 && (*op = *ip++); op++) ; } *op = EOS; *status = op - (char *)cmd; return (XOK); }
static void log_meta(void) { char *cwd; uncolog_write_action_start(&ufp, "meta", 4); // log argv #ifdef __APPLE__ do { char cmdbuf[4096], **argv; int i, argc; argc = *_NSGetArgc(); argv = *_NSGetArgv(); cmdbuf[0] = '\0'; for (i = 0; i != argc; ++i) snprintf(cmdbuf + strlen(cmdbuf), sizeof(cmdbuf) - strlen(cmdbuf), i == 0 ? "%s" : " %s", argv[i]); uncolog_write_argbuf(&ufp, cmdbuf, strlen(cmdbuf)); } while (0); #elif defined(__linux__) do { int fd; char *cmd = NULL; size_t cmdlen, i; if ((fd = open("/proc/self/cmdline", O_RDONLY)) != -1) { cmd = kread_full(fd, &cmdlen); close(fd); } if (cmd != NULL) { for (i = 0; i < cmdlen; ++i) if (cmd[i] == '\0') cmd[i] = ' '; uncolog_write_argbuf(&ufp, cmd, cmdlen); } else { uncolog_write_argbuf(&ufp, "(unknown)", sizeof("(unknown)") - 1); } } while (0); #else # error "unknown env" #endif // log cwd cwd = getcwd(NULL, 0); uncolog_write_argbuf(&ufp, cwd != NULL ? cwd : "", cwd != NULL ? strlen(cwd): 0); free(cwd); // log pid uncolog_write_argn(&ufp, getpid()); // log ppid uncolog_write_argn(&ufp, getppid()); uncolog_write_action_end(&ufp); }
// we move this function to another object file // to optimize static linkage (avoid unnecessary dependencies) StArrayList<StString> StProcess::getArguments() { StArrayList<StString> aList; #if (defined(_WIN32) || defined(__WIN32__)) int argc = 0; stUtfWide_t** argv = CommandLineToArgvW(GetCommandLineW(), &argc); for(int aParamId = 0; aParamId < argc; ++aParamId) { aList.add(StString(argv[aParamId])); } // free memory allocated for CommandLineToArgvW arguments. LocalFree(argv); #elif (defined(__APPLE__)) if(_NSGetArgc() == NULL || _NSGetArgv() == NULL) { return aList; // is it possible? } int anArgsNb = *_NSGetArgc(); char** anArgVec = *_NSGetArgv(); for(int aParamId = 0; aParamId < anArgsNb; ++aParamId) { // automatically convert filenames from decomposed form used by Mac OS X file systems aList.add(stFromUtf8Mac(anArgVec[aParamId])); } #elif (defined(__linux__) || defined(__linux)) stUtf8_t aCmdlineInfoFile[4096]; sprintf(aCmdlineInfoFile, "/proc/%d/cmdline", getpid()); std::ifstream iFile; iFile.open(aCmdlineInfoFile); if(iFile.is_open()) { char aCmdlineInfoBuff[4096]; while(!iFile.eof()) { stMemSet(aCmdlineInfoBuff, 0, sizeof(aCmdlineInfoBuff)); iFile.getline(aCmdlineInfoBuff, 4096, '\0'); if(aCmdlineInfoBuff[0] != '\0') { aList.add(StString(aCmdlineInfoBuff)); } } iFile.close(); } #endif return aList; }
std::vector<u8string> getargv() { #if defined(_WIN32) int argc; wchar_t **wargv = CommandLineToArgvW(GetCommandLineW(), &argc); std::vector<u8string> result; result.reserve(argc); for(int i = 0; i < argc; ++i) result.push_back(u8string::from_wide(wargv[i])); LocalFree(static_cast<void *>(wargv)); return result; #elif defined(__APPLE__) && defined(__MACH__) int argc = *_NSGetArgc(); char **argv = *_NSGetArgv(); std::vector<u8string> result; result.reserve(argc); for(int i = 0; i < argc; ++i) result.push_back(u8string(argv[i])); return result; #else std::ifstream cmdline("/proc/self/cmdline"); if(cmdline.is_open()) { std::vector<u8string> result; for(;;) { u8string argi; for(;;) { char c; if(cmdline.get(c)) if(c != '\0') argi.push_back(c); else break; else if(cmdline.eof() && argi.empty()) return result; else throw std::runtime_error("Unable to get commandline arguments"); } result.push_back(std::move(argi)); } } else throw std::runtime_error("Unable to get commandline arguments"); #endif }
/* static */ void VProcess::_FetchCommandLineArguments( VectorOfVString& outArguments) { VectorOfVString arguments; #if VERSIONWIN int nbArgs = 0; LPWSTR *tabArgs = ::CommandLineToArgvW( ::GetCommandLineW(), &nbArgs); for( int i = 0 ; i < nbArgs ; ++i) { arguments.push_back( VString( tabArgs[i])); } LocalFree( tabArgs); #else #if VERSIONMAC int *argc_p = _NSGetArgc(); char ***argv_p = _NSGetArgv(); int argc = (argc_p == NULL) ? 0 : *argc_p; char **argv = (argv_p == NULL) ? NULL : *argv_p; #elif VERSION_LINUX // Postponed Linux Implementation ! //extern int __libc_argc; jmo - Theese symbols are not available for us ! //extern char ** __libc_argv; int argc = 0; char **argv = NULL; #endif VToUnicodeConverter_UTF8 converter; for( int i = 0 ; i < argc ; ++i) { VString s; converter.ConvertString( argv[i], strlen( argv[i]), NULL, s); arguments.push_back( s); } #endif outArguments.swap( arguments); }
void mac_fork_and_reexec_self() { int argc = *_NSGetArgc(); char ** argv = *_NSGetArgv(); char * newargv[argc+2]; char progname[PATH_MAX]; uint32_t buflen = PATH_MAX; _NSGetExecutablePath(progname, &buflen); bool found_psn = false; for (int i = 0; i < argc; i++) { newargv[i] = argv[i]; } newargv[argc] = "--nofork"; newargv[argc+1] = NULL; int x_fork_result = fork(); switch(x_fork_result) { case -1: #ifndef NDEBUG fprintf(stderr, "Mac OS X workaround fork() failed!\n"); #endif ::_exit(255); break; case 0: // Child execvp(progname, newargv); break; default: // Parent _exit(0); break; } }
// GetCmdLine //------------------------------------------------------------------------------ /*static*/ void Env::GetCmdLine( AString & cmdLine ) { #if defined( __WINDOWS__ ) cmdLine = ::GetCommandLine(); #elif defined( __APPLE__ ) int argc = *_NSGetArgc(); const char ** argv = const_cast< const char ** >( *_NSGetArgv() ); for ( int i=0; i<argc; ++i ) { if ( i > 0 ) { cmdLine += ' '; } cmdLine += argv[i]; } #else FILE* f = fopen( "/proc/self/cmdline", "rb" ); VERIFY( f != 0 ); char buffer[ 4096 ]; for (;;) { int size = fread( buffer, 1, 4096, f ); if ( size == 0 ) { break; } // Append for ( int i=0; i<size; ++i ) { const char c = buffer[ i ]; cmdLine += ( c ? c : ' ' ); // convert nulls in between args back into spaces } } VERIFY( fclose( f ) == 0 ); #endif }
int _CFArgc(void) { return *_NSGetArgc(); }
static #elif DEPLOYMENT_TARGET_WINDOWS CF_EXPORT #endif void __CFInitialize(void) { static int __done = 0; if (!__done) { __done = 1; #if defined(DEBUG) || defined(ENABLE_ZOMBIES) const char *value = getenv("NSZombieEnabled"); if (value && (*value == 'Y' || *value == 'y')) __CFZombieEnabled = 0xff; value = getenv("NSDeallocateZombies"); if (value && (*value == 'Y' || *value == 'y')) __CFDeallocateZombies = 0xff; value = getenv("CFZombieLevel"); if (NULL != value) { #if DEPLOYMENT_TARGET_MACOSX __CFZombieLevel = (uint32_t)strtoul_l(value, NULL, 0, NULL); #else __CFZombieLevel = (uint32_t)strtoul(value, NULL, 0); #endif } if (0x0 == __CFZombieLevel) __CFZombieLevel = 0x0000FC00; // default #endif __CFRuntimeClassTableSize = 1024; __CFRuntimeClassTable = (CFRuntimeClass **)calloc(__CFRuntimeClassTableSize, sizeof(CFRuntimeClass *)); __CFBaseInitialize(); /* Here so that two runtime classes get indices 0, 1. */ __kCFNotATypeTypeID = _CFRuntimeRegisterClass(&__CFNotATypeClass); __kCFTypeTypeID = _CFRuntimeRegisterClass(&__CFTypeClass); /* Here so that __kCFAllocatorTypeID gets index 2. */ __CFAllocatorInitialize(); #if DEPLOYMENT_TARGET_MACOSX { CFIndex idx, cnt; char **args = *_NSGetArgv(); cnt = *_NSGetArgc(); for (idx = 1; idx < cnt - 1; idx++) { if (NULL == args[idx]) continue; if (0 == strcmp(args[idx], "-AppleLanguages") && args[idx + 1]) { CFIndex length = strlen(args[idx + 1]); __CFAppleLanguages = malloc(length + 1); memmove(__CFAppleLanguages, args[idx + 1], length + 1); break; } } } #endif /* CFBag needs to be up before CFString. */ __CFBagInitialize(); #if !__LP64__ // Creating this lazily in CFRetain causes recursive call to CFRetain __CFRuntimeExternRefCountTable = CFBagCreateMutable(kCFAllocatorSystemDefault, 0, NULL); #endif /*** _CFRuntimeCreateInstance() can finally be called generally after this line. ***/ __CFRuntimeClassTableCount = 7; __CFStringInitialize(); // CFString's TypeID must be 0x7, now and forever __CFRuntimeClassTableCount = 16; __CFDictionaryInitialize(); __CFArrayInitialize(); __CFDataInitialize(); __CFSetInitialize(); __CFNullInitialize(); // See above for hard-coding of this position __CFBooleanInitialize(); // See above for hard-coding of this position __CFNumberInitialize(); // See above for hard-coding of this position __CFDateInitialize(); // just initializes the time goo // _CFRuntimeBridgeClasses(CFDateGetTypeID(), objc_lookUpClass("NSCFDate") ? "NSCFDate" : "__NSCFDate"); __CFTimeZoneInitialize(); // _CFRuntimeBridgeClasses(CFTimeZoneGetTypeID(), "NSCFTimeZone"); __CFBinaryHeapInitialize(); __CFBitVectorInitialize(); __CFCharacterSetInitialize(); #if DEPLOYMENT_TARGET_WINDOWS __CFLocaleInitialize(); #endif __CFStorageInitialize(); __CFErrorInitialize(); __CFTreeInitialize(); __CFURLInitialize(); __CFBundleInitialize(); #if DEPLOYMENT_TARGET_MACOSX __CFPlugInInitialize(); __CFPlugInInstanceInitialize(); #endif //__MACH__ __CFUUIDInitialize(); #if DEPLOYMENT_TARGET_MACOSX __CFMessagePortInitialize(); __CFMachPortInitialize(); #endif __CFStreamInitialize(); __CFPreferencesDomainInitialize(); #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_WINDOWS || DEPLOYMENT_TARGET_LINUX __CFRunLoopInitialize(); __CFRunLoopObserverInitialize(); __CFRunLoopSourceInitialize(); __CFRunLoopTimerInitialize(); __CFSocketInitialize(); #endif __CFFileDescriptorInitialize(); __CFNotificationCenterInitialize(); #if DEPLOYMENT_TARGET_MACOSX { CFIndex idx, cnt; char **args; args = *_NSGetArgv(); cnt = *_NSGetArgc(); CFIndex count; CFStringRef *list, buffer[256]; list = (cnt <= 256) ? (CFStringRef*)buffer : (CFStringRef*)malloc(cnt * sizeof(CFStringRef)); for (idx = 0, count = 0; idx < cnt; idx++) { if (NULL == args[idx]) continue; list[count] = CFStringCreateWithCString(kCFAllocatorSystemDefault, args[idx], kCFStringEncodingUTF8); if (NULL == list[count]) { list[count] = CFStringCreateWithCString(kCFAllocatorSystemDefault, args[idx], kCFStringEncodingISOLatin1); // We CANNOT use the string SystemEncoding here; // Do not argue: it is not initialized yet, but these // arguments MUST be initialized before it is. // We should just ignore the argument if the UTF-8 // conversion fails, but out of charity we try once // more with ISO Latin1, a standard unix encoding. } if (NULL != list[count]) count++; } __CFArgStuff = CFArrayCreate(kCFAllocatorSystemDefault, (const void **)list, count, &kCFTypeArrayCallBacks); } #endif _CFProcessPath(); // cache this early if (__CFRuntimeClassTableCount < 256) __CFRuntimeClassTableCount = 256; #if defined(DEBUG) && !DEPLOYMENT_TARGET_WINDOWS CFLog(kCFLogLevelWarning, CFSTR("Assertions enabled")); #endif } }
int _environment_initialize( const application_t application ) { #if FOUNDATION_PLATFORM_WINDOWS int ia; int num_args = 0; DWORD ret = 0; wchar_t module_filename[FOUNDATION_MAX_PATHLEN]; LPWSTR* arg_list = CommandLineToArgvW( GetCommandLineW(), &num_args ); if( !arg_list ) return -1; for( ia = 0; ia < num_args; ++ia ) array_push( _environment_argv, string_allocate_from_wstring( arg_list[ia], 0 ) ); LocalFree( arg_list ); if( GetModuleFileNameW( 0, module_filename, FOUNDATION_MAX_PATHLEN ) ) { char* exe_path = string_allocate_from_wstring( module_filename, 0 ); char* dir_path = path_make_absolute( exe_path ); _environment_set_executable_paths( dir_path ); string_deallocate( dir_path ); string_deallocate( exe_path ); } else { log_errorf( ERROR_SYSTEM_CALL_FAIL, "Unable to get module filename" ); return -1; } #elif FOUNDATION_PLATFORM_APPLE int ia; int* argc_ptr = _NSGetArgc(); char*** argv_ptr = _NSGetArgv(); for( ia = 0; ia < *argc_ptr; ++ia ) array_push( _environment_argv, string_clone( (*argv_ptr)[ia] ) ); FOUNDATION_ASSERT( *argc_ptr > 0 ); char* exe_path = path_make_absolute( (*argv_ptr)[0] ); _environment_set_executable_paths( exe_path ); string_deallocate( exe_path ); #elif FOUNDATION_PLATFORM_POSIX stream_t* cmdline = fs_open_file( "/proc/self/cmdline", STREAM_IN | STREAM_BINARY ); if( !cmdline ) { log_errorf( ERROR_SYSTEM_CALL_FAIL, "Unable to read /proc/self/cmdline" ); return -1; } while( true ) { char* arg = stream_read_string( cmdline ); if( !string_length( arg ) ) { string_deallocate( arg ); break; } array_push( _environment_argv, arg ); } char exelink[FOUNDATION_MAX_PATHLEN] = {0}; if( readlink( "/proc/self/exe", exelink, FOUNDATION_MAX_PATHLEN ) < 0 ) { log_errorf( ERROR_SYSTEM_CALL_FAIL, "Unable to read /proc/self/exe link" ); return -1; } char* exe_path; char* dir_path; exe_path = path_clean( string_clone( exelink ), path_is_absolute( exelink ) ); dir_path = path_make_absolute( exe_path ); _environment_set_executable_paths( dir_path ); string_deallocate( dir_path ); string_deallocate( exe_path ); #else # error Not implemented /*if( array_size( _environment_argv ) > 0 ) { char* exe_path = path_clean( string_clone( _environment_argv[0] ), path_is_absolute( _environment_argv[0] ) ); char* dir_path = path_make_absolute( exe_path ); _environment_set_executable_paths( dir_path ); string_deallocate( dir_path ); string_deallocate( exe_path ); } else if( !string_length( _environment_executable_dir ) ) string_copy( _environment_executable_dir, environment_current_working_directory(), FOUNDATION_MAX_PATHLEN ); */ #endif _environment_app = application; if( uuid_is_null( _environment_app.instance ) ) _environment_app.instance = uuid_generate_random(); string_copy( _environment_initial_working_dir, environment_current_working_directory(), FOUNDATION_MAX_PATHLEN ); environment_temporary_directory(); return 0; }
bool mm_GetGameName(char *buffer, size_t size) { buffer[0] = '\0'; #if defined _WIN32 static char game[128]; LPWSTR pCmdLine = GetCommandLineW(); int argc; LPWSTR *wargv = CommandLineToArgvW(pCmdLine, &argc); for (int i = 0; i < argc; ++i) { if (wcscmp(wargv[i], L"-game") != 0) continue; if (++i >= argc) break; wcstombs(buffer, wargv[i], size); buffer[size-1] = '\0'; break; } LocalFree(wargv); return buffer[0] != 0; #elif defined __APPLE__ int argc = *_NSGetArgc(); char **argv = *_NSGetArgv(); for (int i = 0; i < argc; ++i) { if (strcmp(argv[i], "-game") != 0) continue; if (++i >= argc) break; strncpy(buffer, argv[i], size); buffer[size-1] = '\0'; break; } return buffer[0] != 0; #elif defined __linux__ FILE *pFile = fopen("/proc/self/cmdline", "rb"); if (!pFile) return false; char *arg = NULL; size_t argsize = 0; bool bNextIsGame = false; while (getdelim(&arg, &argsize, 0, pFile) != -1) { if (bNextIsGame) { strncpy(buffer, arg, size); buffer[size-1] = '\0'; break; } if (strcmp(arg, "-game") == 0) { bNextIsGame = true; } } free(arg); fclose(pFile); return buffer[0] != 0; #else #error unsupported platform #endif }