strncpy (tmp, buf, sizeof (tmp)); snprintf (buf, sizeof (buf), "%s:%u", tmp, n);
int imap_sync_mailbox (CONTEXT *ctx, int expunge){  char seq[8];  char buf[LONG_STRING];  char flags[LONG_STRING];  char tmp[LONG_STRING];  int n;  int setstart = 0;  if (CTX_DATA->state != IMAP_SELECTED)  {    dprint (2, (debugfile, "imap_sync_mailbox: no mailbox selected\n"));    return -1;  }    /* if we are expunging anyway, we can do deleted messages very quickly... */  if (expunge && mutt_bit_isset (CTX_DATA->rights, IMAP_ACL_DELETE))  {    buf[0] = '\0';    for (n = 0; n < ctx->msgcount; n++)    {      if (ctx->hdrs[n]->changed && ctx->hdrs[n]->deleted)      {        if (setstart == 0)        {          setstart = n+1;          if (!buf[0])            snprintf (buf, sizeof (buf), "%u", n+1);          else          {            strncpy (tmp, buf, sizeof (tmp));            snprintf (buf, sizeof (buf), "%s,%u", tmp, n+1);          }        }        /* tie up if the last message is also deleted */        else if (n == ctx->msgcount-1)        {          strncpy (tmp, buf, sizeof (tmp));          snprintf (buf, sizeof (buf), "%s:%u", tmp, n+1);        }        /* the normal flag update can skip this message */        ctx->hdrs[n]->changed = 0;      }      else if (setstart && (n > setstart))      {        setstart = 0;        strncpy (tmp, buf, sizeof (tmp));        snprintf (buf, sizeof (buf), "%s:%u", tmp, n);      }    }    /* if we have a message set, then let's delete */    if (buf[0])    {      snprintf (tmp, sizeof (tmp), _("Marking %d messages for deletion..."), ctx->deleted);      mutt_message (tmp);      imap_make_sequence (seq, sizeof (seq));      snprintf (tmp, sizeof (tmp), "%s STORE %s +FLAGS.SILENT (\\Deleted)\r\n",        seq, buf);      if (imap_exec (buf, sizeof (buf), CTX_DATA, seq, tmp, 0) != 0)        /* continue, let regular store try before giving up */        dprint(2, (debugfile, "imap_sync_mailbox: fast delete failed\n"));    }  }  /* save status changes */  for (n = 0; n < ctx->msgcount; n++)  {    if (ctx->hdrs[n]->changed)    {      snprintf (buf, sizeof (buf), _("Saving message status flags... [%d/%d]"),		n+1, ctx->msgcount);      mutt_message (buf);            flags[0] = '\0';            imap_set_flag (ctx, IMAP_ACL_SEEN, ctx->hdrs[n]->read, "\\Seen ",        flags);      imap_set_flag (ctx, IMAP_ACL_WRITE, ctx->hdrs[n]->flagged, "\\Flagged ",        flags);      imap_set_flag (ctx, IMAP_ACL_WRITE, ctx->hdrs[n]->replied,        "\\Answered ", flags);      imap_set_flag (ctx, IMAP_ACL_DELETE, ctx->hdrs[n]->deleted, "\\Deleted ",        flags);      /* now make sure we don't lose custom tags */      imap_add_keywords (flags, ctx->hdrs[n], CTX_DATA->flags);            mutt_remove_trailing_ws (flags);            imap_make_sequence (seq, sizeof (seq));      /* UW-IMAP is OK with null flags, Cyrus isn't. The only solution is to       * explicitly revoke all system flags (if we have permission) */      if (!*flags)      {        imap_set_flag (ctx, IMAP_ACL_SEEN, 1, "\\Seen ", flags);        imap_set_flag (ctx, IMAP_ACL_WRITE, 1, "\\Flagged ", flags);        imap_set_flag (ctx, IMAP_ACL_WRITE, 1, "\\Answered ", flags);        imap_set_flag (ctx, IMAP_ACL_DELETE, 1, "\\Deleted ", flags);        mutt_remove_trailing_ws (flags);        snprintf (buf, sizeof (buf), "%s STORE %d -FLAGS.SILENT (%s)\r\n", seq,          ctx->hdrs[n]->index + 1, flags);      }      else        snprintf (buf, sizeof (buf), "%s STORE %d FLAGS.SILENT (%s)\r\n", seq,          ctx->hdrs[n]->index + 1, flags);      if (imap_exec (buf, sizeof (buf), CTX_DATA, seq, buf, 0) != 0)      {        imap_error ("imap_sync_mailbox()", buf);        /* Give up on this message if we pass here again */        ctx->hdrs[n]->changed = 0;        return -1;      }      ctx->hdrs[n]->changed = 0;    }  }  ctx->changed = 0;  if (expunge == 1)  {    /* expunge is implicit if closing */    if (ctx->closing)    {      mutt_message _("Closing mailbox...");      imap_make_sequence (seq, sizeof (seq));      snprintf (buf, sizeof (buf), "%s CLOSE\r\n", seq);      if (imap_exec (buf, sizeof (buf), CTX_DATA, seq, buf, 0) != 0)      {        imap_error ("imap_sync_mailbox()", buf);        return -1;      }      CTX_DATA->state = IMAP_AUTHENTICATED;    }    else if (mutt_bit_isset(CTX_DATA->rights, IMAP_ACL_DELETE))    {      mutt_message _("Expunging messages from server...");      CTX_DATA->status = IMAP_EXPUNGE;      imap_make_sequence (seq, sizeof (seq));      snprintf (buf, sizeof (buf), "%s EXPUNGE\r\n", seq);      if (imap_exec (buf, sizeof (buf), CTX_DATA, seq, buf, 0) != 0)      {        imap_error ("imap_sync_mailbox()", buf);        return -1;      }      CTX_DATA->status = 0;    }  }  for (n = 0; n < IMAP_CACHE_LEN; n++)  {    if (CTX_DATA->cache[n].path)    {      unlink (CTX_DATA->cache[n].path);      safe_free ((void **) &CTX_DATA->cache[n].path);    }  }  return 0;}