Back to index

tetex-bin  3.0
access.c
Go to the documentation of this file.
00001 /****************************************************************************
00002  * Copyright (c) 1998,2001,2003 Free Software Foundation, Inc.              *
00003  *                                                                          *
00004  * Permission is hereby granted, free of charge, to any person obtaining a  *
00005  * copy of this software and associated documentation files (the            *
00006  * "Software"), to deal in the Software without restriction, including      *
00007  * without limitation the rights to use, copy, modify, merge, publish,      *
00008  * distribute, distribute with modifications, sublicense, and/or sell       *
00009  * copies of the Software, and to permit persons to whom the Software is    *
00010  * furnished to do so, subject to the following conditions:                 *
00011  *                                                                          *
00012  * The above copyright notice and this permission notice shall be included  *
00013  * in all copies or substantial portions of the Software.                   *
00014  *                                                                          *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
00016  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
00017  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
00018  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
00019  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
00020  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
00021  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
00022  *                                                                          *
00023  * Except as contained in this notice, the name(s) of the above copyright   *
00024  * holders shall not be used in advertising or otherwise to promote the     *
00025  * sale, use or other dealings in this Software without prior written       *
00026  * authorization.                                                           *
00027  ****************************************************************************/
00028 
00029 /****************************************************************************
00030  *  Author: Thomas E. Dickey <dickey@clark.net> 1998,2000,2001              *
00031  ****************************************************************************/
00032 
00033 #include <curses.priv.h>
00034 #include <tic.h>
00035 #include <nc_alloc.h>
00036 
00037 MODULE_ID("$Id: access.c,v 1.10 2003/07/05 19:31:28 tom Exp $")
00038 
00039 #define LOWERCASE(c) ((isalpha(UChar(c)) && isupper(UChar(c))) ? tolower(UChar(c)) : (c))
00040 
00041 NCURSES_EXPORT(char *)
00042 _nc_rootname(char *path)
00043 {
00044     char *result = _nc_basename(path);
00045 #if !defined(MIXEDCASE_FILENAMES) || defined(PROG_EXT)
00046     static char *temp;
00047     char *s;
00048 
00049     temp = strdup(result);
00050     result = temp;
00051 #if !defined(MIXEDCASE_FILENAMES)
00052     int n;
00053     for (s = result; *s != '\0'; ++s) {
00054        *s = LOWERCASE(*s);
00055     }
00056 #endif
00057 #if defined(PROG_EXT)
00058     if ((s = strrchr(result, '.')) != 0) {
00059        if (!strcmp(s, PROG_EXT))
00060            *s = '\0';
00061     }
00062 #endif
00063 #endif
00064     return result;
00065 }
00066 
00067 /*
00068  * Return index of the basename
00069  */
00070 NCURSES_EXPORT(unsigned)
00071 _nc_pathlast(const char *path)
00072 {
00073     const char *test = strrchr(path, '/');
00074 #ifdef __EMX__
00075     if (test == 0)
00076        test = strrchr(path, '\\');
00077 #endif
00078     if (test == 0)
00079        test = path;
00080     else
00081        test++;
00082     return (test - path);
00083 }
00084 
00085 NCURSES_EXPORT(char *)
00086 _nc_basename(char *path)
00087 {
00088     return path + _nc_pathlast(path);
00089 }
00090 
00091 NCURSES_EXPORT(int)
00092 _nc_access(const char *path, int mode)
00093 {
00094     if (access(path, mode) < 0) {
00095        if ((mode & W_OK) != 0
00096            && errno == ENOENT
00097            && strlen(path) < PATH_MAX) {
00098            char head[PATH_MAX];
00099            char *leaf = _nc_basename(strcpy(head, path));
00100 
00101            if (leaf == 0)
00102               leaf = head;
00103            *leaf = '\0';
00104            if (head == leaf)
00105               (void) strcpy(head, ".");
00106 
00107            return access(head, R_OK | W_OK | X_OK);
00108        }
00109        return -1;
00110     }
00111     return 0;
00112 }
00113 
00114 #ifndef USE_ROOT_ENVIRON
00115 /*
00116  * Returns true if we allow application to use environment variables that are
00117  * used for searching lists of directories, etc.
00118  */
00119 NCURSES_EXPORT(int)
00120 _nc_env_access(void)
00121 {
00122 #if HAVE_ISSETUGID
00123     if (issetugid())
00124        return FALSE;
00125 #elif HAVE_GETEUID && HAVE_GETEGID
00126     if (getuid() != geteuid()
00127        || getgid() != getegid())
00128        return FALSE;
00129 #endif
00130     return getuid() != 0 && geteuid() != 0;      /* ...finally, disallow root */
00131 }
00132 #endif