Patchwork [04/14] cutils: introduce qemu_fprintf_err()

login
register
mail settings
Submitter Luiz Capitulino
Date May 30, 2012, 2:14 p.m.
Message ID <1338387301-10074-5-git-send-email-lcapitulino@redhat.com>
Download mbox | patch
Permalink /patch/161969/
State New
Headers show

Comments

Luiz Capitulino - May 30, 2012, 2:14 p.m.
A fprintf() wrapper that takes an Error argument.

Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
---
 cutils.c      | 31 +++++++++++++++++++++++++++++++
 qemu-common.h |  1 +
 2 files changed, 32 insertions(+)

Patch

diff --git a/cutils.c b/cutils.c
index 85dbb06..7bbd1b7 100644
--- a/cutils.c
+++ b/cutils.c
@@ -594,3 +594,34 @@  FILE *qemu_fopen_err(const char *path, const char *mode, Error **errp)
 
     return fp;
 }
+
+static void set_write_err(Error **errp, int err_nr)
+{
+    switch (err_nr) {
+    case ENOSPC:
+        error_set(errp, QERR_NO_SPACE);
+        return;
+    case EFBIG:
+        error_set(errp, QERR_FILE_TOO_BIG);
+        return;
+    default:
+        error_set(errp, QERR_IO_ERROR);
+        return;
+    }
+}
+
+int qemu_fprintf_err(Error **errp, FILE *stream, const char *format, ...)
+{
+    va_list ap;
+    int ret;
+
+    va_start(ap, format);
+    ret = vfprintf(stream, format, ap);
+    va_end(ap);
+
+    if (ret < 0) {
+        set_write_err(errp, errno);
+    }
+
+    return ret;
+}
diff --git a/qemu-common.h b/qemu-common.h
index 2cab2a8..58c5197 100644
--- a/qemu-common.h
+++ b/qemu-common.h
@@ -210,6 +210,7 @@  int qemu_recvv(int sockfd, struct iovec *iov, int len, int iov_offset);
 int qemu_sendv(int sockfd, struct iovec *iov, int len, int iov_offset);
 
 FILE *qemu_fopen_err(const char *path, const char *mode, Error **errp);
+int qemu_fprintf_err(Error **errp, FILE *stream, const char *format, ...);
 
 /* Error handling.  */