All pastes #366625 Raw Edit

Anonymous

public text v1 · immutable
#366625 ·published 2007-02-21 20:10 UTC
rendered paste body
#ifdef __UNIXSDL__
#include "gblhdr.h"
#include "linux/safelib.h"
#include <pwd.h>
#else
#ifdef __WIN32__
#include <io.h>
#include <direct.h>
#include <shlobj.h> /* Needed for per user configuration support */
#include "win/safelib.h"
#include "win/lib.h"
#else
#include "dos/lib.h"
#include <unistd.h>
#endif
#include <string.h>
#include <stdlib.h>
#endif
#include <errno.h>

#include "zpath.h"
#include "cfg.h"

#ifdef __WIN32__
#define mkdir_p(path) mkdir(path)
#else
#define mkdir_p(path) mkdir(path, mmode)
#endif

#ifdef __MSDOS__
char ZCfgFile[] = "zsnes.cfg";
#elif defined(__WIN32__)
char ZCfgFile[] = "zsnesw.cfg";
#else
char ZCfgFile[] = "zsnesl.cfg";
#endif

char *ZStartPath = 0, *ZCfgPath = 0, *ZSramPath = 0, *ZRomPath = 0;
char *ZSnapPath = 0, *ZSpcPath = 0, *ZIpsPath = 0, *ZMoviePath = 0;
char *ZChtPath = 0, *ZComboPath = 0, *ZInpPath = 0, *ZSStatePath = 0;
char *ZCartName = 0, *ZSaveName = 0, *ZStateName = 0, *ZSaveST2Name = 0;

static bool ZStartAlloc = false, ZCfgAlloc = false, ZRomAlloc = false;
static bool ZCartAlloc = false, ZSaveAlloc = false, ZStateAlloc = false, ZSaveST2Alloc = false;

#ifdef __UNIXSDL__

void cfgpath_ensure(const char *launch_command)
{
  struct passwd *userinfo;
#ifdef ZCONF
  const char *const zpath = ZCONF;
#else
#ifndef __MACOSX__
  const char *const zpath = ".zsnes";
#else
  const char *const zpath = "Library/Application Support/ZSNES";
#endif
#endif

  if ((userinfo = getpwuid(getuid())))
  {
    ZCfgPath = malloc(PATH_SIZE);
  }
  else
  {
    puts("Error obtaining info about your user.");
  }

  if (ZCfgPath)
  {
    struct stat stat_buffer;

    ZCfgAlloc = true;
    strcpy(ZCfgPath, userinfo->pw_dir);
    strcatslash(ZCfgPath);
    strcat(ZCfgPath, zpath);

    if (mkpath(ZCfgPath, 0755) && !stat(ZCfgPath, &stat_buffer) && S_ISDIR(stat_buffer.st_mode) && !access(ZCfgPath, W_OK))
    {
      strcatslash(ZCfgPath);
    }
    else
    {
      printf("Error creating: %s\n", ZCfgPath);
      free(ZCfgPath);
      ZCfgAlloc = false;

      ZCfgPath = ZStartPath;
    }
  }
  else
  {
    ZCfgPath = ZStartPath;
  }
}

#else

void cfgpath_ensure(const char *launch_command)
{
  ZCfgPath = malloc(PATH_SIZE);
  if (ZCfgPath)
  {
    char *p = 0;
    ZCfgAlloc = true;
    
    /* BEGIN MODIFICATION */
#ifdef __WIN32__
    {
      const char *const zpath = ".zsnes";
      
      /* No problem with buffer size, PATH_SIZE > MAX_PATH on Windows */
      if (SHGetFolderPath(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, ZCfgPath) == 0 /* S_OK */)
      {
        strcatslash(ZCfgPath);
        strcat(ZCfgPath, zpath);
        if (mkpath(ZCfgPath, 0755))
        {
          /* Todo: Need to make sure it is a directory and not a file that already existed */
          strcatslash(ZCfgPath);
          strcat(ZCfgPath, ZCfgFile);
          p = ZCfgPath;
        }
      }  
    }
#endif    
    
    /* Unable to do per-user configuration, fall back to program directory */
    if (p == 0)
    {
      if (isextension(launch_command, "exe"))
      {
        p = realpath(launch_command, ZCfgPath);
      }
      else
      {
        char buff[PATH_SIZE];
        strcpy(buff, launch_command);
        setextension(buff, "exe");
        p = realpath(buff, ZCfgPath);
      }
    }
    /* END MODIFICATION */

    if (p)
    {
      strdirname(ZCfgPath);
      strcatslash(ZCfgPath);
    }
    else
    {
      free(ZCfgPath);
      ZCfgAlloc = false;
      ZCfgPath = ZStartPath;
    }
  }
  else
  {
    ZCfgPath = ZStartPath;
  }
}

#endif