home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3297 / stattimeout.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-07  |  948 b   |  54 lines

  1. /* History:
  2. 5/1/91 DJB baseline
  3. 3/15/91 DJB Extracted into separate library, added timeout arg
  4. Stolen from safe_stat in Dupuy ofiles.
  5. */
  6.  
  7. /* XXX: There are all sorts of race conditions here. DJB */
  8.  
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <signal.h>
  12. #include <setjmp.h>
  13. #include <errno.h>
  14. extern int errno;
  15. static jmp_buf jmpbuf;
  16.  
  17. /* ARGSUSED */
  18. static int wakeup()
  19. {
  20.  (void) alarm(0);
  21.  (void) signal(SIGALRM,SIG_IGN);
  22.  longjmp(jmpbuf,1);
  23. }
  24.  
  25. int stattimeout(path,buf,timeout)
  26. char *path;
  27. struct stat *buf;
  28. int timeout;
  29. {
  30.  int err;
  31.  static int saveerrno;
  32.  
  33.  if (!timeout)
  34.    return stat(path,buf);
  35.  
  36.  saveerrno = errno;
  37.  if (setjmp(jmpbuf))
  38.   {
  39.    (void) alarm(0);
  40.    (void) signal(SIGALRM,SIG_IGN);
  41.    errno = ETIMEDOUT;
  42.    return -1;
  43.   }
  44.  (void) signal(SIGALRM,wakeup);
  45.  (void) alarm(timeout);
  46.  err = stat(path,buf);
  47.  if (err)
  48.    saveerrno = errno;
  49.  (void) alarm(0);
  50.  (void) signal(SIGALRM,SIG_IGN);
  51.  errno = saveerrno;
  52.  return err;
  53. }
  54.