upload tizen2.0 source
[framework/uifw/xorg/lib/libxau.git] / AuGetAddr.c
1 /*
2
3 Copyright 1988, 1998  The Open Group
4
5 Permission to use, copy, modify, distribute, and sell this software and its
6 documentation for any purpose is hereby granted without fee, provided that
7 the above copyright notice appear in all copies and that both that
8 copyright notice and this permission notice appear in supporting
9 documentation.
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21 Except as contained in this notice, the name of The Open Group shall not be
22 used in advertising or otherwise to promote the sale, use or other dealings
23 in this Software without prior written authorization from The Open Group.
24
25 */
26
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30 #include <X11/Xauth.h>
31 #include <X11/Xos.h>
32
33 static int
34 binaryEqual (_Xconst char *a, _Xconst char *b, int len)
35 {
36     while (len--)
37         if (*a++ != *b++)
38             return 0;
39     return 1;
40 }
41
42 Xauth *
43 XauGetAuthByAddr (
44 #if NeedWidePrototypes
45 unsigned int    family,
46 unsigned int    address_length,
47 #else
48 unsigned short  family,
49 unsigned short  address_length,
50 #endif
51 _Xconst char*   address,
52 #if NeedWidePrototypes
53 unsigned int    number_length,
54 #else
55 unsigned short  number_length,
56 #endif
57 _Xconst char*   number,
58 #if NeedWidePrototypes
59 unsigned int    name_length,
60 #else
61 unsigned short  name_length,
62 #endif
63 _Xconst char*   name)
64 {
65     FILE    *auth_file;
66     char    *auth_name;
67     Xauth   *entry;
68
69     auth_name = XauFileName ();
70     if (!auth_name)
71         return NULL;
72     if (access (auth_name, R_OK) != 0)          /* checks REAL id */
73         return NULL;
74     auth_file = fopen (auth_name, "rb");
75     if (!auth_file)
76         return NULL;
77     for (;;) {
78         entry = XauReadAuth (auth_file);
79         if (!entry)
80             break;
81         /*
82          * Match when:
83          *   either family or entry->family are FamilyWild or
84          *    family and entry->family are the same and
85          *     address and entry->address are the same
86          *  and
87          *   either number or entry->number are empty or
88          *    number and entry->number are the same
89          *  and
90          *   either name or entry->name are empty or
91          *    name and entry->name are the same
92          */
93
94         if ((family == FamilyWild || entry->family == FamilyWild ||
95              (entry->family == family &&
96               address_length == entry->address_length &&
97               binaryEqual (entry->address, address, (int)address_length))) &&
98             (number_length == 0 || entry->number_length == 0 ||
99              (number_length == entry->number_length &&
100               binaryEqual (entry->number, number, (int)number_length))) &&
101             (name_length == 0 || entry->name_length == 0 ||
102              (entry->name_length == name_length &&
103               binaryEqual (entry->name, name, (int)name_length))))
104             break;
105         XauDisposeAuth (entry);
106     }
107     (void) fclose (auth_file);
108     return entry;
109 }