SmXdef.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. /* $XConsortium: SmXdef.c /main/4 1995/10/30 09:39:16 rswiston $ */
  24. /* *
  25. * (c) Copyright 1993, 1994 Hewlett-Packard Company *
  26. * (c) Copyright 1993, 1994 International Business Machines Corp. *
  27. * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
  28. * (c) Copyright 1993, 1994 Novell, Inc. *
  29. */
  30. /*************************************<+>*************************************
  31. *****************************************************************************
  32. **
  33. ** File: SmXdef.c
  34. **
  35. ** Project: DT Session Manager (dtsession)
  36. **
  37. ** Description:
  38. ** -----------
  39. ** This file contains routines to manage the Xdefaults file.
  40. **
  41. ** SmXdefMerge() - merge .Xdefaults file into RESOURCE_MANAGER
  42. ** SmXdefSubtract() - subtract .Xdefaults file from given database
  43. **
  44. *****************************************************************************
  45. *************************************<+>*************************************/
  46. #include <stdio.h>
  47. #include <X11/Intrinsic.h>
  48. #include "Sm.h"
  49. #include "SmXrm.h"
  50. /*
  51. * State data
  52. * dbXdefaults - copy of .Xdefaults in Xrm database form
  53. */
  54. static XrmDatabase dbXdefaults = NULL;
  55. /*
  56. * Note:
  57. *
  58. * The memory for dbXdefaults is freed only upon dtsession termination
  59. *
  60. * This code is currently restricted to handling the .Xdefaults file,
  61. * but can easily be extended to handle other default resource files.
  62. *
  63. */
  64. /*************************************<->*************************************
  65. *
  66. * SmXdefMerge(display)
  67. *
  68. * Description:
  69. * -----------
  70. * Merge the .Xdefaults file into the RESOURCE_MANAGER database
  71. *
  72. * Inputs:
  73. * ------
  74. * display - display connection
  75. *
  76. * Outputs:
  77. * -------
  78. *
  79. *
  80. * Comments:
  81. * --------
  82. *
  83. *************************************<->***********************************/
  84. void
  85. SmXdefMerge(Display *display)
  86. {
  87. char *xdefaults;
  88. char *home;
  89. /*
  90. * Load .Xdefaults
  91. */
  92. if ((home = getenv("HOME")) == NULL)
  93. home = "";
  94. if( (xdefaults = (char *)malloc(strlen(home)+12)) != NULL)
  95. {
  96. sprintf(xdefaults,"%s/%s",home,".Xdefaults");
  97. if(access(xdefaults,R_OK) == 0)
  98. {
  99. FILE *fp;
  100. char *b = NULL;
  101. int size;
  102. struct stat statinfo;
  103. /*
  104. * Determine size of file.
  105. */
  106. if (stat(xdefaults, &statinfo) == -1)
  107. {
  108. statinfo.st_size = 0;
  109. }
  110. /*
  111. * Get some memory.
  112. */
  113. if (statinfo.st_size > 0)
  114. {
  115. b = (char *)SM_MALLOC(statinfo.st_size + 1);
  116. }
  117. if (b != NULL)
  118. {
  119. /*
  120. * Read file into memory.
  121. */
  122. if ((fp = fopen(xdefaults, "r")) != NULL)
  123. {
  124. size = fread(b, 1, statinfo.st_size, fp);
  125. fclose(fp);
  126. }
  127. if (size == statinfo.st_size)
  128. {
  129. /*
  130. * Merge .Xdefaults string into RESOURCE_MANAGER database, and
  131. * also convert to Xrm database form for later subtraction.
  132. */
  133. b[size] = '\0';
  134. _DtAddToResource(display, b);
  135. dbXdefaults = XrmGetStringDatabase(b);
  136. SM_FREE(b);
  137. }
  138. }
  139. }
  140. free(xdefaults);
  141. }
  142. }
  143. /*************************************<->*************************************
  144. *
  145. * SmXdefSubtract(db)
  146. *
  147. * Description:
  148. * -----------
  149. * Subract prior merged .Xdefaults file from given database
  150. *
  151. * Inputs:
  152. * ------
  153. * db - Xrm database from which to subtract .Xdefaults
  154. *
  155. * Outputs:
  156. * -------
  157. *
  158. * Return:
  159. * ------
  160. * dbResult - result database
  161. *
  162. * Comments:
  163. * --------
  164. * Caller is responsible for freeing dbResult using XrmDestroyDatabase()
  165. *
  166. *
  167. *************************************<->***********************************/
  168. XrmDatabase
  169. SmXdefSubtract(XrmDatabase db)
  170. {
  171. XrmDatabase dbResult;
  172. if (dbXdefaults)
  173. {
  174. dbResult = SmXrmSubtractDatabase(dbXdefaults, db);
  175. }
  176. else
  177. {
  178. dbResult = NULL;
  179. }
  180. return(dbResult);
  181. }