1 /****************************************************************************
2 * Copyright (c) 1998-2009,2010 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>
40 MODULE_ID("$Id: access.c,v 1.16 2010/01/23 17:57:43 tom Exp $")
42 #define LOWERCASE(c) ((isalpha(UChar(c)) && isupper(UChar(c))) ? tolower(UChar(c)) : (c))
44 NCURSES_EXPORT(char *)
45 _nc_rootname(char *path)
47 char *result = _nc_basename(path);
48 #if !MIXEDCASE_FILENAMES || defined(PROG_EXT)
52 temp = strdup(result);
54 #if !MIXEDCASE_FILENAMES
55 for (s = result; *s != '\0'; ++s) {
60 if ((s = strrchr(result, '.')) != 0) {
61 if (!strcmp(s, PROG_EXT))
70 * Check if a string appears to be an absolute pathname.
73 _nc_is_abs_path(const char *path)
75 #if defined(__EMX__) || defined(__DJGPP__)
76 #define is_pathname(s) ((((s) != 0) && ((s)[0] == '/')) \
77 || (((s)[0] != 0) && ((s)[1] == ':')))
79 #define is_pathname(s) ((s) != 0 && (s)[0] == '/')
81 return is_pathname(path);
85 * Return index of the basename
87 NCURSES_EXPORT(unsigned)
88 _nc_pathlast(const char *path)
90 const char *test = strrchr(path, '/');
93 test = strrchr(path, '\\');
99 return (unsigned) (test - path);
102 NCURSES_EXPORT(char *)
103 _nc_basename(char *path)
105 return path + _nc_pathlast(path);
109 _nc_access(const char *path, int mode)
111 if (access(path, mode) < 0) {
112 if ((mode & W_OK) != 0
114 && strlen(path) < PATH_MAX) {
116 char *leaf = _nc_basename(strcpy(head, path));
122 (void) strcpy(head, ".");
124 return access(head, R_OK | W_OK | X_OK);
132 _nc_is_dir_path(const char *path)
137 if (stat(path, &sb) == 0
138 && (sb.st_mode & S_IFMT) == S_IFDIR) {
145 _nc_is_file_path(const char *path)
150 if (stat(path, &sb) == 0
151 && (sb.st_mode & S_IFMT) == S_IFREG) {
157 #ifndef USE_ROOT_ENVIRON
159 * Returns true if we allow application to use environment variables that are
160 * used for searching lists of directories, etc.
168 #elif HAVE_GETEUID && HAVE_GETEGID
169 if (getuid() != geteuid()
170 || getgid() != getegid())
173 return getuid() != 0 && geteuid() != 0; /* ...finally, disallow root */