libexplain
1.4.D001
|
00001 /* 00002 * libexplain - Explain errno values returned by libc functions 00003 * Copyright (C) 2008, 2009, 2013, 2014 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/limits.h> 00020 #include <libexplain/ac/sys/param.h> 00021 #include <libexplain/ac/unistd.h> 00022 00023 #include <libexplain/buffer/check_fildes_range.h> 00024 #include <libexplain/option.h> 00025 00026 00027 static int 00028 get_open_max(void) 00029 { 00030 int open_max = sysconf(_SC_OPEN_MAX); 00031 if (open_max < 0) 00032 { 00033 #ifdef OPEN_MAX 00034 open_max = OPEN_MAX; /* <sys/param.h> */ 00035 #else 00036 open_max = NOFILE; /* <sys/param.h> */ 00037 #endif 00038 } 00039 return open_max; 00040 } 00041 00042 00043 static void 00044 whinge(explain_string_buffer_t *sb, const char *caption) 00045 { 00046 explain_string_buffer_printf_gettext 00047 ( 00048 sb, 00049 /* 00050 * xgettext: This message is used when a file descriptor 00051 * is detected that is negative, or larger than 00052 * sysconf(_SC_OPEN_MAX). 00053 * 00054 * The range will be printed separately, if available, so do not 00055 * mention sysconf(_SC_OPEN_MAX) in the translation. 00056 */ 00057 i18n("the %s argument is outside the allowed range for file " 00058 "descriptors"), 00059 caption 00060 ); 00061 00062 if (explain_option_dialect_specific()) 00063 { 00064 int open_max = get_open_max(); 00065 explain_string_buffer_printf(sb, " (0..%d)", open_max - 1); 00066 } 00067 } 00068 00069 00070 int 00071 explain_buffer_check_fildes_range(explain_string_buffer_t *sb, 00072 int fildes, const char *caption) 00073 { 00074 int open_max = get_open_max(); 00075 if (fildes < 0 || fildes >= open_max) 00076 { 00077 whinge(sb, caption); 00078 return 0; 00079 } 00080 00081 /* 00082 * No error, yay! 00083 * Except that we wanted to find one. 00084 */ 00085 return -1; 00086 } 00087 00088 00089 /* vim: set ts=8 sw=4 et : */