Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / lib / DtSvc / DtUtil1 / SmUtil.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: SmUtil.c /main/5 1996/06/21 17:26:03 ageorge $ */
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  * File Name: SmUtil.c
33  *
34  *  Contains the DT functions used by an application to communicate with
35  *  dtsession.
36  *
37  *****************************************************************************/
38
39 #if 0
40 #include <sys/param.h>
41 #include <sys/types.h>
42 #endif
43
44 #include <sys/stat.h>
45 #include <X11/Xlib.h>
46 #include <X11/Xatom.h>
47 #include <Dt/WsmP.h>
48 #include <Dt/DtP.h>
49 #include <Dt/Session.h>
50 #include <Dt/SmCreateDirs.h>
51 #include "DtSvcLock.h"
52
53 #ifndef CDE_INSTALLATION_TOP
54 #define CDE_INSTALLATION_TOP "/opt/dt"
55 #endif
56
57 /********    Static Function Declarations    ********/
58
59 static char *getSessionName(Display *, Atom);
60 static Boolean getSessionPath( Widget, char *, char **, char **);
61
62 /********    End Static Function Declarations    ********/
63
64 /*************************************<->*************************************
65  *
66  *  getSessionName (display, prop )
67  *
68  *
69  *  Description:
70  *  -----------
71  *  Returns the session name. 
72  *
73  *  Inputs:
74  *  ------
75  *  display - the display
76  *  prop - the property name of the save or restore session
77  *
78  *  Outputs:
79  *  -------
80  *  
81  *  Return:
82  *  ------
83  *  Returns the session name string or NULL if it could not be obtained. 
84  *  This value should be freed with XFree().
85  *  
86  *
87  *************************************<->***********************************/
88
89 static char *
90 getSessionName(
91         Display *display,
92         Atom prop)
93 {
94   int                 propStatus;
95   Atom                actualType;
96   int                 actualFormat;
97   unsigned long       nitems;
98   unsigned long       leftover;
99   char               *property = NULL;
100
101   propStatus = XGetWindowProperty (display, RootWindow(display, 0), 
102                                    prop, 0L,
103                                    1000000L, False,
104                                    AnyPropertyType, &actualType,
105                                    &actualFormat, &nitems, &leftover, 
106                                    (unsigned char **)&property);
107
108  
109   if(propStatus == Success &&
110      actualType != None &&
111      actualFormat == 8 &&
112      nitems != 0)
113   {
114     return(property);
115   }
116  
117   if (property)
118   {
119     XFree(property);
120   }
121
122   return(NULL);
123 }
124
125 /*************************************<->*************************************
126  *
127  *  getSessionPath (widget, propstring, savePath, saveFile)
128  *
129  *
130  *  Description:
131  *  -----------
132  *  This function generates a full path name for an application's state
133  *  file. If *saveFile is NULL, a new file name is generated, else
134  *  *saveFile is used. It returns True if the path is returned, False
135  *  otherwise.
136  *
137  *
138  *  Inputs:
139  *  ------
140  *  widget - a widget to use to get the display
141  *  propstring - session name property
142  *  savePath - pointer to memory in which to place pointer to path
143  *  saveFile - pointer to filename. If *saveFile is NULL, a new filename
144  *             will be allocated and returned in *saveFile, else *saveFile
145  *             will be used to generate path name
146  *
147  *  Outputs:
148  *  -------
149  *  True - path name returned
150  *  False - path name not returned
151  *
152  *************************************<->***********************************/
153
154 static Boolean
155 getSessionPath(
156         Widget widget,
157         char *propstring,
158         char **savePath,
159         char **saveFile )
160 {
161     Display             *display;
162     char                *tmpPath = NULL;
163     char                *property = NULL;
164     char                *fileName;
165     struct stat         buf;
166     int                 status;
167     
168     display = XtDisplay(widget); 
169     
170     tmpPath = _DtCreateDtDirs(display);
171     if (tmpPath == NULL) goto abort;
172     
173     property = getSessionName(display, 
174                   XInternAtom(display, propstring, False));
175     if (property == NULL) goto abort;
176
177    /*
178     * NOTE: it is assumed that _DtCreateDtDirs() returns a buffer of 
179     *       size MAXPATHLEN+1. This allows us to avoid a extra alloc
180     *       and copy -- at the expense of code maintainability.
181     */
182     if ((strlen(tmpPath) + 1 + strlen(property)) > MAXPATHLEN) goto abort;
183
184    /* 
185     * parse the property string and create directory if needed 
186     */
187     (void)strcat(tmpPath, "/");
188     (void)strcat(tmpPath, property);
189     status = stat(tmpPath, &buf);
190
191    /*
192     * directory does not exist.
193     */
194     if(status == -1)
195     {
196         status = mkdir(tmpPath, 0000);
197         if(status == -1) goto abort;
198
199         (void)chmod(tmpPath, 0755);
200     }
201
202     (void)strcat(tmpPath, "/");
203
204     if (*saveFile == NULL)
205     {
206      /*
207       * No saveFile name was provided, so generate a new one.
208       */
209       int len = strlen(tmpPath);
210
211       (void)strcat(tmpPath, "dtXXXXXX");
212       (void)mktemp(tmpPath);
213
214       *saveFile = (char *) XtMalloc(15 * sizeof(char));
215       if(*saveFile == NULL) goto abort;
216
217       (void)strcpy(*saveFile, tmpPath+len);
218     }
219     else
220     {
221      /*
222       * A saveFile name was provided, so use it.
223       */
224       (void)strcat(tmpPath, *saveFile);
225     }
226
227     *savePath = tmpPath;
228
229     XFree ((char *)property);
230     return(True);
231
232   abort:
233    /*
234     * ObGoto: if it clarifies the logic and reduces code, 
235     *         goto's are ok by me.
236     */
237     *savePath = NULL;
238     if (tmpPath) XtFree ((char *)tmpPath);
239     if (property) XFree ((char *)property);
240     return(False);
241 }
242
243
244 /*************************************<->*************************************
245  *
246  *  DtSessionSavePath (widget, savePath, saveFile)
247  *
248  *
249  *  Description:
250  *  -----------
251  *  This function returns (in it's parameters) the full path name for an
252  *  application to save to, as well as the file name to save away for later
253  *  restoration.  It returns True if the path is returned, False
254  *  otherwise.
255  *
256  *
257  *  Inputs:
258  *  ------
259  *  widget - a widget to use to get the display
260  *
261  *  Outputs:
262  *  -------
263  *  returns a status value
264  *
265  *************************************<->***********************************/
266
267 Boolean 
268 DtSessionSavePath(
269         Widget widget,
270         char **savePath,
271         char **saveFile )
272 {
273     Boolean             result;
274     _DtSvcWidgetToAppContext(widget);
275
276     _DtSvcAppLock(app);
277     *saveFile = NULL;
278     result = getSessionPath(widget, _XA_DT_SAVE_MODE, savePath, saveFile);
279     _DtSvcAppUnlock(app);
280     
281     return(result);
282 } /* END OF FUNCTION DtSessionSavePath */
283
284
285 /*************************************<->*************************************
286  *
287  *  DtSessionRestorePath (widget, restorePath, restoreFile)
288  *
289  *
290  *  Description:
291  *  -----------
292  *  This function returns (in its parameters), the path where the application
293  *  is to restore its file from.
294  *  It returns True if the path is returned, False
295  *  otherwise.
296  *
297  *
298  *  Inputs:
299  *  ------
300  *  widget - a widget to use to get the display
301  *
302  *  Outputs:
303  *  -------
304  *  returns a status value
305  *
306  *************************************<->***********************************/
307
308 Boolean 
309 DtSessionRestorePath(
310         Widget widget,
311         char **savePath,
312         char *saveFile )
313 {
314     Boolean             result;
315     _DtSvcWidgetToAppContext(widget);
316
317     _DtSvcAppLock(app);
318     result = getSessionPath(widget, _XA_DT_RESTORE_MODE, savePath, &saveFile);
319     _DtSvcAppUnlock(app);
320     
321     return(result);
322 } /* END OF FUNCTION DtSessionRestorePath */