Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 1 | /**************************************************************************** |
| 2 | * Copyright (c) 1998-2006,2007 Free Software Foundation, Inc. * |
| 3 | * * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a * |
| 5 | * copy of this software and associated documentation files (the * |
| 6 | * "Software"), to deal in the Software without restriction, including * |
| 7 | * without limitation the rights to use, copy, modify, merge, publish, * |
| 8 | * distribute, distribute with modifications, sublicense, and/or sell * |
| 9 | * copies of the Software, and to permit persons to whom the Software is * |
| 10 | * furnished to do so, subject to the following conditions: * |
| 11 | * * |
| 12 | * The above copyright notice and this permission notice shall be included * |
| 13 | * in all copies or substantial portions of the Software. * |
| 14 | * * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * |
| 16 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * |
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * |
| 18 | * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * |
| 19 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * |
| 20 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * |
| 21 | * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * |
| 22 | * * |
| 23 | * Except as contained in this notice, the name(s) of the above copyright * |
| 24 | * holders shall not be used in advertising or otherwise to promote the * |
| 25 | * sale, use or other dealings in this Software without prior written * |
| 26 | * authorization. * |
| 27 | ****************************************************************************/ |
| 28 | |
| 29 | /**************************************************************************** |
| 30 | * Author: Thomas E. Dickey * |
| 31 | ****************************************************************************/ |
| 32 | |
| 33 | #include <curses.priv.h> |
| 34 | |
| 35 | #include <ctype.h> |
| 36 | #include <sys/stat.h> |
| 37 | |
| 38 | #include <tic.h> |
| 39 | #include <nc_alloc.h> |
| 40 | |
| 41 | MODULE_ID("$Id: access.c,v 1.14 2007/11/18 00:57:53 tom Exp $") |
| 42 | |
| 43 | #define LOWERCASE(c) ((isalpha(UChar(c)) && isupper(UChar(c))) ? tolower(UChar(c)) : (c)) |
| 44 | |
| 45 | NCURSES_EXPORT(char *) |
| 46 | _nc_rootname(char *path) |
| 47 | { |
| 48 | char *result = _nc_basename(path); |
| 49 | #if !MIXEDCASE_FILENAMES || defined(PROG_EXT) |
| 50 | static char *temp; |
| 51 | char *s; |
| 52 | |
| 53 | temp = strdup(result); |
| 54 | result = temp; |
| 55 | #if !MIXEDCASE_FILENAMES |
| 56 | for (s = result; *s != '\0'; ++s) { |
| 57 | *s = LOWERCASE(*s); |
| 58 | } |
| 59 | #endif |
| 60 | #if defined(PROG_EXT) |
| 61 | if ((s = strrchr(result, '.')) != 0) { |
| 62 | if (!strcmp(s, PROG_EXT)) |
| 63 | *s = '\0'; |
| 64 | } |
| 65 | #endif |
| 66 | #endif |
| 67 | return result; |
| 68 | } |
| 69 | |
| 70 | /* |
| 71 | * Check if a string appears to be an absolute pathname. |
| 72 | */ |
| 73 | NCURSES_EXPORT(bool) |
| 74 | _nc_is_abs_path(const char *path) |
| 75 | { |
| 76 | #if defined(__EMX__) || defined(__DJGPP__) |
| 77 | #define is_pathname(s) ((((s) != 0) && ((s)[0] == '/')) \ |
| 78 | || (((s)[0] != 0) && ((s)[1] == ':'))) |
| 79 | #else |
| 80 | #define is_pathname(s) ((s) != 0 && (s)[0] == '/') |
| 81 | #endif |
| 82 | return is_pathname(path); |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * Return index of the basename |
| 87 | */ |
| 88 | NCURSES_EXPORT(unsigned) |
| 89 | _nc_pathlast(const char *path) |
| 90 | { |
| 91 | const char *test = strrchr(path, '/'); |
| 92 | #ifdef __EMX__ |
| 93 | if (test == 0) |
| 94 | test = strrchr(path, '\\'); |
| 95 | #endif |
| 96 | if (test == 0) |
| 97 | test = path; |
| 98 | else |
| 99 | test++; |
| 100 | return (test - path); |
| 101 | } |
| 102 | |
| 103 | NCURSES_EXPORT(char *) |
| 104 | _nc_basename(char *path) |
| 105 | { |
| 106 | return path + _nc_pathlast(path); |
| 107 | } |
| 108 | |
| 109 | NCURSES_EXPORT(int) |
| 110 | _nc_access(const char *path, int mode) |
| 111 | { |
| 112 | if (access(path, mode) < 0) { |
| 113 | if ((mode & W_OK) != 0 |
| 114 | && errno == ENOENT |
| 115 | && strlen(path) < PATH_MAX) { |
| 116 | char head[PATH_MAX]; |
| 117 | char *leaf = _nc_basename(strcpy(head, path)); |
| 118 | |
| 119 | if (leaf == 0) |
| 120 | leaf = head; |
| 121 | *leaf = '\0'; |
| 122 | if (head == leaf) |
| 123 | (void) strcpy(head, "."); |
| 124 | |
| 125 | return access(head, R_OK | W_OK | X_OK); |
| 126 | } |
| 127 | return -1; |
| 128 | } |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | NCURSES_EXPORT(bool) |
| 133 | _nc_is_dir_path(const char *path) |
| 134 | { |
| 135 | bool result = FALSE; |
| 136 | struct stat sb; |
| 137 | |
| 138 | if (stat(path, &sb) == 0 |
| 139 | && (sb.st_mode & S_IFMT) == S_IFDIR) { |
| 140 | result = TRUE; |
| 141 | } |
| 142 | return result; |
| 143 | } |
| 144 | |
| 145 | NCURSES_EXPORT(bool) |
| 146 | _nc_is_file_path(const char *path) |
| 147 | { |
| 148 | bool result = FALSE; |
| 149 | struct stat sb; |
| 150 | |
| 151 | if (stat(path, &sb) == 0 |
| 152 | && (sb.st_mode & S_IFMT) == S_IFREG) { |
| 153 | result = TRUE; |
| 154 | } |
| 155 | return result; |
| 156 | } |
| 157 | |
| 158 | #ifndef USE_ROOT_ENVIRON |
| 159 | /* |
| 160 | * Returns true if we allow application to use environment variables that are |
| 161 | * used for searching lists of directories, etc. |
| 162 | */ |
| 163 | NCURSES_EXPORT(int) |
| 164 | _nc_env_access(void) |
| 165 | { |
| 166 | #if HAVE_ISSETUGID |
| 167 | if (issetugid()) |
| 168 | return FALSE; |
| 169 | #elif HAVE_GETEUID && HAVE_GETEGID |
| 170 | if (getuid() != geteuid() |
| 171 | || getgid() != getegid()) |
| 172 | return FALSE; |
| 173 | #endif |
| 174 | return getuid() != 0 && geteuid() != 0; /* ...finally, disallow root */ |
| 175 | } |
| 176 | #endif |