libexplain
1.4.D001
|
00001 /* 00002 * libexplain - Explain errno values returned by libc functions 00003 * Copyright (C) 2009, 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/fcntl.h> 00020 #include <libexplain/ac/limits.h> /* for PATH_MAX on Solaris */ 00021 #include <libexplain/ac/stdio.h> 00022 #include <libexplain/ac/stdlib.h> 00023 #include <libexplain/ac/stdlib.h> 00024 #include <libexplain/ac/string.h> 00025 #include <libexplain/ac/sys/param.h> /* for PATH_MAX except Solaris */ 00026 #include <libexplain/ac/unistd.h> 00027 00028 #include <libexplain/buffer/fildes.h> 00029 #include <libexplain/buffer/fildes_to_pathname.h> 00030 #include <libexplain/open.h> 00031 00032 00033 void 00034 explain_buffer_fildes(explain_string_buffer_t *sb, int fildes) 00035 { 00036 if (fildes == AT_FDCWD) 00037 { 00038 char path[PATH_MAX + 1]; 00039 explain_string_buffer_puts(sb, "AT_FDCWD"); 00040 if (getcwd(path, sizeof(path))) 00041 { 00042 explain_string_buffer_putc(sb, ' '); 00043 explain_string_buffer_puts_quoted(sb, path); 00044 } 00045 else 00046 { 00047 explain_string_buffer_puts(sb, " \".\""); 00048 } 00049 return; 00050 } 00051 explain_string_buffer_printf(sb, "%d", fildes); 00052 explain_buffer_fildes_to_pathname(sb, fildes); 00053 } 00054 00055 00056 int 00057 explain_parse_fildes_or_die(const char *text, const char *caption) 00058 { 00059 /* First try, it could be a number */ 00060 char *ep; 00061 long result = strtol(text, &ep, 0); 00062 if (text != ep && !*ep) 00063 return result; 00064 00065 if (0 == strcmp(text, "AT_FDCWD")) 00066 return AT_FDCWD; 00067 00068 /* Second try, it could be a specific stream */ 00069 if (0 == strcmp(text, "stdin")) 00070 return fileno(stdin); 00071 if (0 == strcmp(text, "stdout")) 00072 return fileno(stdout); 00073 if (0 == strcmp(text, "stderr")) 00074 return fileno(stderr); 00075 00076 /* Third try, it could be a pathname */ 00077 return explain_open_or_die(text, O_RDONLY, 0); 00078 (void)caption; 00079 } 00080 00081 00082 /* vim: set ts=8 sw=4 et : */ 00083 00084 00085 /* vim: set ts=8 sw=4 et : */