FontNames.c revision f2d49d05
1/* 2 3Copyright 1986, 1998 The Open Group 4 5Permission to use, copy, modify, distribute, and sell this software and its 6documentation for any purpose is hereby granted without fee, provided that 7the above copyright notice appear in all copies and that both that 8copyright notice and this permission notice appear in supporting 9documentation. 10 11The above copyright notice and this permission notice shall be included in 12all copies or substantial portions of the Software. 13 14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 18AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 21Except as contained in this notice, the name of The Open Group shall not be 22used in advertising or otherwise to promote the sale, use or other dealings 23in this Software without prior written authorization from The Open Group. 24 25*/ 26 27 28#ifdef HAVE_CONFIG_H 29#include <config.h> 30#endif 31#include "Xlibint.h" 32#include "reallocarray.h" 33#include <limits.h> 34 35char ** 36XListFonts( 37register Display *dpy, 38_Xconst char *pattern, /* null-terminated */ 39int maxNames, 40int *actualCount) /* RETURN */ 41{ 42 register long nbytes; 43 register unsigned i; 44 register int length; 45 char **flist = NULL; 46 char *ch = NULL; 47 char *chstart; 48 char *chend; 49 int count = 0; 50 xListFontsReply rep; 51 register xListFontsReq *req; 52 unsigned long rlen = 0; 53 54 LockDisplay(dpy); 55 GetReq(ListFonts, req); 56 req->maxNames = maxNames; 57 nbytes = req->nbytes = pattern ? strlen (pattern) : 0; 58 req->length += (nbytes + 3) >> 2; 59 _XSend (dpy, pattern, nbytes); 60 /* use _XSend instead of Data, since following _XReply will flush buffer */ 61 62 if (!_XReply (dpy, (xReply *)&rep, 0, xFalse)) { 63 *actualCount = 0; 64 UnlockDisplay(dpy); 65 SyncHandle(); 66 return (char **) NULL; 67 } 68 69 if (rep.nFonts) { 70 flist = Xmallocarray (rep.nFonts, sizeof(char *)); 71 if (rep.length > 0 && rep.length < (INT_MAX >> 2)) { 72 rlen = rep.length << 2; 73 ch = Xmalloc(rlen + 1); 74 /* +1 to leave room for last null-terminator */ 75 } 76 77 if ((! flist) || (! ch)) { 78 Xfree(flist); 79 Xfree(ch); 80 _XEatDataWords(dpy, rep.length); 81 *actualCount = 0; 82 UnlockDisplay(dpy); 83 SyncHandle(); 84 return (char **) NULL; 85 } 86 87 _XReadPad (dpy, ch, rlen); 88 /* 89 * unpack into null terminated strings. 90 */ 91 chstart = ch; 92 chend = ch + rlen; 93 length = *(unsigned char *)ch; 94 *ch = 1; /* make sure it is non-zero for XFreeFontNames */ 95 for (i = 0; i < rep.nFonts; i++) { 96 if (ch + length < chend) { 97 flist[i] = ch + 1; /* skip over length */ 98 ch += length + 1; /* find next length ... */ 99 length = *(unsigned char *)ch; 100 *ch = '\0'; /* and replace with null-termination */ 101 count++; 102 } else { 103 Xfree(chstart); 104 Xfree(flist); 105 flist = NULL; 106 count = 0; 107 break; 108 } 109 } 110 } 111 *actualCount = count; 112 UnlockDisplay(dpy); 113 SyncHandle(); 114 return (flist); 115} 116 117int 118XFreeFontNames(char **list) 119{ 120 if (list) { 121 if (!*(list[0]-1)) { /* from ListFontsWithInfo */ 122 register char **names; 123 for (names = list+1; *names; names++) 124 Xfree (*names); 125 } 126 Xfree (list[0]-1); 127 Xfree (list); 128 } 129 return 1; 130} 131