Fix warnings on FreeBSD
[oweals/cde.git] / cde / lib / DtSvc / DtUtil2 / Utility.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these libraries and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /*
24  * File:         Utility.c $XConsortium: Utility.c /main/5 1996/06/21 17:20:20 ageorge $
25  * Language:     C
26  *
27  * (c) Copyright 1988, Hewlett-Packard Company, all rights reserved.
28  *
29  * (c) Copyright 1993, 1994 Hewlett-Packard Company                     *
30  * (c) Copyright 1993, 1994 International Business Machines Corp.       *
31  * (c) Copyright 1993, 1994 Sun Microsystems, Inc.                      *
32  * (c) Copyright 1993, 1994 Novell, Inc.                                *
33  */
34
35 #include <stdio.h>
36 #include <sys/types.h>
37
38 #ifdef __hpux
39 #include <ndir.h>
40 #else
41 #if defined(sun) || defined(CSRG_BASED)
42 #include <dirent.h>
43 #else
44 #include <sys/dir.h>
45 #endif
46 #endif
47
48 #include <ctype.h>
49 #ifdef NLS16
50 #include <limits.h>
51 #endif
52 #include <sys/stat.h>
53 #include <sys/param.h>          /* MAXPATHLEN, MAXHOSTNAMELEN */
54 #include <X11/Xlib.h>
55 #include <X11/Intrinsic.h>
56 #include <X11/StringDefs.h>
57 #include <Dt/DtP.h>
58 #include <Dt/Connect.h>
59 #include <Dt/FileUtil.h>
60 #include <Dt/DtNlUtils.h>
61 #include <Dt/Utility.h>
62 #include <Dt/UtilityP.h>
63 #include "DtSvcLock.h"
64
65 #include <string.h>
66
67 #define TRUE            1
68 #define FALSE           0
69
70
71 /********    Static Function Declarations    ********/
72
73 static char * RemapSpecialDisplayName(
74                         char *dispInfo) ;
75
76 /********    End Static Function Declarations    ********/
77
78
79 /******************
80  *
81  * Function Name:  _DtVectorizeInPlace
82  *
83  * Description:
84  *
85  *      A "string vector" is an array of pointers to strings.  The
86  *      end of the array is marked with a null pointer.  This function
87  *      takes a long string and creates a string vector from it by
88  *      allocating the array of pointers, breaking the string up into
89  *      sub-strings (based on a specified separator character), and
90  *      making the array elements point to the sub-strings;
91  *
92  *      NOTE that it does this "in place".  If you don't want the original
93  *      string disturbed, be sure to make a copy of it before calling
94  *      this function.
95  *
96  *      The memory for the array of pointers is owned by the caller.
97  *      It can be safely freed (though this doesn't free the sub-strings).
98  *
99  * Synopsis:
100  *
101  *      svector = _DtVectorizeInPlace (string, separator);
102  *
103  *      char **svector;         The string vector that was created.  The
104  *                              memory is owned by the caller.
105  *      char *string;           A pointer to the string to be vectorized.
106  *                              THIS STRING WILL BE ALTERED.
107  *      char separtor;          The separator character which marks the
108  *                              ends of the sub-strings.
109  *
110  ******************/
111
112 char * * 
113 _DtVectorizeInPlace(
114         char *string,
115         char separator )
116 {
117 /* LOCAL VARIABLES */
118    
119    char **vector, **next_string, *nextc;
120    int num_pieces;
121    
122 /* CODE */
123    
124    /* Count the elements in the string and allocate an appropriate size 
125       vector.  There is one more element than separator characters. */
126    num_pieces = 1;
127    nextc = string;
128    while ((nextc = DtStrchr(nextc, separator)))
129    {
130       num_pieces++;
131       nextc++;
132    }
133    
134    vector = (char **) XtMalloc ((Cardinal)(sizeof(char *) * (num_pieces + 1)));
135    
136    /* Set the first element of the vector to point to the start of
137       the string. */
138    *vector = string;
139    next_string = vector + 1;
140    nextc = string;
141
142    /* Parse out each component, terminating it with a NULL */
143    while ((nextc = DtStrchr(nextc, separator)))
144    {
145       *nextc = '\0';
146       nextc++;
147       *next_string = nextc;
148       next_string++;
149    }
150         
151    /* The last pointer in the vector must be set to NULL. */
152    *next_string = NULL;
153    return (vector);
154 }
155
156 /******************
157  *
158  * Function Name:  _DtFreeStringVector
159  *
160  * Description:
161  *
162  *      A "string vector" is an array of pointers to strings.  
163  *
164  * Synopsis:
165  *
166  *      _DtFreeStringVector (stringv);
167  *
168  *      char **stringv;         The string vector which is freed.
169  *
170  * NOTE:  this function assumes that "stringv" was created by
171  *       "_DtVectorizeInPlace".
172  *
173  ******************/
174
175 void 
176 _DtFreeStringVector(
177         char **stringv )
178 {
179
180 /* CODE */
181    
182    if (stringv)
183    {
184       if (stringv[0]) 
185          XtFree ((char *)stringv[0]);
186
187       XtFree ((char *)stringv);
188    }
189 }
190
191
192 /*
193  * Functions for mapping a display pointer into a display name.  It
194  * special cases 'local:' and 'unix:', and maps these into the real
195  * host name.
196  */
197
198 static char *
199 RemapSpecialDisplayName(
200         char *dispInfo )
201 {
202    static char * localHost = NULL;
203    char * name;
204
205    _DtSvcProcessLock();
206    if (localHost == NULL)
207    {
208       localHost = XtMalloc((Cardinal)30);
209       DtGetShortHostname(localHost, 30);
210    }
211    _DtSvcProcessUnlock();
212
213    name = XtMalloc((Cardinal)(strlen(localHost) + strlen(dispInfo) + 1));
214    (void)strcpy(name, localHost);
215    (void)strcat(name, dispInfo);
216    return(name);
217 }
218
219 /*
220  * Move here from DragUtil.c because the function is useful and DragUtil.c
221  * is going away.
222  */
223 char *
224 _DtGetDisplayName(
225         Display *display )
226 {
227    char * name = DisplayString(display);
228
229    if (strncmp("unix:", name, 5) == 0)
230       return(RemapSpecialDisplayName(name+4));
231    else if (strncmp("local:", name, 6) == 0)
232       return(RemapSpecialDisplayName(name+5));
233    else if (strncmp(":", name, 1) == 0)
234       return(RemapSpecialDisplayName(name));
235
236    return(XtNewString(name));
237 }
238