1 /****************************************************************************
2 * Copyright 2019,2020 Thomas E. Dickey *
3 * Copyright 1998-2011,2012 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 /****************************************************************************
31 * Author: Thomas E. Dickey *
32 ****************************************************************************/
34 #include <curses.priv.h>
40 MODULE_ID("$Id: access.c,v 1.26 2020/07/11 20:55:23 anonymous.maarten Exp $")
42 #define LOWERCASE(c) ((isalpha(UChar(c)) && isupper(UChar(c))) ? tolower(UChar(c)) : (c))
45 # define ACCESS(FN, MODE) access((FN), (MODE)&(R_OK|W_OK))
47 # define ACCESS access
50 NCURSES_EXPORT(char *)
51 _nc_rootname(char *path)
53 char *result = _nc_basename(path);
54 #if !MIXEDCASE_FILENAMES || defined(PROG_EXT)
58 temp = strdup(result);
60 #if !MIXEDCASE_FILENAMES
61 for (s = result; *s != '\0'; ++s) {
62 *s = (char) LOWERCASE(*s);
66 if ((s = strrchr(result, '.')) != 0) {
67 if (!strcmp(s, PROG_EXT))
76 * Check if a string appears to be an absolute pathname.
79 _nc_is_abs_path(const char *path)
81 #if defined(__EMX__) || defined(__DJGPP__)
82 #define is_pathname(s) ((((s) != 0) && ((s)[0] == '/')) \
83 || (((s)[0] != 0) && ((s)[1] == ':')))
85 #define is_pathname(s) ((s) != 0 && (s)[0] == '/')
87 return is_pathname(path);
91 * Return index of the basename
93 NCURSES_EXPORT(unsigned)
94 _nc_pathlast(const char *path)
96 const char *test = strrchr(path, '/');
99 test = strrchr(path, '\\');
105 return (unsigned) (test - path);
108 NCURSES_EXPORT(char *)
109 _nc_basename(char *path)
111 return path + _nc_pathlast(path);
115 _nc_access(const char *path, int mode)
121 } else if (ACCESS(path, mode) < 0) {
122 if ((mode & W_OK) != 0
124 && strlen(path) < PATH_MAX) {
128 _nc_STRCPY(head, path, sizeof(head));
129 leaf = _nc_basename(head);
134 _nc_STRCPY(head, ".", sizeof(head));
136 result = ACCESS(head, R_OK | W_OK | X_OK);
147 _nc_is_dir_path(const char *path)
152 if (stat(path, &sb) == 0
153 && S_ISDIR(sb.st_mode)) {
160 _nc_is_file_path(const char *path)
165 if (stat(path, &sb) == 0
166 && S_ISREG(sb.st_mode)) {
172 #ifndef USE_ROOT_ENVIRON
174 * Returns true if we allow application to use environment variables that are
175 * used for searching lists of directories, etc.
183 #elif HAVE_GETEUID && HAVE_GETEGID
184 if (getuid() != geteuid()
185 || getgid() != getegid())
188 /* ...finally, disallow root */
189 return (getuid() != ROOT_UID) && (geteuid() != ROOT_UID);