Exemplo n.º 1
0
    FileHandle read_file(std::string _path, bool _text)
    {
        FileHandle file;
        
        char* buffer;
        
#ifdef __APPLE__
        std::string cwd = get_current_working_directory();
#endif
        
        for (int i = 0; i < m_directory_list.size(); i++)
        {
            std::string currentDirectory;
            
#ifdef __APPLE__
            currentDirectory = cwd;
#endif
            currentDirectory += m_directory_list[i] + "/";
            currentDirectory += _path;
            
            FILE* currentFile = open_file_from_directory(currentDirectory, _text);
            
            std::cout << get_current_working_directory() << std::endl;
            
            if (currentFile)
            {
                fseek(currentFile, 0, SEEK_END);
                long fsize = ftell(currentFile);
                fseek(currentFile, 0, SEEK_SET);
                buffer = (char*)malloc(fsize + 1);
                fread(buffer, fsize, 1, currentFile);
                
                if (_text)
                    buffer[fsize] = '\0';
                
                close_file_from_directory(currentFile);
            
                file.buffer = buffer;
                file.size = fsize;
                
                return file;
            }
            
            // If file does not exist, the FILE handle should be NULL, so i should be able to remove the following line.
            close_file_from_directory(currentFile);
        }
        
        return file;
    }
Exemplo n.º 2
0
temp_working_dirt::temp_working_dirt(const std::string &name_template):
  temp_dirt(name_template)
{
  old_working_directory=get_current_working_directory();
  if(chdir(path.c_str())!=0)
    assert(false);
}
Exemplo n.º 3
0
char *
which (const char *name)
{
  const char *path_list = getenv ("PATH");
  char *full_path = NULL;
  char *result = NULL;

  if (!name || !*name)
    return NULL;

  get_current_working_directory ();

  if (path_list && *path_list != '\0')
    {
      int path_index = 0;

      result = find_command_in_path (name, path_list, &path_index);
      if (result)
        {
          full_path = path_clean_up (result);
          free (result);
          return xstrdup (full_path);
        }
      else
        {
          return NULL;
        }
    }
  return NULL;
}
Exemplo n.º 4
0
/*
  filename_expand -- try to convert the given filename into a fully-qualified
		     pathname.
*/
static void
filename_expand(char *fullpath, char *filename, size_t fullsize)
{
/* fullpath - returned full pathname */
/* filename - filename to expand */
	size_t len;
	fullpath[0] = '\0';

	if (filename[0] && filename[0] == '/') {
		/* Absolute (unix-style) pathname.  Do nothing */
		strncat(fullpath, filename, fullsize-1);
	} else {
		/* Assume relative Unix style path.  Get the current directory
		 * and prepend it.  FIXME: need to fix the case of DOS paths
		 * like "\foo", where we need to get the current drive. */
		strncat(fullpath, get_current_working_directory(), fullsize-1);
		len = strlen(fullpath);

		/* trailing slash already? */
		if (len > 0 && fullpath[len - 1] == '/') {
			/* yep */
			;
		} else if (len < fullsize-1) {
			/* nope, append trailing slash */
			strcat(fullpath, "/");
		}
		/* Don't forget to add the filename! */
		strncat(fullpath, filename, fullsize - len - 1);
	}
	return;
}
Exemplo n.º 5
0
jm_status_enu_t jm_portability_get_current_working_directory(char* buffer, size_t len)
{
	int ilen = (int)len;
	if(ilen != len) ilen = FILENAME_MAX + 2;
	setlocale(LC_CTYPE, "en_US.UTF-8"); /* just in case, does not seem to have an effect */
	if (get_current_working_directory(buffer, ilen) == NULL) {
		return jm_status_error;
	} else {
		return jm_status_success;
	}
}
Exemplo n.º 6
0
static char *path_clean_up(const char *path)
{
    static char result[256];

    const char *p1 = path;
    char *p2 = result;

    int saw_slash = 0, saw_slash_dot = 0, saw_slash_dot_dot = 0;

#ifndef WIN32
    if (*p1 != DIRSEP)
    {
        get_current_working_directory();
        strcpy(result, cwd);
        saw_slash = 1;
        p2 = &result[cwdlen];
    }
#endif

    do
    {
        if (!saw_slash || *p1 != DIRSEP)
            *p2++ = *p1;
        if (saw_slash_dot && (*p1 == DIRSEP))
            p2 -= 2;
        if (saw_slash_dot_dot && (*p1 == DIRSEP))
        {
            int cnt = 0;
            do
            {
                if (--p2 < result)
                {
                    strcpy(result, path);
                    return result;
                }
                if (*p2 == DIRSEP)
                    ++cnt;
            }
            while (cnt != 3);
            ++p2;
        }
        saw_slash_dot_dot = saw_slash_dot && (*p1 == '.');
        saw_slash_dot = saw_slash && (*p1 == '.');
        saw_slash = (*p1 == DIRSEP);
    }
    while (*p1++);

    return result;
}
Exemplo n.º 7
0
/*
  filename_expand -- try to convert the given filename into a fully-qualified
  		     pathname.
*/
static void
filename_expand (char *fullpath, char *filename)
  /* fullpath - returned full pathname */
  /* filename - filename to expand */
{
#ifdef  CYGWIN
  char cygwinFilename[QXE_PATH_MAX+1];
  extern void cygwin_conv_to_posix_path(const char *, char *);
#endif

  int len;
  fullpath[0] = fullpath[QXE_PATH_MAX] = '\0';

#ifdef  CYGWIN
  /*
    If we're in cygwin, just convert it and let the unix stuff handle it.
  */
  cygwin_conv_to_posix_path(filename, cygwinFilename);
  filename = cygwinFilename;
#endif

  if (filename[0] && filename[0] == '/')
     {
       /* Absolute (unix-style) pathname.  Do nothing */
       strncat (fullpath, filename, QXE_PATH_MAX);
     }
  else
    {
      /* Assume relative Unix style path.  Get the current directory
       and prepend it.  FIXME: need to fix the case of DOS paths like
       "\foo", where we need to get the current drive. */

      strncat (fullpath, get_current_working_directory (), QXE_PATH_MAX);
      len = strlen (fullpath);

      /* If no trailing slash, add one */
      if (len <= 0 || (fullpath[len - 1] != '/' && len < QXE_PATH_MAX))
	{
	  strcat (fullpath, "/");
	  len++;
	}

      /* Don't forget to add the filename! */
      strncat (fullpath, filename, QXE_PATH_MAX - len);
    }
} /* filename_expand */
Exemplo n.º 8
0
    void asdf_multiplat_t::init(std::string _exec_dir) {
        LOG("--- Initializing This Crazy Contraption ---");

        running = true;

        exec_dir = _exec_dir;
        working_directory = get_current_working_directory();
        SetCurrentDir(exec_dir.c_str());

        LOG("Exec Dir: %s", exec_dir.c_str());
        LOG("Working Dir: %s", working_directory.c_str());

        init_SDL();
        in_focus = true;

        renderer = make_unique<asdf_renderer_t>();
        renderer->init();

        Content.init();
        spritebatch = make_shared<spritebatch_t>();
        spritebatch->spritebatch_shader = Content.shaders["spritebatch"];

        main_view = make_shared<ui_view_t>(glm::vec2(0, 0), glm::vec2(settings.resolution_width, settings.resolution_height));
    }
Exemplo n.º 9
0
temp_working_dirt::temp_working_dirt(const std::string &name_template):
  temp_dirt(name_template)
{
  old_working_directory=get_current_working_directory();
  chdir(path.c_str());
}
Exemplo n.º 10
0
int main(int argc, char *argv[])
{
    const char *path_list = getenv("PATH");
    int short_option, long_option, fail_count = 0, path_index;
    int show_dot = 0, show_tilde = 0, tty_only = 0;
    struct option longopts[] = {
        {"version", 0, &long_option, opt_version},
        {"skip-dot", 0, &long_option, opt_skip_dot},
        {"skip-tilde", 0, &long_option, opt_skip_tilde},
        {"show-dot", 0, &long_option, opt_show_dot},
        {"show-tilde", 0, &long_option, opt_show_tilde},
        {"tty-only", 0, &long_option, opt_tty_only},
        {"all", 0, NULL, 'a'},
        {NULL, 0, NULL, 0}
    };

    progname = argv[0];
    while ((short_option = getopt_long(argc, argv, "avV", longopts, NULL)) != -1)
    {
        switch (short_option)
        {
        case 0:
            switch (long_option)
            {
            case opt_version:
                print_version();
                return 0;
            case opt_skip_dot:
                skip_dot = !tty_only;
                break;
            case opt_skip_tilde:
                skip_tilde = !tty_only;
                break;
            case opt_show_dot:
                show_dot = !tty_only;
                break;
#ifndef WIN32
            case opt_show_tilde:
                show_tilde = (!tty_only && geteuid() != 0);
                break;
#endif
            case opt_tty_only:
                tty_only = !isatty(1);
                break;
            }
            break;
        case 'a':
            show_all = 1;
            break;
        case 'v':
        case 'V':
            print_version();
            return 0;
        }
    }

    if (show_dot)
        get_current_working_directory();

    if (show_tilde || skip_tilde)
    {
        const char *h;

        if (!(h = getenv("HOME")))
        {
            fprintf(stderr, "%s: ", progname);
            if (show_tilde)
                fprintf(stderr, "--show-tilde");
            else
                fprintf(stderr, "--skip-tilde");
            fprintf(stderr, ": Environment variable HOME not set\n");
            show_tilde = skip_tilde = 0;
        }
        else
        {
            strncpy(home, h, sizeof(home));
            home[sizeof(home) - 1] = 0;
            homelen = strlen(home);
            if (home[homelen - 1] != DIRSEP && homelen < sizeof(home) - 1)
            {
                strcat(home, "/");
                ++homelen;
            }
        }
    }

    argv += optind;

    if (!*argv)
    {
        print_usage();
        return -1;
    }

    for (; *argv; ++argv)
    {
        char *result = NULL;
        int found_something = 0;

        if (path_list && *path_list != '\0')
        {
            int next;
            path_index = 0;
            do
            {
                next = show_all;
                result = find_command_in_path(*argv, path_list, &path_index);
                if (result)
                {
                    const char *full_path = path_clean_up(result);
                    int in_home = (show_tilde || skip_tilde) && !strncmp(full_path, home, homelen);
                    if (!(skip_tilde && in_home) && show_dot && found_path_starts_with_dot && !strncmp(full_path, cwd, cwdlen))
                    {
                        full_path += cwdlen;
                        fprintf(stdout, "./");
                    }
                    else if (in_home)
                    {
                        if (skip_tilde)
                        {
                            next = 1;
                            continue;
                        }
                        if (show_tilde)
                        {
                            full_path += homelen;
                            fprintf(stdout, "~/");
                        }
                    }
                    fprintf(stdout, "%s\n", full_path);
                    free(result);
                    found_something = 1;
                }
                else
                    break;
            }
            while(next);
        }
        if (!found_something)
        {
            print_fail(absolute_path_given ? strrchr(*argv, DIRSEP) + 1 : *argv, absolute_path_given ? abs_path : path_list);
            ++fail_count;
        }
    }

    return fail_count;
}