void put_check_for_nan(const CppAD::vector<Base>& vec, std::string& file_name) { size_t char_size = sizeof(Base) * vec.size(); const char* char_ptr = reinterpret_cast<const char*>( vec.data() ); # if CPPAD_HAS_MKSTEMP char pattern[] = "/tmp/fileXXXXXX"; int fd = mkstemp(pattern); file_name = pattern; write(fd, char_ptr, char_size); close(fd); # else # if CPPAD_HAS_TMPNAM_S std::vector<char> name(L_tmpnam_s); if( tmpnam_s( name.data(), L_tmpnam_s ) != 0 ) { CPPAD_ASSERT_KNOWN( false, "Cannot create a temporary file name" ); } file_name = name.data(); # else file_name = tmpnam( CPPAD_NULL ); # endif std::fstream file_out(file_name.c_str(), std::ios::out|std::ios::binary ); file_out.write(char_ptr, char_size); file_out.close(); # endif return; }
Aws::String CreateTempFilePath() { #ifdef _MSC_VER #pragma warning(disable: 4996) // _CRT_SECURE_NO_WARNINGS #endif char s_tempName[L_tmpnam_s+1]; /* Prior to VS 2014, tmpnam/tmpnam_s generated root level files ("\filename") which were not appropriate for our usage, so for the windows version, we prepended a '.' to make it a tempfile in the current directory. Starting with VS2014, the behavior of tmpnam/tmpnam_s was changed to be a full, valid filepath based on the current user ("C:\Users\username\AppData\Local\Temp\..."). See the tmpnam section in http://blogs.msdn.com/b/vcblog/archive/2014/06/18/crt-features-fixes-and-breaking-changes-in-visual-studio-14-ctp1.aspx for more details. */ #if _MSC_VER >= 1900 tmpnam_s(s_tempName, L_tmpnam_s); #else s_tempName[0] = '.'; tmpnam_s(s_tempName + 1, L_tmpnam_s); #endif // _MSC_VER return s_tempName; }
TempFile::TempFile() { if (tmpnam_s(m_buffer)) { throw std::runtime_error("Could not get a temp filename"); } if (fopen_s(&m_file, m_buffer, "w")) { char buffer[100]; if (strerror_s(buffer, errno)) { throw std::runtime_error("Could not translate errno to string"); } throw std::runtime_error("Could not open the temp file: " + std::string(m_buffer) + buffer); } }
TemporaryFile::TemporaryFile(const std::string& parentDirectory, const std::string& contents) { char fileName[L_tmpnam]; tmpnam_s(fileName, L_tmpnam); std::string filePath = parentDirectory + fileName; FILE* file = nullptr; errno_t result = fopen_s(&file, filePath.c_str(), "w"); if (result != 0) ThrowError("fopen", perror); if (fputs(contents.c_str(), file) == EOF) ThrowError("fputs", ferror); fclose(file); m_Name = fileName; }
void Database::OnThreadProc_Save() { // Daten auf die Festplatte schreiben char tmpFilename[1024]; tmpnam_s(tmpFilename, 1024); std::ofstream file(tmpFilename, std::ios_base::trunc); if (!file.fail()) { for (std::vector<std::string>::iterator iter = _Vector->begin(); iter != _Vector->end(); iter++) file << (*iter).c_str() << std::endl; file.close(); if (FileExists(_Filename)) remove(_Filename.c_str()); rename(tmpFilename, _Filename.c_str()); _Filename = ""; if (_Vector != NULL) { delete _Vector; _Vector = NULL; } _CriticalSection.Enter(); _Status = Status::Ok; _Mode = Modes::Nothing; _CriticalSection.Leave(); } else { _Filename = ""; if (_Vector != NULL) { delete _Vector; _Vector = NULL; } _CriticalSection.Enter(); _Status = Status::Error; _Mode = Modes::Nothing; _CriticalSection.Leave(); } }
void MDTRA_PrepWaterShellDialog :: exec_on_prep( void ) { int streamIndex = sCombo->itemData( sCombo->currentIndex() ).toInt(); const MDTRA_Stream *pStream = m_pMainWindow->getProject()->fetchStreamByIndex( streamIndex ); if (!pStream || !pStream->pdb) { QMessageBox::warning(this, tr(APPLICATION_TITLE_SMALL), tr("Bad stream(s) selected!")); return; } txtOutput->clear(); btnRun->setEnabled( false ); char tempBuf[512]; int errNo = 0; FILE *tempFp = NULL; if ( !*m_szTempName ) tmpnam_s( m_szTempName ); if ( ( errNo = (int)fopen_s( &tempFp, m_szTempName, "w" ) ) != 0 ) { strerror_s( tempBuf, sizeof(tempBuf), errNo ); QString cmdErr = QString( "* Could not open temp file:\n %1\n* %2 gave the error message:\n \"%3\"\n" ).arg( m_szTempName ).arg( s_OSName ).arg( QString::fromLocal8Bit( tempBuf ) ); txtOutput->appendPlainText( cmdErr ); btnRun->setEnabled( true ); if ( tempFp ) fclose( tempFp ); return; } int trajectoryMin = 1; int trajectoryMax = pStream->files.count(); if (trajectoryRange->isChecked()) { trajectoryMin = MDTRA_MAX( trajectoryMin, sIndex->value() ); trajectoryMax = MDTRA_MIN( trajectoryMax, eIndex->value()); } // write stream data for ( int i = trajectoryMin - 1; i < trajectoryMax; ++i ) fprintf_s( tempFp, "%s\n", pStream->files.at( i ).toLocal8Bit().data() ); fclose( tempFp ); // build command line QString cmdName, cmdArgs, cmdOutFile; #if defined(WIN32) cmdName = "./utils/" PREP_PROGRAM_NAME ".exe"; #else cmdName = "./utils/" PREP_PROGRAM_NAME; #endif cmdOutFile = txtOutputPDB->text(); #if QT_VERSION >= 0x040700 if ( cmdOutFile.isEmpty() ) cmdOutFile = txtOutputPDB->placeholderText(); #endif cmdArgs = QString( "-i \"%1\" -o \"%2\"" ).arg( m_szTempName ).arg( cmdOutFile ); QString cmdReport = QString( "** Executing...\n** Command: %1\n** Parameters: %2\n" ).arg( cmdName ).arg( cmdArgs ); txtOutput->appendPlainText( cmdReport ); // execute prep program m_pPipe = new MDTRA_Pipe( cmdName, cmdArgs, txtOutput ); if ( m_pPipe ) { if ( m_pPipe->exec() ) { if ( m_bInterrupt ) txtOutput->appendPlainText( QString( "** Interrupted by user.\n" ) ); else txtOutput->appendPlainText( QString( "** Finished!\n" ) ); } delete m_pPipe; m_pPipe = NULL; } m_bInterrupt = false; #if defined(WIN32) _unlink( m_szTempName ); #else unlink( m_szTempName ); #endif btnRun->setEnabled( true ); }
void main( int argc, char *argv[] ) { char filename[NUM_FILES][L_tmpnam_s]; FILE *fp; int ctr, ctr2; struct stat info; errno_t rc; int violations = NumViolations; #ifdef __SW_BW FILE *my_stdout; my_stdout = freopen( "tmp.log", "a", stdout ); if( my_stdout == NULL ) { fprintf( stderr, "Unable to redirect stdout\n" ); exit( -1 ); } #endif argc=argc; /*** Initialize ***/ strcpy( ProgramName, strlwr( argv[0] ) ); /* store filename */ /***********************************************************************/ /* set constraint-handler */ /***********************************************************************/ set_constraint_handler_s( my_constraint_handler ); /* test runtime constraints */ rc = tmpnam_s( NULL, sizeof( filename[0] ) ); VERIFYS( rc != 0 ); VERIFYS( NumViolations == ++violations ); filename[0][0] = 'x'; rc = tmpnam_s( filename[0], 0 ); VERIFYS( rc != 0 ); VERIFYS( filename[0][0] == 'x' ); VERIFYS( NumViolations == ++violations ); rc = tmpnam_s( filename[0], 4 ); /* only space for .aaa */ VERIFYS( rc != 0 ); VERIFYS( filename[0][0] == '\0' ); VERIFYS( NumViolations == ++violations ); for( ctr = 0; ctr < NUM_FILES; ++ctr ) { rc = tmpnam_s( filename[ctr], sizeof( filename[0] ) ); VERIFYS( rc == 0 ); VERIFY( remove( filename[ctr] ) != 0 ); // remove should fail VERIFY( errno == ENOENT ); for( ctr2 = 0; ctr2 < ctr; ++ctr2 ) { VERIFY( strcmp( filename[ctr], filename[ctr2] ) != 0 ); // Make sure all tmpfiles are unique } } VERIFY( utime( filename[0], NULL ) == -1 ); EXPECT( errno == ENOENT ); for( ctr = 0; ctr < 2; ++ctr ) { if( ( fp = fopen( filename[ctr], "w" ) ) == NULL ) { fprintf( stderr, "Internal: fopen() failed\n" ); perror( filename[ctr] ); exit( -1 ); } fclose( fp ); } #ifndef __UNIX__ // rename is allowed VERIFY( rename( filename[0], filename[1] ) != 0 ); // rename should fail EXPECT( (errno == EACCES)||(errno == EEXIST) ); #endif VERIFY( rename( filename[0], filename[2] ) == 0 ); VERIFY( remove( filename[0] ) != 0 ); // filename[0] was renamed -> DNE EXPECT( errno == ENOENT ); VERIFY( remove( filename[2] ) == 0 ); VERIFY( access( filename[2], F_OK ) == -1 );// filename[2] was removed EXPECT( errno == ENOENT ); VERIFY( access( filename[1], F_OK ) == 0 ); // filename[1] is still here VERIFY( access( filename[1], R_OK ) == 0 ); // read permission VERIFY( access( filename[1], W_OK ) == 0 ); // write permission VERIFY( access( argv[0], X_OK ) == 0 ); // execute permission VERIFY( chmod( filename[1], S_IRUSR | S_IRGRP ) == 0 ); // filename[1] is now read-only VERIFY( access( filename[1], W_OK ) == -1 ); EXPECT( errno == EACCES ); #if !defined( __UNIX__ ) && !defined( __RDOS__ ) // remove would succeed VERIFY( remove( filename[1] ) != 0 ); EXPECT( errno == EACCES ); #endif VERIFY( chmod( filename[1], S_IRWXU | S_IRWXG ) == 0 ); VERIFY( stat( filename[1], &info ) == 0 ); #ifndef __UNIX__ EXPECT( 0 <= info.st_dev && info.st_dev < 26 ); #endif VERIFY( utime( filename[1], NULL ) == 0 ); VERIFY( remove( filename[1] ) == 0 ); printf( "Tests completed (%s).\n", strlwr( argv[0] ) ); if( warnings ) { printf( "Total number of warning(s) = %d.\n", warnings ); exit( -1 ); } #ifdef __SW_BW { fprintf( stderr, "Tests completed (%s).\n", strlwr( argv[0] ) ); fclose( my_stdout ); _dwShutDown(); } #endif exit( 0 ); }