libexplain
1.4.D001
|
00001 /* 00002 * libexplain - Explain errno values returned by libc functions 00003 * Copyright (C) 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 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 00022 #include <libexplain/errno_info.h> 00023 #include <libexplain/option.h> 00024 00025 00026 const char * 00027 explain_internal_strerror(int n) 00028 { 00029 if (explain_option_internal_strerror()) 00030 { 00031 const explain_errno_info_t *ep; 00032 00033 ep = explain_errno_info_by_number(n); 00034 if (ep) 00035 return ep->description; 00036 } 00037 return strerror(n); 00038 } 00039 00040 00041 void 00042 explain_internal_strerror_r(int errnum, char *data, size_t data_size) 00043 { 00044 if (explain_option_internal_strerror()) 00045 { 00046 const explain_errno_info_t *ep; 00047 00048 ep = explain_errno_info_by_number(errnum); 00049 if (ep) 00050 { 00051 explain_strendcpy(data, ep->description, data + data_size); 00052 return; 00053 } 00054 } 00055 00056 #if 0 /* def HAVE_STRERROR_R */ 00057 /* 00058 * For reasons unknown, strerror_r is broken (for me) on Linux. 00059 * FIXME: look into this deeper. 00060 */ 00061 # if STRERROR_R_CHAR_P 00062 { 00063 const char *s; 00064 00065 s = strerror_r(errnum, data, data_size); 00066 if (!s || memcmp(s, "Unknown error", 13) == 0) 00067 data[0] = '\0'; 00068 } 00069 # else 00070 if (strerror_r(errnum, data, data_size) != 0) 00071 data[0] = '\0'; 00072 # endif 00073 #else 00074 { 00075 const char *s; 00076 00077 s = strerror(errnum); 00078 if (!s || memcmp(s, "Unknown error", 13) == 0) 00079 data[0] = '\0'; 00080 else 00081 explain_strendcpy(data, s, data + data_size); 00082 } 00083 #endif 00084 } 00085 00086 00087 /* vim: set ts=8 sw=4 et : */