home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / UNIX / Mail / qpopper-2.4-MIHS / flock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-09-02  |  2.2 KB  |  85 lines

  1. /*
  2.  * Copyright (c) 1989 Mark Davies
  3.  * Copyright (c) 1990 Andy Linton
  4.  * Copyright (c) 1990 Victoria University of Wellington.
  5.  * All rights reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms are permitted provided
  8.  * that: (1) source distributions retain this entire copyright notice and
  9.  * comment, and (2) distributions including binaries display the following
  10.  * acknowledgement:  ``This product includes software developed by the
  11.  * Victoria University of Wellington, New Zealand and its contributors''
  12.  * in the documentation or other materials provided with the distribution
  13.  * and in all advertising materials mentioning features or use of this
  14.  * software.
  15.  * Neither the name of the University nor the names of its contributors may
  16.  * be used to endorse or promote products derived from this software without
  17.  * specific prior written permission.
  18.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  19.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  20.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  21.  */
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25.  
  26. #ifndef HAVE_FLOCK /*  defined(SYSV) || defined(ISC) */
  27.  
  28. #include <unistd.h>
  29. #include <sys/types.h>
  30. #include <errno.h>
  31. #ifdef ISC
  32. # include <net/errno.h>
  33. #endif
  34. #include <flock.h>               /* defines normally in <sys/file.h> */
  35.  
  36.  
  37. #if defined(F_SETLK) && defined(F_SETLKW)
  38.  
  39. flock(fd, operation)
  40. int fd, operation;
  41. {
  42.     int op, ret;
  43.     struct flock arg;
  44.     extern int errno;
  45.  
  46.     op = (LOCK_NB & operation) ? F_SETLK : F_SETLKW;
  47.     
  48.     arg.l_type = (LOCK_EX & operation) ? F_WRLCK :
  49.         (LOCK_SH & operation) ? F_RDLCK : F_UNLCK;
  50.     arg.l_whence = 0;
  51.     arg.l_start = 0;
  52.     arg.l_len = 0;
  53.     arg.l_pid = 0;
  54.     
  55.     if ((ret = fcntl(fd, op, &arg)) == -1) {
  56.         if (errno == EACCES || errno == EAGAIN)
  57.             errno = EWOULDBLOCK;
  58.     }
  59.     return (ret);
  60. }
  61.  
  62. #else
  63.  
  64. flock(fd, operation)
  65. int fd, operation;
  66. {
  67.     int op, ret;
  68.     extern int errno;
  69.  
  70.     op = (LOCK_UN & operation) ? F_ULOCK :
  71.       (LOCK_NB & operation) ? F_TLOCK : F_LOCK;
  72.  
  73.     if ((ret = lockf(fd, op, 0)) == -1) {
  74.         if (errno == EACCES || errno == EAGAIN)
  75.             errno = EWOULDBLOCK;
  76.     }
  77.  
  78.     return (ret);
  79. }
  80.  
  81. #endif /* F_SETLK && F_SETLKW */
  82.  
  83. #endif /* HAVE_FLOCK */
  84.  
  85.