iobuf_close (iobuf_t a)
{
  iobuf_t a2;
  size_t dummy_len = 0;
  int rc = 0;

  if (a && a->directfp)
    {
      fclose (a->directfp);
      xfree (a->real_fname);
      if (DBG_IOBUF)
	log_debug ("iobuf_close -> %p\n", a->directfp);
      return 0;
    }

  for (; a && !rc; a = a2)
    {
      a2 = a->chain;
      if (a->use == 2 && (rc = iobuf_flush (a)))
	log_error ("iobuf_flush failed on close: %s\n", gpg_strerror (rc));

      if (DBG_IOBUF)
	log_debug ("iobuf-%d.%d: close '%s'\n", a->no, a->subno,
                   a->desc?a->desc:"?");
      if (a->filter && (rc = a->filter (a->filter_ov, IOBUFCTRL_FREE,
					a->chain, NULL, &dummy_len)))
	log_error ("IOBUFCTRL_FREE failed on close: %s\n", gpg_strerror (rc));
      xfree (a->real_fname);
      if (a->d.buf)
	{
	  memset (a->d.buf, 0, a->d.size);	/* erase the buffer */
	  xfree (a->d.buf);
	}
      xfree (a);
    }
  return rc;
}
iobuf_close (iobuf_t a)
{
  iobuf_t a2;
  size_t dummy_len = 0;
  int rc = 0;

  if (a && a->directfp)
    {
      fclose (a->directfp);
      xfree (a->real_fname);
      if (DBG_IOBUF)
	log_debug ("iobuf_close -> %p\n", a->directfp);
      return 0;
    }

  for (; a && !rc; a = a2)
    {
      a2 = a->chain;
      if (a->use == 2 && (rc = iobuf_flush (a)))
	log_error ("iobuf_flush failed on close: %s\n", gpg_strerror (rc));

      if (DBG_IOBUF)
	log_debug ("iobuf-%d.%d: close '%s'\n", a->no, a->subno,
                   a->desc?a->desc:"?");
      if (a->filter && (rc = a->filter (a->filter_ov, IOBUFCTRL_FREE,
					a->chain, NULL, &dummy_len)))
	log_error ("IOBUFCTRL_FREE failed on close: %s\n", gpg_strerror (rc));
      xfree (a->real_fname);
      if (a->d.buf)
	{
	  memset (a->d.buf, 0, a->d.size);	 
	  xfree (a->d.buf);
	}
      xfree (a);
    }
  return rc;
}