コード例 #1
0
ファイル: shell.c プロジェクト: justiceeq/shell-lite
int execute_cd(char** words) {

	if(words == NULL){
		return EXIT_FAILURE;
	}
	if(words[0] == NULL){
		return EXIT_FAILURE;
	}
	if(words[1] == NULL){
		return EXIT_FAILURE;
	}
	if(strcmp(words[0], "cd")!= 0){
		return EXIT_FAILURE;
	}


	char path[MAX_DIRNAME];
	strcpy(path, words[1]);
	char currentdir[MAX_DIRNAME];

	if(is_relative(words[1]) == 1){
		getcwd(currentdir, sizeof(currentdir));
		strcat(currentdir, "/");
		strcat(currentdir, words[1]);
		chdir(currentdir);
		return 0;
	}
	else if(is_relative(words[1]) == 0){
		chdir(words[1]);
		return 0;
	}
	else{
		return EXIT_FAILURE;
	}
}
コード例 #2
0
ファイル: web_crawler.cpp プロジェクト: kubivan/webcrawler
void web_crawler::job_downloader(web_crawler::urls_t& urls, web_crawler::pages_t& pages)
{
    while (!urls.empty())
    {
        uri::uri url = urls.front();
        urls.pop();
        std::cout << url.string() <<std::endl;

        if (pages.find(url) != pages.end())
        {
            std::cout << "url already processed " <<std::endl;
            continue;
        }

        if (!url.is_valid())
        {
            std::cout << "invalid url " <<std::endl;
            continue;
        }

        try
        {
            thread_local http::client client;
            http::client::response response;

            auto request = http::client::request(url);
            response = client.get(request);
            auto st = status(response);
            if (st > 400)
            {
                std::cout << "error " << st <<std::endl;
                continue;
            }

            const auto page = body(response);
            auto links = search_for_links(page);
            std::remove_if(links.begin(), links.end(), [&pages](const uri::uri& link) {
                return pages.find(link) == pages.end();
            });

            for (const auto& link : links)
            {
                auto url_to_push = is_relative(link) ? link.string() : url.string() + link.string();
                urls.push(url_to_push);
            }
            pages[url] = page;

            cv.notify_all();
        }
        catch(std::exception& e)
        {
            std::cout<<"error " << e.what()<<std::endl;
            std::cout <<urls.size() << " " <<pages.size() <<std::endl;
            urls.push(url);
            std::this_thread::sleep_for(std::chrono::milliseconds(5000));
            continue;
        }

    }
}
コード例 #3
0
ファイル: shell.c プロジェクト: dklambauer/shell
/**
 * Changes directory to a path specified in the words argument;
 * For example: words[0] = "cd"
 *              words[1] = "csc209/assignment3/"
 * Your command should handle both relative paths to the current 
 * working directory, and absolute paths relative to root,
 * e.g., relative path:  cd csc209/assignment3/
 *       absolute path:  cd /u/bogdan/csc209/assignment3/
 */
int execute_cd(char** words) {
        
        /** 
	 * The first word contains the "cd" string, the second one contains 
         * the path.
         * Check possible errors:
         * - The words pointer could be NULL, the first string or the second 
         *   string could be NULL, or the first string is not a cd command
         * - If so, return an EXIT_FAILURE status to indicate something is 
         *   wrong.
         */
         if(!words || !words[0] || !words[1] || strcmp(words[0], "cd")) {
            return EXIT_FAILURE;
        }

        /**
         * Change directory to words[1], and return the result of that
	 * chdir call. If the directory to be changed to (words[1] or
	 * the cwd plus '/' and words[1]) is too long, return
	 * EXIT_FAILURE
         */
        if(is_relative(words[1])) {
            char dir[MAX_DIRNAME];
            getcwd(dir, MAX_DIRNAME);
            if (strlen(words[1]) + strlen(dir) + 1 >= MAX_DIRNAME) {
                return EXIT_FAILURE;
            }
        } else {
            if(strlen(words[1]) >= MAX_DIRNAME) {
                return EXIT_FAILURE;
            }
        }
        return chdir(words[1]);
}
コード例 #4
0
double tolupp(std::string str, double sign)
{
    size_t start = 0;
    reltypes_et reltype;
    double val1, val2, factor;

    reltype = is_relative(str);

    if(ABS==reltype){
        // absolute tolerance value
        // first number
        val1 = norm_value(str,start);
        //second number
        val2 = norm_value(str,start);
        if(isnan(val2)) return sign*abs(val1); // if only one value - negative lower bound
        if(sign*val2>sign*val1) return val2;
        return val1;
    }else{
        //relative tolerance value
        factor = getfactor(reltype);
        // first number
        val1 = getval(str,start) * factor;
        //second number
        val2 = getval(str,start) * factor;
        if(isnan(val2)) return sign*abs(val1); // if only one value - negative lower bound
        if(sign*val2>sign*val1) return val2;
        return val1;
    }
}
コード例 #5
0
/** \brief return the number of level of this file_path2_t
 */
size_t	file_path2_t::size()			const throw()
{
	if( is_relative() ){
		if( name_db[0] == FS_DIR_PLACEHOLDER )	return name_db.size();
		return name_db.size() + 1;
	}
	return name_db.size();
}
コード例 #6
0
/** \brief return the name at idx in this file_path2_t
 */
file_path2_t	file_path2_t::operator[](size_t idx)	const throw()
{
	if( is_relative() ){
		if( idx == 0 )	return FS_DIR_PLACEHOLDER;
		if( name_db[0] != FS_DIR_PLACEHOLDER )	idx--;
	}else{
		if( idx == 0 )	return FS_DIR_SEPARATOR;
	}
	return name_db[idx];
}
コード例 #7
0
ファイル: path.cpp プロジェクト: ksherlock/mpw-shell
	path path::relative_path() const {
		// first pathname *after* the root path
		// root_path is first / in this implementation.

		if (is_relative()) return *this;

		auto pos = _path.find_first_not_of(separator);
		if (pos == _path.npos) return path();

		return path(_path.substr(pos));
	}
コード例 #8
0
ファイル: KeyFile.cpp プロジェクト: qd-cae/qd
/** Update the include path
 *
 * @return include filepaths
 *
 * The include path are the directories, in which all includes will be
 * searched for.
 */
const std::vector<std::string>&
KeyFile::get_include_dirs(bool _update)
{

  if (!_update)
    return include_dirs;

  // lets take the long way ...
  std::set<std::string> new_include_dirs;

  // dir of file
  std::string directory = "";
  auto my_filepath = get_filepath();
  size_t pos = my_filepath.find_last_of("/\\");
  if (pos != std::string::npos)
    directory = my_filepath.substr(0, pos) + "/";

  if (!directory.empty())
    new_include_dirs.insert(directory);

  // update
  for (auto& kw : include_path_keywords) {
    auto kw_inc_path = std::static_pointer_cast<IncludePathKeyword>(kw);
    bool is_relative_dir = kw_inc_path->is_relative();

    // append
    for (const auto& dirpath : kw_inc_path->get_include_dirs()) {
      if (is_relative_dir)
        new_include_dirs.insert(join_path(directory, dirpath));
      else
        new_include_dirs.insert(dirpath);
    }
  }

  // check also the includes
  for (auto& include_kw : include_keywords)
    for (auto& include_kf : include_kw->get_includes()) {
      auto paths = include_kf->get_include_dirs(true);
      new_include_dirs.insert(paths.begin(), paths.end());
    }

#ifdef QD_DEBUG
  std::cout << "Include dirs:\n";
  for (const auto& entry : include_dirs)
    std::cout << entry << '\n';
#endif

  include_dirs =
    std::vector<std::string>(new_include_dirs.begin(), new_include_dirs.end());

  return include_dirs;
}
コード例 #9
0
ファイル: uri.cpp プロジェクト: Corvusoft/restbed
    string Uri::get_authority( void ) const
    {
        string authority = String::empty;
        if ( is_relative( ) ) return authority;
        
        smatch match;
        static const regex pattern( "^[a-zA-Z][a-zA-Z0-9+\\-.]*://(([a-zA-Z0-9\\-._~%!$&'()*+,;=]+)(:([a-zA-Z0-9\\-._~%!$&'()*+,;=]+))?@)?([a-zA-Z0-9\\-._~%]+|\\[[a-zA-Z0-9\\-._~%!$&'()*+,;=:]+\\])" );
        if ( regex_search( m_pimpl->m_uri, match, pattern ) ) authority = match[ 5 ];

        if ( authority.front( ) == '[' ) authority.erase( 0, 1 );
        if ( authority.back( ) == ']' ) authority.erase( authority.length( ) - 1, 1 );
        return authority;
    }
コード例 #10
0
/** \brief Return the fullpath2 of this file_path2_t
 * 
 * - WARNING: this function assume that the file_path2_t is relative to the 
 *            *CURRENT* directory !
 *   - so if the file_path2_t is created for with a relative path2 with a given 
 *     current directory, then the current directory is changed, and then
 *     fullpath2() is called, it will return the full path2 in the SECOND
 *     current directory
 */
file_path2_t	file_path2_t::fullpath2()		const throw()
{
	// if this file_path2_t is already 'absolute', return it as is
	if( is_absolute() )	return *this;
	// sanity check - here the 
	DBG_ASSERT( is_relative() );
#if	0
	// prepend the current directory to this relative path2
	return file_utils_t::get_current_dir() / *this;
#else
//	EXP_ASSERT(0);
	KLOG_ERR("file_utils_t::get_current_dir() return file_path_t. so wont work for file_path2_t");
	return file_path2_t();
#endif
}
コード例 #11
0
ファイル: path_help.cpp プロジェクト: PaulFSherwood/cplusplus
CL_String CL_PathHelp::make_relative(
	const CL_String &base_path,
	const CL_String &absolute_path,
	PathType path_type)
{
	CL_String base = add_trailing_slash(normalize(base_path, path_type), path_type);
	CL_String absolute = normalize(absolute_path, path_type);

	if (path_type == path_type_file)
	{
		CL_String base_location = get_location(base, path_type_file);
		CL_String absolute_location = get_location(absolute, path_type_file);

		if (is_relative(base, path_type))
		{
#ifdef WIN32
			if (base_location.length() == 2 && base_location[1] == ':')
			{
				int drive = 0;
				if (base_location[0] >= 'A' && base_location[0] <= 'Z')
					drive = base_location[0] - 'A' + 1;
				else if (base_location[0] >= 'a' && base_location[0] <= 'z')
					drive = base_location[0] - 'a' + 1;
				else
					throw CL_Exception(cl_format("Invalid drive: %1", base_location));
				TCHAR working_dir[MAX_PATH];
				memset(working_dir, 0, sizeof(TCHAR)*MAX_PATH);
				if (_tgetdcwd(drive, working_dir, MAX_PATH) == 0)
					throw CL_Exception(cl_format("Unable to get current working directory for %1!", base_location));

				base = add_trailing_slash(working_dir, path_type) + base;
			}
			else if (base_location.empty())
			{
				TCHAR working_dir[MAX_PATH];
				memset(working_dir, 0, sizeof(TCHAR)*MAX_PATH);
				if (GetCurrentDirectory(MAX_PATH, working_dir) == FALSE)
					throw CL_Exception(cl_format("Unable to get current working directory for %1!", base_location));

				base = add_trailing_slash(working_dir, path_type) + base;
			}
			else
			{
				throw CL_Exception(cl_format("Error in make_relative with base path: %1", base_path));
			}
#else
			char working_dir[1024];
			memset(working_dir, 0, 1024);
			if (getcwd(working_dir, 1024) == 0)
				throw CL_Exception("Unable to get current working directory!");
			base = add_trailing_slash(working_dir, path_type) + base;
#endif
		}
		if (is_relative(absolute, path_type))
		{
#ifdef WIN32
			if (absolute_location.length() == 2 && absolute_location[1] == ':')
			{
				int drive = 0;
				if (absolute_location[0] >= 'A' && absolute_location[0] <= 'Z')
					drive = absolute_location[0] - 'A' + 1;
				else if (absolute_location[0] >= 'a' && absolute_location[0] <= 'z')
					drive = absolute_location[0] - 'a' + 1;
				else
					throw CL_Exception(cl_format("Invalid drive: %1", absolute_location));
				TCHAR working_dir[MAX_PATH];
				memset(working_dir, 0, sizeof(TCHAR)*MAX_PATH);
				if (_tgetdcwd(drive, working_dir, MAX_PATH) == 0)
					throw CL_Exception(cl_format("Unable to get current working directory for %1!", absolute_location));

				absolute = add_trailing_slash(working_dir, path_type) + absolute;
			}
			else if (absolute_location.empty())
			{
				TCHAR working_dir[MAX_PATH];
				memset(working_dir, 0, sizeof(TCHAR)*MAX_PATH);
				if (GetCurrentDirectory(MAX_PATH, working_dir) == FALSE)
					throw CL_Exception(cl_format("Unable to get current working directory for %1!", absolute_location));

				absolute = add_trailing_slash(working_dir, path_type) + absolute;
			}
			else
			{
				throw CL_Exception(cl_format("Error in make_relative with absolute path: %1", absolute_path));
			}
#else
			char working_dir[1024];
			memset(working_dir, 0, 1024);
			if (getcwd(working_dir, 1024) == 0)
				throw CL_Exception("Unable to get current working directory!");
			absolute = add_trailing_slash(working_dir, path_type) + absolute;
#endif
		}

		base_location = get_location(base, path_type_file);
		absolute_location = get_location(absolute, path_type_file);
		if (CL_StringHelp::compare(absolute_location, base_location, true) != 0)
			return absolute_path;
	}

	if (is_relative(base, path_type))
		throw CL_Exception(cl_format("Relative path %1 used as base path for make_relative", base_path));
	if (is_relative(absolute, path_type))
		throw CL_Exception(cl_format("Relative path %1 used as absolute path for make_relative", absolute_path));

	CL_String relative;
	CL_String relative_end;

	bool differs = false;
	CL_String::size_type start_pos = 0, end_pos = 0;
	while (true)
	{
		if (path_type == path_type_file)
		{
			end_pos = base.find_first_of("\\/", start_pos);
		}
		else
		{
			end_pos = base.find('/', start_pos);
		}
		if (end_pos == CL_String::npos)
			break;

		if (!differs)
		{
			CL_String base_element = base.substr(start_pos, end_pos - start_pos + 1);
			CL_String absolute_element = absolute.substr(start_pos, end_pos - start_pos + 1);

			bool same_element = false;
			if (path_type == path_type_file)
			{
#ifdef WIN32
				same_element = (CL_StringHelp::compare(base_element, absolute_element, true) == 0);
#else
				same_element = (base_element == absolute_element);
#endif
			}
			else
			{
				same_element = (base_element == absolute_element);
			}

			if (!same_element)
			{
				relative_end = absolute.substr(start_pos);
				differs = true;
			}
			else
			{
				relative_end = absolute.substr(end_pos+1);
			}
		}

		if (differs)
		{
			if (path_type_file)
			{
#ifdef WIN32
				relative += "..\\";
#else
				relative += "../";
#endif
			}
			else
			{
				relative += "../";
			}
		}

		start_pos = end_pos + 1;
	}

	return relative + relative_end;
}
コード例 #12
0
void
HOSTFXR_UTILITY::GetHostFxrParameters(
    const fs::path     &processPath,
    const fs::path     &applicationPhysicalPath,
    const std::wstring &applicationArguments,
    fs::path           &hostFxrDllPath,
    fs::path           &dotnetExePath,
    std::vector<std::wstring> &arguments
)
{
    LOG_INFOF(L"Resolving hostfxr parameters for application: '%ls' arguments: '%ls' path: '%ls'",
        processPath.c_str(),
        applicationArguments.c_str(),
        applicationPhysicalPath.c_str());
    arguments = std::vector<std::wstring>();

    fs::path expandedProcessPath = Environment::ExpandEnvironmentVariables(processPath);
    const auto expandedApplicationArguments = Environment::ExpandEnvironmentVariables(applicationArguments);

    LOG_INFOF(L"Known dotnet.exe location: '%ls'", dotnetExePath.c_str());

    if (!expandedProcessPath.has_extension())
    {
        // The only executable extension inprocess supports
        expandedProcessPath.replace_extension(".exe");
    }
    else if (!ends_with(expandedProcessPath, L".exe", true))
    {
        throw InvalidOperationException(format(L"Process path '%s' doesn't have '.exe' extension.", expandedProcessPath.c_str()));
    }

    // Check if the absolute path is to dotnet or not.
    if (IsDotnetExecutable(expandedProcessPath))
    {
        LOG_INFOF(L"Process path '%ls' is dotnet, treating application as portable", expandedProcessPath.c_str());

        if (applicationArguments.empty())
        {
            throw InvalidOperationException(L"Application arguments are empty.");
        }

        if (dotnetExePath.empty())
        {
            dotnetExePath = GetAbsolutePathToDotnet(applicationPhysicalPath, expandedProcessPath);
        }

        hostFxrDllPath = GetAbsolutePathToHostFxr(dotnetExePath);

        arguments.push_back(dotnetExePath);
        AppendArguments(
            expandedApplicationArguments,
            applicationPhysicalPath,
            arguments,
            true);
    }
    else
    {
        LOG_INFOF(L"Process path '%ls' is not dotnet, treating application as standalone or portable with bootstrapper", expandedProcessPath.c_str());

        auto executablePath = expandedProcessPath;

        if (executablePath.is_relative())
        {
            executablePath = applicationPhysicalPath / expandedProcessPath;
        }

        //
        // The processPath is a path to the application executable
        // like: C:\test\MyApp.Exe or MyApp.Exe
        // Check if the file exists, and if it does, get the parameters for a standalone application
        //
        if (is_regular_file(executablePath))
        {
            auto applicationDllPath = executablePath;
            applicationDllPath.replace_extension(".dll");

            LOG_INFOF(L"Checking application.dll at '%ls'", applicationDllPath.c_str());
            if (!is_regular_file(applicationDllPath))
            {
                throw InvalidOperationException(format(L"Application .dll was not found at %s", applicationDllPath.c_str()));
            }

            hostFxrDllPath = executablePath.parent_path() / "hostfxr.dll";
            LOG_INFOF(L"Checking hostfxr.dll at '%ls'", hostFxrDllPath.c_str());
            if (is_regular_file(hostFxrDllPath))
            {
                LOG_INFOF(L"hostfxr.dll found app local at '%ls', treating application as standalone", hostFxrDllPath.c_str());
                // For standalone apps we need .exe to be argv[0], dll would be discovered next to it
                arguments.push_back(executablePath);
            }
            else
            {
                LOG_INFOF(L"hostfxr.dll found app local at '%ls', treating application as portable with launcher", hostFxrDllPath.c_str());

                // passing "dotnet" here because we don't know where dotnet.exe should come from
                // so trying all fallbacks is appropriate
                if (dotnetExePath.empty())
                {
                    dotnetExePath = GetAbsolutePathToDotnet(applicationPhysicalPath, L"dotnet");
                }
                hostFxrDllPath = GetAbsolutePathToHostFxr(dotnetExePath);

                // For portable with launcher apps we need dotnet.exe to be argv[0] and .dll be argv[1]
                arguments.push_back(dotnetExePath);
                arguments.push_back(applicationDllPath);
            }

            AppendArguments(
                expandedApplicationArguments,
                applicationPhysicalPath,
                arguments);
        }
        else
        {
            //
            // If the processPath file does not exist and it doesn't include dotnet.exe or dotnet
            // then it is an invalid argument.
            //
            throw InvalidOperationException(format(L"Executable was not found at '%s'", executablePath.c_str()));
        }
    }
}
コード例 #13
-1
// The processPath ends with dotnet.exe or dotnet
// like: C:\Program Files\dotnet\dotnet.exe, C:\Program Files\dotnet\dotnet, dotnet.exe, or dotnet.
// Get the absolute path to dotnet. If the path is already an absolute path, it will return that path
fs::path
HOSTFXR_UTILITY::GetAbsolutePathToDotnet(
     const fs::path & applicationPath,
     const fs::path & requestedPath
)
{
    LOG_INFOF(L"Resolving absolute path to dotnet.exe from '%ls'", requestedPath.c_str());

    auto processPath = requestedPath;
    if (processPath.is_relative())
    {
        processPath = applicationPath / processPath;
    }

    //
    // If we are given an absolute path to dotnet.exe, we are done
    //
    if (is_regular_file(processPath))
    {
        LOG_INFOF(L"Found dotnet.exe at '%ls'", processPath.c_str());

        return processPath;
    }

    // At this point, we are calling where.exe to find dotnet.
    // If we encounter any failures, try getting dotnet.exe from the
    // backup location.
    // Only do it if no path is specified
    if (requestedPath.has_parent_path())
    {
        LOG_INFOF(L"Absolute path to dotnet.exe was not found at '%ls'", requestedPath.c_str());

        throw InvalidOperationException(format(L"Could not find dotnet.exe at '%s'", processPath.c_str()));
    }

    const auto dotnetViaWhere = InvokeWhereToFindDotnet();
    if (dotnetViaWhere.has_value())
    {
        LOG_INFOF(L"Found dotnet.exe via where.exe invocation at '%ls'", dotnetViaWhere.value().c_str());

        return dotnetViaWhere.value();
    }

    const auto programFilesLocation = GetAbsolutePathToDotnetFromProgramFiles();
    if (programFilesLocation.has_value())
    {
        LOG_INFOF(L"Found dotnet.exe in Program Files at '%ls'", programFilesLocation.value().c_str());

        return programFilesLocation.value();
    }

    LOG_INFOF(L"dotnet.exe not found");
    throw InvalidOperationException(format(
        L"Could not find dotnet.exe at '%s' or using the system PATH environment variable."
        " Check that a valid path to dotnet is on the PATH and the bitness of dotnet matches the bitness of the IIS worker process.",
        processPath.c_str()));
}