Esempio n. 1
0
void
g_scanner_sync_file_offset (GScanner *scanner)
{
  g_return_if_fail (scanner != NULL);

  /* for file input, rewind the filedescriptor to the current
   * buffer position and blow the file read ahead buffer. usefull for
   * third party uses of our filedescriptor, which hooks onto the current
   * scanning position.
   */

  if (scanner->input_fd >= 0 && scanner->text_end > scanner->text)
    {
      gint buffered;

      buffered = scanner->text_end - scanner->text;
     //scott
	  // if (lseek (scanner->input_fd, - buffered, SEEK_CUR) >= 0)
	  if( fseek( _wfdopen( scanner->input_fd,L"r"),- buffered,SEEK_CUR) ==0  && ftell( _wfdopen(scanner->input_fd,L"r") )>=0)	  
	{
	  /* we succeeded, blow our buffer's contents now */
	  scanner->text = NULL;
	  scanner->text_end = NULL;
	}
      else
	glib_errno = 0;
    }
}
Esempio n. 2
0
static FILE * open_socket (char *host, unsigned short port)
{
   FILE * file = NULL;
   struct sockaddr_in sa;
   os_socket sock;
   char* errorMessage;

   if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
   {
      errorMessage = os_reportErrnoToString(socketErrorNo());
      fprintf(stderr, "socket: %s\n", errorMessage);
      os_free(errorMessage);
      return NULL;
   }

   memset((char *)&sa, 0, sizeof(sa));
   sa.sin_family = AF_INET;
   sa.sin_port = htons(port);
   sa.sin_addr.s_addr = inet_addr (host);

   if (connect (sock, (struct sockaddr *)&sa, sizeof(sa)) < 0)
   {
      errorMessage = os_reportErrnoToString(socketErrorNo());
      fprintf(stderr, "connect: %s\n", errorMessage);
      os_free(errorMessage);
      return NULL;
   }
#ifdef WINCE
   file = _wfdopen ((int)sock, L"w");
#else
   file = fdopen ((int)sock, "w");
#endif

   return file;
}
Esempio n. 3
0
static FILE *rt_file_fdopen(int fd, EIF_FILENAME type)
{
#ifdef EIF_WINDOWS
	return _wfdopen(fd, type);
#else
	return fdopen(fd, type);
#endif
}
Esempio n. 4
0
FILE* _wcefdopen( int handle, const char *mode )
{
	WCHAR wmode [32];
	FILE* fp = NULL;

	mbstowcs(wmode,mode,32);
	fp = _wfdopen( (void*)handle, wmode );

	return fp;
}
Esempio n. 5
0
static guchar
g_scanner_get_char (GScanner	*scanner,
		    guint	*line_p,
		    guint	*position_p)
{
  guchar fchar;

  if (scanner->text < scanner->text_end)
    fchar = *(scanner->text++);
  else if (scanner->input_fd >= 0)
    {
      gint count;
      gchar *buffer;

      buffer = scanner->buffer;
      do
	{//scott
	  //count = read (scanner->input_fd, buffer, READ_BUFFER_SIZE);
		count = fread (buffer,1,READ_BUFFER_SIZE,_wfdopen(scanner->input_fd,L"r")); // scott
	}
      while (count == -1 && (glib_errno == EINTR || glib_errno == EAGAIN));

      if (count < 1)
	{
	  scanner->input_fd = -1;
	  fchar = 0;
	}
      else
	{
	  scanner->text = buffer + 1;
	  scanner->text_end = buffer + count;
	  fchar = *buffer;
	  if (!fchar)
	    {
	      g_scanner_sync_file_offset (scanner);
	      scanner->text_end = scanner->text;
	      scanner->input_fd = -1;
	    }
	}
    }
  else
    fchar = 0;
  
  if (fchar == '\n')
    {
      (*position_p) = 0;
      (*line_p)++;
    }
  else if (fchar)
    {
      (*position_p)++;
    }
  
  return fchar;
}
FILE *fdopen(int handle, const char *mode)
{
	WCHAR *wmode = to_wide_string(mode);
	FILE *result;

	if(wmode != NULL)
		result = _wfdopen((void *)handle, wmode);
	else
		result = NULL;
	free(wmode);
	return result;
}
static FILE *
    hdb_open_file(TSK_TCHAR *file_path)
{
    FILE *file = NULL;

#ifdef TSK_WIN32
    int fd = 0;
    if (_wsopen_s(&fd, file_path, _O_RDONLY | _O_BINARY, _SH_DENYNO, 0) == 0) {
        file = _wfdopen(fd, L"rb");
    }
#else
    file = fopen(file_path, "rb");
#endif

    return file;
}
Esempio n. 8
0
    virtual QVariant defaultValue(const QApplicationArgument &argument) const
    {
        if(argument.name() == QLatin1String("output"))
        {
            QFile *const out = new QFile();

#ifdef Q_OS_WIN
            /* If we don't open stdout in "binary" mode on Windows, it will translate
             * 0xA into 0xD 0xA. */
            _setmode(_fileno(stdout), _O_BINARY);
            m_stdout = _wfdopen(_fileno(stdout), L"wb");
            out->open(m_stdout, QIODevice::WriteOnly);
#else
            out->open(stdout, QIODevice::WriteOnly);
#endif

            return QVariant::fromValue(static_cast<QIODevice *>(out));
        }
        else
            return QApplicationArgumentParser::defaultValue(argument);
    }
Esempio n. 9
0
void StdioFileHelper::SetLockedFilePointer()
{
	CleanUp();

	HANDLE hFile = ::CreateFile(m_fileName.c_str(), GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, 0, 
		OPEN_EXISTING, 0, 0);
	if(INVALID_HANDLE_VALUE == hFile)
	{
		char buffer[255];
		::sprintf_s(buffer, 255, "Failed to open [%s]", m_fileName);
		throw std::exception(buffer);
	}

	int fileNumber = _open_osfhandle((intptr_t)hFile, 1);
	if(-1 == fileNumber)
		throw std::exception("We failed to convert the File HANDLE to a C-Runtime descriptor.");

	FILE* pFile = _wfdopen(fileNumber, _T("rb"));
	if(0 == pFile)
		throw std::exception("We failed to associate the stream with the converted file descriptor.");

	m_pFile = pFile ;
}
Esempio n. 10
0
static guchar
g_scanner_peek_next_char (GScanner *scanner)
{
  if (scanner->text < scanner->text_end)
    {
      return *scanner->text;
    }
  else if (scanner->input_fd >= 0)
    {
      gint count;
      gchar *buffer;

      buffer = scanner->buffer;
      do
	{//scott
	  //count = read (scanner->input_fd, buffer, READ_BUFFER_SIZE);
		count = fread (buffer,1,READ_BUFFER_SIZE, _wfdopen( scanner->input_fd,L"r") );
	}
      while (count == -1 && (glib_errno == EINTR || glib_errno == EAGAIN));

      if (count < 1)
	{
	  scanner->input_fd = -1;

	  return 0;
	}
      else
	{
	  scanner->text = buffer;
	  scanner->text_end = buffer + count;

	  return *buffer;
	}
    }
  else
    return 0;
}
Esempio n. 11
0
void ConfigParser::ProcessConfiguration(HANDLE hmod)
{
#if defined(ENABLE_DEBUG_CONFIG_OPTIONS)
    bool hasOutput = false;
    char16 modulename[_MAX_PATH];

    GetModuleFileName((HMODULE)hmod, modulename, _MAX_PATH);

    // Win32 specific console creation code
    // xplat-todo: Consider having this mechanism available on other
    // platforms
    // Not a pressing need since ChakraCore runs only in consoles by
    // default so we don't need to allocate a second console for this
#if CONFIG_CONSOLE_AVAILABLE
    if (Js::Configuration::Global.flags.Console)
    {
        int fd;
        FILE *fp;

        // fail usually means there is an existing console. We don't really care.
        AllocConsole();

        fd = _open_osfhandle((intptr_t)GetStdHandle(STD_OUTPUT_HANDLE), O_TEXT);
        fp = _wfdopen(fd, _u("w"));

        if (fp != nullptr)
        {
            *stdout = *fp;
            setvbuf(stdout, nullptr, _IONBF, 0);

            fd = _open_osfhandle((intptr_t)GetStdHandle(STD_ERROR_HANDLE), O_TEXT);
            fp = _wfdopen(fd, _u("w"));

            if (fp != nullptr)
            {
                *stderr = *fp;
                setvbuf(stderr, nullptr, _IONBF, 0);

                char16 buffer[_MAX_PATH + 70];

                if (ConfigParserAPI::FillConsoleTitle(buffer, _MAX_PATH + 20, modulename))
                {
                    SetConsoleTitle(buffer);
                }

                hasOutput = true;
            }
        }
    }
#endif

    if (Js::Configuration::Global.flags.IsEnabled(Js::OutputFileFlag)
        && Js::Configuration::Global.flags.OutputFile != nullptr)
    {
        SetOutputFile(Js::Configuration::Global.flags.OutputFile, Js::Configuration::Global.flags.OutputFileOpenMode);
        hasOutput = true;
    }

    if (Js::Configuration::Global.flags.DebugWindow)
    {
        Output::UseDebuggerWindow();
        hasOutput = true;
    }

#ifdef ENABLE_TRACE
    if (CONFIG_FLAG(InMemoryTrace))
    {
        Output::SetInMemoryLogger(
            Js::MemoryLogger::Create(::GetOutputAllocator1(),
            CONFIG_FLAG(InMemoryTraceBufferSize) * 3));   // With stack each trace is 3 entries (header, msg, stack).
        hasOutput = true;
    }

#ifdef STACK_BACK_TRACE
    if (CONFIG_FLAG(TraceWithStack))
    {
        Output::SetStackTraceHelper(Js::StackTraceHelper::Create(::GetOutputAllocator2()));
    }
#endif // STACK_BACK_TRACE
#endif // ENABLE_TRACE

    if (hasOutput)
    {
        ConfigParserAPI::DisplayInitialOutput(modulename);

        Output::Print(_u("\n"));

        Js::Configuration::Global.flags.VerboseDump();
        Output::Flush();
    }

    if (Js::Configuration::Global.flags.ForceSerialized)
    {
        // Can't generate or execute byte code under forced serialize
        Js::Configuration::Global.flags.GenerateByteCodeBufferReturnsCantGenerate = true;
        Js::Configuration::Global.flags.ExecuteByteCodeBufferReturnsInvalidByteCode = true;
    }

    ForcedMemoryConstraint::Apply();
#endif

#ifdef MEMSPECT_TRACKING
    bool all = false;
    if (Js::Configuration::Global.flags.Memspect.IsEnabled(Js::AllPhase))
    {
        all = true;
    }
    if (all || Js::Configuration::Global.flags.Memspect.IsEnabled(Js::RecyclerPhase))
    {
        RecyclerMemoryTracking::Activate();
    }
    if (all || Js::Configuration::Global.flags.Memspect.IsEnabled(Js::PageAllocatorPhase))
    {
        PageTracking::Activate();
    }
    if (all || Js::Configuration::Global.flags.Memspect.IsEnabled(Js::ArenaPhase))
    {
        ArenaMemoryTracking::Activate();
    }
#endif
}
Esempio n. 12
0
std::wstring CommonUtility::GetPythonPath()
{
	// This function does four things(!) : 1) Executes which.exe python.exe
	// to find the full path of python executable using CreateProcess. 
	// 2) Opens a pipe to read from the standard output of the CreateProcess.
	// 3) Sets the locale to utf-8 and read the standard output to a wstring.
	// 4) Converts each of \ with \\ so that it is directly useful for CreateProcess.
	static std::wstring output;
	if (!output.empty())
		return output;

	HANDLE hChildStd_OUT_Rd = NULL;
	HANDLE hChildStd_OUT_Wr = NULL;
	SECURITY_ATTRIBUTES sa;
	sa.nLength = sizeof(SECURITY_ATTRIBUTES);
	sa.bInheritHandle = TRUE;
	sa.lpSecurityDescriptor = NULL;
	if (!CreatePipe(&hChildStd_OUT_Rd, &hChildStd_OUT_Wr, &sa, 0) ||
		!SetHandleInformation(hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0))
		return L"";

	PROCESS_INFORMATION piProcInfo;
	TCHAR systemPath[MAX_PATH];
	GetSystemDirectory(systemPath, MAX_PATH);
	const TCHAR pythonQuery[] = L"\\where.exe python.exe";
	std::wstring python = std::wstring(systemPath) + pythonQuery;
	STARTUPINFO siStartInfo;
	bool bSuccess = FALSE;

	ZeroMemory(&piProcInfo, sizeof(PROCESS_INFORMATION));
	ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
	siStartInfo.cb = sizeof(STARTUPINFO);
	siStartInfo.hStdOutput = hChildStd_OUT_Wr;
	siStartInfo.dwFlags |= (STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW);
	siStartInfo.wShowWindow = SW_HIDE;

	CreateProcess(NULL,
		const_cast<wchar_t*>(python.c_str()),
		NULL, NULL, TRUE, 0, NULL, NULL,
		&siStartInfo, &piProcInfo);

	CloseHandle(hChildStd_OUT_Wr);

	int fd = _open_osfhandle((intptr_t)hChildStd_OUT_Rd, 0);
	if (-1 == fd)
		return L"";
	FILE* file = _wfdopen(fd, L"rb");
	if (NULL == file)
		return L"";
	
	std::wifstream stream(file);

	const std::locale empty_locale = std::locale::empty();
	typedef std::codecvt_utf8<wchar_t> converter_type;
	const std::codecvt_utf8<wchar_t>* converter = new std::codecvt_utf8 < wchar_t > ;
	const std::locale utf8_locale(empty_locale, converter);

	stream.imbue(utf8_locale);
	stream >> output;
	stream.close();

	std::wstringstream canonical;
	std::wstring::iterator it(output.begin());
	while (it != output.end())
	{
		if (*it == '\\')
			canonical << "\\\\";
		else
			canonical << *it;
		++it;
	}
	return canonical.str();
}
Esempio n. 13
0
bool FileStdStream::Open(uint32 nAccess, String::EFormat nStringFormat)
{
	// If a standard stream has been used, we cannot open/reopen it
	if (m_bStream)
		return false;

	// Close file
	Close();

	// Set file mode
	char szMode[4];
	if ((nAccess & File::FileWrite) && !(nAccess & File::FileCreate) && (nAccess & File::FileAppend)) {
		// Append at the end of the file
		if (nAccess & File::FileRead)
			strcpy(szMode, "a+");	// Append, read and write
		else
			strcpy(szMode, "a");	// Append, write only
	} else if ((nAccess & File::FileWrite) && (nAccess & File::FileCreate) && !(nAccess & File::FileAppend)) {
		// Create and open writable
		if (nAccess & File::FileRead)
			strcpy(szMode, "w+");	// Create, read and write
		else
			strcpy(szMode, "w");	// Create, write only
	} else if ((nAccess & File::FileWrite) && !(nAccess & File::FileCreate) && !(nAccess & File::FileAppend)) {
		// Open writable
		if (nAccess & File::FileRead)
			strcpy(szMode, "r+");	// Open, read and write

		// We need to check whether the file already exist, if so, we can go on...
		else {
			if (Exists())
				strcpy(szMode, "w");	// Open, write only
			else
				return false;			// Invalid
		}
	} else if (!(nAccess & File::FileWrite) && !(nAccess & File::FileCreate) && !(nAccess & File::FileAppend)) {
		// Open not writable
		if (nAccess & File::FileRead)
			strcpy(szMode, "r");	// Open, read only
		else
			return false;			// Invalid
	} else {
		// Invalid combination
		return false;
	}

	// Set text or binary mode
	strcat(szMode, (nAccess & File::FileText) ? "t" : "b");

	// Save access modes and string encoding format
	m_nAccess       = nAccess;
	m_nStringFormat = nStringFormat;

	// Get OS file handle and open file
	#if defined(WIN32)
		const int nFile = _open_osfhandle(m_hFile, 0);
		if (nFile > -1) {
			// ASCII or Unicode?
			if (nStringFormat == String::ASCII) {
				// ASCII
				m_pFile = _fdopen(nFile, szMode);
			} else {
				// Unicode
				const wchar_t szModeUnicode[4] = {szMode[0], szMode[1], szMode[2], szMode[3]};
				m_pFile = _wfdopen(nFile, szModeUnicode);
			}
		}
	#elif defined(LINUX)
		// [TODO] Support for "nStringFormat"-parameter?
		if (m_hFile > -1)
			m_pFile = _fdopen(m_hFile, szMode);
	#endif

	// Done
	return (m_pFile != nullptr);
}