libexplain  1.4.D001
libexplain/buffer/permission_mode.c
Go to the documentation of this file.
00001 /*
00002  * libexplain - Explain errno values returned by libc functions
00003  * Copyright (C) 2008, 2009, 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/sys/stat.h>
00021 
00022 #include <libexplain/buffer/permission_mode.h>
00023 #include <libexplain/option.h>
00024 #include <libexplain/parse_bits.h>
00025 #include <libexplain/sizeof.h>
00026 #include <libexplain/string_buffer.h>
00027 
00028 
00029 static const explain_parse_bits_table_t short_table[] =
00030 {
00031     { "S_ISUID", S_ISUID },
00032     { "S_ISGID", S_ISGID },
00033     { "S_ISVTX", S_ISVTX },
00034 };
00035 
00036 
00037 static const explain_parse_bits_table_t long_table[] =
00038 {
00039     { "S_ISUID", S_ISUID },
00040     { "S_ISGID", S_ISGID },
00041     { "S_ISVTX", S_ISVTX },
00042     { "S_IRWXU", S_IRWXU },
00043     { "S_IRUSR", S_IRUSR },
00044     { "S_IWUSR", S_IWUSR },
00045     { "S_IXUSR", S_IXUSR },
00046     { "S_IRWXG", S_IRWXG },
00047     { "S_IRGRP", S_IRGRP },
00048     { "S_IWGRP", S_IWGRP },
00049     { "S_IXGRP", S_IXGRP },
00050     { "S_IRWXO", S_IRWXO },
00051     { "S_IROTH", S_IROTH },
00052     { "S_IWOTH", S_IWOTH },
00053     { "S_IXOTH", S_IXOTH },
00054 };
00055 
00056 
00057 void
00058 explain_buffer_permission_mode(explain_string_buffer_t *sb, mode_t mode)
00059 {
00060     const explain_parse_bits_table_t *table;
00061     const explain_parse_bits_table_t *table_end;
00062     const explain_parse_bits_table_t *tp;
00063     int             first;
00064 
00065     if (mode == 0)
00066     {
00067         explain_string_buffer_putc(sb, '0');
00068         return;
00069     }
00070     first = 1;
00071     table = short_table;
00072     table_end = short_table + SIZEOF(short_table);
00073     if (explain_option_symbolic_mode_bits())
00074     {
00075         table = long_table;
00076         table_end = long_table + SIZEOF(long_table);
00077     }
00078     for (tp = table; tp < table_end; ++tp)
00079     {
00080         unsigned value = tp->value;
00081         if (value != 0 && (mode & value) == value)
00082         {
00083             if (!first)
00084                 explain_string_buffer_puts(sb, " | ");
00085             explain_string_buffer_puts(sb, tp->name);
00086             first = 0;
00087             mode -= value;
00088         }
00089     }
00090     if (mode != 0)
00091     {
00092         if (!first)
00093             explain_string_buffer_puts(sb, " | ");
00094         explain_string_buffer_printf(sb, "%#o", mode);
00095     }
00096 }
00097 
00098 
00099 mode_t
00100 explain_permission_mode_parse_or_die(const char *text, const char *caption)
00101 {
00102     return
00103         explain_parse_bits_or_die
00104         (
00105             text,
00106             long_table,
00107             SIZEOF(long_table),
00108             caption
00109         );
00110 }
00111 
00112 
00113 /* vim: set ts=8 sw=4 et : */