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

  1. /*
  2.  * Xau - X Authorization Database Library
  3.  *
  4.  * $XConsortium: AuGetBest.c,v 1.4 91/01/08 15:09:20 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. XauGetBestAuthByAddr (
  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.     int            types_length,
  53.     char**        types,
  54.     _Xconst int*    type_lengths)
  55. #else
  56. Xauth *
  57. XauGetBestAuthByAddr (family, address_length, address,
  58.               number_length, number,
  59.               types_length, types, type_lengths)
  60.     unsigned short    family;
  61.     unsigned short    address_length;
  62.     char        *address;
  63.     unsigned short    number_length;
  64.     char        *number;
  65.     int            types_length;
  66.     char        **types;
  67.     int            *type_lengths;
  68. #endif
  69. {
  70.     FILE    *auth_file;
  71.     char    *auth_name;
  72.     Xauth   *entry;
  73.     Xauth   *best;
  74.     int        best_type;
  75.     int        type;
  76.  
  77.     auth_name = XauFileName ();
  78.     if (!auth_name)
  79.     return 0;
  80.     if (access (auth_name, R_OK) != 0)        /* checks REAL id */
  81.     return 0;
  82.     auth_file = fopen (auth_name, "r");
  83.     if (!auth_file)
  84.     return 0;
  85.     best = 0;
  86.     best_type = types_length;
  87.     for (;;) {
  88.     entry = XauReadAuth (auth_file);
  89.     if (!entry)
  90.         break;
  91.     /*
  92.      * Match when:
  93.      *   either family or entry->family are FamilyWild or
  94.      *    family and entry->family are the same
  95.      *  and
  96.      *   either address or entry->address are empty or
  97.      *    address and entry->address are the same
  98.      *  and
  99.      *   either number or entry->number are empty or
  100.      *    number and entry->number are the same
  101.      *  and
  102.      *   name matches one of the specified names, or no names
  103.      *    were specified
  104.      */
  105.  
  106.     if ((family == FamilyWild || entry->family == FamilyWild ||
  107.          (entry->family == family &&
  108.           address_length == entry->address_length &&
  109.           binaryEqual (entry->address, address, (int)address_length))) &&
  110.         (number_length == 0 || entry->number_length == 0 ||
  111.          (number_length == entry->number_length &&
  112.           binaryEqual (entry->number, number, (int)number_length))))
  113.     {
  114.         if (best_type == 0)
  115.         {
  116.         best = entry;
  117.         break;
  118.         }
  119.         for (type = 0; type < best_type; type++)
  120.         if (type_lengths[type] == entry->name_length &&
  121.             !(strncmp (types[type], entry->name, entry->name_length)))
  122.         {
  123.             break;
  124.         }
  125.         if (type < best_type)
  126.         {
  127.         if (best)
  128.             XauDisposeAuth (best);
  129.         best = entry;
  130.         best_type = type;
  131.         if (type == 0)
  132.             break;
  133.         continue;
  134.         }
  135.     }
  136.     XauDisposeAuth (entry);
  137.     }
  138.     (void) fclose (auth_file);
  139.     return best;
  140. }
  141.