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 /* $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 *****************************************************************************/
36 #include <sys/param.h>
37 #include <sys/types.h>
42 #include <X11/Xlibint.h>
43 #include <X11/Intrinsic.h>
44 #include <Dt/DtNlUtils.h>
45 #include <Dt/DtPStrings.h>
48 /******** Private Function Declarations ********/
56 GetSessionDirProperty (
64 /*************************************<->*************************************
66 * _DtCreateDtDirs (display)
68 * 1. Creates ~/.dt, ~/.dt/types, ~/.dt/tmp and either
69 * ~/.dt/sessions or ~/.dt/<display_name>
71 * 2. Returns the name of the session directory
75 * Returns the session directory or NULL if malloc fails or ~/.dt
78 *************************************<->***********************************/
85 Boolean needSessionsDir = False;
86 Boolean useOldSession = False;
94 * Sanity check - make sure there's an existing display
99 if ((home =getenv("HOME")) == NULL)
102 tmpPath = (char *) XtMalloc((MAXPATHLEN + 1) * sizeof(char));
107 * If the $HOME/.dt directory does not exist, create it
109 strcpy(tmpPath, home);
110 strcat(tmpPath, "/" DtPERSONAL_CONFIG_DIRECTORY);
112 status = stat(tmpPath, &buf);
114 status = mkdir(tmpPath, 0000);
119 (void)chmod(tmpPath, 0755);
123 * Create the personal DB directory if it does not exist.
125 strcpy(tmpPath, home);
126 strcat(tmpPath, "/" 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 strcpy(tmpPath, home);
137 strcat(tmpPath, "/" DtPERSONAL_TMP_DIRECTORY);
139 if ((status = stat (tmpPath, &buf)) == -1) {
140 if ((status = mkdir (tmpPath, 0000)) != -1)
141 (void) chmod (tmpPath, 0755);
145 * The creation of the session directory is tricky:
147 * For CDE 1.0 sessions, if a display-specific directory exists,
148 * it will take precedence. CDE 1.0 does not support the selection
151 * However, for newer CDE implementations, if a specific session
152 * was selected, the specified session will be used. If no session
153 * was selected, the CDE 1.0 mechanism will be used.
155 * If a CDEnext session is being used, the session directory will
156 * be on a property on the root window.
158 * Must check for this property and use it if is set.
160 if ((sessionDir = GetSessionDirProperty (display)) != NULL) {
161 if (!strcmp (sessionDir, DtSM_SESSION_DIRECTORY)) {
163 * Need to create a DtSM_SESSION_DIRECTORY dir if it
166 needSessionsDir = True;
168 } else if (!strcmp (sessionDir, DtSM_SESSION_DISPLAY_DIRECTORY)) {
170 * Create a directory for a display-specific session if necessary
172 if ((displayName = GetDisplayName (display)) != NULL) {
174 strcpy (tmpPath, home);
175 strcat (tmpPath, "/" DtPERSONAL_CONFIG_DIRECTORY);
176 strcat (tmpPath, "/");
177 strcat (tmpPath, displayName);
179 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 strcpy (tmpPath, home);
217 strcat (tmpPath, "/" DtPERSONAL_CONFIG_DIRECTORY);
218 strcat (tmpPath, "/");
219 strcat (tmpPath, displayName);
221 free(displayName); /* CDExc22771 */
223 if ((status = stat(tmpPath, &buf)) != 0)
225 * The display directory does not exist
227 needSessionsDir = True;
230 needSessionsDir = True;
236 * If we don't have an old style directory - we check for a sessions
237 * directory, and create it if it doesn't exist
239 strcpy (tmpPath, home);
240 strcat (tmpPath, "/" DtPERSONAL_CONFIG_DIRECTORY);
241 strcat (tmpPath, "/" DtSM_SESSION_DIRECTORY);
243 if ((status = stat(tmpPath, &buf)) == -1) {
244 if ((status = mkdir(tmpPath, 0000)) == -1) {
248 (void)chmod(tmpPath, 0755);
255 /*------------------------------------------------------------------------+*/
260 unsigned int bufsize )
265 if (status = gethostname(buffer, bufsize))
266 return status; /* failed gethostname */
267 if (ptr = strstr(buffer, (char *)"."))
268 *ptr = '\0'; /* delete domain name if there is one */
272 /*------------------------------------------------------------------------+*/
275 * GetSessionDirProperty -
278 GetSessionDirProperty (
284 unsigned long nitems;
285 unsigned long leftover;
286 char *property = NULL;
289 tmpAtom = XInternAtom(display, _XA_DT_RESTORE_DIR, False);
291 propStatus = XGetWindowProperty (display, RootWindow(display, 0),
294 AnyPropertyType, &actualType,
295 &actualFormat, &nitems, &leftover,
296 (unsigned char **)&property);
298 if (propStatus == Success && actualType != None &&
299 actualFormat == 8 && nitems != 0)
307 /*------------------------------------------------------------------------+*/
317 char hostName[101], displayName[101];
318 char *pch, *tmpNumber = NULL;
322 * Create the display name and append it to the current path.
324 (void)strcpy(hostName, display->display_name);
325 (void)strcpy(displayName, display->display_name);
328 * If this is run to unix or local get the host name - otherwise
329 * just use what we have
333 * Strip host name to nothing but the unqualified (short) host name
335 if (pch = DtStrchr(hostName, ':'))
338 if (pch = DtStrchr(hostName, '.'))
341 if((!strcmp(hostName, "unix")) || (!strcmp(hostName, "local"))
342 || (!strcmp(hostName, "")))
345 * host name is local - get the real name
347 (void) GetShortHostname(hostName, 25);
351 * Strip screen off of display name
353 if (tmpNumber = DtStrchr(displayName, ':'))
354 if (pch = DtStrchr(tmpNumber, '.'))
358 * Strip it down to 14 chars (actually, 14 bytes or less)
360 if((strlen(tmpNumber) + strlen(hostName)) > (size_t)14)
366 /* Pare display number to at most 12 bytes */
367 while ((tnLen = strlen(tmpNumber)) > (size_t)12)
369 /* Remove the last character, an try again */
370 DtLastChar(tmpNumber, &lastCh, &lastChLen);
374 /* Pare down host name, if necessary */
375 while ((strlen(hostName) + tnLen) > (size_t)14)
377 /* Remove the last character, and try again */
378 DtLastChar(hostName, &lastCh, &lastChLen);
383 strcat (hostName, tmpNumber);
385 returnPath = strdup (hostName);