diff options
author | Colin Walters <walters@verbum.org> | 2014-12-17 22:36:57 -0500 |
---|---|---|
committer | Colin Walters <walters@verbum.org> | 2014-12-17 22:52:35 -0500 |
commit | ed106741f7a0596dc8b960b31fdae671d31d666d (patch) | |
tree | bd2ca09e22715b08e1643ece4f7fd2907b9aae19 | |
parent | ad3a28c5f2520d0688730aa83eaee815fb9a5762 (diff) | |
download | libgsystem-ed106741f7a0596dc8b960b31fdae671d31d666d.tar.gz |
gsystem-errors.h: Add gs_set_error_from_errno()
This is a long overdue move of this function to public API.
-rw-r--r-- | Makefile-libgsystem.am | 2 | ||||
-rw-r--r-- | src/gsystem-errors.c | 75 | ||||
-rw-r--r-- | src/gsystem-errors.h | 37 | ||||
-rw-r--r-- | src/libgsystem.h | 1 |
4 files changed, 115 insertions, 0 deletions
diff --git a/Makefile-libgsystem.am b/Makefile-libgsystem.am index b75d517..676a230 100644 --- a/Makefile-libgsystem.am +++ b/Makefile-libgsystem.am @@ -25,6 +25,7 @@ libgsystemheader_HEADERS = \ src/gsystem-glib-compat.h \ src/gsystem-shutil.h \ src/gsystem-log.h \ + src/gsystem-errors.h \ src/gsystem-subprocess-context.h \ src/gsystem-subprocess.h \ src/libgsystem.h \ @@ -35,6 +36,7 @@ libgsystem_la_SOURCES = \ src/gsystem-console.c \ src/gsystem-file-utils.c \ src/gsystem-shutil.c \ + src/gsystem-errors.c \ src/gsystem-log.c \ src/gsystem-subprocess-context-private.h \ src/gsystem-subprocess-context.c \ diff --git a/src/gsystem-errors.c b/src/gsystem-errors.c new file mode 100644 index 0000000..af7fd09 --- /dev/null +++ b/src/gsystem-errors.c @@ -0,0 +1,75 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- + * + * Copyright (C) 2014 Colin Walters <walters@verbum.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include "config.h" + +#include "gsystem-errors.h" +#include "gsystem-local-alloc.h" +#include <glib-unix.h> + +/** + * gs_set_error_from_errno: + * @error: (allow-none): Error + * @saved_errno: errno value + * + * Set @error to an error with domain %G_IO_ERROR, and code based on + * the value of @saved_errno. The error message is set using a + * literal return from g_strerror(). + */ +void +gs_set_error_from_errno (GError **error, gint saved_errno) +{ + g_set_error_literal (error, + G_IO_ERROR, + g_io_error_from_errno (saved_errno), + g_strerror (saved_errno)); + errno = saved_errno; +} + +/** + * gs_set_prefix_error_from_errno: + * @error: (allow-none): Error + * @saved_errno: errno value + * @format: Format string for printf + * + * Set @error to an error with domain %G_IO_ERROR, and code based on + * the value of @saved_errno. The error message is prefixed with the + * result of @format, a colon and space, then the result of + * g_strerror(). + */ +void +gs_set_prefix_error_from_errno (GError **error, + gint saved_errno, + const char *format, + ...) +{ + gs_free char *formatted = NULL; + va_list args; + + va_start (args, format); + formatted = g_strdup_vprintf (format, args); + va_end (args); + + g_set_error (error, + G_IO_ERROR, + g_io_error_from_errno (saved_errno), + "%s: %s", formatted, g_strerror (saved_errno)); + errno = saved_errno; +} diff --git a/src/gsystem-errors.h b/src/gsystem-errors.h new file mode 100644 index 0000000..15898bc --- /dev/null +++ b/src/gsystem-errors.h @@ -0,0 +1,37 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- + * + * Copyright (C) 2014 Colin Walters <walters@verbum.org>. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifndef __GSYSTEM_ERRORS_H__ +#define __GSYSTEM_ERRORS_H__ + +#include <gio/gio.h> +#include <sys/stat.h> + +G_BEGIN_DECLS + +void gs_set_error_from_errno (GError **error, gint saved_errno); +void gs_set_prefix_error_from_errno (GError **error, + gint errsv, + const char *format, + ...) G_GNUC_PRINTF (3,4); + +G_END_DECLS + +#endif diff --git a/src/libgsystem.h b/src/libgsystem.h index 60884b6..9fc0c5c 100644 --- a/src/libgsystem.h +++ b/src/libgsystem.h @@ -40,6 +40,7 @@ G_BEGIN_DECLS #include <gsystem-subprocess.h> #endif #include <gsystem-log.h> +#include <gsystem-errors.h> #ifndef _GSYSTEM_NO_LOCAL_ALLOC #include <gsystem-local-alloc.h> #endif |