+static const int STALLTIME = 5;
+#endif
+
+static void close_and_delete_outfile(FILE* output, char *fname_out, int do_continue)
+{
+ if (output != stdout && do_continue==0) {
+ fclose(output);
+ unlink(fname_out);
+ }
+}
+
+/* Read NMEMB elements of SIZE bytes into PTR from STREAM. Returns the
+ * number of elements read, and a short count if an eof or non-interrupt
+ * error is encountered. */
+static size_t safe_fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
+{
+ size_t ret = 0;
+
+ do {
+ clearerr(stream);
+ ret += fread((char *)ptr + (ret * size), size, nmemb - ret, stream);
+ } while (ret < nmemb && ferror(stream) && errno == EINTR);
+
+ return ret;
+}
+
+/* Write NMEMB elements of SIZE bytes from PTR to STREAM. Returns the
+ * number of elements written, and a short count if an eof or non-interrupt
+ * error is encountered. */
+static size_t safe_fwrite(void *ptr, size_t size, size_t nmemb, FILE *stream)
+{
+ size_t ret = 0;
+
+ do {
+ clearerr(stream);
+ ret += fwrite((char *)ptr + (ret * size), size, nmemb - ret, stream);
+ } while (ret < nmemb && ferror(stream) && errno == EINTR);
+
+ return ret;
+}
+
+/* Read a line or SIZE - 1 bytes into S, whichever is less, from STREAM.
+ * Returns S, or NULL if an eof or non-interrupt error is encountered. */
+static char *safe_fgets(char *s, int size, FILE *stream)
+{
+ char *ret;
+
+ do {
+ clearerr(stream);
+ ret = fgets(s, size, stream);
+ } while (ret == NULL && ferror(stream) && errno == EINTR);
+
+ return ret;
+}
+
+#define close_delete_and_die(s...) { \
+ close_and_delete_outfile(output, fname_out, do_continue); \
+ error_msg_and_die(s); }
+
+
+#ifdef BB_FEATURE_WGET_AUTHENTICATION
+/*
+ * Base64-encode character string
+ * oops... isn't something similar in uuencode.c?
+ * It would be better to use already existing code
+ */
+char *base64enc(char *p, char *buf, int len) {
+
+ char al[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
+ "0123456789+/";
+ char *s = buf;
+
+ while(*p) {
+ if (s >= buf+len-4)
+ error_msg_and_die("buffer overflow");
+ *(s++) = al[(*p >> 2) & 0x3F];
+ *(s++) = al[((*p << 4) & 0x30) | ((*(p+1) >> 4) & 0x0F)];
+ *s = *(s+1) = '=';
+ *(s+2) = 0;
+ if (! *(++p)) break;
+ *(s++) = al[((*p << 2) & 0x3C) | ((*(p+1) >> 6) & 0x03)];
+ if (! *(++p)) break;
+ *(s++) = al[*(p++) & 0x3F];
+ }
+
+ return buf;
+}