Utility.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. #include <stdio.h>
  35. #include <sys/types.h>
  36. #if defined(sun) || defined(CSRG_BASED)
  37. #include <dirent.h>
  38. #else
  39. #include <sys/dir.h>
  40. #endif
  41. #include <ctype.h>
  42. #ifdef NLS16
  43. #include <limits.h>
  44. #endif
  45. #include <sys/stat.h>
  46. #include <sys/param.h> /* MAXPATHLEN, MAXHOSTNAMELEN */
  47. #include <X11/Xlib.h>
  48. #include <X11/Intrinsic.h>
  49. #include <X11/StringDefs.h>
  50. #include <Dt/DtP.h>
  51. #include <Dt/Connect.h>
  52. #include <Dt/FileUtil.h>
  53. #include <Dt/DtNlUtils.h>
  54. #include <Dt/Utility.h>
  55. #include <Dt/UtilityP.h>
  56. #include "DtSvcLock.h"
  57. #include <string.h>
  58. #define TRUE 1
  59. #define FALSE 0
  60. /******** Static Function Declarations ********/
  61. static char * RemapSpecialDisplayName(
  62. char *dispInfo) ;
  63. /******** End Static Function Declarations ********/
  64. /******************
  65. *
  66. * Function Name: _DtVectorizeInPlace
  67. *
  68. * Description:
  69. *
  70. * A "string vector" is an array of pointers to strings. The
  71. * end of the array is marked with a null pointer. This function
  72. * takes a long string and creates a string vector from it by
  73. * allocating the array of pointers, breaking the string up into
  74. * sub-strings (based on a specified separator character), and
  75. * making the array elements point to the sub-strings;
  76. *
  77. * NOTE that it does this "in place". If you don't want the original
  78. * string disturbed, be sure to make a copy of it before calling
  79. * this function.
  80. *
  81. * The memory for the array of pointers is owned by the caller.
  82. * It can be safely freed (though this doesn't free the sub-strings).
  83. *
  84. * Synopsis:
  85. *
  86. * svector = _DtVectorizeInPlace (string, separator);
  87. *
  88. * char **svector; The string vector that was created. The
  89. * memory is owned by the caller.
  90. * char *string; A pointer to the string to be vectorized.
  91. * THIS STRING WILL BE ALTERED.
  92. * char separtor; The separator character which marks the
  93. * ends of the sub-strings.
  94. *
  95. ******************/
  96. char * *
  97. _DtVectorizeInPlace(
  98. char *string,
  99. char separator )
  100. {
  101. /* LOCAL VARIABLES */
  102. char **vector, **next_string, *nextc;
  103. int num_pieces;
  104. /* CODE */
  105. /* Count the elements in the string and allocate an appropriate size
  106. vector. There is one more element than separator characters. */
  107. num_pieces = 1;
  108. nextc = string;
  109. while ((nextc = DtStrchr(nextc, separator)))
  110. {
  111. num_pieces++;
  112. nextc++;
  113. }
  114. vector = (char **) XtMalloc ((Cardinal)(sizeof(char *) * (num_pieces + 1)));
  115. /* Set the first element of the vector to point to the start of
  116. the string. */
  117. *vector = string;
  118. next_string = vector + 1;
  119. nextc = string;
  120. /* Parse out each component, terminating it with a NULL */
  121. while ((nextc = DtStrchr(nextc, separator)))
  122. {
  123. *nextc = '\0';
  124. nextc++;
  125. *next_string = nextc;
  126. next_string++;
  127. }
  128. /* The last pointer in the vector must be set to NULL. */
  129. *next_string = NULL;
  130. return (vector);
  131. }
  132. /******************
  133. *
  134. * Function Name: _DtFreeStringVector
  135. *
  136. * Description:
  137. *
  138. * A "string vector" is an array of pointers to strings.
  139. *
  140. * Synopsis:
  141. *
  142. * _DtFreeStringVector (stringv);
  143. *
  144. * char **stringv; The string vector which is freed.
  145. *
  146. * NOTE: this function assumes that "stringv" was created by
  147. * "_DtVectorizeInPlace".
  148. *
  149. ******************/
  150. void
  151. _DtFreeStringVector(
  152. char **stringv )
  153. {
  154. /* CODE */
  155. if (stringv)
  156. {
  157. if (stringv[0])
  158. XtFree ((char *)stringv[0]);
  159. XtFree ((char *)stringv);
  160. }
  161. }
  162. /*
  163. * Functions for mapping a display pointer into a display name. It
  164. * special cases 'local:' and 'unix:', and maps these into the real
  165. * host name.
  166. */
  167. static char *
  168. RemapSpecialDisplayName(
  169. char *dispInfo )
  170. {
  171. static char * localHost = NULL;
  172. char * name;
  173. _DtSvcProcessLock();
  174. if (localHost == NULL)
  175. {
  176. localHost = XtMalloc((Cardinal)30);
  177. DtGetShortHostname(localHost, 30);
  178. }
  179. _DtSvcProcessUnlock();
  180. name = XtMalloc((Cardinal)(strlen(localHost) + strlen(dispInfo) + 1));
  181. (void)strcpy(name, localHost);
  182. (void)strcat(name, dispInfo);
  183. return(name);
  184. }
  185. /*
  186. * Move here from DragUtil.c because the function is useful and DragUtil.c
  187. * is going away.
  188. */
  189. char *
  190. _DtGetDisplayName(
  191. Display *display )
  192. {
  193. char * name = DisplayString(display);
  194. if (strncmp("unix:", name, 5) == 0)
  195. return(RemapSpecialDisplayName(name+4));
  196. else if (strncmp("local:", name, 6) == 0)
  197. return(RemapSpecialDisplayName(name+5));
  198. else if (strncmp(":", name, 1) == 0)
  199. return(RemapSpecialDisplayName(name));
  200. return(XtNewString(name));
  201. }