2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
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
23 /* $XConsortium: SmUtil.c /main/5 1996/06/21 17:26:03 ageorge $ */
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. *
30 /******************************************************************************
34 * Contains the DT functions used by an application to communicate with
37 *****************************************************************************/
40 #include <sys/param.h>
41 #include <sys/types.h>
46 #include <X11/Xatom.h>
49 #include <Dt/Session.h>
50 #include <Dt/SmCreateDirs.h>
51 #include "DtSvcLock.h"
53 #ifndef CDE_INSTALLATION_TOP
54 #define CDE_INSTALLATION_TOP "/opt/dt"
57 /******** Static Function Declarations ********/
59 static char *getSessionName(Display *, Atom);
60 static Boolean getSessionPath( Widget, char *, char **, char **);
62 /******** End Static Function Declarations ********/
64 /*************************************<->*************************************
66 * getSessionName (display, prop )
71 * Returns the session name.
75 * display - the display
76 * prop - the property name of the save or restore session
83 * Returns the session name string or NULL if it could not be obtained.
84 * This value should be freed with XFree().
87 *************************************<->***********************************/
98 unsigned long leftover;
99 char *property = NULL;
101 propStatus = XGetWindowProperty (display, RootWindow(display, 0),
104 AnyPropertyType, &actualType,
105 &actualFormat, &nitems, &leftover,
106 (unsigned char **)&property);
109 if(propStatus == Success &&
110 actualType != None &&
125 /*************************************<->*************************************
127 * getSessionPath (widget, propstring, savePath, saveFile)
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
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
149 * True - path name returned
150 * False - path name not returned
152 *************************************<->***********************************/
162 char *tmpPath = NULL;
163 char *property = NULL;
168 display = XtDisplay(widget);
170 tmpPath = _DtCreateDtDirs(display);
171 if (tmpPath == NULL) goto abort;
173 property = getSessionName(display,
174 XInternAtom(display, propstring, False));
175 if (property == NULL) goto abort;
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.
182 if ((strlen(tmpPath) + 1 + strlen(property)) > MAXPATHLEN) goto abort;
185 * parse the property string and create directory if needed
187 (void)strcat(tmpPath, "/");
188 (void)strcat(tmpPath, property);
189 status = stat(tmpPath, &buf);
192 * directory does not exist.
196 status = mkdir(tmpPath, 0000);
197 if(status == -1) goto abort;
199 (void)chmod(tmpPath, 0755);
202 (void)strcat(tmpPath, "/");
204 if (*saveFile == NULL)
207 * No saveFile name was provided, so generate a new one.
209 int len = strlen(tmpPath);
211 (void)strcat(tmpPath, "dtXXXXXX");
212 (void)mktemp(tmpPath);
214 *saveFile = (char *) XtMalloc(15 * sizeof(char));
215 if(*saveFile == NULL) goto abort;
217 (void)strcpy(*saveFile, tmpPath+len);
222 * A saveFile name was provided, so use it.
224 (void)strcat(tmpPath, *saveFile);
229 XFree ((char *)property);
234 * ObGoto: if it clarifies the logic and reduces code,
235 * goto's are ok by me.
238 if (tmpPath) XtFree ((char *)tmpPath);
239 if (property) XFree ((char *)property);
244 /*************************************<->*************************************
246 * DtSessionSavePath (widget, savePath, saveFile)
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
259 * widget - a widget to use to get the display
263 * returns a status value
265 *************************************<->***********************************/
274 _DtSvcWidgetToAppContext(widget);
278 result = getSessionPath(widget, _XA_DT_SAVE_MODE, savePath, saveFile);
279 _DtSvcAppUnlock(app);
282 } /* END OF FUNCTION DtSessionSavePath */
285 /*************************************<->*************************************
287 * DtSessionRestorePath (widget, restorePath, restoreFile)
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
300 * widget - a widget to use to get the display
304 * returns a status value
306 *************************************<->***********************************/
309 DtSessionRestorePath(
315 _DtSvcWidgetToAppContext(widget);
318 result = getSessionPath(widget, _XA_DT_RESTORE_MODE, savePath, &saveFile);
319 _DtSvcAppUnlock(app);
322 } /* END OF FUNCTION DtSessionRestorePath */