#include
struct flock* file_lock(int type, int whence) {
static struct flock ret ;
ret.l_type = type ;
ret.l_start = 0 ;
ret.l_whence = whence ;
ret.l_len = 0 ;
ret.l_pid = getpid() ;
return &ret ;
}
read_lock(FILE *f) { /* a shared lock on an entire file */
fcntl(fileno(f), F_SETLKW, file_lock(F_RDLCK, SEEK_SET));
}
write_lock(FILE *f) { /* an exclusive lock on an entire file */
fcntl(fileno(f), F_SETLKW, file_lock(F_WRLCK, SEEK_SET));
}
append_lock(FILE *f) { /* a lock on the _end_ of a file -- other
processes may access existing records */
fcntl(fileno(f), F_SETLKW, file_lock(F_WRLCK, SEEK_END));
}
un_lock(FILE *f) { /* ulock an entire file */
fcntl(fileno(f), F_SETLKW, file_lock(F_UNLCK, SEEK_SET));
}