Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtsession / SmXdef.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 librararies 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
47 #include <stdio.h>
48 #include <X11/Intrinsic.h>
49 #include "Sm.h"
50 #include "SmXrm.h"
51
52 /*
53  * State data
54  *   dbXdefaults - copy of .Xdefaults in Xrm database form
55  */
56  static XrmDatabase dbXdefaults = NULL;
57
58
59 /*
60  * Note:
61  * 
62  * The memory for dbXdefaults is freed only upon dtsession termination
63  *
64  * This code is currently restricted to handling the .Xdefaults file,
65  * but can easily be extended to handle other default resource files.
66  *
67  */
68
69 \f
70 /*************************************<->*************************************
71  *
72  *  SmXdefMerge(display)
73  *
74  *  Description:
75  *  -----------
76  *  Merge the .Xdefaults file into the RESOURCE_MANAGER database
77  *
78  *  Inputs:
79  *  ------
80  *  display - display connection
81  *
82  *  Outputs:
83  *  -------
84  *
85  *
86  *  Comments:
87  *  --------
88  *
89  *************************************<->***********************************/
90 void
91 SmXdefMerge(Display *display)
92 {
93   char *xdefaults;
94
95  /*
96   * Load .Xdefaults
97   */
98   if( (xdefaults = (char *)malloc(strlen(getenv("HOME"))+12)) != NULL)  
99   {
100     sprintf(xdefaults,"%s/%s",getenv("HOME"),".Xdefaults");
101     if(access(xdefaults,R_OK) == 0)
102     {
103       FILE *fp;
104       char *b = NULL;
105       int size;
106       struct stat statinfo; 
107
108      /*
109       * Determine size of file.
110       */
111       if (stat(xdefaults, &statinfo) == -1)
112       {
113         statinfo.st_size = 0;
114       }
115
116      /*
117       * Get some memory.
118       */
119       if (statinfo.st_size > 0)
120       {
121         b = (char *)SM_MALLOC(statinfo.st_size + 1);
122       }
123
124       if (b != NULL)
125       {
126        /*
127         * Read file into memory.
128         */
129         if ((fp = fopen(xdefaults, "r")) != NULL)
130         {
131           size = fread(b, 1, statinfo.st_size, fp);
132           fclose(fp);
133         }
134
135         if (size == statinfo.st_size)
136         {
137          /*
138           * Merge .Xdefaults string into RESOURCE_MANAGER database, and
139           * also convert to Xrm database form for later subtraction.
140           */
141           b[size] = '\0';
142           _DtAddToResource(display, b);
143           dbXdefaults = XrmGetStringDatabase(b);  
144           SM_FREE(b);
145         }
146       }
147     }
148     free(xdefaults);
149   }
150 }
151
152 \f
153 /*************************************<->*************************************
154  *
155  *  SmXdefSubtract(db)
156  *
157  *  Description:
158  *  -----------
159  *  Subract prior merged .Xdefaults file from given database
160  *
161  *  Inputs:
162  *  ------
163  *  db - Xrm database from which to subtract .Xdefaults
164  *
165  *  Outputs:
166  *  -------
167  *
168  *  Return: 
169  *  ------
170  *  dbResult - result database
171  *
172  *  Comments:
173  *  --------
174  *  Caller is responsible for freeing dbResult using XrmDestroyDatabase()
175  *
176  *
177  *************************************<->***********************************/
178 XrmDatabase
179 SmXdefSubtract(XrmDatabase db)
180 {
181   XrmDatabase dbResult;
182
183   if (dbXdefaults)
184   {
185     dbResult = SmXrmSubtractDatabase(dbXdefaults, db);
186   }
187   else
188   {
189     dbResult = NULL;
190   }
191   return(dbResult);
192 }