strncpy (tmp, buf, buflen); snprintf (buf, buflen, "%s:%u", tmp, HEADER_DATA (hdrs[n])->uid);
snprintf (uid, sizeof (uid), ":%u", HEADER_DATA (hdrs[n])->uid); mutt_buffer_addstr (buf, uid);
int imap_make_msg_set (IMAP_DATA* idata, char* buf, size_t buflen, int flag,  int changed){  HEADER** hdrs;	/* sorted local copy */  int count = 0;	/* number of messages in message set */  int match = 0;	/* whether current message matches flag condition */  int setstart = 0;	/* start of current message range */  char* tmp;  int n;  short oldsort;	/* we clobber reverse, must restore it */  /* sanity-check */  if (!buf || buflen < 2)    return 0;  *buf = '\0';  /* make copy of header pointers to sort in natural order */  hdrs = safe_calloc (idata->ctx->msgcount, sizeof (HEADER*));  memcpy (hdrs, idata->ctx->hdrs, idata->ctx->msgcount * sizeof (HEADER*));  if (Sort != SORT_ORDER)  {    oldsort = Sort;    Sort = SORT_ORDER;    qsort ((void*) hdrs, idata->ctx->msgcount, sizeof (HEADER*),      mutt_get_sort_func (SORT_ORDER));    Sort = oldsort;  }    tmp = safe_malloc (buflen);  for (n = 0; n < idata->ctx->msgcount; n++)  {    match = 0;    /* don't include pending expunged messages */    if (hdrs[n]->active)      switch (flag)      {        case M_DELETE:	  if (hdrs[n]->deleted)	    match = 1;	  break;        case M_TAG:	  if (hdrs[n]->tagged)	    match = 1;	  break;      }    if (match && (!changed || hdrs[n]->changed))    {      count++;      if (setstart == 0)      {        setstart = HEADER_DATA (hdrs[n])->uid;        if (!buf[0])          snprintf (buf, buflen, "%u", HEADER_DATA (hdrs[n])->uid);        else        {          strncpy (tmp, buf, buflen);          snprintf (buf, buflen, "%s,%u", tmp, HEADER_DATA (hdrs[n])->uid);        }      }      /* tie up if the last message also matches */      else if (n == idata->ctx->msgcount-1)      {        strncpy (tmp, buf, buflen);        snprintf (buf, buflen, "%s:%u", tmp, HEADER_DATA (hdrs[n])->uid);      }    }    /* this message is not expunged and doesn't match. End current set. */    else if (setstart && hdrs[n]->active)    {      if (HEADER_DATA (hdrs[n-1])->uid > setstart)      {        strncpy (tmp, buf, buflen);        snprintf (buf, buflen, "%s:%u", tmp, HEADER_DATA (hdrs[n-1])->uid);      }      setstart = 0;    }  }  safe_free ((void**) &tmp);  safe_free ((void**) &hdrs);  return count;}