FILE *tempfile(void) { rmtmpfile(); /* make sure the old temp file is already closed */ tfn = tmpfn(); if (!tfn) return 0; return tfp = fopen(tfn, "w+b"); }
CompilerPtr CompilerLocatorGCC::Locate(const wxString& folder) { m_compilers.clear(); wxFileName gcc(folder, "gcc"); wxFileName tmpfn(folder, ""); wxString name; if( tmpfn.GetDirCount() > 1 && tmpfn.GetDirs().Last() == "bin" ) { tmpfn.RemoveLastDir(); name = tmpfn.GetDirs().Last(); } #ifdef __WXMSW__ gcc.SetExt("exe"); #endif bool found = gcc.FileExists(); if ( ! found ) { // try to see if we have a bin folder here gcc.AppendDir("bin"); found = gcc.FileExists(); } if ( found ) { CompilerPtr compiler( new Compiler(NULL) ); compiler->SetCompilerFamily(COMPILER_FAMILY_GCC); // get the compiler version compiler->SetName( name.IsEmpty() ? "GCC" : name ); compiler->SetGenerateDependeciesFile(true); m_compilers.push_back( compiler ); // we path the bin folder AddTools(compiler, gcc.GetPath()); return compiler; } return NULL; }
/* * process the command line and execute the appropriate functions * full input/output redirection and piping are supported */ void parsecommandline(char *s) { char *in = NULL; char *out = NULL; char *fname0 = NULL; char *fname1 = NULL; char *nextcmd; int of_attrib = O_CREAT | O_TRUNC | O_TEXT | O_WRONLY; int num; /* first thing we do is alias expansion */ assert(s); assert(oldinfd == -1); /* if fails something is wrong; should NEVER */ assert(oldoutfd == -1); /* happen! -- 2000/01/13 ska*/ dprintf(("[parsecommandline (%s)]\n", s)); #ifdef FEATURE_ALIASES aliasexpand(s, MAX_INTERNAL_COMMAND_SIZE); dprintf(("[alias expanded to (%s)]\n", s)); #endif if (tracemode) { /* Question after the variables expansion and make sure _all_ executed commands will honor the trace mode */ printf("%s [Enter=Yes, ESC=No] ", s); /* If the user hits ^Break, it has the same effect as usually: If he is in a batch file, he is asked if to abort all the batchfiles or just the current one */ if (!strchr("Y\r\n", vcgetcstr("\x1bYN\r\n"))) /* Pressed either "No" or ^Break */ return; } num = get_redirection(s, &in, &out, &of_attrib); if (num < 0) /* error */ goto abort; /* Set up the initial conditions ... */ if (in || (num > 1)) /* Need to preserve stdin */ oldinfd = dup(0); if (in) /* redirect input from this file name */ { close(0); if (0 != devopen(in, O_TEXT | O_RDONLY, S_IREAD)) { displayString(TEXT_ERROR_REDIRECT_FROM_FILE, in); goto abort; } } if (out || (num > 1)) /* Need to preserve stdout */ oldoutfd = dup(1); /* Now do all but the last pipe command */ while (num-- > 1) { close(1); /* Close current output file */ if ((fname0 = tmpfn()) == NULL) goto abort; open(fname0, O_CREAT | O_TRUNC | O_TEXT | O_WRONLY, S_IREAD | S_IWRITE); nextcmd = s + strlen(s) + 1; docommand(s); close(1); dup2(oldoutfd, 1); close(0); killtmpfn(fname1); /* fname1 can by NULL */ fname1 = fname0; fname0 = NULL; open(fname1, O_TEXT | O_RDONLY, S_IREAD); s = nextcmd; } /* Now set up the end conditions... */ if (out) /* Final output to here */ { close(1); if (1 != devopen(out, of_attrib, S_IREAD | S_IWRITE)) { displayString(TEXT_ERROR_REDIRECT_TO_FILE, out); goto abort; } if (of_attrib & O_APPEND) lseek(1, 0, SEEK_END); } else if (oldoutfd != -1) /* Restore original stdout */ { close(1); dup2(oldoutfd, 1); close(oldoutfd); oldoutfd = -1; } docommand(s); /* process final command */ abort: if (oldinfd != -1) /* Restore original STDIN */ { close(0); dup2(oldinfd, 0); close(oldinfd); oldinfd = -1; } if (oldoutfd != -1) /* Restore original STDOUT */ { close(1); dup2(oldoutfd, 1); close(oldoutfd); oldoutfd = -1; } killtmpfn(fname1); killtmpfn(fname0); if (out) free(out); if (in) free(in); }
bool C4StartupNetDlg::DoOK() { // OK in chat mode? Forward to chat control if (GetDlgMode() == SNDM_Chat) return pChatCtrl->DlgEnter(); // OK on editbox with text enetered: Add the specified IP for reference retrieval if (GetFocus() == pJoinAddressEdt) { const char *szDirectJoinAddress = pJoinAddressEdt->GetText(); if (szDirectJoinAddress && *szDirectJoinAddress) { // First do some acrobatics to avoid trying to resolve addresses with leading // or trailing whitespace, which is easily pasted in with an IP address. // We can trivially skip whitespace at the beginning, but we need a copy to // omit whitespace at the end. while (std::isspace(*szDirectJoinAddress)) // skip whitespace at the beginning ++szDirectJoinAddress; if (!*szDirectJoinAddress) // entry empty, apart from whitespace return true; const char *szDirectJoinAddressEnd = szDirectJoinAddress + std::strlen(szDirectJoinAddress) - 1; while (std::isspace(*szDirectJoinAddressEnd)) // skip whitespace at the end --szDirectJoinAddressEnd; if (*++szDirectJoinAddressEnd) { // Make a temporary copy of the part that is not trailing whitespace, if any std::string strDirectJoinAddressStripped(szDirectJoinAddress, szDirectJoinAddressEnd - szDirectJoinAddress); AddReferenceQuery(strDirectJoinAddressStripped.c_str(), C4StartupNetListEntry::NRQT_DirectJoin); } else AddReferenceQuery(szDirectJoinAddress, C4StartupNetListEntry::NRQT_DirectJoin); // Switch focus to list so another OK joins the specified address SetFocus(pGameSelList, true); return true; } } if (GetFocus() == pSearchFieldEdt) { UpdateList(); return true; } // get currently selected item C4GUI::Element *pSelection = pGameSelList->GetSelectedItem(); StdCopyStrBuf strNoJoin(LoadResStr("IDS_NET_NOJOIN")); if (!pSelection) { // no ref selected: Oh noes! ::pGUI->ShowMessageModal( LoadResStr("IDS_NET_NOJOIN_NOREF"), strNoJoin.getData(), C4GUI::MessageDialog::btnOK, C4GUI::Ico_Error); return true; } C4StartupNetListEntry *pRefEntry = static_cast<C4StartupNetListEntry *>(pSelection); const char *szError; if ((szError = pRefEntry->GetError())) { // erroneous ref selected: Oh noes! ::pGUI->ShowMessageModal( FormatString(LoadResStr("IDS_NET_NOJOIN_BADREF"), szError).getData(), strNoJoin.getData(), C4GUI::MessageDialog::btnOK, C4GUI::Ico_Error); return true; } C4Network2Reference *pRef = pRefEntry->GetReference(); const char *szDirectJoinAddress = pRefEntry->GetJoinAddress(); if (!pRef && !(szDirectJoinAddress && *szDirectJoinAddress)) { // something strange has been selected (e.g., a masterserver entry). Error. ::pGUI->ShowMessageModal( LoadResStr("IDS_NET_NOJOIN_NOREF"), strNoJoin.getData(), C4GUI::MessageDialog::btnOK, C4GUI::Ico_Error); return true; } // check if join to this reference is possible at all if (pRef) { // version mismatch C4GameVersion verThis; if (!(pRef->getGameVersion() == verThis)) { ::pGUI->ShowMessageModal( FormatString(LoadResStr("IDS_NET_NOJOIN_BADVER"), pRef->getGameVersion().GetString().getData(), verThis.GetString().getData()).getData(), strNoJoin.getData(), C4GUI::MessageDialog::btnOK, C4GUI::Ico_Error); return true; } if (pRef->getGameStatus().isPastLobby()) { // no runtime join if (!pRef->isJoinAllowed()) { if (!::pGUI->ShowMessageModal( LoadResStr("IDS_NET_NOJOIN_NORUNTIME"), strNoJoin.getData(), C4GUI::MessageDialog::btnYes | C4GUI::MessageDialog::btnNo, C4GUI::Ico_Error)) { return true; } } else { if (!::pGUI->ShowMessageModal( LoadResStr("IDS_NET_NOJOIN_RUNTIMEBROKEN"), strNoJoin.getData(), C4GUI::MessageDialog::btnYes | C4GUI::MessageDialog::btnNo, C4GUI::Ico_Error)) { return true; } } } } // OK; joining! if (pRef->isEditor()) { bool success = false; // Editor mode join: Serialize reference to temp file and join on that // (could pass through environment, but that's hard to do platform-independent // (QProcessEnvironment? But then there's a Qt dependency in the network init code)) StdStrBuf tmpfn(Config.AtTempPath("ocjoin"), true); MakeTempFilename(&tmpfn); StdStrBuf join_data = DecompileToBuf<StdCompilerINIWrite>(mkNamingAdapt(*pRef, "Reference")); if (join_data.getSize()) { if (join_data.SaveToFile(tmpfn.getData())) { if (RestartApplication({"--editor", FormatString("--join=%s%s", C4Game::DirectJoinFilePrefix, tmpfn.getData()).getData()})) // hope for no " in temp path { // Application.Quit() has been called. Will quit after returning from this callback. // The temp file will be deleted by the new instance success = true; } else { EraseFile(tmpfn.getData()); } } } if (!success) { C4GUI::TheScreen.ShowErrorMessage(LoadResStr("IDS_ERR_STARTEDITOR")); } return true; } else { // Player mode join // Take over reference pRefEntry->GrabReference(); // Set join parameters *Game.ScenarioFilename = '\0'; if (szDirectJoinAddress) SCopy(szDirectJoinAddress, Game.DirectJoinAddress, _MAX_PATH); else *Game.DirectJoinAddress = '\0'; SCopy("Objects.ocd", Game.DefinitionFilenames); Game.NetworkActive = true; Game.fObserve = false; Game.pJoinReference.reset(pRef); // start with this set! Application.OpenGame(); return true; } }
/* * process the command line and execute the appropriate functions * full input/output redirection and piping are supported */ void parsecommandline(char *s, int redirect) { char *in = 0; char *out = 0; char *fname0 = 0; char *fname1 = 0; char *nextcmd; int of_attrib = O_CREAT | O_TRUNC | O_WRONLY; int num; assert(s); dprintf(("[parsecommandline (%s)]\n", s)); #ifdef FEATURE_ALIASES aliasexpand(s, MAX_INTERNAL_COMMAND_SIZE); dprintf(("[alias expanded to (%s)]\n", s)); #endif if (tracemode) { /* Question after the variables expansion and make sure _all_ executed commands will honor the trace mode */ /* * Commands may be nested ("if errorlevel 1 echo done>test"). To * prevent redirecting FreeCOM prompts to user file, temporarily * revert redirection. */ int redir_fdin, redir_fdout, answer; if (oldinfd != -1) { redir_fdin = dup (0); dup2 (oldinfd, 0); } if (oldoutfd != -1) { redir_fdout = dup (1); dup2 (oldoutfd, 1); } printprompt(); outs(s); /* If the user hits ^Break, it has the same effect as usually: If he is in a batch file, he is asked if to abort all the batchfiles or just the current one */ answer = userprompt(PROMPT_YES_NO); if (oldinfd != -1) { dup2 (redir_fdin, 0); dos_close (redir_fdin); } if (oldoutfd != -1) { dup2 (redir_fdout, 1); dos_close (redir_fdout); } if (answer != 1) return; /* "No" or ^Break */ } if(!redirect) { docommand(s); return; } assert(oldinfd == -1); /* if fails something is wrong; should NEVER */ assert(oldoutfd == -1); /* happen! -- 2000/01/13 ska*/ num = get_redirection(s, &in, &out, &of_attrib); if (num < 0) /* error */ goto abort; /* Set up the initial conditions ... */ if (in || (num > 1)) /* Need to preserve stdin */ oldinfd = dup(0); if (in) /* redirect input from this file name */ { dos_close(0); if (0 != devopen(in, O_RDONLY)) { error_redirect_from_file(in); goto abort; } } if (out || (num > 1)) /* Need to preserve stdout */ oldoutfd = dup(1); /* Now do all but the last pipe command */ while (num-- > 1) { dos_close(1); /* Close current output file */ if ((fname0 = tmpfn()) == 0) goto abort; dos_creat(fname0, 0); nextcmd = s + strlen(s) + 1; docommand(s); dos_close(1); dup2(oldoutfd, 1); dos_close(0); killtmpfn(fname1); /* fname1 can by NULL */ fname1 = fname0; fname0 = 0; dos_open(fname1, O_RDONLY); s = nextcmd; } /* Now set up the end conditions... */ if (out) /* Final output to here */ { dos_close(1); if (1 != devopen(out, of_attrib)) { error_redirect_to_file(out); goto abort; } } else if (oldoutfd != -1) /* Restore original stdout */ { dos_close(1); dup2(oldoutfd, 1); dos_close(oldoutfd); oldoutfd = -1; } docommand(s); /* process final command */ abort: if (oldinfd != -1) /* Restore original STDIN */ { dos_close(0); dup2(oldinfd, 0); dos_close(oldinfd); oldinfd = -1; } if (oldoutfd != -1) /* Restore original STDOUT */ { dos_close(1); dup2(oldoutfd, 1); dos_close(oldoutfd); oldoutfd = -1; } killtmpfn(fname1); killtmpfn(fname0); if (out) free(out); if (in) free(in); }
// Tests for functions that are changed by ShouldFollowLink() void FileNameTestCase::TestSymlinks() { const wxString tmpdir(wxStandardPaths::Get().GetTempDir()); wxFileName tmpfn(wxFileName::DirName(tmpdir)); // Create a temporary directory #ifdef __VMS wxString name = tmpdir + ".filenametestXXXXXX]"; mkdir( name.char_str() , 0222 ); wxString tempdir = name; #else wxString name = tmpdir + "/filenametestXXXXXX"; wxString tempdir = wxString::From8BitData(mkdtemp(name.char_str())); tempdir << wxFileName::GetPathSeparator(); #endif wxFileName tempdirfn(wxFileName::DirName(tempdir)); CPPUNIT_ASSERT(tempdirfn.DirExists()); // Create a regular file in that dir, to act as a symlink target wxFileName targetfn(wxFileName::CreateTempFileName(tempdir)); CPPUNIT_ASSERT(targetfn.FileExists()); // Create a symlink to that file wxFileName linktofile(tempdir, "linktofile"); CPPUNIT_ASSERT_EQUAL(0, symlink(targetfn.GetFullPath().c_str(), linktofile.GetFullPath().c_str())); // ... and another to the temporary directory const wxString linktodirName(tempdir + "/linktodir"); wxFileName linktodir(wxFileName::DirName(linktodirName)); CPPUNIT_ASSERT_EQUAL(0, symlink(tmpfn.GetFullPath().c_str(), linktodirName.c_str())); // And symlinks to both of those symlinks wxFileName linktofilelnk(tempdir, "linktofilelnk"); CPPUNIT_ASSERT_EQUAL(0, symlink(linktofile.GetFullPath().c_str(), linktofilelnk.GetFullPath().c_str())); wxFileName linktodirlnk(tempdir, "linktodirlnk"); CPPUNIT_ASSERT_EQUAL(0, symlink(linktodir.GetFullPath().c_str(), linktodirlnk.GetFullPath().c_str())); // Run the tests twice: once in the default symlink following mode and the // second time without following symlinks. bool deref = true; for ( int n = 0; n < 2; ++n, deref = !deref ) { const std::string msg(deref ? " failed for the link target" : " failed for the path itself"); if ( !deref ) { linktofile.DontFollowLink(); linktodir.DontFollowLink(); linktofilelnk.DontFollowLink(); linktodirlnk.DontFollowLink(); } // Test SameAs() CPPUNIT_ASSERT_EQUAL_MESSAGE ( "Comparison with file" + msg, deref, linktofile.SameAs(targetfn) ); CPPUNIT_ASSERT_EQUAL_MESSAGE ( "Comparison with directory" + msg, deref, linktodir.SameAs(tmpfn) ); // A link-to-a-link should dereference through to the final target CPPUNIT_ASSERT_EQUAL_MESSAGE ( "Comparison with link to a file" + msg, deref, linktofilelnk.SameAs(targetfn) ); CPPUNIT_ASSERT_EQUAL_MESSAGE ( "Comparison with link to a directory" + msg, deref, linktodirlnk.SameAs(tmpfn) ); // Test GetTimes() wxDateTime dtAccess, dtMod, dtCreate; CPPUNIT_ASSERT_MESSAGE ( "Getting times of a directory" + msg, linktodir.GetTimes(&dtAccess, &dtMod, &dtCreate) ); // Test (File|Dir)Exists() CPPUNIT_ASSERT_EQUAL_MESSAGE ( "Testing file existence" + msg, deref, linktofile.FileExists() ); CPPUNIT_ASSERT_EQUAL_MESSAGE ( "Testing directory existence" + msg, deref, linktodir.DirExists() ); // Test wxFileName::Exists // The wxFILE_EXISTS_NO_FOLLOW flag should override DontFollowLink() CPPUNIT_ASSERT_EQUAL_MESSAGE ( "Testing file existence" + msg, false, linktofile.Exists(wxFILE_EXISTS_REGULAR | wxFILE_EXISTS_NO_FOLLOW) ); CPPUNIT_ASSERT_EQUAL_MESSAGE ( "Testing directory existence" + msg, false, linktodir.Exists(wxFILE_EXISTS_DIR | wxFILE_EXISTS_NO_FOLLOW) ); // and the static versions CPPUNIT_ASSERT_EQUAL_MESSAGE ( "Testing file existence" + msg, false, wxFileName::Exists(linktofile.GetFullPath(), wxFILE_EXISTS_REGULAR | wxFILE_EXISTS_NO_FOLLOW) ); CPPUNIT_ASSERT_EQUAL_MESSAGE ( "Testing file existence" + msg, true, wxFileName::Exists(linktofile.GetFullPath(), wxFILE_EXISTS_REGULAR) ); CPPUNIT_ASSERT_EQUAL_MESSAGE ( "Testing directory existence" + msg, false, wxFileName::Exists(linktodir.GetFullPath(), wxFILE_EXISTS_DIR | wxFILE_EXISTS_NO_FOLLOW) ); CPPUNIT_ASSERT_EQUAL_MESSAGE ( "Testing directory existence" + msg, true, wxFileName::Exists(linktodir.GetFullPath(), wxFILE_EXISTS_DIR) ); } // Finally test Exists() after removing the file. CPPUNIT_ASSERT(wxRemoveFile(targetfn.GetFullPath())); // This should succeed, as the symlink still exists and // the default wxFILE_EXISTS_ANY implies wxFILE_EXISTS_NO_FOLLOW CPPUNIT_ASSERT(wxFileName(tempdir, "linktofile").Exists()); // So should this one, as wxFILE_EXISTS_SYMLINK does too CPPUNIT_ASSERT(wxFileName(tempdir, "linktofile"). Exists(wxFILE_EXISTS_SYMLINK)); // but not this one, as the now broken symlink is followed CPPUNIT_ASSERT(!wxFileName(tempdir, "linktofile"). Exists(wxFILE_EXISTS_REGULAR)); CPPUNIT_ASSERT(linktofile.Exists()); // This is also a convenient place to test Rmdir() as we have things to // remove. // First, check that removing a symlink to a directory fails. CPPUNIT_ASSERT( !wxFileName::Rmdir(linktodirName) ); // And recursively removing it only removes the symlink itself, not the // directory. CPPUNIT_ASSERT( wxFileName::Rmdir(linktodirName, wxPATH_RMDIR_RECURSIVE) ); CPPUNIT_ASSERT( tmpfn.Exists() ); // Finally removing the directory itself does remove everything. CPPUNIT_ASSERT(tempdirfn.Rmdir(wxPATH_RMDIR_RECURSIVE)); CPPUNIT_ASSERT( !tempdirfn.Exists() ); }
int cmd_set(char *param) { char *value; char *promptBuf = 0, tempcmd[255]; int ret; optC = promptUser = upCaseValue = optExecute = 0; if(leadOptions(¶m, opt_set, 0) != E_None) return 1; switch(breakVarAssign(ctxtEnvironment, param, &value)) { case 1: /* no equal sign */ #ifdef FEATURE_CMD_SET_PRINT if( ( value = getEnv( param ) ) != NULL ) printf( "%s\n", value ); else { error_env_var_not_found( param ); return( 1 ); } return( 0 ); #else error_syntax(0); return 1; #endif case 0: /* displayed */ return 0; #ifdef DEBUG case 2: break; default: dprintf(("[SET: Invalid response from breakVarAssign()]\n")); return 1; #endif } if(promptUser) { /* -> Display the value, then read and assign */ assert(value); fputs(value, stdout); promptBuf = malloc(promptBuffer); if(!promptBuf) { error_out_of_memory(); return E_NoMem; } fgets(promptBuf, promptBuffer, stdin); if(cbreak) { free(promptBuf); return E_CBreak; } value = strchr(promptBuf, '\0'); while(--value >= promptBuf && (*value == '\n' || *value == '\r')); value[1] = '\0'; /* strip trailing newlines */ value = promptBuf; } if (optExecute) { char *tempfile = tmpfn(); FILE *fhandle; if (!tempfile) return (1); sprintf (tempcmd, "%s>%s", value, tempfile); parsecommandline (tempcmd, TRUE); fhandle = fopen (tempfile, "r"); if (!fhandle) { unlink (tempfile); free (tempfile); return (1); } fgets (tempcmd, 255, fhandle); value = strchr(tempcmd, '\n'); if (value) *value = '\0'; value = tempcmd; fclose (fhandle); unlink (tempfile); free (tempfile); } /* If the value is just blanks, it means to delete the value; but otherwise even leading and trailing spaces must be kept */ if(is_empty(value)) value = 0; if (upCaseValue) StrUpr(value); /* set value as upper case, eg for if testing */ ret = chgEnvCase(optC, param, value); free(promptBuf); return ret; }