FSFtNames.c revision fe4c343a
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, 1994, 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 ** 58FSListFonts( 59 FSServer *svr, 60 const char *pattern, 61 int maxNames, 62 int *actualCount) 63{ 64 unsigned int i, 65 length; 66 char **flist; 67 char *c; 68 fsListFontsReply rep; 69 fsListFontsReq *req; 70 unsigned long rlen; 71 72 GetReq(ListFonts, req); 73 req->maxNames = (CARD32) maxNames; 74 req->nbytes = 0; 75 if (pattern != NULL) { 76 size_t nbytes; 77 78#ifdef HAVE_STRNLEN 79 nbytes = strnlen(pattern, FSMaxRequestBytes(svr)); 80#else 81 nbytes = strlen(pattern); 82#endif 83 84 if (nbytes <= (FSMaxRequestBytes(svr) - SIZEOF(fsListFontsReq))) { 85 req->nbytes = (CARD16) nbytes; 86 req->length += (CARD16) ((nbytes + 3) >> 2); 87 _FSSend(svr, pattern, (long) nbytes); 88 } 89 } 90 91 if (!_FSReply(svr, (fsReply *) & rep, 92 (SIZEOF(fsListFontsReply) - SIZEOF(fsGenericReply)) >> 2, fsFalse)) 93 return (char **) NULL; 94 95 if (rep.nFonts 96#if (SIZE_MAX >> 2) <= UINT_MAX 97 && rep.nFonts <= SIZE_MAX / sizeof(char *) 98 && rep.length <= (SIZE_MAX >> 2) 99#endif 100 ) { 101 flist = FSmalloc(rep.nFonts * sizeof(char *)); 102 rlen = (rep.length << 2) - SIZEOF(fsListFontsReply); 103 c = FSmalloc(rlen + 1); 104 105 if ((!flist) || (!c)) { 106 if (flist) 107 FSfree(flist); 108 if (c) 109 FSfree(c); 110 _FSEatData(svr, rlen); 111 SyncHandle(); 112 return (char **) NULL; 113 } 114 _FSReadPad(svr, c, (long) rlen); 115 /* unpack */ 116 length = *(unsigned char *)c; 117 for (i = 0; i < rep.nFonts; i++) { 118 flist[i] = c + 1; 119 c += length + 1; 120 length = *(unsigned char *)c; 121 *c = '\0'; 122 } 123 } else { 124 125 flist = (char **) NULL; 126 } 127 128 *actualCount = rep.nFonts; 129 SyncHandle(); 130 return flist; 131 132} 133 134int FSFreeFontNames(char **list) 135{ 136 if (list) { 137 FSfree(list[0] - 1); 138 FSfree(list); 139 } 140 return 1; 141} 142