1/* $XConsortium: AuWrite.c,v 1.6 94/04/17 20:15:45 gildea Exp $ */
2
3/*
4
5Copyright (c) 1988  X Consortium
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in
15all copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
20X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
24Except as contained in this notice, the name of the X Consortium shall not be
25used in advertising or otherwise to promote the sale, use or other dealings
26in this Software without prior written authorization from the X Consortium.
27
28*/
29
30#ifdef HAVE_CONFIG_H
31#include <config.h>
32RCSID("$Id$");
33#endif
34
35#include <X11/Xauth.h>
36
37static int
38write_short (unsigned short s, FILE *file)
39{
40    unsigned char   file_short[2];
41
42    file_short[0] = (s & (unsigned)0xff00) >> 8;
43    file_short[1] = s & 0xff;
44    if (fwrite (file_short, sizeof (file_short), 1, file) != 1)
45	return 0;
46    return 1;
47}
48
49static int
50write_counted_string (unsigned short count, char *string, FILE *file)
51{
52    if (write_short (count, file) == 0)
53	return 0;
54    if (fwrite (string, (int) sizeof (char), (int) count, file) != count)
55	return 0;
56    return 1;
57}
58
59int
60XauWriteAuth (FILE *auth_file, Xauth *auth)
61{
62    if (write_short (auth->family, auth_file) == 0)
63	return 0;
64    if (write_counted_string (auth->address_length, auth->address, auth_file) == 0)
65	return 0;
66    if (write_counted_string (auth->number_length, auth->number, auth_file) == 0)
67	return 0;
68    if (write_counted_string (auth->name_length, auth->name, auth_file) == 0)
69	return 0;
70    if (write_counted_string (auth->data_length, auth->data, auth_file) == 0)
71	return 0;
72    return 1;
73}
74