Beispiel #1
0
/* Path search algorithm, for tmpnam, tmpfile, etc.  If DIR is
   non-null and exists, uses it; otherwise uses the first of $TMPDIR,
   P_tmpdir, /tmp that exists.  Copies into TMPL a template suitable
   for use with mk[s]temp.  Will fail (-1) if DIR is non-null and
   doesn't exist, none of the searched dirs exists, or there's not
   enough space in TMPL. */
int attribute_hidden ___path_search (char *tmpl, size_t tmpl_len, const char *dir,
	const char *pfx /*, int try_tmpdir*/)
{
    /*const char *d; */
    size_t dlen, plen;

    if (!pfx || !pfx[0])
    {
	pfx = "file";
	plen = 4;
    }
    else
    {
	plen = strlen (pfx);
	if (plen > 5)
	    plen = 5;
    }

    /* Disable support for $TMPDIR */
#if 0
    if (try_tmpdir)
    {
	d = __secure_getenv ("TMPDIR");
	if (d != NULL && direxists (d))
	    dir = d;
	else if (dir != NULL && direxists (dir))
	    /* nothing */ ;
	else
	    dir = NULL;
    }
#endif
    if (dir == NULL)
    {
	if (direxists (P_tmpdir))
	    dir = P_tmpdir;
	else if (strcmp (P_tmpdir, "/tmp") != 0 && direxists ("/tmp"))
	    dir = "/tmp";
	else
	{
	    __set_errno (ENOENT);
	    return -1;
	}
    }

    dlen = strlen (dir);
    while (dlen > 1 && dir[dlen - 1] == '/')
	dlen--;			/* remove trailing slashes */

    /* check we have room for "${dir}/${pfx}XXXXXX\0" */
    if (tmpl_len < dlen + 1 + plen + 6 + 1)
    {
	__set_errno (EINVAL);
	return -1;
    }

    sprintf (tmpl, "%.*s/%.*sXXXXXX", dlen, dir, plen, pfx);
    return 0;
}
Beispiel #2
0
void rmDir(const char* path) {
  DIR* dir = opendir(path);
  if(!dir) {
    return;
  }
  struct dirent * dp;
  while((dp = readdir(dir)) != NULL) {
    const char* name = dp->d_name;
    if(strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
      continue;
    }
    char filePath[1024];
    if ((int) (sizeof(filePath)-1) < snprintf(filePath, sizeof(filePath), "%s/%s", path, name)) {
      continue; // buffer overflow
    }
    if(direxists(filePath)) {
      rmDir(filePath);
    }
    else {
      unlink(filePath);
    }
  }
  closedir(dir);
  rmdir(path);
}
Beispiel #3
0
int checkfirstrun(char *progname)
{
	// construct the user's path to .config
	char upath[PATH_MAX];
	sprintf(upath, "%s/.config/%s/", getenv("HOME"), progname);
	return direxists(upath);
} // checkfirstrun()
Beispiel #4
0
/* see if a file exists */
int FileExists( char *name )
{
    FILE    *handle;

    /* try to open the file */
    handle = fopen( name, "rb" );
    if (handle == NULL) return 0;
    fclose( handle );
#ifdef unix    
    if(direxists(name))return 0;
#endif
    return 1;
}
Beispiel #5
0
int mkpath(const char *dir, const mode_t mode)
{
	int i = 0, len = 0, ret = 1;
	char *tmp = NULL;

	if (!strlen(dir)) {
		if (debug)
			printf("Error: mkpath(), no directory given\n");
		return 0;
	}

	if (direxists(dir)) {
		if (debug)
			printf("already exists: %s\n", dir);
		return 1;
	}

	if (!cfg.createdirs) {
		return 0;
	}

	tmp = strdup(dir);
	if (tmp == NULL) {
		return 0;
	}

	len = strlen(tmp);
	if (tmp[len-1] == '/') {
		tmp[len-1] = '\0';
	}

	if (tmp[0] == '/') {
		i++;
	}

	for (; i<len; i++) {
		if (tmp[i] == '/') {
			tmp[i] = '\0';
			if (!direxists(tmp)) {
				if (mkdir(tmp, mode)!=0) {
					if (debug)
						printf("Error: mkdir() \"%s\": %s\n", tmp, strerror(errno));
					ret = 0;
					break;
				}
			}
			tmp[i] = '/';
		}
	}
	if (ret) {
		if (mkdir(tmp, mode)!=0) {
			if (debug)
				printf("Error: mkdir() \"%s\": %s\n", tmp, strerror(errno));
			ret = 0;
		} else if (debug) {
			printf("created: %s\n", tmp);
		}
	}

	free(tmp);
	return ret;
}
/* Path search algorithm, for tmpnam, tmpfile, etc.  If DIR is
   non-null and exists, uses it; otherwise uses the first of $TMPDIR,
   P_tmpdir, /tmp that exists.  Copies into TMPL a template suitable
   for use with mk[s]temp.  Will fail (-1) if DIR is non-null and
   doesn't exist, none of the searched dirs exists, or there's not
   enough space in TMPL. */
int
path_search (char *tmpl, size_t tmpl_len, const char *dir, const char *pfx,
             bool try_tmpdir)
{
  const char *d;
  size_t dlen, plen;
  bool add_slash;

  if (!pfx || !pfx[0])
    {
      pfx = "file";
      plen = 4;
    }
  else
    {
      plen = strlen (pfx);
      if (plen > 5)
        plen = 5;
    }

  if (try_tmpdir)
    {
      d = __libc_secure_getenv ("TMPDIR");
      if (d != NULL && direxists (d))
        dir = d;
      else if (dir != NULL && direxists (dir))
        /* nothing */ ;
      else
        dir = NULL;
    }
  if (dir == NULL)
    {
#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
      char dirbuf[PATH_MAX];
      DWORD retval;

      /* Find Windows temporary file directory.
         We try this before P_tmpdir because Windows defines P_tmpdir to "\\"
         and will therefore try to put all temporary files in the root
         directory (unless $TMPDIR is set).  */
      retval = GetTempPath (PATH_MAX, dirbuf);
      if (retval > 0 && retval < PATH_MAX && direxists (dirbuf))
        dir = dirbuf;
      else
#endif
      if (direxists (P_tmpdir))
        dir = P_tmpdir;
      else if (strcmp (P_tmpdir, "/tmp") != 0 && direxists ("/tmp"))
        dir = "/tmp";
      else
        {
          __set_errno (ENOENT);
          return -1;
        }
    }

  dlen = strlen (dir);
#ifdef __VMS
  add_slash = 0;
#else
  add_slash = dlen != 0 && !ISSLASH (dir[dlen - 1]);
#endif

  /* check we have room for "${dir}/${pfx}XXXXXX\0" */
  if (tmpl_len < dlen + add_slash + plen + 6 + 1)
    {
      __set_errno (EINVAL);
      return -1;
    }

  memcpy (tmpl, dir, dlen);
  sprintf (tmpl + dlen, &"/%.*sXXXXXX"[!add_slash], (int) plen, pfx);
  return 0;
}
Beispiel #7
0
void AndroidInit2(int width, int height)
{
  PMPLOG(("AndroidInit2 %d,%d", width, height));

  gInitialized = true;
  PMPBEGIN(("AndroidInit2"));
  quakeparms_t parms;
  int j;
  int c = 0;
  const char* v[] = {"quake", (char*) 0};

  scr_width = width;
  scr_height = height;

//	static char cwd[1024];

//	signal(SIGFPE, floating_point_exception_handler);
//  signal(SIGFPE, SIG_IGN);

  memset(&parms, 0, sizeof(parms));

  if (gArgc) {
      COM_InitArgv(gArgc, (const char**) gArgv);
  }
  else {
      COM_InitArgv(c, (const char**) v);
  }

  parms.argc = com_argc;
  parms.argv = com_argv;

  parms.memsize = 16*1024*1024;

  j = COM_CheckParm("-mem");
  if (j)
    parms.memsize = (int) (Q_atof(com_argv[j+1]) * 1024 * 1024);
  parms.membase = malloc (parms.memsize);

  const char* basedir = basedir2;
  if(direxists(basedir1))
  {
    basedir = basedir1;
  }
  else if(direxists(basedir2))
  {
    basedir = basedir2;
  }
  else
  {
    Sys_Error("Could not find data directories %s or %s", basedir1, basedir2);
  }
  parms.basedir = basedir;

  CheckGLCacheVersion(basedir);

// caching is disabled by default, use -cachedir to enable
//	parms.cachedir = cachedir;

#if 0 // FNDELAY not implemented
  noconinput = COM_CheckParm("-noconinput");
  if (!noconinput)
    fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
#endif

  if (COM_CheckParm("-nostdout"))
    nostdout = 1;

  Sys_Init();

    Host_Init(&parms);

    g_oldtime = Sys_FloatTime ();
  PMPEND(("AndroidInit2"));
}