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