123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335 |
- /*
- * CDE - Common Desktop Environment
- *
- * Copyright (c) 1993-2012, The Open Group. All rights reserved.
- *
- * These libraries and programs are free software; you can
- * redistribute them and/or modify them under the terms of the GNU
- * Lesser General Public License as published by the Free Software
- * Foundation; either version 2 of the License, or (at your option)
- * any later version.
- *
- * These libraries and programs are distributed in the hope that
- * they will be useful, but WITHOUT ANY WARRANTY; without even the
- * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
- * PURPOSE. See the GNU Lesser General Public License for more
- * details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with these libraries and programs; if not, write
- * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
- * Floor, Boston, MA 02110-1301 USA
- */
- /* $XConsortium: SmUtil.c /main/5 1996/06/21 17:26:03 ageorge $ */
- /* *
- * (c) Copyright 1993, 1994 Hewlett-Packard Company *
- * (c) Copyright 1993, 1994 International Business Machines Corp. *
- * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
- * (c) Copyright 1993, 1994 Novell, Inc. *
- */
- /******************************************************************************
- *
- * File Name: SmUtil.c
- *
- * Contains the DT functions used by an application to communicate with
- * dtsession.
- *
- *****************************************************************************/
- #if 0
- #include <sys/param.h>
- #include <sys/types.h>
- #endif
- #include <sys/stat.h>
- #include <X11/Xlib.h>
- #include <X11/Xatom.h>
- #include <Dt/WsmP.h>
- #include <Dt/DtP.h>
- #include <Dt/Session.h>
- #include <Dt/SmCreateDirs.h>
- #include "DtSvcLock.h"
- #ifndef CDE_INSTALLATION_TOP
- #define CDE_INSTALLATION_TOP "/opt/dt"
- #endif
- /******** Static Function Declarations ********/
- static char *getSessionName(Display *, Atom);
- static Boolean getSessionPath( Widget, char *, char **, char **);
- /******** End Static Function Declarations ********/
- /*************************************<->*************************************
- *
- * getSessionName (display, prop )
- *
- *
- * Description:
- * -----------
- * Returns the session name.
- *
- * Inputs:
- * ------
- * display - the display
- * prop - the property name of the save or restore session
- *
- * Outputs:
- * -------
- *
- * Return:
- * ------
- * Returns the session name string or NULL if it could not be obtained.
- * This value should be freed with XFree().
- *
- *
- *************************************<->***********************************/
- static char *
- getSessionName(
- Display *display,
- Atom prop)
- {
- int propStatus;
- Atom actualType;
- int actualFormat;
- unsigned long nitems;
- unsigned long leftover;
- char *property = NULL;
- propStatus = XGetWindowProperty (display, RootWindow(display, 0),
- prop, 0L,
- 1000000L, False,
- AnyPropertyType, &actualType,
- &actualFormat, &nitems, &leftover,
- (unsigned char **)&property);
-
- if(propStatus == Success &&
- actualType != None &&
- actualFormat == 8 &&
- nitems != 0)
- {
- return(property);
- }
-
- if (property)
- {
- XFree(property);
- }
- return(NULL);
- }
- /*************************************<->*************************************
- *
- * getSessionPath (widget, propstring, savePath, saveFile)
- *
- *
- * Description:
- * -----------
- * This function generates a full path name for an application's state
- * file. If *saveFile is NULL, a new file name is generated, else
- * *saveFile is used. It returns True if the path is returned, False
- * otherwise.
- *
- *
- * Inputs:
- * ------
- * widget - a widget to use to get the display
- * propstring - session name property
- * savePath - pointer to memory in which to place pointer to path
- * saveFile - pointer to filename. If *saveFile is NULL, a new filename
- * will be allocated and returned in *saveFile, else *saveFile
- * will be used to generate path name
- *
- * Outputs:
- * -------
- * True - path name returned
- * False - path name not returned
- *
- *************************************<->***********************************/
- static Boolean
- getSessionPath(
- Widget widget,
- char *propstring,
- char **savePath,
- char **saveFile )
- {
- Display *display;
- char *tmpPath = NULL;
- char *property = NULL;
- char *fileName;
- struct stat buf;
- int status;
-
- display = XtDisplay(widget);
-
- tmpPath = _DtCreateDtDirs(display);
- if (tmpPath == NULL) goto abort;
-
- property = getSessionName(display,
- XInternAtom(display, propstring, False));
- if (property == NULL) goto abort;
- /*
- * NOTE: it is assumed that _DtCreateDtDirs() returns a buffer of
- * size MAXPATHLEN. This allows us to avoid a extra alloc
- * and copy -- at the expense of code maintainability.
- *
- * JET - 2020. This is stupid. At least account for the strings
- * you are adding further on down... This "solution" isn't great
- * either. Real fix would be to have all callers pass in bufptr
- * and len all the way down the chain instead of tmpPath.
- */
- if ((strlen(tmpPath)
- + 1 /* "/" */
- + strlen(property)
- + 1 /* "/" */
- + ((*saveFile == NULL) ? strlen("dtXXXXXX") + 1 : strlen(*saveFile))
- ) >= MAXPATHLEN)
- {
- goto abort;
- }
- /*
- * parse the property string and create directory if needed
- */
- (void)strcat(tmpPath, "/");
- (void)strcat(tmpPath, property);
- status = stat(tmpPath, &buf);
- /*
- * directory does not exist.
- */
- if(status == -1)
- {
- status = mkdir(tmpPath, 0000);
- if(status == -1) goto abort;
- (void)chmod(tmpPath, 0755);
- }
- (void)strcat(tmpPath, "/");
- if (*saveFile == NULL)
- {
- /*
- * No saveFile name was provided, so generate a new one.
- */
- int len = strlen(tmpPath);
- (void)strcat(tmpPath, "dtXXXXXX");
- (void)mktemp(tmpPath);
- *saveFile = (char *) XtMalloc(15 * sizeof(char));
- if(*saveFile == NULL) goto abort;
- (void)strcpy(*saveFile, tmpPath+len);
- }
- else
- {
- /*
- * A saveFile name was provided, so use it.
- */
- (void)strcat(tmpPath, *saveFile);
- }
- *savePath = tmpPath;
- XFree ((char *)property);
- return(True);
- abort:
- /*
- * ObGoto: if it clarifies the logic and reduces code,
- * goto's are ok by me.
- */
- *savePath = NULL;
- if (tmpPath) XtFree ((char *)tmpPath);
- if (property) XFree ((char *)property);
- return(False);
- }
- /*************************************<->*************************************
- *
- * DtSessionSavePath (widget, savePath, saveFile)
- *
- *
- * Description:
- * -----------
- * This function returns (in it's parameters) the full path name for an
- * application to save to, as well as the file name to save away for later
- * restoration. It returns True if the path is returned, False
- * otherwise.
- *
- *
- * Inputs:
- * ------
- * widget - a widget to use to get the display
- *
- * Outputs:
- * -------
- * returns a status value
- *
- *************************************<->***********************************/
- Boolean
- DtSessionSavePath(
- Widget widget,
- char **savePath,
- char **saveFile )
- {
- Boolean result;
- _DtSvcWidgetToAppContext(widget);
- _DtSvcAppLock(app);
- *saveFile = NULL;
- result = getSessionPath(widget, _XA_DT_SAVE_MODE, savePath, saveFile);
- _DtSvcAppUnlock(app);
-
- return(result);
- } /* END OF FUNCTION DtSessionSavePath */
- /*************************************<->*************************************
- *
- * DtSessionRestorePath (widget, restorePath, restoreFile)
- *
- *
- * Description:
- * -----------
- * This function returns (in its parameters), the path where the application
- * is to restore its file from.
- * It returns True if the path is returned, False
- * otherwise.
- *
- *
- * Inputs:
- * ------
- * widget - a widget to use to get the display
- *
- * Outputs:
- * -------
- * returns a status value
- *
- *************************************<->***********************************/
- Boolean
- DtSessionRestorePath(
- Widget widget,
- char **savePath,
- char *saveFile )
- {
- Boolean result;
- _DtSvcWidgetToAppContext(widget);
- _DtSvcAppLock(app);
- result = getSessionPath(widget, _XA_DT_RESTORE_MODE, savePath, &saveFile);
- _DtSvcAppUnlock(app);
-
- return(result);
- } /* END OF FUNCTION DtSessionRestorePath */
|