paul@uxc.cso.uiuc.edu (Paul Pomes - UofIllinois CSO) (10/03/90)
I have a program that makes use of flock() scattered around several files. Rather than cluttering the code with #ifdefs for both flock() and lockf(), I'd like to redefine the flock calls in a common include file to use lockf instead when #ifndef BSD is true. Has anyone already created such a beast? /pbp -- Paul Pomes UUCP: {att,iuvax,uunet}!uiucuxc!paul Internet, BITNET: paul@uxc.cso.uiuc.edu US Mail: UofIllinois, CSO, 1304 W Springfield Ave, Urbana, IL 61801-2910
mark@comp.vuw.ac.nz (Mark Davies) (10/03/90)
In article <1990Oct2.213652.20025@ux1.cso.uiuc.edu> paul@uxc.cso.uiuc.edu (Paul Pomes - UofIllinois CSO) writes: >I have a program that makes use of flock() scattered around several files. >Rather than cluttering the code with #ifdefs for both flock() and lockf(), >I'd like to redefine the flock calls in a common include file to use lockf >instead when #ifndef BSD is true. Has anyone already created such a beast? This is the flock() emulation we use for sendmail and a few other programs on our hpux boxes and I think its now also been run on a SysV box. cheers mark #! /bin/sh # This is a shell archive. Remove anything before this line, then unpack # it by saving it into a file and typing "sh file". To overwrite existing # files, type "sh file -c". You can also feed this as standard input via # unshar, or by typing "sh <file", e.g.. If this archive is complete, you # will see the following message at the end: # "End of shell archive." # Contents: flock.c flock.h # Wrapped by mark@the-taj.comp.vuw.ac.nz on Wed Oct 3 14:30:18 1990 PATH=/bin:/usr/bin:/usr/ucb ; export PATH if test -f 'flock.c' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'flock.c'\" else echo shar: Extracting \"'flock.c'\" \(2049 characters\) sed "s/^X//" >'flock.c' <<'END_OF_FILE' X/* X * Copyright (c) 1989 Mark Davies X * Copyright (c) 1990 Andy Linton X * Copyright (c) 1990 Victoria University of Wellington. X * All rights reserved. X * X * Redistribution and use in source and binary forms are permitted provided X * that: (1) source distributions retain this entire copyright notice and X * comment, and (2) distributions including binaries display the following X * acknowledgement: ``This product includes software developed by the X * Victoria University of Wellington, New Zealand and its contributors'' X * in the documentation or other materials provided with the distribution X * and in all advertising materials mentioning features or use of this X * software. X * Neither the name of the University nor the names of its contributors may X * be used to endorse or promote products derived from this software without X * specific prior written permission. X * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED X * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF X * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. X */ X X#include <unistd.h> X#include <sys/types.h> X#include <errno.h> X#include <fcntl.h> X#include "flock.h" /* defines normally in <sys/file.h> */ X X X#if defined(F_SETLK) && defined(F_SETLKW) X Xflock(fd, operation) Xint fd, operation; X{ X int op, ret; X struct flock arg; X extern int errno; X X op = (LOCK_NB & operation) ? F_SETLK : F_SETLKW; X X arg.l_type = (LOCK_EX & operation) ? F_WRLCK : X (LOCK_SH & operation) ? F_RDLCK : F_UNLCK; X arg.l_whence = 0; X arg.l_start = 0; X arg.l_len = 0; X arg.l_pid = 0; X X if ((ret = fcntl(fd, op, &arg)) == -1) { X if (errno == EACCES || errno == EAGAIN) X errno = EWOULDBLOCK; X } X return (ret); X} X X#else X Xflock(fd, operation) Xint fd, operation; X{ X int op, ret; X extern int errno; X X op = (LOCK_UN & operation) ? F_ULOCK : X (LOCK_NB & operation) ? F_TLOCK : F_LOCK; X X if ((ret = lockf(fd, op, 0)) == -1) { X if (errno == EACCES || errno == EAGAIN) X errno = EWOULDBLOCK; X } X X return (ret); X} X X#endif /* F_SETLK && F_SETLKW */ X END_OF_FILE if test 2049 -ne `wc -c <'flock.c'`; then echo shar: \"'flock.c'\" unpacked with wrong size! fi # end of 'flock.c' fi if test -f 'flock.h' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'flock.h'\" else echo shar: Extracting \"'flock.h'\" \(413 characters\) sed "s/^X//" >'flock.h' <<'END_OF_FILE' X/* These definitions are in <sys/file.h> on BSD 4.3 */ X X#if defined(F_SETLK) && defined(F_SETLKW) X# define FCNTL_FLOCK X#else X# define LOCKF_FLOCK X#endif /* F_SETLK && F_SETLKW */ X/* X * Flock call. X */ X#define LOCK_SH 1 /* shared lock */ X#define LOCK_EX 2 /* exclusive lock */ X#define LOCK_NB 4 /* don't block when locking */ X#define LOCK_UN 8 /* unlock */ END_OF_FILE if test 413 -ne `wc -c <'flock.h'`; then echo shar: \"'flock.h'\" unpacked with wrong size! fi chmod +x 'flock.h' # end of 'flock.h' fi echo shar: End of shell archive. exit 0