DECLHIDDEN(int) rtProcInitExePath(char *pszPath, size_t cchPath) { /* * Query the image name from the dynamic linker, convert and return it. */ _execname(pszPath, cchPath); char const *pszTmp; int rc = rtPathFromNative(&pszTmp, pszPath, NULL); AssertMsgRCReturn(rc, ("rc=%Rrc pszLink=\"%s\"\nhex: %.*Rhxs\n", rc, pszPath, cchPath, pszPath), rc); if (pszTmp != pszPath) { rc = RTStrCopy(pszPath, cchPath, pszTmp); rtPathFreeIprt(pszTmp, pszPath); } return rc; }
void ruby_init_loadpath_safe(int safe_level, const char* szRoot) { VALUE load_path; #if defined LOAD_RELATIVE char libpath[MAXPATHLEN + 1]; char *p; int rest; if ( szRoot ) strncpy(libpath, szRoot, sizeof(libpath) - 1); else { #if defined _WIN32 || defined __CYGWIN__ GetModuleFileNameA(libruby, libpath, sizeof libpath); #elif defined(__EMX__) _execname(libpath, sizeof(libpath) - 1); #endif } //RHO libpath[sizeof(libpath) - 1] = '\0'; #if defined DOSISH translate_char(libpath, '\\', '/'); #elif defined __CYGWIN__ { char rubylib[FILENAME_MAX]; cygwin_conv_to_posix_path(libpath, rubylib); strncpy(libpath, rubylib, sizeof(libpath)); } #endif p = strrchr(libpath, '/'); if (p) { *p = 0; if (p - libpath > 3 && !STRCASECMP(p - 4, "/bin")) { p -= 4; *p = 0; } } else { strcpy(libpath, "."); p = libpath + 1; } rest = sizeof(libpath) - 1 - (p - libpath); #define RUBY_RELATIVE(path) (strncpy(p, (path), rest), libpath) #else #define RUBY_RELATIVE(path) (path) #endif #define incpush(path) rb_ary_push(load_path, rubylib_mangled_path2(path)) load_path = GET_VM()->load_path; //RHO incpush(RUBY_RELATIVE(RUBY_LIB)); incpush(RUBY_RELATIVE(RUBY_SITE_LIB)); incpush(RUBY_RELATIVE(RUBY_SITE_LIB2)); /* if (safe_level == 0) { ruby_push_include(getenv("RUBYLIB"), identical_path); } #ifdef RUBY_SEARCH_PATH incpush(RUBY_RELATIVE(RUBY_SEARCH_PATH)); #endif incpush(RUBY_RELATIVE(RUBY_SITE_LIB2)); #ifdef RUBY_SITE_THIN_ARCHLIB incpush(RUBY_RELATIVE(RUBY_SITE_THIN_ARCHLIB)); #endif incpush(RUBY_RELATIVE(RUBY_SITE_ARCHLIB)); incpush(RUBY_RELATIVE(RUBY_SITE_LIB)); incpush(RUBY_RELATIVE(RUBY_VENDOR_LIB2)); #ifdef RUBY_VENDOR_THIN_ARCHLIB incpush(RUBY_RELATIVE(RUBY_VENDOR_THIN_ARCHLIB)); #endif incpush(RUBY_RELATIVE(RUBY_VENDOR_ARCHLIB)); incpush(RUBY_RELATIVE(RUBY_VENDOR_LIB)); incpush(RUBY_RELATIVE(RUBY_LIB)); #ifdef RUBY_THIN_ARCHLIB incpush(RUBY_RELATIVE(RUBY_THIN_ARCHLIB)); #endif incpush(RUBY_RELATIVE(RUBY_ARCHLIB)); if (safe_level == 0) { incpush("."); } */ //RHO }
void ruby_init_loadpath_safe(int safe_level, const char* szRoot) { VALUE load_path; ID id_initial_load_path_mark; extern const char ruby_initial_load_paths[]; const char *paths = ruby_initial_load_paths; #if defined LOAD_RELATIVE # if defined HAVE_DLADDR || (defined __CYGWIN__ && defined CCP_WIN_A_TO_POSIX) # define VARIABLE_LIBPATH 1 # else # define VARIABLE_LIBPATH 0 # endif # if VARIABLE_LIBPATH char *libpath; VALUE sopath; # else char libpath[MAXPATHLEN + 1]; # endif size_t baselen; char *p; if ( szRoot ) strncpy(libpath, szRoot, sizeof(libpath) - 1); else { #if defined _WIN32 || defined __CYGWIN__ # if VARIABLE_LIBPATH sopath = rb_str_new(0, MAXPATHLEN); libpath = RSTRING_PTR(sopath); GetModuleFileName(libruby, libpath, MAXPATHLEN); # else GetModuleFileName(libruby, libpath, sizeof libpath); # endif #elif defined(__EMX__) _execname(libpath, sizeof(libpath) - 1); #elif defined(HAVE_DLADDR) Dl_info dli; if (dladdr((void *)(VALUE)expand_include_path, &dli)) { VALUE fname = rb_str_new_cstr(dli.dli_fname); sopath = rb_file_absolute_path(fname, Qnil); rb_str_resize(fname, 0); } else { sopath = rb_str_new(0, 0); } libpath = RSTRING_PTR(sopath); #endif } //RHO #if !VARIABLE_LIBPATH libpath[sizeof(libpath) - 1] = '\0'; #endif #if defined DOSISH translit_char(libpath, '\\', '/'); #elif defined __CYGWIN__ { # if VARIABLE_LIBPATH const int win_to_posix = CCP_WIN_A_TO_POSIX | CCP_RELATIVE; size_t newsize = cygwin_conv_path(win_to_posix, libpath, 0, 0); if (newsize > 0) { VALUE rubylib = rb_str_new(0, newsize); p = RSTRING_PTR(rubylib); if (cygwin_conv_path(win_to_posix, libpath, p, newsize) == 0) { rb_str_resize(sopath, 0); sopath = rubylib; libpath = p; } } # else char rubylib[FILENAME_MAX]; cygwin_conv_to_posix_path(libpath, rubylib); strncpy(libpath, rubylib, sizeof(libpath)); # endif } #endif p = strrchr(libpath, '/'); if (p) { *p = 0; if (p - libpath > 3 && !(STRCASECMP(p - 4, "/bin") && strcmp(p - 4, "/lib"))) { p -= 4; *p = 0; } } #if !VARIABLE_LIBPATH else { strlcpy(libpath, ".", sizeof(libpath)); p = libpath + 1; } baselen = p - libpath; #define PREFIX_PATH() rb_str_new(libpath, baselen) #else baselen = p - libpath; rb_str_set_len(sopath, baselen); libpath = RSTRING_PTR(sopath); #define PREFIX_PATH() sopath #endif #define BASEPATH() rb_str_buf_cat(rb_str_buf_new(baselen+len), libpath, baselen) #define RUBY_RELATIVE(path, len) rb_str_buf_cat(BASEPATH(), path, len) #else static const char exec_prefix[] = RUBY_EXEC_PREFIX; #define RUBY_RELATIVE(path, len) rubylib_mangled_path(path, len) #define PREFIX_PATH() RUBY_RELATIVE(exec_prefix, sizeof(exec_prefix)-1) #endif load_path = GET_VM()->load_path; if (safe_level == 0) { ruby_push_include(getenv("RUBYLIB"), identical_path); } id_initial_load_path_mark = rb_intern_const("@gem_prelude_index"); while (*paths) { size_t len = strlen(paths); VALUE path = RUBY_RELATIVE(paths, len); rb_ivar_set(path, id_initial_load_path_mark, path); rb_ary_push(load_path, path); paths += len + 1; } rb_const_set(rb_cObject, rb_intern_const("TMP_RUBY_PREFIX"), rb_obj_freeze(PREFIX_PATH())); }
/** * Returns the full path to the executable. * * @returns IPRT status code. * @param pszPath Where to store it. * @param cchPath How big that buffer is. */ static void supR3HardenedGetFullExePath(void) { /* * Get the program filename. * * Most UNIXes have no API for obtaining the executable path, but provides a symbolic * link in the proc file system that tells who was exec'ed. The bad thing about this * is that we have to use readlink, one of the weirder UNIX APIs. * * Darwin, OS/2 and Windows all have proper APIs for getting the program file name. */ #if defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD) || defined(RT_OS_SOLARIS) # ifdef RT_OS_LINUX int cchLink = readlink("/proc/self/exe", &g_szSupLibHardenedExePath[0], sizeof(g_szSupLibHardenedExePath) - 1); # elif defined(RT_OS_SOLARIS) char szFileBuf[PATH_MAX + 1]; sprintf(szFileBuf, "/proc/%ld/path/a.out", (long)getpid()); int cchLink = readlink(szFileBuf, &g_szSupLibHardenedExePath[0], sizeof(g_szSupLibHardenedExePath) - 1); # else /* RT_OS_FREEBSD */ int aiName[4]; aiName[0] = CTL_KERN; aiName[1] = KERN_PROC; aiName[2] = KERN_PROC_PATHNAME; aiName[3] = getpid(); size_t cbPath = sizeof(g_szSupLibHardenedExePath); if (sysctl(aiName, RT_ELEMENTS(aiName), g_szSupLibHardenedExePath, &cbPath, NULL, 0) < 0) supR3HardenedFatal("supR3HardenedExecDir: sysctl failed\n"); g_szSupLibHardenedExePath[sizeof(g_szSupLibHardenedExePath) - 1] = '\0'; int cchLink = strlen(g_szSupLibHardenedExePath); /* paranoid? can't we use cbPath? */ # endif if (cchLink < 0 || cchLink == sizeof(g_szSupLibHardenedExePath) - 1) supR3HardenedFatal("supR3HardenedExecDir: couldn't read \"%s\", errno=%d cchLink=%d\n", g_szSupLibHardenedExePath, errno, cchLink); g_szSupLibHardenedExePath[cchLink] = '\0'; #elif defined(RT_OS_OS2) || defined(RT_OS_L4) _execname(g_szSupLibHardenedExePath, sizeof(g_szSupLibHardenedExePath)); #elif defined(RT_OS_DARWIN) const char *pszImageName = _dyld_get_image_name(0); if (!pszImageName) supR3HardenedFatal("supR3HardenedExecDir: _dyld_get_image_name(0) failed\n"); size_t cchImageName = strlen(pszImageName); if (!cchImageName || cchImageName >= sizeof(g_szSupLibHardenedExePath)) supR3HardenedFatal("supR3HardenedExecDir: _dyld_get_image_name(0) failed, cchImageName=%d\n", cchImageName); memcpy(g_szSupLibHardenedExePath, pszImageName, cchImageName + 1); #elif defined(RT_OS_WINDOWS) HMODULE hExe = GetModuleHandle(NULL); if (!GetModuleFileName(hExe, &g_szSupLibHardenedExePath[0], sizeof(g_szSupLibHardenedExePath))) supR3HardenedFatal("supR3HardenedExecDir: GetModuleFileName failed, rc=%d\n", GetLastError()); #else # error needs porting. #endif /* * Strip off the filename part (RTPathStripFilename()). */ strcpy(g_szSupLibHardenedDirPath, g_szSupLibHardenedExePath); suplibHardenedPathStripFilename(g_szSupLibHardenedDirPath); }
void ruby_init_loadpath_safe(int safe_level) { VALUE load_path; ID id_initial_load_path_mark; extern const char ruby_initial_load_paths[]; const char *paths = ruby_initial_load_paths; #if defined LOAD_RELATIVE # if defined HAVE_DLADDR || defined HAVE_CYGWIN_CONV_PATH # define VARIABLE_LIBPATH 1 # else # define VARIABLE_LIBPATH 0 # endif # if VARIABLE_LIBPATH char *libpath; VALUE sopath; # else char libpath[MAXPATHLEN + 1]; # endif size_t baselen; char *p; #if defined _WIN32 || defined __CYGWIN__ # if VARIABLE_LIBPATH sopath = rb_str_new(0, MAXPATHLEN); libpath = RSTRING_PTR(sopath); GetModuleFileName(libruby, libpath, MAXPATHLEN); # else GetModuleFileName(libruby, libpath, sizeof libpath); # endif #elif defined(__EMX__) _execname(libpath, sizeof(libpath) - 1); #elif defined(HAVE_DLADDR) Dl_info dli; if (dladdr((void *)(VALUE)expand_include_path, &dli)) { char fbuf[MAXPATHLEN]; char *f = dln_find_file_r(dli.dli_fname, getenv(PATH_ENV), fbuf, sizeof(fbuf)); VALUE fname = rb_str_new_cstr(f ? f : dli.dli_fname); rb_str_freeze(fname); sopath = rb_realpath_internal(Qnil, fname, 1); } else { sopath = rb_str_new(0, 0); } libpath = RSTRING_PTR(sopath); #endif #if !VARIABLE_LIBPATH libpath[sizeof(libpath) - 1] = '\0'; #endif #if defined DOSISH translit_char(libpath, '\\', '/'); #elif defined __CYGWIN__ { # if VARIABLE_LIBPATH const int win_to_posix = CCP_WIN_A_TO_POSIX | CCP_RELATIVE; size_t newsize = cygwin_conv_path(win_to_posix, libpath, 0, 0); if (newsize > 0) { VALUE rubylib = rb_str_new(0, newsize); p = RSTRING_PTR(rubylib); if (cygwin_conv_path(win_to_posix, libpath, p, newsize) == 0) { rb_str_resize(sopath, 0); sopath = rubylib; libpath = p; } } # else char rubylib[FILENAME_MAX]; cygwin_conv_to_posix_path(libpath, rubylib); strncpy(libpath, rubylib, sizeof(libpath)); # endif } #endif p = strrchr(libpath, '/'); if (p) { static const char bindir[] = "/bin"; #ifdef LIBDIR_BASENAME static const char libdir[] = "/"LIBDIR_BASENAME; #else static const char libdir[] = "/lib"; #endif const ptrdiff_t bindir_len = (ptrdiff_t)sizeof(bindir) - 1; const ptrdiff_t libdir_len = (ptrdiff_t)sizeof(libdir) - 1; *p = 0; if (p - libpath >= bindir_len && !STRCASECMP(p - bindir_len, bindir)) { p -= bindir_len; *p = 0; } else if (p - libpath >= libdir_len && !STRCASECMP(p - libdir_len, libdir)) { p -= libdir_len; *p = 0; } } #if !VARIABLE_LIBPATH else { strlcpy(libpath, ".", sizeof(libpath)); p = libpath + 1; } baselen = p - libpath; #define PREFIX_PATH() rb_str_new(libpath, baselen) #else baselen = p - libpath; rb_str_resize(sopath, baselen); libpath = RSTRING_PTR(sopath); #define PREFIX_PATH() sopath #endif #define BASEPATH() rb_str_buf_cat(rb_str_buf_new(baselen+len), libpath, baselen) #define RUBY_RELATIVE(path, len) rb_str_buf_cat(BASEPATH(), (path), (len)) #else static const char exec_prefix[] = RUBY_EXEC_PREFIX; #define RUBY_RELATIVE(path, len) rubylib_mangled_path((path), (len)) #define PREFIX_PATH() RUBY_RELATIVE(exec_prefix, sizeof(exec_prefix)-1) #endif load_path = GET_VM()->load_path; if (safe_level == 0) { #ifdef MANGLED_PATH rubylib_mangled_path("", 0); #endif ruby_push_include(getenv("RUBYLIB"), identical_path); } id_initial_load_path_mark = rb_intern_const("@gem_prelude_index"); while (*paths) { size_t len = strlen(paths); VALUE path = RUBY_RELATIVE(paths, len); rb_ivar_set(path, id_initial_load_path_mark, path); rb_ary_push(load_path, path); paths += len + 1; } rb_const_set(rb_cObject, rb_intern_const("TMP_RUBY_PREFIX"), rb_obj_freeze(PREFIX_PATH())); }
/** * Initializes the shared structures. */ static void shared_init() { APIRET arc; int rc; arc = DosExitList(EXLST_ADD, ProcessExit); ASSERT_MSG(arc == NO_ERROR, "%ld", arc); #if defined(TRACE_ENABLED) && !defined(TRACE_USE_LIBC_LOG) /* * Allocate a larger buffer to fit lengthy TRACE messages and disable * auto-flush on EOL (to avoid breaking lines by stdout operations * from other threads/processes). */ setvbuf(stdout, NULL, _IOFBF, 0x10000); #endif while (1) { /* First, try to open the mutex */ arc = DosOpenMutexSem(MUTEX_LIBCX, &gMutex); TRACE("DosOpenMutexSem = %lu\n", arc); if (arc == NO_ERROR) { /* * Init is (being) done by another process, request the mutex to * guarantee shared memory is already alloated, then get access to * it and open shared heap located in that memory. */ arc = DosRequestMutexSem(gMutex, SEM_INDEFINITE_WAIT); ASSERT_MSG(arc == NO_ERROR, "%ld", arc); if (arc == NO_ERROR) { arc = DosGetNamedSharedMem((PPVOID)&gpData, SHAREDMEM_LIBCX, PAG_READ | PAG_WRITE); TRACE("DosGetNamedSharedMem = %lu\n", arc); if (arc) { /* * This failure means that another process was too fast to do what * it wanted and initiated global uninitialization before we got the * mutex so shared memory was already freed by this time. Retry. */ DosReleaseMutexSem(gMutex); DosCloseMutexSem(gMutex); continue; } /* * It's an ordinary LIBCx process. Increase coutners. */ TRACE("gpData->heap = %p\n", gpData->heap); ASSERT(gpData->heap); rc = _uopen(gpData->heap); ASSERT_MSG(rc == 0, "%d (%d)", rc, errno); ASSERT(gpData->refcnt); gpData->refcnt++; TRACE("gpData->refcnt = %d\n", gpData->refcnt); } break; } if (arc == ERROR_SEM_NOT_FOUND) { /* We are the first process, create the mutex */ arc = DosCreateMutexSem(MUTEX_LIBCX, &gMutex, 0, TRUE); TRACE("DosCreateMutexSem = %ld\n", arc); if (arc == ERROR_DUPLICATE_NAME) { /* Another process is faster, attempt to open the mutex again */ continue; } } ASSERT_MSG(arc == NO_ERROR, "%ld", arc); /* * It's a process that successfully created the main mutex, i.e. the first * LIBCx process. Proceed with the initial setup by allocating shared * memory and heap. */ /* Allocate shared memory */ arc = DosAllocSharedMem((PPVOID)&gpData, SHAREDMEM_LIBCX, HEAP_SIZE, PAG_READ | PAG_WRITE | OBJ_ANY); TRACE("DosAllocSharedMem(OBJ_ANY) = %ld\n", arc); if (arc && arc != ERROR_ALREADY_EXISTS) { /* High memory may be unavailable, try w/o OBJ_ANY */ arc = DosAllocSharedMem((PPVOID)&gpData, SHAREDMEM_LIBCX, HEAP_SIZE, PAG_READ | PAG_WRITE); TRACE("DosAllocSharedMem = %ld\n", arc); } ASSERT_MSG(arc == NO_ERROR, "%ld", arc); TRACE("gpData %p\n", gpData); /* Commit the initial block */ arc = DosSetMem(gpData, HEAP_INIT_SIZE, PAG_DEFAULT | PAG_COMMIT); ASSERT_MSG(arc == NO_ERROR, "%ld", arc); gpData->size = HEAP_INIT_SIZE; /* Create shared heap */ gpData->heap = _ucreate(gpData + 1, HEAP_INIT_SIZE - sizeof(*gpData), _BLOCK_CLEAN, _HEAP_REGULAR | _HEAP_SHARED, mem_alloc, NULL); TRACE("gpData->heap = %p\n", gpData->heap); ASSERT(gpData->heap); rc = _uopen(gpData->heap); ASSERT_MSG(rc == 0, "%d (%d)", rc, errno); gpData->refcnt = 1; /* Initialize common structures */ GLOBAL_NEW_ARRAY(gpData->procs, PROC_DESC_HASH_SIZE); TRACE("gpData->procs = %p\n", gpData->procs); ASSERT(gpData->procs); GLOBAL_NEW_ARRAY(gpData->files, FILE_DESC_HASH_SIZE); TRACE("gpData->files = %p\n", gpData->files); ASSERT(gpData->files); break; } /* * Perform common initialization (both for the first and ordinary processes). */ /* Make sure a process description for this process is created */ ProcDesc *proc = get_proc_desc(getpid()); ASSERT(proc); /* Initialize individual components */ mmap_init(proc); fcntl_locking_init(proc); /* Check if it's a spawn2 wrapper (e.g. spawn2-wrapper.c) */ { char dll[CCHMAXPATH + sizeof(SPAWN2_WRAPPERNAME) + 1]; if (get_module_name(dll, sizeof(dll))) { strcpy(_getname(dll), SPAWN2_WRAPPERNAME); char exe[CCHMAXPATH + 1]; if (_execname(exe, sizeof(exe)) == 0 && stricmp(dll, exe) == 0) { proc->flags |= Proc_Spawn2Wrapper; TRACE("spawn2 wrapper\n"); /* Make sure the semaphore is available (needed for spawn2-wrapper.c) */ ASSERT(gpData->spawn2_sem); global_spawn2_sem(proc); } } } DosReleaseMutexSem(gMutex); TRACE("done\n"); }
void ruby_init_loadpath_safe(int safe_level) { VALUE load_path; extern const char ruby_initial_load_paths[]; const char *paths = ruby_initial_load_paths; #if defined LOAD_RELATIVE # if defined HAVE_DLADDR || (defined __CYGWIN__ && defined CCP_WIN_A_TO_POSIX) # define VARIABLE_LIBPATH 1 # else # define VARIABLE_LIBPATH 0 # endif # if VARIABLE_LIBPATH char *libpath; VALUE sopath; # else char libpath[MAXPATHLEN + 1]; size_t baselen; # endif char *p; #if defined _WIN32 || defined __CYGWIN__ # if VARIABLE_LIBPATH sopath = rb_str_tmp_new(MAXPATHLEN); libpath = RSTRING_PTR(sopath); GetModuleFileName(libruby, libpath, MAXPATHLEN); # else GetModuleFileName(libruby, libpath, sizeof libpath); # endif #elif defined(__EMX__) _execname(libpath, sizeof(libpath) - 1); #elif defined(HAVE_DLADDR) Dl_info dli; if (dladdr(expand_include_path, &dli)) { VALUE fname = rb_str_new_cstr(dli.dli_fname); sopath = rb_file_absolute_path(fname, Qnil); rb_str_resize(fname, 0); } else { sopath = rb_str_new(0, 0); } libpath = RSTRING_PTR(sopath); #endif #if !VARIABLE_LIBPATH libpath[sizeof(libpath) - 1] = '\0'; #endif #if defined DOSISH translit_char(libpath, '\\', '/'); #elif defined __CYGWIN__ { # if VARIABLE_LIBPATH const int win_to_posix = CCP_WIN_A_TO_POSIX | CCP_RELATIVE; size_t newsize = cygwin_conv_path(win_to_posix, libpath, 0, 0); if (newsize > 0) { VALUE rubylib = rb_str_tmp_new(newsize); p = RSTRING_PTR(rubylib); if (cygwin_conv_path(win_to_posix, libpath, p, newsize) == 0) { rb_str_resize(sopath, 0); sopath = rubylib; libpath = p; } } # else char rubylib[FILENAME_MAX]; cygwin_conv_to_posix_path(libpath, rubylib); strncpy(libpath, rubylib, sizeof(libpath)); # endif } #endif p = strrchr(libpath, '/'); if (p) { *p = 0; if (p - libpath > 3 && !(STRCASECMP(p - 4, "/bin") && strcmp(p - 4, "/lib"))) { p -= 4; *p = 0; } } #if !VARIABLE_LIBPATH else { strlcpy(libpath, ".", sizeof(libpath)); p = libpath + 1; } baselen = p - libpath; #define BASEPATH() rb_str_buf_cat(rb_str_buf_new(baselen+len), libpath, baselen) #else rb_str_set_len(sopath, p - libpath); #define BASEPATH() rb_str_dup(sopath) #endif #define RUBY_RELATIVE(path, len) rb_str_buf_cat(BASEPATH(), path, len) #else #define RUBY_RELATIVE(path, len) rubylib_mangled_path(path, len) #endif #define incpush(path) rb_ary_push(load_path, (path)) load_path = GET_VM()->load_path; if (safe_level == 0) { ruby_push_include(getenv("RUBYLIB"), identical_path); } while (*paths) { size_t len = strlen(paths); incpush(RUBY_RELATIVE(paths, len)); paths += len + 1; } }
int main(int argc_orig, char **argv) #endif { int i; /* We want the current value of argc to persist across a LONGJMP from int_error(). * Without this the compiler may put it on the stack, which LONGJMP clobbers. * Here we try make it a volatile variable that optimization will not affect. * Why do we not have to do the same for argv? I don't know. * But the test cases that broke with generic argc seem fine with generic argv. */ static volatile int argc; argc = argc_orig; #ifdef LINUXVGA LINUX_setup(); /* setup VGA before dropping privilege DBT 4/5/99 */ drop_privilege(); #endif /* make sure that we really have revoked root access, this might happen if gnuplot is compiled without vga support but is installed suid by mistake */ #ifdef __linux__ if (setuid(getuid()) != 0) { fprintf(stderr,"gnuplot: refusing to run at elevated privilege\n"); exit(EXIT_FAILURE); } #endif /* HBB: Seems this isn't needed any more for DJGPP V2? */ /* HBB: disable all floating point exceptions, just keep running... */ #if defined(DJGPP) && (DJGPP!=2) _control87(MCW_EM, MCW_EM); #endif #if defined(OS2) { int rc; #ifdef OS2_IPC char semInputReadyName[40]; sprintf(semInputReadyName, "\\SEM32\\GP%i_Input_Ready", getpid()); rc = DosCreateEventSem(semInputReadyName, &semInputReady, 0, 0); if (rc != 0) fputs("DosCreateEventSem error\n", stderr); #endif rc = RexxRegisterSubcomExe("GNUPLOT", (PFN) RexxInterface, NULL); } #endif /* malloc large blocks, otherwise problems with fragmented mem */ #ifdef MALLOCDEBUG malloc_debug(7); #endif /* init progpath and get helpfile from executable directory */ #if defined(MSDOS) || defined(OS2) { char *s; #ifdef __EMX__ _execname(progpath, sizeof(progpath)); #else safe_strncpy(progpath, argv[0], sizeof(progpath)); #endif /* convert '/' to '\\' */ for (s = progpath; *s != NUL; s++) if (*s == DIRSEP2) *s = DIRSEP1; /* cut program name */ s = strrchr(progpath, DIRSEP1); if (s != NULL) s++; else s = progpath; *s = NUL; /* init HelpFile */ strcpy(HelpFile, progpath); strcat(HelpFile, "gnuplot.gih"); /* remove trailing "bin/" from progpath */ if ((s != NULL) && (s - progpath >= 4)) { s -= 4; if (strncasecmp(s, "bin", 3) == 0) *s = NUL; } } #endif /* DJGPP */ #if (defined(PIPE_IPC) || defined(_WIN32)) && (defined(HAVE_LIBREADLINE) || (defined(HAVE_LIBEDITLINE) && defined(X11))) /* Editline needs this to be set before the very first call to readline(). */ /* Support for rl_getc_function is broken for utf-8 in editline. Since it is only really required for X11, disable this section when building without X11. */ rl_getc_function = getc_wrapper; #endif #if defined(HAVE_LIBREADLINE) || defined(HAVE_LIBEDITLINE) /* T.Walter 1999-06-24: 'rl_readline_name' must be this fix name. * It is used to parse a 'gnuplot' specific section in '~/.inputrc' * or gnuplot specific commands in '.editrc' (when using editline * instead of readline) */ rl_readline_name = "Gnuplot"; rl_terminal_name = getenv("TERM"); #if defined(HAVE_LIBREADLINE) using_history(); #else history_init(); #endif #endif #if defined(HAVE_LIBREADLINE) && !defined(MISSING_RL_TILDE_EXPANSION) rl_complete_with_tilde_expansion = 1; #endif for (i = 1; i < argc; i++) { if (!argv[i]) continue; if (!strcmp(argv[i], "-V") || !strcmp(argv[i], "--version")) { printf("gnuplot %s patchlevel %s\n", gnuplot_version, gnuplot_patchlevel); return 0; } else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { printf( "Usage: gnuplot [OPTION] ... [FILE]\n" #ifdef X11 "for X11 options see 'help X11->command-line-options'\n" #endif " -V, --version\n" " -h, --help\n" " -p --persist\n" " -s --slow\n" " -d --default-settings\n" " -c scriptfile ARG1 ARG2 ... \n" " -e \"command1; command2; ...\"\n" "gnuplot %s patchlevel %s\n", gnuplot_version, gnuplot_patchlevel); #ifdef DEVELOPMENT_VERSION printf( #ifdef DIST_CONTACT "Report bugs to "DIST_CONTACT"\n" " or %s\n", #else "Report bugs to %s\n", #endif bug_email); #endif return 0; } else if (!strncmp(argv[i], "-persist", 2) || !strcmp(argv[i], "--persist") #ifdef _WIN32 || !stricmp(argv[i], "-noend") || !stricmp(argv[i], "/noend") #endif ) { persist_cl = TRUE; } else if (!strncmp(argv[i], "-slow", 2) || !strcmp(argv[i], "--slow")) { slow_font_startup = TRUE; } else if (!strncmp(argv[i], "-d", 2) || !strcmp(argv[i], "--default-settings")) { /* Skip local customization read from ~/.gnuplot */ skip_gnuplotrc = TRUE; } } #ifdef X11 /* the X11 terminal removes tokens that it recognizes from argv. */ { int n = X11_args(argc, argv); argv += n; argc -= n; } #endif setbuf(stderr, (char *) NULL); #ifdef HAVE_SETVBUF /* This was once setlinebuf(). Docs say this is * identical to setvbuf(,NULL,_IOLBF,0), but MS C * faults this (size out of range), so we try with * size of 1024 instead. [SAS/C does that, too. -lh] */ if (setvbuf(stdout, (char *) NULL, _IOLBF, (size_t) 1024) != 0) (void) fputs("Could not linebuffer stdout\n", stderr); /* Switching to unbuffered mode causes all characters in the input * buffer to be lost. So the only safe time to do it is on program entry. * Do any non-X platforms suffer from this problem? * EAM - Jan 2013 YES. */ setvbuf(stdin, (char *) NULL, _IONBF, 0); #endif gpoutfile = stdout; /* Initialize pre-loaded user variables */ /* "pi" is hard-wired as the first variable */ (void) add_udv_by_name("GNUTERM"); (void) add_udv_by_name("NaN"); init_constants(); udv_user_head = &(udv_NaN->next_udv); init_memory(); interactive = FALSE; /* April 2017: We used to call init_terminal() here, but now */ /* We defer initialization until error handling has been set up. */ # if defined(_WIN32) && !defined(WGP_CONSOLE) interactive = TRUE; # else interactive = isatty(fileno(stdin)); # endif /* Note: we want to know whether this is an interactive session so that we can * decide whether or not to write status information to stderr. The old test * for this was to see if (argc > 1) but the addition of optional command line * switches broke this. What we really wanted to know was whether any of the * command line arguments are file names or an explicit in-line "-e command". */ for (i = 1; i < argc; i++) { # ifdef _WIN32 if (!stricmp(argv[i], "/noend")) continue; # endif if ((argv[i][0] != '-') || (argv[i][1] == 'e') || (argv[i][1] == 'c') ) { interactive = FALSE; break; } } /* Need this before show_version is called for the first time */ if (interactive) show_version(stderr); else show_version(NULL); /* Only load GPVAL_COMPILE_OPTIONS */ update_gpval_variables(3); /* update GPVAL_ variables available to user */ #ifdef VMS /* initialise screen management routines for command recall */ { unsigned int ierror; if (ierror = smg$create_virtual_keyboard(&vms_vkid) != SS$_NORMAL) done(ierror); if (ierror = smg$create_key_table(&vms_ktid) != SS$_NORMAL) done(ierror); } #endif /* VMS */ if (!SETJMP(command_line_env, 1)) { /* first time */ interrupt_setup(); get_user_env(); init_loadpath(); init_locale(); memset(&sm_palette, 0, sizeof(sm_palette)); init_fit(); /* Initialization of fitting module */ #ifdef READLINE /* When using the built-in readline, we set the initial encoding according to the locale as this is required to properly handle keyboard input. */ init_encoding(); #endif init_gadgets(); /* April 2017: Now that error handling is in place, it is safe parse * GNUTERM during terminal initialization. * atexit processing is done in reverse order. We want * the generic terminal shutdown in term_reset to be executed before * any terminal specific cleanup requested by individual terminals. */ init_terminal(); push_terminal(0); /* remember the initial terminal */ gp_atexit(term_reset); /* Execute commands in ~/.gnuplot */ init_session(); if (interactive && term != 0) { /* not unknown */ #ifdef GNUPLOT_HISTORY #if (defined(HAVE_LIBREADLINE) || defined(HAVE_LIBEDITLINE)) && !defined(_WIN32) expanded_history_filename = tilde_expand(GNUPLOT_HISTORY_FILE); #else expanded_history_filename = gp_strdup(GNUPLOT_HISTORY_FILE); gp_expand_tilde(&expanded_history_filename); #endif read_history(expanded_history_filename); /* * It is safe to ignore the return values of 'atexit()' and * 'on_exit()'. In the worst case, there is no history of your * currrent session and you have to type all again in your next * session. */ gp_atexit(wrapper_for_write_history); #endif /* GNUPLOT_HISTORY */ #if defined(READLINE) && defined(WGP_CONSOLE) fprintf(stderr, "Encoding set to '%s'.\n", encoding_names[encoding]); #endif } /* if (interactive && term != 0) */ } else { /* come back here from int_error() */ if (!successful_initialization) { /* Only print the warning once */ successful_initialization = TRUE; fprintf(stderr,"WARNING: Error during initialization\n\n"); } if (interactive == FALSE) exit_status = EXIT_FAILURE; #ifdef HAVE_READLINE_RESET else { /* reset properly readline after a SIGINT+longjmp */ rl_reset_after_signal (); } #endif load_file_error(); /* if we were in load_file(), cleanup */ SET_CURSOR_ARROW; #ifdef VMS /* after catching interrupt */ /* VAX stuffs up stdout on SIGINT while writing to stdout, so reopen stdout. */ if (gpoutfile == stdout) { if ((stdout = freopen("SYS$OUTPUT", "w", stdout)) == NULL) { /* couldn't reopen it so try opening it instead */ if ((stdout = fopen("SYS$OUTPUT", "w")) == NULL) { /* don't use int_error here - causes infinite loop! */ fputs("Error opening SYS$OUTPUT as stdout\n", stderr); } } gpoutfile = stdout; } #endif /* VMS */ /* Why a goto? Because we exited the loop below via int_error */ /* using LONGJMP. The compiler was not expecting this, and */ /* "optimized" the handling of argc and argv such that simply */ /* entering the loop again from the top finds them messed up. */ /* If we reenter the loop via a goto then there is some hope */ /* that code reordering does not hurt us. */ if (reading_from_dash && interactive) goto RECOVER_FROM_ERROR_IN_DASH; reading_from_dash = FALSE; if (!interactive && !noinputfiles) { term_reset(); gp_exit(EXIT_FAILURE); /* exit on non-interactive error */ } } /* load filenames given as arguments */ while (--argc > 0) { ++argv; c_token = 0; if (!strncmp(*argv, "-persist", 2) || !strcmp(*argv, "--persist") #ifdef _WIN32 || !stricmp(*argv, "-noend") || !stricmp(*argv, "/noend") #endif ) { FPRINTF((stderr,"'persist' command line option recognized\n")); } else if (strcmp(*argv, "-") == 0) { #if defined(_WIN32) && !defined(WGP_CONSOLE) TextShow(&textwin); interactive = TRUE; #else interactive = isatty(fileno(stdin)); #endif RECOVER_FROM_ERROR_IN_DASH: reading_from_dash = TRUE; while (!com_line()); reading_from_dash = FALSE; interactive = FALSE; noinputfiles = FALSE; } else if (strcmp(*argv, "-e") == 0) { int save_state = interactive; --argc; ++argv; if (argc <= 0) { fprintf(stderr, "syntax: gnuplot -e \"commands\"\n"); return 0; } interactive = FALSE; noinputfiles = FALSE; do_string(*argv); interactive = save_state; } else if (!strncmp(*argv, "-slow", 2) || !strcmp(*argv, "--slow")) { slow_font_startup = TRUE; } else if (!strncmp(*argv, "-d", 2) || !strcmp(*argv, "--default-settings")) { /* Ignore this; it already had its effect */ FPRINTF((stderr, "ignoring -d\n")); } else if (strcmp(*argv, "-c") == 0) { /* Pass command line arguments to the gnuplot script in the next * argument. This consumes the remainder of the command line */ interactive = FALSE; noinputfiles = FALSE; --argc; ++argv; if (argc <= 0) { fprintf(stderr, "syntax: gnuplot -c scriptname args\n"); gp_exit(EXIT_FAILURE); } call_argc = GPMIN(9, argc - 1); for (i=0; i<=call_argc; i++) { /* Need to stash argv[i] somewhere visible to load_file() */ call_args[i] = gp_strdup(argv[i+1]); } load_file(loadpath_fopen(*argv, "r"), gp_strdup(*argv), 5); gp_exit(EXIT_SUCCESS); } else if (*argv[0] == '-') { fprintf(stderr, "unrecognized option %s\n", *argv); } else { interactive = FALSE; noinputfiles = FALSE; load_file(loadpath_fopen(*argv, "r"), gp_strdup(*argv), 4); } } /* take commands from stdin */ if (noinputfiles) { while (!com_line()) ctrlc_flag = FALSE; /* reset asynchronous Ctrl-C flag */ } #ifdef _WIN32 /* On Windows, handle 'persist' by keeping the main input loop running (windows/wxt), */ /* but only if there are any windows open. Note that qt handles this properly. */ if (persist_cl) { if (WinAnyWindowOpen()) { #ifdef WGP_CONSOLE if (!interactive) { /* no further input from pipe */ while (WinAnyWindowOpen()) win_sleep(100); } else #endif { interactive = TRUE; while (!com_line()) ctrlc_flag = FALSE; /* reset asynchronous Ctrl-C flag */ interactive = FALSE; } } } #endif #if (defined(HAVE_LIBREADLINE) || defined(HAVE_LIBEDITLINE)) && defined(GNUPLOT_HISTORY) #if !defined(HAVE_ATEXIT) && !defined(HAVE_ON_EXIT) /* You should be here if you neither have 'atexit()' nor 'on_exit()' */ wrapper_for_write_history(); #endif /* !HAVE_ATEXIT && !HAVE_ON_EXIT */ #endif /* (HAVE_LIBREADLINE || HAVE_LIBEDITLINE) && GNUPLOT_HISTORY */ #ifdef OS2 RexxDeregisterSubcom("GNUPLOT", NULL); #endif /* HBB 20040223: Not all compilers like exit() to end main() */ /* exit(exit_status); */ #if ! defined(_WIN32) /* Windows does the cleanup later */ gp_exit_cleanup(); #endif return exit_status; }