libexplain  1.4.D001
libexplain/buffer/caption_name_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  * 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
00008  * published by the Free Software Foundation; either version 3 of the
00009  * License, or (at 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 #include <libexplain/ac/sys/stat.h>
00022 
00023 #include <libexplain/buffer/caption_name_type.h>
00024 #include <libexplain/buffer/file_type.h>
00025 #include <libexplain/buffer/gettext.h>
00026 #include <libexplain/option.h>
00027 
00028 
00029 static void
00030 explain_buffer_current_directory(explain_string_buffer_t *sb)
00031 {
00032     explain_buffer_gettext
00033     (
00034         sb,
00035         /*
00036          * xgettext: the name of the current directory, rather than "." that
00037          * not all users understand.
00038          */
00039         i18n("current directory")
00040     );
00041 }
00042 
00043 
00044 void
00045 explain_buffer_caption_name_type(explain_string_buffer_t *sb,
00046     const char *caption, const char *name, int st_mode)
00047 {
00048     if (name && S_ISDIR(st_mode) && 0 == strcmp(name, "."))
00049     {
00050         explain_buffer_current_directory(sb);
00051         return;
00052     }
00053 
00054     /* the rule is: [caption] [name] type */
00055     if (caption)
00056     {
00057         explain_string_buffer_puts(sb, caption);
00058         explain_string_buffer_putc(sb, ' ');
00059     }
00060     if (name)
00061     {
00062         explain_string_buffer_puts_quoted(sb, name);
00063         explain_string_buffer_putc(sb, ' ');
00064     }
00065     if (st_mode < 0)
00066         explain_string_buffer_puts(sb, "file");
00067     else
00068         explain_buffer_file_type(sb, st_mode);
00069 }
00070 
00071 
00072 void
00073 explain_buffer_caption_name_type_st(explain_string_buffer_t *sb,
00074     const char *caption, const char *name, const struct stat *st)
00075 {
00076     if (!st)
00077     {
00078         explain_buffer_caption_name_type(sb, caption, name, -1);
00079         return;
00080     }
00081     if (!explain_option_extra_device_info())
00082     {
00083         explain_buffer_caption_name_type(sb, caption, name, st->st_mode);
00084         return;
00085     }
00086     if (name && S_ISDIR(st->st_mode) && 0 == strcmp(name, "."))
00087     {
00088         explain_buffer_current_directory(sb);
00089         return;
00090     }
00091 
00092     /* the rule is: [caption] [name] type */
00093     if (caption)
00094     {
00095         explain_string_buffer_puts(sb, caption);
00096         explain_string_buffer_putc(sb, ' ');
00097     }
00098     if (name)
00099     {
00100         explain_string_buffer_puts_quoted(sb, name);
00101         explain_string_buffer_putc(sb, ' ');
00102     }
00103     explain_buffer_file_type_st(sb, st);
00104 }
00105 
00106 
00107 /* vim: set ts=8 sw=4 et : */