2 % (c) The GRASP/AQUA Project, Glasgow University, 1994
4 \subsection[getLock.lc]{stdin/stout/stderr Runtime Support}
11 #ifdef HAVE_SYS_TYPES_H
12 #include <sys/types.h>
15 #ifdef HAVE_SYS_STAT_H
28 #define FD_SETSIZE 256
37 static Lock readLock[FD_SETSIZE];
38 static Lock writeLock[FD_SETSIZE];
40 static int readLocks = 0;
41 static int writeLocks = 0;
44 lockFile(fd, exclusive)
51 while (fstat(fd, &sb) < 0) {
57 /* Only lock regular files */
58 if (!S_ISREG(sb.st_mode))
61 for (i = 0; i < writeLocks; i++)
62 if (writeLock[i].inode == sb.st_ino && writeLock[i].device == sb.st_dev) {
69 readLock[i].device = sb.st_dev;
70 readLock[i].inode = sb.st_ino;
75 for (i = 0; i < readLocks; i++)
76 if (readLock[i].inode == sb.st_ino && readLock[i].device == sb.st_dev) {
82 writeLock[i].device = sb.st_dev;
83 writeLock[i].inode = sb.st_ino;
94 for (i = 0; i < readLocks; i++)
95 if (readLock[i].fd == fd) {
96 while (++i < readLocks)
97 readLock[i - 1] = readLock[i];
102 for (i = 0; i < writeLocks; i++)
103 if (writeLock[i].fd == fd) {
104 while (++i < writeLocks)
105 writeLock[i - 1] = writeLock[i];
112 getLock(fp, exclusive)
116 if (lockFile(fileno((FILE *) fp), exclusive) < 0) {
127 ghc_errtype = ERR_RESOURCEBUSY;
128 ghc_errstr = "file is locked";
131 (void) fclose((FILE *) fp);