1 /****************************************************************************
2 * Copyright (c) 1998-2011,2012 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Thomas E. Dickey *
31 ****************************************************************************/
33 #include <curses.priv.h>
39 MODULE_ID("$Id: access.c,v 1.23 2012/09/01 19:21:29 tom Exp $")
42 #define ROOT_UID 65535
49 #define LOWERCASE(c) ((isalpha(UChar(c)) && isupper(UChar(c))) ? tolower(UChar(c)) : (c))
51 NCURSES_EXPORT(char *)
52 _nc_rootname(char *path)
54 char *result = _nc_basename(path);
55 #if !MIXEDCASE_FILENAMES || defined(PROG_EXT)
59 temp = strdup(result);
61 #if !MIXEDCASE_FILENAMES
62 for (s = result; *s != '\0'; ++s) {
63 *s = (char) LOWERCASE(*s);
67 if ((s = strrchr(result, '.')) != 0) {
68 if (!strcmp(s, PROG_EXT))
77 * Check if a string appears to be an absolute pathname.
80 _nc_is_abs_path(const char *path)
82 #if defined(__EMX__) || defined(__DJGPP__)
83 #define is_pathname(s) ((((s) != 0) && ((s)[0] == '/')) \
84 || (((s)[0] != 0) && ((s)[1] == ':')))
86 #define is_pathname(s) ((s) != 0 && (s)[0] == '/')
88 return is_pathname(path);
92 * Return index of the basename
94 NCURSES_EXPORT(unsigned)
95 _nc_pathlast(const char *path)
97 const char *test = strrchr(path, '/');
100 test = strrchr(path, '\\');
106 return (unsigned) (test - path);
109 NCURSES_EXPORT(char *)
110 _nc_basename(char *path)
112 return path + _nc_pathlast(path);
116 _nc_access(const char *path, int mode)
122 } else if (access(path, mode) < 0) {
123 if ((mode & W_OK) != 0
125 && strlen(path) < PATH_MAX) {
129 _nc_STRCPY(head, path, sizeof(head));
130 leaf = _nc_basename(head);
135 _nc_STRCPY(head, ".", sizeof(head));
137 result = access(head, R_OK | W_OK | X_OK);
148 _nc_is_dir_path(const char *path)
153 if (stat(path, &sb) == 0
154 && S_ISDIR(sb.st_mode)) {
161 _nc_is_file_path(const char *path)
166 if (stat(path, &sb) == 0
167 && S_ISREG(sb.st_mode)) {
173 #ifndef USE_ROOT_ENVIRON
175 * Returns true if we allow application to use environment variables that are
176 * used for searching lists of directories, etc.
184 #elif HAVE_GETEUID && HAVE_GETEGID
185 if (getuid() != geteuid()
186 || getgid() != getegid())
189 /* ...finally, disallow root */
190 return (getuid() != ROOT_UID) && (geteuid() != ROOT_UID);