]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/access.c
ncurses 5.9 - patch 20110507
[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.17 2011/05/07 15:02:03 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     if (access(path, mode) < 0) {
120         if ((mode & W_OK) != 0
121             && errno == ENOENT
122             && strlen(path) < PATH_MAX) {
123             char head[PATH_MAX];
124             char *leaf = _nc_basename(strcpy(head, path));
125
126             if (leaf == 0)
127                 leaf = head;
128             *leaf = '\0';
129             if (head == leaf)
130                 (void) strcpy(head, ".");
131
132             return access(head, R_OK | W_OK | X_OK);
133         }
134         return -1;
135     }
136     return 0;
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         && (sb.st_mode & S_IFMT) == S_IFDIR) {
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         && (sb.st_mode & S_IFMT) == S_IFREG) {
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