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