access.c revision 97049
1/****************************************************************************
2 * Copyright (c) 1998,2000,2001 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 <dickey@clark.net> 1998,2000,2001              *
31 ****************************************************************************/
32
33#include <curses.priv.h>
34#include <tic.h>
35#include <nc_alloc.h>
36
37MODULE_ID("$Id: access.c,v 1.9 2001/06/23 22:11:49 tom Exp $")
38
39#define LOWERCASE(c) ((isalpha(UChar(c)) && isupper(UChar(c))) ? tolower(UChar(c)) : (c))
40
41NCURSES_EXPORT(char *)
42_nc_rootname(char *path)
43{
44    char *result = _nc_basename(path);
45#if !defined(MIXEDCASE_FILENAMES) || defined(PROG_EXT)
46    static char *temp;
47    char *s;
48
49    temp = strdup(result);
50    result = temp;
51#if !defined(MIXEDCASE_FILENAMES)
52    int n;
53    for (s = result; *s != '\0'; ++s) {
54	*s = LOWERCASE(*s);
55    }
56#endif
57#if defined(PROG_EXT)
58    if ((s = strrchr(result, '.')) != 0) {
59	if (!strcmp(s, PROG_EXT))
60	    *s = '\0';
61    }
62#endif
63#endif
64    return result;
65}
66
67NCURSES_EXPORT(char *)
68_nc_basename(char *path)
69{
70    char *result = strrchr(path, '/');
71#ifdef __EMX__
72    if (result == 0)
73	result = strrchr(path, '\\');
74#endif
75    if (result == 0)
76	result = path;
77    else
78	result++;
79    return result;
80}
81
82NCURSES_EXPORT(int)
83_nc_access(const char *path, int mode)
84{
85    if (access(path, mode) < 0) {
86	if ((mode & W_OK) != 0
87	    && errno == ENOENT
88	    && strlen(path) < PATH_MAX) {
89	    char head[PATH_MAX];
90	    char *leaf = _nc_basename(strcpy(head, path));
91
92	    if (leaf == 0)
93		leaf = head;
94	    *leaf = '\0';
95	    if (head == leaf)
96		(void) strcpy(head, ".");
97
98	    return access(head, R_OK | W_OK | X_OK);
99	}
100	return -1;
101    }
102    return 0;
103}
104
105#ifndef USE_ROOT_ENVIRON
106/*
107 * Returns true if we allow application to use environment variables that are
108 * used for searching lists of directories, etc.
109 */
110NCURSES_EXPORT(int)
111_nc_env_access(void)
112{
113#if HAVE_ISSETUGID
114    if (issetugid())
115	return FALSE;
116#elif HAVE_GETEUID && HAVE_GETEGID
117    if (getuid() != geteuid()
118	|| getgid() != getegid())
119	return FALSE;
120#endif
121    return getuid() != 0 && geteuid() != 0;	/* ...finally, disallow root */
122}
123#endif
124