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_open.c < prev    next >
C/C++ Source or Header  |  1993-01-12  |  2KB  |  102 lines

  1.  
  2. static char rcsid[] = "@(#)$Id: can_open.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_open.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_open - can this user open 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_open(file, mode)
  44. char *file, *mode;
  45. {
  46.     /** Returns 0 iff user can open the file.  This is not
  47.         the same as can_access - it's used for when the file might
  48.         not exist... **/
  49.  
  50.     FILE *fd;
  51.     int the_stat = 0, pid, w, preexisted = 0; 
  52.     void _exit();
  53. #if defined(BSD) && !defined(WEXITSTATUS)
  54.     union wait status;
  55. #else
  56.     int status;
  57. #endif
  58.     register SIGHAND_TYPE (*istat)(), (*qstat)();
  59.     
  60. #ifdef VFORK
  61.     if ((pid = vfork()) == 0) {
  62. #else
  63.     if ((pid = fork()) == 0) {
  64. #endif
  65.       setgid(groupid);
  66.       setuid(userid);        /** back to normal userid **/
  67.       errno = 0;
  68.       if (access(file, ACCESS_EXISTS) == 0)
  69.         preexisted = 1;
  70.       if ((fd = fopen(file, mode)) == NULL)
  71.         _exit(errno);
  72.       else {
  73.         fclose(fd);        /* don't just leave it open! */
  74.         if(!preexisted)    /* don't leave it if this test created it! */
  75.           unlink(file);
  76.         _exit(0);
  77.       }
  78.       _exit(127);
  79.     }
  80.  
  81.     istat = signal(SIGINT, SIG_IGN);
  82.     qstat = signal(SIGQUIT, SIG_IGN);
  83.  
  84.     while ((w = wait(&status)) != pid && w != -1)
  85.         ;
  86.  
  87. #ifdef WEXITSTATUS
  88.     the_stat = WEXITSTATUS(status);
  89. #else
  90. #ifdef BSD
  91.     the_stat = status.w_retcode;
  92. #else
  93.     the_stat = status >> 8;
  94. #endif
  95. #endif /*WEXITSTATUS*/
  96.     
  97.     signal(SIGINT, istat);
  98.     signal(SIGQUIT, qstat);
  99.  
  100.     return(the_stat);
  101. }
  102.