Esempio n. 1
0
static int
lock_file_1 (char *lfname, int force)
{
  int err;
  int symlink_errno;
  USE_SAFE_ALLOCA;

  /* Call this first because it can GC.  */
  printmax_t boot = get_boot_time ();

  Lisp_Object luser_name = Fuser_login_name (Qnil);
  char const *user_name = STRINGP (luser_name) ? SSDATA (luser_name) : "";
  Lisp_Object lhost_name = Fsystem_name ();
  char const *host_name = STRINGP (lhost_name) ? SSDATA (lhost_name) : "";
  ptrdiff_t lock_info_size = (strlen (user_name) + strlen (host_name)
			      + 2 * INT_STRLEN_BOUND (printmax_t)
			      + sizeof "@.:");
  char *lock_info_str = SAFE_ALLOCA (lock_info_size);
  printmax_t pid = getpid ();

  esprintf (lock_info_str, boot ? "%s@%s.%"pMd":%"pMd : "%s@%s.%"pMd,
	    user_name, host_name, pid, boot);

  err = symlink (lock_info_str, lfname);
  if (errno == EEXIST && force)
    {
      unlink (lfname);
      err = symlink (lock_info_str, lfname);
    }

  symlink_errno = errno;
  SAFE_FREE ();
  errno = symlink_errno;
  return err == 0;
}
Esempio n. 2
0
static int
lock_file_1 (char *lfname, int force)
{
  register int err;
  time_t boot;
  const char *user_name;
  const char *host_name;
  char *lock_info_str;

  /* Call this first because it can GC.  */
  boot = get_boot_time ();

  if (STRINGP (Fuser_login_name (Qnil)))
    user_name = SSDATA (Fuser_login_name (Qnil));
  else
    user_name = "";
  if (STRINGP (Fsystem_name ()))
    host_name = SSDATA (Fsystem_name ());
  else
    host_name = "";
  lock_info_str = (char *)alloca (strlen (user_name) + strlen (host_name)
				  + LOCK_PID_MAX + 30);

  if (boot)
    sprintf (lock_info_str, "%s@%s.%lu:%lu", user_name, host_name,
	     (unsigned long) getpid (), (unsigned long) boot);
  else
    sprintf (lock_info_str, "%s@%s.%lu", user_name, host_name,
	     (unsigned long) getpid ());

  err = symlink (lock_info_str, lfname);
  if (errno == EEXIST && force)
    {
      unlink (lfname);
      err = symlink (lock_info_str, lfname);
    }

  return err == 0;
}
Esempio n. 3
0
static void
smc_save_yourself_CB (SmcConn smcConn,
		      SmPointer clientData,
		      int saveType,
		      Bool shutdown,
		      int interactStyle,
		      Bool fast)
{
#define NR_PROPS 5

  SmProp *props[NR_PROPS];
  SmProp prop_ptr[NR_PROPS];

  SmPropValue values[20], *vp;
  int val_idx = 0, vp_idx = 0;
  int props_idx = 0;
  int i;
  char *smid_opt, *chdir_opt = NULL;
  Lisp_Object user_login_name = Fuser_login_name (Qnil);

  /* Must have these.  */
  if (! STRINGP (Vinvocation_name) || ! STRINGP (user_login_name))
    return;

  /* How to start a new instance of Emacs.  */
  props[props_idx] = &prop_ptr[props_idx];
  props[props_idx]->name = xstrdup (SmCloneCommand);
  props[props_idx]->type = xstrdup (SmLISTofARRAY8);
  props[props_idx]->num_vals = 1;
  props[props_idx]->vals = &values[val_idx++];
  props[props_idx]->vals[0].length = strlen (emacs_program);
  props[props_idx]->vals[0].value = emacs_program;
  ++props_idx;

  /* The name of the program.  */
  props[props_idx] = &prop_ptr[props_idx];
  props[props_idx]->name = xstrdup (SmProgram);
  props[props_idx]->type = xstrdup (SmARRAY8);
  props[props_idx]->num_vals = 1;
  props[props_idx]->vals = &values[val_idx++];
  props[props_idx]->vals[0].length = SBYTES (Vinvocation_name);
  props[props_idx]->vals[0].value = SDATA (Vinvocation_name);
  ++props_idx;

  /* User id.  */
  props[props_idx] = &prop_ptr[props_idx];
  props[props_idx]->name = xstrdup (SmUserID);
  props[props_idx]->type = xstrdup (SmARRAY8);
  props[props_idx]->num_vals = 1;
  props[props_idx]->vals = &values[val_idx++];
  props[props_idx]->vals[0].length = SBYTES (user_login_name);
  props[props_idx]->vals[0].value = SDATA (user_login_name);
  ++props_idx;

  char *cwd = get_current_dir_name ();
  if (cwd)
    {
      props[props_idx] = &prop_ptr[props_idx];
      props[props_idx]->name = xstrdup (SmCurrentDirectory);
      props[props_idx]->type = xstrdup (SmARRAY8);
      props[props_idx]->num_vals = 1;
      props[props_idx]->vals = &values[val_idx++];
      props[props_idx]->vals[0].length = strlen (cwd);
      props[props_idx]->vals[0].value = cwd;
      ++props_idx;
    }


  /* How to restart Emacs.  */
  props[props_idx] = &prop_ptr[props_idx];
  props[props_idx]->name = xstrdup (SmRestartCommand);
  props[props_idx]->type = xstrdup (SmLISTofARRAY8);
  /* /path/to/emacs, --smid=xxx --no-splash --chdir=dir ... */
  if (INT_ADD_WRAPV (initial_argc, 3, &i))
    memory_full (SIZE_MAX);
  props[props_idx]->num_vals = i;
  vp = xnmalloc (i, sizeof *vp);
  props[props_idx]->vals = vp;
  props[props_idx]->vals[vp_idx].length = strlen (emacs_program);
  props[props_idx]->vals[vp_idx++].value = emacs_program;

  smid_opt = xmalloc (strlen (SMID_OPT) + strlen (client_id) + 1);
  strcpy (stpcpy (smid_opt, SMID_OPT), client_id);

  props[props_idx]->vals[vp_idx].length = strlen (smid_opt);
  props[props_idx]->vals[vp_idx++].value = smid_opt;

  props[props_idx]->vals[vp_idx].length = strlen (NOSPLASH_OPT);
  props[props_idx]->vals[vp_idx++].value = NOSPLASH_OPT;

  if (cwd)
    {
      chdir_opt = xmalloc (strlen (CHDIR_OPT) + strlen (cwd) + 1);
      strcpy (stpcpy (chdir_opt, CHDIR_OPT), cwd);

      props[props_idx]->vals[vp_idx].length = strlen (chdir_opt);
      props[props_idx]->vals[vp_idx++].value = chdir_opt;
    }

  for (i = 1; i < initial_argc; ++i)
    {
      props[props_idx]->vals[vp_idx].length = strlen (initial_argv[i]);
      props[props_idx]->vals[vp_idx++].value = initial_argv[i];
    }

  ++props_idx;

  SmcSetProperties (smcConn, props_idx, props);

  xfree (smid_opt);
  xfree (chdir_opt);
  xfree (cwd);
  xfree (vp);

  for (i = 0; i < props_idx; ++i)
    {
      xfree (props[i]->type);
      xfree (props[i]->name);
    }

  /* See if we maybe shall interact with the user.  */
  if (interactStyle != SmInteractStyleAny
      || ! shutdown
      || saveType == SmSaveLocal
      || ! SmcInteractRequest (smcConn, SmDialogNormal, smc_interact_CB, 0))
    {
      /* No interaction, we are done saving ourself.  */
      SmcSaveYourselfDone (smcConn, True);
    }
}
Esempio n. 4
0
static void
mswindows_format_file (WIN32_FIND_DATA *file, char *buf, int display_size,
		       int add_newline)
{
  char			*cptr;
  int			len;
  Lisp_Object		luser;
  double		file_size;

  len = strlen(file->cFileName);
  file_size =
    file->nFileSizeHigh * (double)UINT_MAX + file->nFileSizeLow;
  cptr = buf;
#if INDENT_LISTING
  *cptr++ = ' ';
  *cptr++ = ' ';
#endif
  if (display_size)
    {
      sprintf(cptr, "%6d ", (int)((file_size + 1023.) / 1024.));
      cptr += 7;
    }
  if (file->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    {
      *cptr++ = 'd';
    } else {
      *cptr++ = '-';
    }
  cptr[0] = cptr[3] = cptr[6] = 'r';
  if (file->dwFileAttributes & FILE_ATTRIBUTE_READONLY)
    {
      cptr[1] = cptr[4] = cptr[7] = '-';
    } else {
      cptr[1] = cptr[4] = cptr[7] = 'w';
    }
  if ((file->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
      (len > 4 &&
       (_stricmp(&file->cFileName[len - 4], ".exe") == 0
	|| _stricmp(&file->cFileName[len - 4], ".com") == 0
	|| _stricmp(&file->cFileName[len - 4], ".bat") == 0
#if 0
	|| _stricmp(&file->cFileName[len - 4], ".pif") == 0
#endif
	)))
    {
      cptr[2] = cptr[5] = cptr[8] = 'x';
    } else {
      cptr[2] = cptr[5] = cptr[8] = '-';
    }
  cptr += 9;
  if (file->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    {
      strcpy(cptr, "   2 ");
    } else {
      strcpy(cptr, "   1 ");
    }
  cptr += 5;
  luser = Fuser_login_name(Qnil);
  if (!STRINGP(luser))
    {
      sprintf(cptr, "%-9d", 0);
    } else {
      char		*str;

      str = XSTRING_DATA(luser);
      sprintf(cptr, "%-8s ", str);
    }
  while (*cptr)
    {
      ++cptr;
    }
  sprintf(cptr, "%-8d ", getgid());
  cptr += 9;
  if (file_size > 99999999.0)
    {
      file_size = (file_size + 1023.0) / 1024.;
      if (file_size > 999999.0)
	{
	  sprintf(cptr, "%6.0fMB ", (file_size + 1023.0) / 1024.);
	} else {
	  sprintf(cptr, "%6.0fKB ", file_size);
	}
    } else {
      sprintf(cptr, "%8.0f ", file_size);
    }
  while (*cptr)
    {
      ++cptr;
    }
  {
    time_t		t, now;
    char		*ctimebuf;
    extern char		*sys_ctime(const time_t *t);	/* in nt.c */

    if (
#if 0
	/*
	 * This doesn't work.
	 * This code should be correct ...
	 */
	FileTimeToLocalFileTime(&file->ftLastWriteTime, &localtime) &&
	((t = convert_time(localtime)) != 0) &&
#else
	/*
	 * But this code "works" ...
	 */
	((t = convert_time(file->ftLastWriteTime)) != 0) &&
#endif
	((ctimebuf = sys_ctime(&t)) != NULL))
      {
	memcpy(cptr, &ctimebuf[4], 7);
	now = time(NULL);
	if (now - t > (365. / 2.0) * 86400.)
	  {
	    /* more than 6 months */
	    cptr[7] = ' ';
	    memcpy(&cptr[8], &ctimebuf[20], 4);
	  } else {
	    /* less than 6 months */
	    memcpy(&cptr[7], &ctimebuf[11], 5);
	  }
	cptr += 12;
	*cptr++ = ' ';
      }
  }
  if (add_newline)
    {
      sprintf(cptr, "%s\n", file->cFileName);
    }
  else
    {
      strcpy(cptr, file->cFileName);
    }
}