home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / lib / Xau / AuLock.c.orig < prev    next >
Encoding:
Text File  |  1991-04-16  |  2.3 KB  |  93 lines

  1. /*
  2.  * Xau - X Authorization Database Library
  3.  *
  4.  * $XConsortium: AuLock.c,v 1.7 91/04/17 10:59:27 rws Exp $
  5.  *
  6.  * Copyright 1988 Massachusetts Institute of Technology
  7.  *
  8.  * Permission to use, copy, modify, and distribute this software and its
  9.  * documentation for any purpose and without fee is hereby granted, provided
  10.  * that the above copyright notice appear in all copies and that both that
  11.  * copyright notice and this permission notice appear in supporting
  12.  * documentation, and that the name of M.I.T. not be used in advertising or
  13.  * publicity pertaining to distribution of the software without specific,
  14.  * written prior permission.  M.I.T. makes no representations about the
  15.  * suitability of this software for any purpose.  It is provided "as is"
  16.  * without express or implied warranty.
  17.  *
  18.  * Author:  Keith Packard, MIT X Consortium
  19.  */
  20.  
  21. #include <X11/Xauth.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #ifndef X_NOT_POSIX
  25. #include <errno.h>
  26. #else
  27. #include <sys/errno.h>
  28. #endif
  29.  
  30. #if NeedFunctionPrototypes
  31. int
  32. XauLockAuth (
  33. _Xconst char *file_name,
  34. int    retries,
  35. int    timeout,
  36. long    dead)
  37. #else
  38. int
  39. XauLockAuth (file_name, retries, timeout, dead)
  40. char    *file_name;
  41. int    retries;
  42. int    timeout;
  43. long    dead;
  44. #endif
  45. {
  46.     char    creat_name[1025], link_name[1025];
  47.     char    *strcpy (), *strcat ();
  48.     long    time ();
  49.     unsigned    sleep ();
  50.     struct stat    statb;
  51.     long    now;
  52.     int        creat_fd = -1;
  53.     extern int    errno;
  54.  
  55.     if (strlen (file_name) > 1022)
  56.     return LOCK_ERROR;
  57.     (void) strcpy (creat_name, file_name);
  58.     (void) strcat (creat_name, "-c");
  59.     (void) strcpy (link_name, file_name);
  60.     (void) strcat (link_name, "-l");
  61.     if (stat (creat_name, &statb) != -1) {
  62.     now = time ((long *) 0);
  63.     /*
  64.      * NFS may cause ctime to be before now, special
  65.      * case a 0 deadtime to force lock removal
  66.      */
  67.     if (dead == 0 || now - statb.st_ctime > dead) {
  68.         (void) unlink (creat_name);
  69.         (void) unlink (link_name);
  70.     }
  71.     }
  72.     
  73.     while (retries > 0) {
  74.     if (creat_fd == -1) {
  75.         creat_fd = creat (creat_name, 0666);
  76.         if (creat_fd == -1) {
  77.         if (errno != EACCES)
  78.             return LOCK_ERROR;
  79.         } else
  80.         (void) close (creat_fd);
  81.     }
  82.     if (creat_fd != -1) {
  83.         if (link (creat_name, link_name) != -1)
  84.         return LOCK_SUCCESS;
  85.         if (errno != EEXIST)
  86.         return LOCK_ERROR;
  87.     }
  88.     (void) sleep ((unsigned) timeout);
  89.     --retries;
  90.     }
  91.     return LOCK_TIMEOUT;
  92. }
  93.