- for (di=readdir(d); di; di=readdir(d)) {
- char *dname = d_name(di);
- if (strcmp(dname,".")==0 ||
- strcmp(dname,"..")==0)
- continue;
- snprintf(buf, sizeof(buf), "%s/%s", fname, dname);
- if (verbose > 0)
- rprintf(FINFO,"deleting %s\n", buf);
- if (delete_file(buf) != 0) {
- closedir(d);
+/* This function converts the chars in the "in" xbuf into characters in the
+ * "out" xbuf. The ".len" chars of the "in" xbuf is used starting from its
+ * ".pos". The ".size" of the "out" xbuf restricts how many characters can
+ * be stored, starting at its ".pos+.len" position. Note that the last byte
+ * of the "out" xbuf is not used, which reserves space for a trailing '\0'
+ * (though it is up to the caller to store a trailing '\0', as needed).
+ *
+ * We return a 0 on success or a -1 on error. An error also sets errno to
+ * E2BIG, EILSEQ, or EINVAL (see below); otherwise errno will be set to 0.
+ * The "in" xbuf is altered to update ".pos" and ".len". The "out" xbuf has
+ * data appended, and its ".len" incremented (see below for a ".size" note).
+ *
+ * If ICB_CIRCULAR_OUT is set in "flags", the chars going into the "out" xbuf
+ * can wrap around to the start, and the xbuf may have its ".size" reduced
+ * (presumably by 1 byte) if the iconv code doesn't have space to store a
+ * multi-byte character at the physical end of the ".buf" (though no reducing
+ * happens if ".pos" is <= 1, since there is no room to wrap around).
+ *
+ * If ICB_EXPAND_OUT is set in "flags", the "out" xbuf will be allocated if
+ * empty, and (as long as ICB_CIRCULAR_OUT is not set) expanded if too small.
+ * This prevents the return of E2BIG (except for a circular xbuf).
+ *
+ * If ICB_INCLUDE_BAD is set in "flags", any badly-encoded chars are included
+ * verbatim in the "out" xbuf, so EILSEQ will not be returned.
+ *
+ * If ICB_INCLUDE_INCOMPLETE is set in "flags", any incomplete multi-byte
+ * chars are included, which ensures that EINVAL is not returned.
+ *
+ * If ICB_INIT is set, the iconv() conversion state is initialized prior to
+ * processing the characters. */
+int iconvbufs(iconv_t ic, xbuf *in, xbuf *out, int flags)
+{
+ ICONV_CONST char *ibuf;
+ size_t icnt, ocnt, opos;
+ char *obuf;
+
+ if (!out->size && flags & ICB_EXPAND_OUT) {
+ size_t siz = ROUND_UP_1024(in->len * 2);
+ alloc_xbuf(out, siz);
+ } else if (out->len+1 >= out->size) {
+ /* There is no room to even start storing data. */
+ if (!(flags & ICB_EXPAND_OUT) || flags & ICB_CIRCULAR_OUT) {
+ errno = E2BIG;