home *** CD-ROM | disk | FTP | other *** search
- /* History:
- 5/1/91 DJB baseline
- 3/15/91 DJB Extracted into separate library, added timeout arg
- Stolen from safe_stat in Dupuy ofiles.
- */
-
- /* XXX: There are all sorts of race conditions here. DJB */
-
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <signal.h>
- #include <setjmp.h>
- #include <errno.h>
- extern int errno;
- static jmp_buf jmpbuf;
-
- /* ARGSUSED */
- static int wakeup()
- {
- (void) alarm(0);
- (void) signal(SIGALRM,SIG_IGN);
- longjmp(jmpbuf,1);
- }
-
- int stattimeout(path,buf,timeout)
- char *path;
- struct stat *buf;
- int timeout;
- {
- int err;
- static int saveerrno;
-
- if (!timeout)
- return stat(path,buf);
-
- saveerrno = errno;
- if (setjmp(jmpbuf))
- {
- (void) alarm(0);
- (void) signal(SIGALRM,SIG_IGN);
- errno = ETIMEDOUT;
- return -1;
- }
- (void) signal(SIGALRM,wakeup);
- (void) alarm(timeout);
- err = stat(path,buf);
- if (err)
- saveerrno = errno;
- (void) alarm(0);
- (void) signal(SIGALRM,SIG_IGN);
- errno = saveerrno;
- return err;
- }
-