libexplain  1.4.D001
libexplain/buffer/strerror.c
Go to the documentation of this file.
00001 /*
00002  * libexplain - Explain errno values returned by libc functions
00003  * Copyright (C) 2008-2010, 2013 Peter Miller
00004  * Written by Peter Miller <pmiller@opensource.org.au>
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU Lesser General Public License as published by
00008  * the Free Software Foundation; either version 3 of the License, or (at
00009  * your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public License
00017  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00018  */
00019 
00020 #include <libexplain/ac/string.h>
00021 
00022 #include <libexplain/buffer/strerror.h>
00023 #include <libexplain/errno_info.h>
00024 #include <libexplain/gettext.h>
00025 #include <libexplain/option.h>
00026 #include <libexplain/string_buffer.h>
00027 
00028 
00029 void
00030 explain_buffer_strerror(explain_string_buffer_t *sb, int errnum)
00031 {
00032     const explain_errno_info_t *eip;
00033     int             first;
00034     char            errbuf[1024];
00035 
00036     explain_internal_strerror_r(errnum, errbuf, sizeof(errbuf));
00037     if (!errbuf[0])
00038     {
00039         const char      *no_idea;
00040 
00041         no_idea =
00042             explain_gettext
00043             (
00044                 /*
00045                  * xgettext: This message is used when streror (or strerror_r)
00046                  * is unable to translate an errno value, in which ase this
00047                  * fall-back message is used.  This does not occur with glibc,
00048                  * but other libc implemntations are more flakey.
00049                  */
00050                 i18n("unknown system error")
00051             );
00052         explain_strendcpy(errbuf, no_idea, errbuf + sizeof(errbuf));
00053     }
00054     explain_string_buffer_puts(sb, errbuf);
00055     first = 1;
00056     if (explain_option_numeric_errno())
00057     {
00058         explain_string_buffer_printf(sb, " (%d", errnum);
00059         first = 0;
00060     }
00061     eip = explain_errno_info_by_number(errnum);
00062     if (eip)
00063     {
00064         if (first)
00065             explain_string_buffer_puts(sb, " (");
00066         else
00067             explain_string_buffer_puts(sb, ", ");
00068         explain_string_buffer_puts(sb, eip->name);
00069         first = 0;
00070     }
00071     if (!first)
00072         explain_string_buffer_putc(sb, ')');
00073 }
00074 
00075 
00076 /* vim: set ts=8 sw=4 et : */