home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / sendmail / sendmail-5.65c+IDA-1.4.4.1 / support / flock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-07  |  2.1 KB  |  84 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.  
  23. #include <unistd.h>
  24. #include <sys/types.h>
  25. #include <errno.h>
  26. #include "flock.h"               /* defines normally in <sys/file.h> */
  27.  
  28. #ifndef    lint
  29. static char sccsid[] = "%W%";
  30. static char  rcsid[] = "@(#)$Id$";
  31. #endif
  32.   
  33.  
  34. #if defined(F_SETLK) && defined(F_SETLKW)
  35.  
  36. int
  37. flock(fd, operation)
  38. int fd, operation;
  39. {
  40.     int op, ret;
  41.     struct flock arg;
  42.     extern int errno;
  43.     extern int fcntl();
  44.  
  45.     op = (LOCK_NB & operation) ? F_SETLK : F_SETLKW;
  46.     
  47.     arg.l_type = (LOCK_EX & operation) ? F_WRLCK :
  48.         (LOCK_SH & operation) ? F_RDLCK : F_UNLCK;
  49.     arg.l_whence = 0;
  50.     arg.l_start = 0;
  51.     arg.l_len = 0;
  52.     arg.l_pid = 0;
  53.     
  54.     if ((ret = fcntl(fd, op, &arg)) == -1) {
  55.         if (errno == EACCES || errno == EAGAIN)
  56.             errno = EWOULDBLOCK;
  57.     }
  58.     return (ret);
  59. }
  60.  
  61. #else
  62.  
  63. int
  64. flock(fd, operation)
  65. int fd, operation;
  66. {
  67.     int op, ret;
  68.     extern int errno;
  69.     extern int lockf();
  70.  
  71.     op = (LOCK_UN & operation) ? F_ULOCK :
  72.       (LOCK_NB & operation) ? F_TLOCK : F_LOCK;
  73.  
  74.     if ((ret = lockf(fd, op, 0)) == -1) {
  75.         if (errno == EACCES || errno == EAGAIN)
  76.             errno = EWOULDBLOCK;
  77.     }
  78.  
  79.     return (ret);
  80. }
  81.  
  82. #endif /* F_SETLK && F_SETLKW */
  83.  
  84.