home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / lib / Xau / AuGetAddr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-08  |  3.1 KB  |  119 lines

  1. /*
  2.  * Xau - X Authorization Database Library
  3.  *
  4.  * $XConsortium: AuGetAddr.c,v 1.11 91/01/08 15:09:05 gildea 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 <X11/Xos.h>
  23.  
  24. static
  25. binaryEqual (a, b, len)
  26. register char    *a, *b;
  27. register int    len;
  28. {
  29.     while (len--)
  30.     if (*a++ != *b++)
  31.         return 0;
  32.     return 1;
  33. }
  34.  
  35. #if NeedFunctionPrototypes
  36. Xauth *
  37. XauGetAuthByAddr (
  38. #if NeedWidePrototypes
  39. unsigned int    family,
  40. unsigned int    address_length,
  41. #else
  42. unsigned short    family,
  43. unsigned short    address_length,
  44. #endif
  45. _Xconst char*    address,
  46. #if NeedWidePrototypes
  47. unsigned int    number_length,
  48. #else
  49. unsigned short    number_length,
  50. #endif
  51. _Xconst char*    number,
  52. #if NeedWidePrototypes
  53. unsigned int    name_length,
  54. #else
  55. unsigned short    name_length,
  56. #endif
  57. _Xconst char*    name)
  58. #else
  59. Xauth *
  60. XauGetAuthByAddr (family, address_length, address,
  61.               number_length, number,
  62.               name_length, name)
  63. unsigned short    family;
  64. unsigned short    address_length;
  65. char    *address;
  66. unsigned short    number_length;
  67. char    *number;
  68. unsigned short    name_length;
  69. char    *name;
  70. #endif
  71. {
  72.     FILE    *auth_file;
  73.     char    *auth_name;
  74.     Xauth   *entry;
  75.  
  76.     auth_name = XauFileName ();
  77.     if (!auth_name)
  78.     return 0;
  79.     if (access (auth_name, R_OK) != 0)        /* checks REAL id */
  80.     return 0;
  81.     auth_file = fopen (auth_name, "r");
  82.     if (!auth_file)
  83.     return 0;
  84.     for (;;) {
  85.     entry = XauReadAuth (auth_file);
  86.     if (!entry)
  87.         break;
  88.     /*
  89.      * Match when:
  90.      *   either family or entry->family are FamilyWild or
  91.      *    family and entry->family are the same
  92.      *  and
  93.      *   either address or entry->address are empty or
  94.      *    address and entry->address are the same
  95.      *  and
  96.      *   either number or entry->number are empty or
  97.      *    number and entry->number are the same
  98.      *  and
  99.      *   either name or entry->name are empty or
  100.      *    name and entry->name are the same
  101.      */
  102.  
  103.     if ((family == FamilyWild || entry->family == FamilyWild ||
  104.          (entry->family == family &&
  105.           address_length == entry->address_length &&
  106.           binaryEqual (entry->address, address, (int)address_length))) &&
  107.         (number_length == 0 || entry->number_length == 0 ||
  108.          (number_length == entry->number_length &&
  109.           binaryEqual (entry->number, number, (int)number_length))) &&
  110.         (name_length == 0 || entry->name_length == 0 ||
  111.          (entry->name_length == name_length &&
  112.            binaryEqual (entry->name, name, (int)name_length))))
  113.         break;
  114.     XauDisposeAuth (entry);
  115.     }
  116.     (void) fclose (auth_file);
  117.     return entry;
  118. }
  119.