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 libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $TOG: SmCreateDirs.c /main/9 1997/02/24 09:23:16 barstow $ */
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 /******************************************************************************
32 * File Name: SmCreateDirs.c
34 *****************************************************************************/
37 #include <sys/param.h>
38 #include <sys/types.h>
44 #include <X11/Xlibint.h>
45 #include <X11/Intrinsic.h>
46 #include <Dt/DtNlUtils.h>
47 #include <Dt/DtPStrings.h>
50 /******** Private Function Declarations ********/
58 GetSessionDirProperty (
66 /*************************************<->*************************************
68 * _DtCreateDtDirs (display)
70 * 1. Creates ~/.dt, ~/.dt/types, ~/.dt/tmp and either
71 * ~/.dt/sessions or ~/.dt/<display_name>
73 * 2. Returns the name of the session directory
77 * Returns the session directory or NULL if malloc fails or ~/.dt
80 *************************************<->***********************************/
87 Boolean needSessionsDir = False;
88 Boolean useOldSession = False;
92 char *sessionDir = NULL;
93 char *displayName = NULL;
96 * Sanity check - make sure there's an existing display
101 if ((home = getenv("HOME")) == NULL)
104 tmpPath = XtCalloc(1, MAXPATHLEN);
109 * If the $HOME/.dt directory does not exist, create it
111 snprintf(tmpPath, MAXPATHLEN, "%s/%s", home, DtPERSONAL_CONFIG_DIRECTORY);
113 status = stat(tmpPath, &buf);
115 status = mkdir(tmpPath, 0000);
120 (void)chmod(tmpPath, 0755);
124 * Create the personal DB directory if it does not exist.
126 snprintf(tmpPath, MAXPATHLEN, "%s/%s", home, DtPERSONAL_DB_DIRECTORY);
128 if ((status = stat (tmpPath, &buf)) == -1) {
129 if ((status = mkdir (tmpPath, 0000)) != -1)
130 (void) chmod (tmpPath, 0755);
134 * Create the personal tmp dir if it does not exist.
136 snprintf(tmpPath, MAXPATHLEN, "%s/%s", home, DtPERSONAL_TMP_DIRECTORY);
138 if ((status = stat (tmpPath, &buf)) == -1) {
139 if ((status = mkdir (tmpPath, 0000)) != -1)
140 (void) chmod (tmpPath, 0755);
144 * The creation of the session directory is tricky:
146 * For CDE 1.0 sessions, if a display-specific directory exists,
147 * it will take precedence. CDE 1.0 does not support the selection
150 * However, for newer CDE implementations, if a specific session
151 * was selected, the specified session will be used. If no session
152 * was selected, the CDE 1.0 mechanism will be used.
154 * If a CDEnext session is being used, the session directory will
155 * be on a property on the root window.
157 * Must check for this property and use it if is set.
159 if ((sessionDir = GetSessionDirProperty (display)) != NULL) {
160 if (!strcmp (sessionDir, DtSM_SESSION_DIRECTORY)) {
162 * Need to create a DtSM_SESSION_DIRECTORY dir if it
165 needSessionsDir = True;
167 } else if (!strcmp (sessionDir, DtSM_SESSION_DISPLAY_DIRECTORY)) {
169 * Create a directory for a display-specific session if necessary
171 if ((displayName = GetDisplayName (display)) != NULL) {
173 snprintf(tmpPath, MAXPATHLEN, "%s/%s/%s",
175 DtPERSONAL_CONFIG_DIRECTORY,
178 free(displayName); /* CDExc22771 */
181 if ((status = stat (tmpPath, &buf)) == -1) {
182 if ((status = mkdir (tmpPath, 0000)) != -1)
183 (void) chmod (tmpPath, 0755);
185 useOldSession = True;
190 * Something's wrong with the display, use the fallback
192 useOldSession = True;
196 * The property contains an unrecognized value, fallback to
197 * other session selection algorithm.
199 useOldSession = True;
204 useOldSession = True;
208 * Check for a display dependent directory. If one exists,
211 * This is done for backward compatibility - THE DISPLAY
212 * DEPENDENT DIR TAKES PRECEDENT.
214 if ((displayName = GetDisplayName (display)) != NULL) {
216 snprintf(tmpPath, MAXPATHLEN, "%s/%s/%s",
218 DtPERSONAL_CONFIG_DIRECTORY,
221 free(displayName); /* CDExc22771 */
224 if ((status = stat(tmpPath, &buf)) != 0)
226 * The display directory does not exist
228 needSessionsDir = True;
231 needSessionsDir = True;
237 * If we don't have an old style directory - we check for a sessions
238 * directory, and create it if it doesn't exist
240 snprintf(tmpPath, MAXPATHLEN, "%s/%s/%s",
242 DtPERSONAL_CONFIG_DIRECTORY,
243 DtSM_SESSION_DIRECTORY);
245 if ((status = stat(tmpPath, &buf)) == -1) {
246 if ((status = mkdir(tmpPath, 0000)) == -1) {
250 (void)chmod(tmpPath, 0755);
257 /*------------------------------------------------------------------------+*/
262 unsigned int bufsize )
267 if ((status = gethostname(buffer, bufsize)))
268 return status; /* failed gethostname */
269 if ((ptr = strstr(buffer, (char *)".")))
270 *ptr = '\0'; /* delete domain name if there is one */
274 /*------------------------------------------------------------------------+*/
277 * GetSessionDirProperty -
280 GetSessionDirProperty (
286 unsigned long nitems;
287 unsigned long leftover;
288 char *property = NULL;
291 tmpAtom = XInternAtom(display, _XA_DT_RESTORE_DIR, False);
293 propStatus = XGetWindowProperty (display, RootWindow(display, 0),
296 AnyPropertyType, &actualType,
297 &actualFormat, &nitems, &leftover,
298 (unsigned char **)&property);
300 if (propStatus == Success && actualType != None &&
301 actualFormat == 8 && nitems != 0)
309 /*------------------------------------------------------------------------+*/
319 char hostName[101], displayName[101];
320 char *pch, *tmpNumber = NULL;
324 * Create the display name and append it to the current path.
326 snprintf(hostName, sizeof(hostName), "%s", display->display_name);
327 snprintf(displayName, sizeof(displayName), "%s", display->display_name);
330 * If this is run to unix or local get the host name - otherwise
331 * just use what we have
335 * Strip host name to nothing but the unqualified (short) host name
337 if ((pch = DtStrchr(hostName, ':')))
340 if ((pch = DtStrchr(hostName, '.')))
343 if((!strcmp(hostName, "unix")) || (!strcmp(hostName, "local"))
344 || (!strcmp(hostName, "")))
347 * host name is local - get the real name
349 (void) GetShortHostname(hostName, 25);
353 * Strip screen off of display name
355 if ((tmpNumber = DtStrchr(displayName, ':')))
356 if ((pch = DtStrchr(tmpNumber, '.')))
360 * Strip it down to 14 chars (actually, 14 bytes or less)
362 if((strlen(tmpNumber) + strlen(hostName)) > (size_t)14)
368 /* Pare display number to at most 12 bytes */
369 while ((tnLen = strlen(tmpNumber)) > (size_t)12)
371 /* Remove the last character, an try again */
372 DtLastChar(tmpNumber, &lastCh, &lastChLen);
376 /* Pare down host name, if necessary */
377 while ((strlen(hostName) + tnLen) > (size_t)14)
379 /* Remove the last character, and try again */
380 DtLastChar(hostName, &lastCh, &lastChLen);
385 strcat (hostName, tmpNumber);
387 returnPath = strdup (hostName);