home *** CD-ROM | disk | FTP | other *** search
/ ftp.uv.es / 2014.11.ftp.uv.es.tar / ftp.uv.es / pub / unix / elm-2.4-pl20.tar.Z / elm-2.4-pl20.tar / lib / can_access.c < prev    next >
C/C++ Source or Header  |  1993-01-12  |  2KB  |  109 lines

  1.  
  2. static char rcsid[] = "@(#)$Id: can_access.c,v 5.2 1992/12/12 01:29:26 syd Exp $";
  3.  
  4. /*******************************************************************************
  5.  *  The Elm Mail System  -  $Revision: 5.2 $   $State: Exp $
  6.  *
  7.  *            Copyright (c) 1988-1992 USENET Community Trust
  8.  *            Copyright (c) 1986,1987 Dave Taylor
  9.  *******************************************************************************
  10.  * Bug reports, patches, comments, suggestions should be sent to:
  11.  *
  12.  *    Syd Weinstein, Elm Coordinator
  13.  *    elm@DSI.COM            dsinc!elm
  14.  *
  15.  *******************************************************************************
  16.  * $Log: can_access.c,v $
  17.  * Revision 5.2  1992/12/12  01:29:26  syd
  18.  * Fix double inclusion of sys/types.h
  19.  * From: Tom Moore <tmoore@wnas.DaytonOH.NCR.COM>
  20.  *
  21.  * Revision 5.1  1992/10/03  22:41:36  syd
  22.  * Initial checkin as of 2.4 Release at PL0
  23.  *
  24.  *
  25.  ******************************************************************************/
  26.  
  27. /** can_access - can this user access this file using their normal uid/gid
  28.  
  29. **/
  30.  
  31. #include "headers.h"
  32. #include <sys/stat.h>
  33. #include <ctype.h>
  34. #include <errno.h>
  35.  
  36. #ifdef BSD
  37. # include <sys/wait.h>
  38. #endif
  39.  
  40. extern int errno;        /* system error number */
  41.  
  42. int
  43. can_access(file, mode)
  44. char *file; 
  45. int   mode;
  46. {
  47.     /** returns ZERO iff user can access file or "errno" otherwise **/
  48.  
  49.     int the_stat = 0, pid, w; 
  50.     struct stat stat_buf;
  51.     void _exit();
  52. #if defined(BSD) && !defined(WEXITSTATUS)
  53.     union wait status;
  54. #else
  55.     int status;
  56. #endif
  57.     register SIGHAND_TYPE (*istat)(), (*qstat)();
  58.     
  59. #ifdef VFORK
  60.     if ((pid = vfork()) == 0) {
  61. #else
  62.     if ((pid = fork()) == 0) {
  63. #endif
  64.       setgid(groupid);
  65.       setuid(userid);        /** back to normal userid **/
  66.  
  67.       errno = 0;
  68.  
  69.       if (access(file, mode) == 0) 
  70.         _exit(0);
  71.       else 
  72.         _exit(errno != 0? errno : 1);    /* never return zero! */
  73.       _exit(127);
  74.     }
  75.  
  76.     istat = signal(SIGINT, SIG_IGN);
  77.     qstat = signal(SIGQUIT, SIG_IGN);
  78.  
  79.     while ((w = wait(&status)) != pid && w != -1)
  80.         ;
  81.  
  82. #if    defined(WEXITSTATUS)
  83.     /* Use POSIX macro if defined */
  84.     the_stat = WEXITSTATUS(status);
  85. #else
  86. #ifdef BSD
  87.     the_stat = status.w_retcode;
  88. #else
  89.     the_stat = status >> 8;
  90. #endif
  91. #endif    /*WEXITSTATUS*/
  92.  
  93.     signal(SIGINT, istat);
  94.     signal(SIGQUIT, qstat);
  95.     if (the_stat == 0) {
  96.       if (stat(file, &stat_buf) == 0) {
  97.         w = stat_buf.st_mode & S_IFMT;
  98. #ifdef S_IFLNK
  99.         if (w != S_IFREG && w != S_IFLNK)
  100. #else
  101.         if (w != S_IFREG)
  102. #endif
  103.           the_stat = 1;
  104.       }
  105.     }
  106.  
  107.     return(the_stat);
  108. }
  109.