]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/access.c
ncurses 6.1 - patch 20191015
[ncurses.git] / ncurses / tinfo / access.c
1 /****************************************************************************
2  * Copyright (c) 1998-2012,2019 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
37 #include <tic.h>
38
39 MODULE_ID("$Id: access.c,v 1.24 2019/01/19 15:38:07 tom Exp $")
40
41 #define LOWERCASE(c) ((isalpha(UChar(c)) && isupper(UChar(c))) ? tolower(UChar(c)) : (c))
42
43 NCURSES_EXPORT(char *)
44 _nc_rootname(char *path)
45 {
46     char *result = _nc_basename(path);
47 #if !MIXEDCASE_FILENAMES || defined(PROG_EXT)
48     static char *temp;
49     char *s;
50
51     temp = strdup(result);
52     result = temp;
53 #if !MIXEDCASE_FILENAMES
54     for (s = result; *s != '\0'; ++s) {
55         *s = (char) LOWERCASE(*s);
56     }
57 #endif
58 #if defined(PROG_EXT)
59     if ((s = strrchr(result, '.')) != 0) {
60         if (!strcmp(s, PROG_EXT))
61             *s = '\0';
62     }
63 #endif
64 #endif
65     return result;
66 }
67
68 /*
69  * Check if a string appears to be an absolute pathname.
70  */
71 NCURSES_EXPORT(bool)
72 _nc_is_abs_path(const char *path)
73 {
74 #if defined(__EMX__) || defined(__DJGPP__)
75 #define is_pathname(s) ((((s) != 0) && ((s)[0] == '/')) \
76                   || (((s)[0] != 0) && ((s)[1] == ':')))
77 #else
78 #define is_pathname(s) ((s) != 0 && (s)[0] == '/')
79 #endif
80     return is_pathname(path);
81 }
82
83 /*
84  * Return index of the basename
85  */
86 NCURSES_EXPORT(unsigned)
87 _nc_pathlast(const char *path)
88 {
89     const char *test = strrchr(path, '/');
90 #ifdef __EMX__
91     if (test == 0)
92         test = strrchr(path, '\\');
93 #endif
94     if (test == 0)
95         test = path;
96     else
97         test++;
98     return (unsigned) (test - path);
99 }
100
101 NCURSES_EXPORT(char *)
102 _nc_basename(char *path)
103 {
104     return path + _nc_pathlast(path);
105 }
106
107 NCURSES_EXPORT(int)
108 _nc_access(const char *path, int mode)
109 {
110     int result;
111
112     if (path == 0) {
113         result = -1;
114     } else if (access(path, mode) < 0) {
115         if ((mode & W_OK) != 0
116             && errno == ENOENT
117             && strlen(path) < PATH_MAX) {
118             char head[PATH_MAX];
119             char *leaf;
120
121             _nc_STRCPY(head, path, sizeof(head));
122             leaf = _nc_basename(head);
123             if (leaf == 0)
124                 leaf = head;
125             *leaf = '\0';
126             if (head == leaf)
127                 _nc_STRCPY(head, ".", sizeof(head));
128
129             result = access(head, R_OK | W_OK | X_OK);
130         } else {
131             result = -1;
132         }
133     } else {
134         result = 0;
135     }
136     return result;
137 }
138
139 NCURSES_EXPORT(bool)
140 _nc_is_dir_path(const char *path)
141 {
142     bool result = FALSE;
143     struct stat sb;
144
145     if (stat(path, &sb) == 0
146         && S_ISDIR(sb.st_mode)) {
147         result = TRUE;
148     }
149     return result;
150 }
151
152 NCURSES_EXPORT(bool)
153 _nc_is_file_path(const char *path)
154 {
155     bool result = FALSE;
156     struct stat sb;
157
158     if (stat(path, &sb) == 0
159         && S_ISREG(sb.st_mode)) {
160         result = TRUE;
161     }
162     return result;
163 }
164
165 #ifndef USE_ROOT_ENVIRON
166 /*
167  * Returns true if we allow application to use environment variables that are
168  * used for searching lists of directories, etc.
169  */
170 NCURSES_EXPORT(int)
171 _nc_env_access(void)
172 {
173 #if HAVE_ISSETUGID
174     if (issetugid())
175         return FALSE;
176 #elif HAVE_GETEUID && HAVE_GETEGID
177     if (getuid() != geteuid()
178         || getgid() != getegid())
179         return FALSE;
180 #endif
181     /* ...finally, disallow root */
182     return (getuid() != ROOT_UID) && (geteuid() != ROOT_UID);
183 }
184 #endif