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;
96 * Sanity check - make sure there's an existing display
101 if ((home =getenv("HOME")) == NULL)
104 tmpPath = XtCalloc(1, MAXPATHLEN + 1);
109 * If the $HOME/.dt directory does not exist, create it
111 strncpy(tmpPath, home, MAXPATHLEN);
112 strncat(tmpPath, "/" DtPERSONAL_CONFIG_DIRECTORY, MAXPATHLEN);
114 status = stat(tmpPath, &buf);
116 status = mkdir(tmpPath, 0000);
121 (void)chmod(tmpPath, 0755);
125 * Create the personal DB directory if it does not exist.
127 strncpy(tmpPath, home, MAXPATHLEN);
128 strncat(tmpPath, "/" DtPERSONAL_DB_DIRECTORY, MAXPATHLEN);
130 if ((status = stat (tmpPath, &buf)) == -1) {
131 if ((status = mkdir (tmpPath, 0000)) != -1)
132 (void) chmod (tmpPath, 0755);
136 * Create the personal tmp dir if it does not exist.
138 strncpy(tmpPath, home, MAXPATHLEN);
139 strncat(tmpPath, "/" DtPERSONAL_TMP_DIRECTORY, MAXPATHLEN);
141 if ((status = stat (tmpPath, &buf)) == -1) {
142 if ((status = mkdir (tmpPath, 0000)) != -1)
143 (void) chmod (tmpPath, 0755);
147 * The creation of the session directory is tricky:
149 * For CDE 1.0 sessions, if a display-specific directory exists,
150 * it will take precedence. CDE 1.0 does not support the selection
153 * However, for newer CDE implementations, if a specific session
154 * was selected, the specified session will be used. If no session
155 * was selected, the CDE 1.0 mechanism will be used.
157 * If a CDEnext session is being used, the session directory will
158 * be on a property on the root window.
160 * Must check for this property and use it if is set.
162 if ((sessionDir = GetSessionDirProperty (display)) != NULL) {
163 if (!strcmp (sessionDir, DtSM_SESSION_DIRECTORY)) {
165 * Need to create a DtSM_SESSION_DIRECTORY dir if it
168 needSessionsDir = True;
170 } else if (!strcmp (sessionDir, DtSM_SESSION_DISPLAY_DIRECTORY)) {
172 * Create a directory for a display-specific session if necessary
174 if ((displayName = GetDisplayName (display)) != NULL) {
176 strncpy (tmpPath, home, MAXPATHLEN);
177 strncat (tmpPath, "/" DtPERSONAL_CONFIG_DIRECTORY, MAXPATHLEN);
178 strncat (tmpPath, "/", MAXPATHLEN);
179 strncat (tmpPath, displayName, MAXPATHLEN);
181 free(displayName); /* CDExc22771 */
183 if ((status = stat (tmpPath, &buf)) == -1) {
184 if ((status = mkdir (tmpPath, 0000)) != -1)
185 (void) chmod (tmpPath, 0755);
187 useOldSession = True;
192 * Something's wrong with the display, use the fallback
194 useOldSession = True;
198 * The property contains an unrecognized value, fallback to
199 * other session selection algorithm.
201 useOldSession = True;
206 useOldSession = True;
210 * Check for a display dependent directory. If one exists,
213 * This is done for backward compatibility - THE DISPLAY
214 * DEPENDENT DIR TAKES PRECEDENT.
216 if ((displayName = GetDisplayName (display)) != NULL) {
218 strncpy (tmpPath, home, MAXPATHLEN);
219 strncat (tmpPath, "/" DtPERSONAL_CONFIG_DIRECTORY, MAXPATHLEN);
220 strncat (tmpPath, "/", MAXPATHLEN);
221 strncat (tmpPath, displayName, MAXPATHLEN);
223 free(displayName); /* CDExc22771 */
225 if ((status = stat(tmpPath, &buf)) != 0)
227 * The display directory does not exist
229 needSessionsDir = True;
232 needSessionsDir = True;
238 * If we don't have an old style directory - we check for a sessions
239 * directory, and create it if it doesn't exist
241 strncpy (tmpPath, home, MAXPATHLEN);
242 strncat (tmpPath, "/" DtPERSONAL_CONFIG_DIRECTORY, MAXPATHLEN);
243 strncat (tmpPath, "/" DtSM_SESSION_DIRECTORY, MAXPATHLEN);
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);