libexplain  1.4.D001
libexplain/buffer/wrong_file_type.c
Go to the documentation of this file.
00001 /*
00002  * libexplain - Explain errno values returned by libc functions
00003  * Copyright (C) 2008, 2009, 2011, 2013 Peter Miller
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU Lesser General Public License as
00007  * published by the Free Software Foundation; either version 3 of the
00008  * License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public License
00016  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00017  */
00018 
00019 #include <libexplain/ac/sys/socket.h>
00020 #include <libexplain/ac/sys/stat.h>
00021 
00022 #include <libexplain/buffer/file_type.h>
00023 #include <libexplain/buffer/wrong_file_type.h>
00024 
00025 
00026 void
00027 explain_buffer_wrong_file_type_st(explain_string_buffer_t *sb,
00028     const struct stat *st, const char *caption, int required_file_type)
00029 {
00030     if (st)
00031     {
00032         explain_string_buffer_t wrong_sb;
00033         explain_string_buffer_t right_sb;
00034         char            wrong[FILE_TYPE_BUFFER_SIZE_MIN];
00035         char            right[FILE_TYPE_BUFFER_SIZE_MIN];
00036 
00037         explain_string_buffer_init(&wrong_sb, wrong, sizeof(wrong));
00038         explain_buffer_file_type_st(&wrong_sb, st);
00039         explain_string_buffer_init(&right_sb, right, sizeof(right));
00040         explain_buffer_file_type(&right_sb, required_file_type);
00041 
00042         explain_string_buffer_printf_gettext
00043         (
00044             sb,
00045             /*
00046              * xgettext: This message is used when a file descriptor is
00047              * passed to a system call, and it has the wrong file type.
00048              *
00049              * %1$s => The name of the offending system call argument
00050              * %2$s => the actual (wrong) file type, already translated.
00051              * %3$s => the required file type, already translated.
00052              */
00053             i18n("%s refers to a %s, not a %s"),
00054             caption,
00055             wrong,
00056             right
00057         );
00058     }
00059     else
00060     {
00061         explain_string_buffer_t right_sb;
00062         char            right[FILE_TYPE_BUFFER_SIZE_MIN];
00063 
00064         explain_string_buffer_init(&right_sb, right, sizeof(right));
00065         explain_buffer_file_type(&right_sb, required_file_type);
00066 
00067         explain_string_buffer_printf_gettext
00068         (
00069             sb,
00070             /*
00071              * xgettext: This message is used when a file descriptor is
00072              * passed to a system call, and it has the wrong file type,
00073              * but the actual file type is unavailable.
00074              *
00075              * %1$s => The name of the offending system call argument
00076              * %2$s => the required file type, already translated.
00077              */
00078             i18n("%s does not refer to a %s"),
00079             caption,
00080             right
00081         );
00082     }
00083 }
00084 
00085 
00086 void
00087 explain_buffer_wrong_file_type(explain_string_buffer_t *sb, int fildes,
00088     const char *caption, int want_file_type)
00089 {
00090     struct stat     st;
00091 
00092     if (fstat(fildes, &st) >= 0)
00093         explain_buffer_wrong_file_type_st(sb, &st, caption, want_file_type);
00094     else
00095         explain_buffer_wrong_file_type_st(sb, 0, caption, want_file_type);
00096 }
00097 
00098 
00099 /* vim: set ts=8 sw=4 et : */