FSListCats.c revision da1f2d5d
1/*
2 * Copyright 1990 Network Computing Devices;
3 * Portions Copyright 1987 by Digital Equipment Corporation
4 *
5 * Permission to use, copy, modify, distribute, and sell this software
6 * and its documentation for any purpose is hereby granted without fee,
7 * provided that the above copyright notice appear in all copies and
8 * that both that copyright notice and this permission notice appear
9 * in supporting documentation, and that the names of Network Computing
10 * Devices or Digital not be used in advertising or publicity pertaining
11 * to distribution of the software without specific, written prior
12 * permission. Network Computing Devices or Digital make no representations
13 * about the suitability of this software for any purpose.  It is provided
14 * "as is" without express or implied warranty.
15 *
16 * NETWORK COMPUTING DEVICES AND  DIGITAL DISCLAIM ALL WARRANTIES WITH
17 * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL NETWORK COMPUTING DEVICES
19 * OR DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES
20 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
21 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
22 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
23 * SOFTWARE.
24 */
25
26/*
27
28Copyright 1987, 1998  The Open Group
29
30Permission to use, copy, modify, distribute, and sell this software and its
31documentation for any purpose is hereby granted without fee, provided that
32the above copyright notice appear in all copies and that both that
33copyright notice and this permission notice appear in supporting
34documentation.
35
36The above copyright notice and this permission notice shall be included in
37all copies or substantial portions of the Software.
38
39THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
40IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
41FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
42OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
43AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
44CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
45
46Except as contained in this notice, the name of The Open Group shall not be
47used in advertising or otherwise to promote the sale, use or other dealings
48in this Software without prior written authorization from The Open Group.
49
50*/
51
52#ifdef HAVE_CONFIG_H
53#include <config.h>
54#endif
55#include	"FSlibint.h"
56
57char      **
58FSListCatalogues(
59    FSServer	*svr,
60    const char	*pattern,
61    int		 maxNames,
62    int		*actualCount)
63{
64    int         length;
65    char      **clist;
66    char       *c;
67    fsListCataloguesReply rep;
68    fsListCataloguesReq *req;
69    unsigned long rlen;
70
71    GetReq(ListCatalogues, req);
72    req->maxNames = (CARD32) maxNames;
73    req->nbytes = 0;
74    if (pattern != NULL) {
75        size_t nbytes;
76
77#ifdef HAVE_STRNLEN
78        nbytes = strnlen(pattern, FSMaxRequestBytes(svr));
79#else
80        nbytes = strlen(pattern);
81#endif
82
83        if (nbytes <= (FSMaxRequestBytes(svr) - SIZEOF(fsListCataloguesReq))) {
84            req->nbytes = (CARD16) nbytes;
85            req->length += (CARD16) ((nbytes + 3) >> 2);
86            _FSSend(svr, pattern, (long) nbytes);
87        }
88    }
89
90    if (!_FSReply(svr, (fsReply *) & rep,
91    (SIZEOF(fsListCataloguesReply) - SIZEOF(fsGenericReply)) >> 2, fsFalse))
92	return (char **) NULL;
93
94    if (rep.num_catalogues
95#if (SIZE_MAX >> 2) <= UINT_MAX
96	&& rep.num_catalogues <= SIZE_MAX/sizeof(char *)
97	&& rep.length <= (SIZE_MAX>>2)
98#endif
99	) {
100	clist = FSmallocarray(rep.num_catalogues, sizeof(char *));
101	rlen = (rep.length << 2) - SIZEOF(fsListCataloguesReply);
102	c = FSmalloc(rlen + 1);
103
104	if ((!clist) || (!c)) {
105	    if (clist)
106		FSfree(clist);
107	    if (c)
108		FSfree(c);
109	    _FSEatData(svr, rlen);
110	    SyncHandle();
111	    return (char **) NULL;
112	}
113	_FSReadPad(svr, c, (long) rlen);
114	/* unpack */
115	length = *c;
116	for (CARD32 i = 0; i < rep.num_catalogues; i++) {
117	    clist[i] = c + 1;
118	    c += length + 1;
119	    length = *c;
120	    *c = '\0';
121	}
122    } else {
123
124	clist = (char **) NULL;
125    }
126
127    *actualCount = rep.num_catalogues;
128    SyncHandle();
129    return clist;
130
131}
132
133int FSFreeCatalogues(char **list)
134{
135    if (list) {
136	FSfree(list[0] - 1);
137	FSfree(list);
138    }
139    return 1;
140}
141