Urlader

 view release on metacpan or  search on metacpan

urlib.c  view on Meta::CPAN

}

static void
u_munmap (void *addr, unsigned int len)
{
#ifdef _WIN32
  UnmapViewOfFile (addr);
#else
  munmap (addr, len);
#endif
}

/////////////////////////////////////////////////////////////////////////////

typedef struct
{
  char *addr;
  unsigned int used;
  unsigned int size;
} u_dynbuf;

static void *
u_dynbuf_append (u_dynbuf *dynbuf, void *data, unsigned int len)
{
  char *dest;

  if ((dynbuf->used += len) > dynbuf->size)
    {
      unsigned int new_size = dynbuf->size ? dynbuf->size * 2 : 4096;
      dynbuf->addr = u_realloc (dynbuf->addr, dynbuf->size, new_size);
      dynbuf->size = new_size;
    }

  dest = dynbuf->addr + dynbuf->used - len;

  if (data)
    memcpy (dest, data, len);

  return dest;
}

/////////////////////////////////////////////////////////////////////////////

static void
u_set_datadir (void)
{
#ifdef _WIN32
  if (SHGetFolderPath (0, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, datadir) != S_OK)
    u_fatal ("unable to find application data directory");

  u_mkdir (datadir);
  u_append (datadir, URLADER);

#else
  char *home = getenv ("HOME");

  if (!home)
    {
      struct passwd *pw;

      if ((pw = getpwuid (getuid ())))
        home = pw->pw_dir;
      else
        home = "/tmp";
    }

  u_mkdir (home);
  //strcat (strcat (strcpy (datadir, home), "/."), URLADER);
  sprintf (datadir, "%s/.%s", home, URLADER);
#endif

  u_setenv ("URLADER_DATADIR", datadir);
}

static void
u_set_exe_info (void)
{
  strcpy (exe_dir, datadir);
  u_append (exe_dir, exe_id);
  u_mkdir (exe_dir);

  strcpy (execdir, exe_dir);
  u_append (execdir, "i-");
  strcat (execdir, exe_ver);

  u_setenv ("URLADER_EXECDIR", execdir);
  u_setenv ("URLADER_EXE_ID" , exe_id);
  u_setenv ("URLADER_EXE_DIR", exe_dir);
  u_setenv ("URLADER_EXE_VER", exe_ver);
}

/////////////////////////////////////////////////////////////////////////////

static u_handle
u_lock (const char *path, int excl, int dowait)
{
  u_handle h;

  h = u_lockfile (path);
  if (!u_valid (h))
    return h;

  u_cloexec (h);

  for (;;)
    {
      int success;

      // acquire the lock
#ifdef _WIN32
      OVERLAPPED ov = { 0 };

      success = LockFileEx (h,
                            (excl ? LOCKFILE_EXCLUSIVE_LOCK : 0)
                            | (dowait ? 0 : LOCKFILE_FAIL_IMMEDIATELY),
                            0,
                            1, 0,
                            &ov);
#else
      struct flock lck = { 0 };



( run in 2.348 seconds using v1.01-cache-2.11-cpan-a49fcb8fa48 )