+
+/**
+ * Allocate a sufficiently large string and print into it.
+ *
+ * \param fmt A usual format string.
+ *
+ * Produce output according to \p fmt. No artificial bound on the length of the
+ * resulting string is imposed.
+ *
+ * \return This function either returns a pointer to a string that must be
+ * freed by the caller or \p NULL if memory allocation failed.
+ *
+ * \sa printf(3).
+ */
+__must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...)
+{
+ int n;
+ size_t size = 100;
+ char *p = malloc(size);
+
+ if (!p)
+ return NULL;
+ while (1) {
+ char *q;
+ va_list ap;
+ /* Try to print in the allocated space. */
+ va_start(ap, fmt);
+ n = vsnprintf(p, size, fmt, ap);
+ va_end(ap);
+ /* If that worked, return the string. */
+ if (n > -1 && n < size)
+ break;
+ /* Else try again with more space. */
+ if (n > -1) /* glibc 2.1 */
+ size = n + 1; /* precisely what is needed */
+ else /* glibc 2.0 */
+ size *= 2; /* twice the old size */
+ q = realloc(p, size);
+ if (!q) {
+ free(p);
+ return NULL;
+ }
+ p = q;
+ }
+ return p;
+}
+
+/**
+ * A wrapper for truncate(2)
+ *
+ * \param path Name of the regular file to truncate
+ * \param size Number of bytes to \b shave \b off
+ *
+ * Truncate the regular file named by \a path by \a size bytes.
+ *
+ * \return Standard.
+ *
+ * \sa truncate(2)
+ */
+int truncate_file(const char *path, off_t size)
+{
+ int ret;
+ struct stat statbuf;
+
+ ret = osl_stat(path, &statbuf);
+ if (ret < 0)
+ return ret;
+ ret = -E_OSL_BAD_SIZE;
+ if (statbuf.st_size < size)
+ return ret;
+ if (truncate(path, statbuf.st_size - size) < 0)
+ return -ERRNO_TO_ERROR(errno);
+ return 1;
+}