1998-03-01 12:21:12 +08:00
|
|
|
/****************************************************************************
|
2009-04-12 06:39:24 +08:00
|
|
|
* Copyright (c) 1998-2007,2009 Free Software Foundation, Inc. *
|
1998-03-01 12:21:12 +08:00
|
|
|
* *
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a *
|
|
|
|
* copy of this software and associated documentation files (the *
|
|
|
|
* "Software"), to deal in the Software without restriction, including *
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish, *
|
|
|
|
* distribute, distribute with modifications, sublicense, and/or sell *
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is *
|
|
|
|
* furnished to do so, subject to the following conditions: *
|
|
|
|
* *
|
|
|
|
* The above copyright notice and this permission notice shall be included *
|
|
|
|
* in all copies or substantial portions of the Software. *
|
|
|
|
* *
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
|
|
|
|
* IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
|
|
|
|
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
|
|
|
|
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
|
|
|
|
* THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
|
|
|
|
* *
|
|
|
|
* Except as contained in this notice, the name(s) of the above copyright *
|
|
|
|
* holders shall not be used in advertising or otherwise to promote the *
|
|
|
|
* sale, use or other dealings in this Software without prior written *
|
|
|
|
* authorization. *
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2006-12-18 12:32:42 +08:00
|
|
|
* Author: Thomas E. Dickey *
|
1998-03-01 12:21:12 +08:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <curses.priv.h>
|
2006-12-18 12:32:42 +08:00
|
|
|
|
2007-11-18 10:09:05 +08:00
|
|
|
#include <ctype.h>
|
2006-12-18 12:32:42 +08:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2000-10-21 12:42:11 +08:00
|
|
|
#include <tic.h>
|
2002-10-13 11:35:53 +08:00
|
|
|
#include <nc_alloc.h>
|
1998-03-01 12:21:12 +08:00
|
|
|
|
2009-04-12 06:39:24 +08:00
|
|
|
MODULE_ID("$Id: access.c,v 1.15 2009/04/05 00:03:10 tom Exp $")
|
1998-03-01 12:21:12 +08:00
|
|
|
|
2002-10-13 11:35:53 +08:00
|
|
|
#define LOWERCASE(c) ((isalpha(UChar(c)) && isupper(UChar(c))) ? tolower(UChar(c)) : (c))
|
|
|
|
|
|
|
|
NCURSES_EXPORT(char *)
|
|
|
|
_nc_rootname(char *path)
|
|
|
|
{
|
|
|
|
char *result = _nc_basename(path);
|
2007-11-18 10:09:05 +08:00
|
|
|
#if !MIXEDCASE_FILENAMES || defined(PROG_EXT)
|
2002-10-13 11:35:53 +08:00
|
|
|
static char *temp;
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
temp = strdup(result);
|
|
|
|
result = temp;
|
2007-11-18 10:09:05 +08:00
|
|
|
#if !MIXEDCASE_FILENAMES
|
2002-10-13 11:35:53 +08:00
|
|
|
for (s = result; *s != '\0'; ++s) {
|
|
|
|
*s = LOWERCASE(*s);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if defined(PROG_EXT)
|
|
|
|
if ((s = strrchr(result, '.')) != 0) {
|
|
|
|
if (!strcmp(s, PROG_EXT))
|
|
|
|
*s = '\0';
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2006-12-18 12:32:42 +08:00
|
|
|
/*
|
|
|
|
* Check if a string appears to be an absolute pathname.
|
|
|
|
*/
|
|
|
|
NCURSES_EXPORT(bool)
|
|
|
|
_nc_is_abs_path(const char *path)
|
|
|
|
{
|
|
|
|
#if defined(__EMX__) || defined(__DJGPP__)
|
|
|
|
#define is_pathname(s) ((((s) != 0) && ((s)[0] == '/')) \
|
|
|
|
|| (((s)[0] != 0) && ((s)[1] == ':')))
|
|
|
|
#else
|
|
|
|
#define is_pathname(s) ((s) != 0 && (s)[0] == '/')
|
|
|
|
#endif
|
|
|
|
return is_pathname(path);
|
|
|
|
}
|
|
|
|
|
2004-02-09 10:15:26 +08:00
|
|
|
/*
|
|
|
|
* Return index of the basename
|
|
|
|
*/
|
|
|
|
NCURSES_EXPORT(unsigned)
|
|
|
|
_nc_pathlast(const char *path)
|
1998-03-01 12:21:12 +08:00
|
|
|
{
|
2004-02-09 10:15:26 +08:00
|
|
|
const char *test = strrchr(path, '/');
|
2000-10-21 12:42:11 +08:00
|
|
|
#ifdef __EMX__
|
2004-02-09 10:15:26 +08:00
|
|
|
if (test == 0)
|
|
|
|
test = strrchr(path, '\\');
|
2000-10-21 12:42:11 +08:00
|
|
|
#endif
|
2004-02-09 10:15:26 +08:00
|
|
|
if (test == 0)
|
|
|
|
test = path;
|
2000-10-21 12:42:11 +08:00
|
|
|
else
|
2004-02-09 10:15:26 +08:00
|
|
|
test++;
|
2009-04-12 06:39:24 +08:00
|
|
|
return (unsigned) (test - path);
|
2004-02-09 10:15:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NCURSES_EXPORT(char *)
|
|
|
|
_nc_basename(char *path)
|
|
|
|
{
|
|
|
|
return path + _nc_pathlast(path);
|
2000-10-21 12:42:11 +08:00
|
|
|
}
|
|
|
|
|
2002-10-13 11:35:53 +08:00
|
|
|
NCURSES_EXPORT(int)
|
2000-10-21 12:42:11 +08:00
|
|
|
_nc_access(const char *path, int mode)
|
|
|
|
{
|
|
|
|
if (access(path, mode) < 0) {
|
|
|
|
if ((mode & W_OK) != 0
|
|
|
|
&& errno == ENOENT
|
|
|
|
&& strlen(path) < PATH_MAX) {
|
|
|
|
char head[PATH_MAX];
|
|
|
|
char *leaf = _nc_basename(strcpy(head, path));
|
|
|
|
|
|
|
|
if (leaf == 0)
|
|
|
|
leaf = head;
|
|
|
|
*leaf = '\0';
|
|
|
|
if (head == leaf)
|
|
|
|
(void) strcpy(head, ".");
|
|
|
|
|
|
|
|
return access(head, R_OK | W_OK | X_OK);
|
1998-03-01 12:21:12 +08:00
|
|
|
}
|
2000-10-21 12:42:11 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-12-18 12:32:42 +08:00
|
|
|
NCURSES_EXPORT(bool)
|
|
|
|
_nc_is_dir_path(const char *path)
|
|
|
|
{
|
|
|
|
bool result = FALSE;
|
|
|
|
struct stat sb;
|
|
|
|
|
|
|
|
if (stat(path, &sb) == 0
|
|
|
|
&& (sb.st_mode & S_IFMT) == S_IFDIR) {
|
|
|
|
result = TRUE;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NCURSES_EXPORT(bool)
|
|
|
|
_nc_is_file_path(const char *path)
|
|
|
|
{
|
|
|
|
bool result = FALSE;
|
|
|
|
struct stat sb;
|
|
|
|
|
|
|
|
if (stat(path, &sb) == 0
|
|
|
|
&& (sb.st_mode & S_IFMT) == S_IFREG) {
|
|
|
|
result = TRUE;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2000-10-21 12:42:11 +08:00
|
|
|
#ifndef USE_ROOT_ENVIRON
|
|
|
|
/*
|
|
|
|
* Returns true if we allow application to use environment variables that are
|
|
|
|
* used for searching lists of directories, etc.
|
|
|
|
*/
|
2002-10-13 11:35:53 +08:00
|
|
|
NCURSES_EXPORT(int)
|
2000-10-21 12:42:11 +08:00
|
|
|
_nc_env_access(void)
|
|
|
|
{
|
|
|
|
#if HAVE_ISSETUGID
|
|
|
|
if (issetugid())
|
|
|
|
return FALSE;
|
|
|
|
#elif HAVE_GETEUID && HAVE_GETEGID
|
|
|
|
if (getuid() != geteuid()
|
2002-10-13 11:35:53 +08:00
|
|
|
|| getgid() != getegid())
|
2000-10-21 12:42:11 +08:00
|
|
|
return FALSE;
|
|
|
|
#endif
|
2002-10-13 11:35:53 +08:00
|
|
|
return getuid() != 0 && geteuid() != 0; /* ...finally, disallow root */
|
1998-03-01 12:21:12 +08:00
|
|
|
}
|
2000-10-21 12:42:11 +08:00
|
|
|
#endif
|