1 /*
2  * Copyright 1993 Network Computing Devices, Inc.
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation, and that the name Network Computing Devices, Inc. not be
9  * used in advertising or publicity pertaining to distribution of this
10  * software without specific, written prior permission.
11  *
12  * THIS SOFTWARE IS PROVIDED 'AS-IS'.  NETWORK COMPUTING DEVICES, INC.,
13  * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT
14  * LIMITATION ALL IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
15  * PARTICULAR PURPOSE, OR NONINFRINGEMENT.  IN NO EVENT SHALL NETWORK
16  * COMPUTING DEVICES, INC., BE LIABLE FOR ANY DAMAGES WHATSOEVER, INCLUDING
17  * SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES, INCLUDING LOSS OF USE, DATA,
18  * OR PROFITS, EVEN IF ADVISED OF THE POSSIBILITY THEREOF, AND REGARDLESS OF
19  * WHETHER IN AN ACTION IN CONTRACT, TORT OR NEGLIGENCE, ARISING OUT OF OR IN
20  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  *
22  * $NCDId: @(#)GetDevAttr.c,v 1.6 1994/04/07 20:41:53 greg Exp $
23  */
24 
25 #include "Alibint.h"
26 
27 AuDeviceAttributes *
AuGetDeviceAttributes(AuServer * aud,AuDeviceID device,AuStatus * ret_status)28 AuGetDeviceAttributes(
29                       AuServer       *aud,
30                       AuDeviceID      device,
31                       AuStatus       *ret_status
32                       )
33 {
34     auResourceReq *req;
35     auGetDeviceAttributesReply rep;
36     auDeviceAttributes a;
37     AuDeviceAttributes *attr;
38 
39     if (ret_status)
40 	*ret_status = AuSuccess;
41 
42     _AuLockServer();
43     _AuGetResReq(GetDeviceAttributes, device, req, aud);
44 
45     (void) _AuReply(aud, (auReply *) & rep, 0, auFalse, ret_status);
46 
47     _AuReadPad(aud, (char *) &a, SIZEOF(auDeviceAttributes));
48 
49     if (!(attr = (AuDeviceAttributes *)
50 	  Aucalloc(1, sizeof(AuDeviceAttributes))))
51     {
52 	_AuUnlockServer();
53 	_AuSyncHandle(aud);
54 	return NULL;
55     }
56 
57     _xferDeviceAttributes(&a, *attr);
58 
59     if ((AuDeviceValueMask(attr) & AuCompCommonDescriptionMask) &&
60 	AuDeviceDescription(attr)->len)
61     {
62 	if (!(AuDeviceDescription(attr)->data = (char *)
63 	      Aumalloc(AuDeviceDescription(attr)->len + 1)))
64 	{
65 	    AuFreeDeviceAttributes(aud, 1, attr);
66 	    _AuUnlockServer();
67 	    _AuSyncHandle(aud);
68 	    return NULL;
69 	}
70 
71 	_AuReadPad(aud, AuDeviceDescription(attr)->data,
72 		   AuDeviceDescription(attr)->len);
73 
74 	AuDeviceDescription(attr)->data[AuDeviceDescription(attr)->len] = 0;
75     }
76 
77     if ((AuDeviceValueMask(attr) & AuCompDeviceChildrenMask) &&
78 	AuDeviceNumChildren(attr))
79     {
80 	if (!(AuDeviceChildren(attr) = (AuDeviceID *)
81 	      Aumalloc(AuDeviceNumChildren(attr) * sizeof(AuDeviceID))))
82 	{
83 	    AuFreeDeviceAttributes(aud, 1, attr);
84 	    _AuUnlockServer();
85 	    _AuSyncHandle(aud);
86 	    return NULL;
87 	}
88 
89 	_AuReadPad(aud, (char *) AuDeviceChildren(attr),
90 		   AuDeviceNumChildren(attr) * sizeof(AuDeviceID));
91     }
92 
93     _AuUnlockServer();
94     _AuSyncHandle(aud);
95 
96     return attr;
97 }
98 
99 /* ARGSUSED */
100 void
AuFreeDeviceAttributes(AuServer * aud,int num,AuDeviceAttributes * attr)101 AuFreeDeviceAttributes(
102                        AuServer       *aud,
103                        int             num,
104                        AuDeviceAttributes *attr
105                        )
106 {
107     AuDeviceAttributes *p = attr;
108 
109     while (num--)
110     {
111 	if (AuDeviceDescription(p)->data)
112 	    Aufree(AuDeviceDescription(p)->data);
113 
114 	if (AuDeviceChildren(p))
115 	    Aufree(AuDeviceChildren(p));
116 
117 	p++;
118     }
119 
120     Aufree(attr);
121 }
122