1 /****************************************************************************
2 * Copyright (c) 1998-2012,2019 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.24 2019/01/19 15:38:07 tom Exp $")
41 #define LOWERCASE(c) ((isalpha(UChar(c)) && isupper(UChar(c))) ? tolower(UChar(c)) : (c))
43 NCURSES_EXPORT(char *)
44 _nc_rootname(char *path)
46 char *result = _nc_basename(path);
47 #if !MIXEDCASE_FILENAMES || defined(PROG_EXT)
51 temp = strdup(result);
53 #if !MIXEDCASE_FILENAMES
54 for (s = result; *s != '\0'; ++s) {
55 *s = (char) LOWERCASE(*s);
59 if ((s = strrchr(result, '.')) != 0) {
60 if (!strcmp(s, PROG_EXT))
69 * Check if a string appears to be an absolute pathname.
72 _nc_is_abs_path(const char *path)
74 #if defined(__EMX__) || defined(__DJGPP__)
75 #define is_pathname(s) ((((s) != 0) && ((s)[0] == '/')) \
76 || (((s)[0] != 0) && ((s)[1] == ':')))
78 #define is_pathname(s) ((s) != 0 && (s)[0] == '/')
80 return is_pathname(path);
84 * Return index of the basename
86 NCURSES_EXPORT(unsigned)
87 _nc_pathlast(const char *path)
89 const char *test = strrchr(path, '/');
92 test = strrchr(path, '\\');
98 return (unsigned) (test - path);
101 NCURSES_EXPORT(char *)
102 _nc_basename(char *path)
104 return path + _nc_pathlast(path);
108 _nc_access(const char *path, int mode)
114 } else if (access(path, mode) < 0) {
115 if ((mode & W_OK) != 0
117 && strlen(path) < PATH_MAX) {
121 _nc_STRCPY(head, path, sizeof(head));
122 leaf = _nc_basename(head);
127 _nc_STRCPY(head, ".", sizeof(head));
129 result = access(head, R_OK | W_OK | X_OK);
140 _nc_is_dir_path(const char *path)
145 if (stat(path, &sb) == 0
146 && S_ISDIR(sb.st_mode)) {
153 _nc_is_file_path(const char *path)
158 if (stat(path, &sb) == 0
159 && S_ISREG(sb.st_mode)) {
165 #ifndef USE_ROOT_ENVIRON
167 * Returns true if we allow application to use environment variables that are
168 * used for searching lists of directories, etc.
176 #elif HAVE_GETEUID && HAVE_GETEGID
177 if (getuid() != geteuid()
178 || getgid() != getegid())
181 /* ...finally, disallow root */
182 return (getuid() != ROOT_UID) && (geteuid() != ROOT_UID);