SmUtil.c 8.8 KB

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