dtpad: emit error on catopen() failure
[oweals/cde.git] / cde / programs / dtsession / SmGlobals.c
index 36c351197d55a539a336a687278381ff78da5330..0bb24e7edd643581c35c1564b667b6f111c490b9 100644 (file)
@@ -16,7 +16,7 @@
  * details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with these librararies and programs; if not, write
+ * 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
  */
  *****************************************************************************
  *************************************<+>*************************************/
 #include <stdio.h>
+#include <errno.h>
+#include <string.h>
 #include <ctype.h>
 #include <sys/wait.h>
 #include <sys/types.h>
 #include <sys/utsname.h>
 #include <unistd.h>
-#ifdef __apollo
-#include <X11/apollosys.h>      /* for pid_t, in hp-ux sys/types.h */
-#endif
 #include <sys/stat.h>
 #include <sys/param.h>
 #include <X11/Intrinsic.h>
@@ -222,6 +221,12 @@ static XtResource sessionResources[]=
    {SmNignoreEnvironment, SmCignoreEnvironment, XtRString, sizeof(String),
         XtOffset(SessionResourcesPtr, ignoreEnvironment),
         XtRImmediate, (XtPointer) NULL},
+#if defined(USE_XINERAMA)      /* JET - Xinerama */
+   {SmNxineramaPreferredScreen, SmCxineramaPreferredScreen, XtRInt, sizeof(int),
+        XtOffset(SessionResourcesPtr, xineramaPreferredScreen),
+        XtRImmediate, (XtPointer) 0},
+#endif
+
 }
 ;
 
@@ -470,7 +475,7 @@ InitSMGlobals( void )
      /*
       * Pull screen saver resources from Dtsession*<name>.
       */
-      smGD.SmNextension = smGD.SmNextension = smGD.extensionSpec = "";
+      smGD.SmNextension = smGD.SmCextension = smGD.extensionSpec = "";
     }
 
     XtGetSubresources(smGD.topLevelWid, (XtPointer) &smSaverRes,
@@ -678,7 +683,7 @@ SetRestorePath(
              */
             if (getenv("DISPLAY") == 0)
             {
-                sprintf(tmpDisplayName, "DISPLAY=%s", displayName);
+                snprintf(tmpDisplayName, MAXPATHLEN, "DISPLAY=%s", displayName);
                 putenv(tmpDisplayName);
             }
         }
@@ -697,7 +702,7 @@ SetRestorePath(
 
                pch = strdup ((char *) GETMESSAGE (40, 15,
                        " No session name was provided for the -session command line option."));
-               if (!pch) 
+               if (pch) 
                { 
                    DtMsgLogMessage (argv[0], DtMsgLogWarning, pch);
                    free (pch);
@@ -714,9 +719,9 @@ SetRestorePath(
      */
     if(smGD.compatMode == True)
     {
-       smGD.clientPath[0] = NULL;
-       smGD.resourcePath[0] = NULL;
-       smGD.settingPath[0] = NULL;
+       smGD.clientPath[0] = 0;
+       smGD.resourcePath[0] = 0;
+       smGD.settingPath[0] = 0;
        smGD.sessionType = DEFAULT_SESSION;
        smGD.restoreSession = NULL;
         return(0);
@@ -836,7 +841,7 @@ SetSysDefaults( void )
      */
     strcpy(smGD.resourcePath, "");
     strcpy(smGD.clientPath, "");
-    smGD.settingPath[0] = NULL;
+    smGD.settingPath[0] = 0;
     smGD.sessionType = DEFAULT_SESSION;
     smGD.restoreSession = (char *) SM_SYSTEM_DIRECTORY;
 
@@ -845,7 +850,7 @@ SetSysDefaults( void )
     {
         strcat(smGD.clientPath, "/");
         strncat(smGD.clientPath, langSpec, MAXPATHLEN-2);
-        smGD.clientPath[MAXPATHLEN-1];
+        smGD.clientPath[MAXPATHLEN-1] = 0;
     }
     
     strcat(smGD.clientPath, "/");
@@ -865,8 +870,8 @@ SetSysDefaults( void )
             PrintErrnoError(DtError, GETMESSAGE(4, 2,
                         "No defaults files exist.  "
                         "No applications will be restarted."));
-            smGD.clientPath[0] = NULL;
-            smGD.resourcePath[0] = NULL;
+            smGD.clientPath[0] = 0;
+            smGD.resourcePath[0] = 0;
         }
         else
         {
@@ -881,8 +886,8 @@ SetSysDefaults( void )
                 PrintErrnoError(DtError, GETMESSAGE(4, 3,
                             "No defaults files exist.  "
                             "No applications will be restarted."));
-                smGD.clientPath[0] = NULL;
-                smGD.resourcePath[0] = NULL;
+                smGD.clientPath[0] = 0;
+                smGD.resourcePath[0] = 0;
             }
         }
     }
@@ -934,13 +939,13 @@ SetResSet( void )
     status = stat(smGD.resourcePath, &buf);
     if(status == -1)
     {
-        smGD.resourcePath[0] = NULL;
+        smGD.resourcePath[0] = 0;
     }
 
     status = stat(smGD.settingPath, &buf);
     if(status == -1)
     {
-        smGD.settingPath[0] = NULL;
+        smGD.settingPath[0] = 0;
     }
     return(0);
 }
@@ -954,7 +959,7 @@ SetResSet( void )
  *
  *************************************<->***********************************/
 void 
-UndoSetSavePath ( )
+UndoSetSavePath (void)
 {
        char                    * buf;
 
@@ -1039,9 +1044,9 @@ SetSavePath(
     if(smGD.savePath == NULL)
     {
         PrintErrnoError(DtError, smNLS.cantCreateDirsString);
-        smGD.clientPath[0] = NULL;
-        smGD.settingPath[0] = NULL;
-        smGD.resourcePath[0] = NULL;
+        smGD.clientPath[0] = 0;
+        smGD.settingPath[0] = 0;
+        smGD.resourcePath[0] = 0;
         return(-1);
     }
         
@@ -1098,34 +1103,43 @@ SetSavePath(
             * runs a Current session and saves the session.
             */
            strcpy (savedDir, smGD.clientPath);
-            sprintf(smGD.etcPath, "%s.%s", smGD.clientPath, SM_OLD_EXTENSION);
+            snprintf(smGD.etcPath, sizeof(smGD.etcPath), "%s.%s", smGD.clientPath, SM_OLD_EXTENSION);
             status = stat(smGD.etcPath, &buf);
             if(status == 0)
            {
                char            * tmpName;
-               char            * tmpDir;
-
-                strcpy (savedOldDir, smGD.etcPath);
-
-               tmpName = (char *) XtMalloc (strlen (smGD.restoreSession) + 2);
-               sprintf (tmpName, "%s.", smGD.restoreSession);
-               if (strlen (tmpName) > 5) {
-                       tmpName[4] = '.';
-                       tmpName[5] = '\000';
-               }
-               tmpDir = (char *) tempnam (smGD.savePath, tmpName);
-                MoveDirectory (smGD.etcPath, tmpDir, False);
-
-               strcpy (savedTmpDir, tmpDir);
-               free (tmpDir);
-               XtFree ((char *) tmpName);
+                int len, tfd;
+
+                strcpy(savedOldDir, smGD.etcPath);
+
+                len = strlen(smGD.savePath) + strlen(smGD.restoreSession) 
+                  + strlen("XXXXXX") + 3;
+               tmpName = (char *) XtCalloc(1, len);
+
+               sprintf(tmpName, "%s/%s.XXXXXX", smGD.savePath, 
+                        smGD.restoreSession);
+
+                if ((tfd = mkstemp(tmpName)) == -1)
+                  {
+                    PrintErrnoError(DtError, smNLS.cantCreateDirsString);
+                  }
+                else
+                  {
+                    close(tfd);
+                    unlink(tmpName);
+
+                    MoveDirectory(smGD.etcPath, tmpName, False);
+                    
+                    strncpy(savedTmpDir, tmpName, len - 1);
+                  }
+                XtFree((char *) tmpName);
            }
            MoveDirectory(smGD.clientPath, smGD.etcPath, False);
         }
 
-        smGD.clientPath[0] = NULL;
-        smGD.settingPath[0] = NULL;
-        smGD.resourcePath[0] = NULL;
+        smGD.clientPath[0] = 0;
+        smGD.settingPath[0] = 0;
+        smGD.resourcePath[0] = 0;
     }
     else
     {
@@ -1141,12 +1155,15 @@ SetSavePath(
             if(status == -1)
             {
                 PrintErrnoError(DtError, smNLS.cantCreateDirsString);
-                smGD.clientPath[0] = NULL;
-                smGD.settingPath[0] = NULL;
-                smGD.resourcePath[0] = NULL;
+                smGD.clientPath[0] = 0;
+                smGD.settingPath[0] = 0;
+                smGD.resourcePath[0] = 0;
                 return(-1);
             }
-            chmod(smGD.clientPath, 0755);
+            if(-1 == chmod(smGD.clientPath, 0755))
+            {
+                               fprintf(stderr, "%s chmod error %s\n", smGD.clientPath, strerror(errno));
+                       }
         }
         else
         {
@@ -1159,25 +1176,35 @@ SetSavePath(
             * save is complete.
             */
            char                * tmpName;
-           char                * tmpDir;
 
-            sprintf(smGD.etcPath, "%s.%s", smGD.clientPath, SM_OLD_EXTENSION);
+            snprintf(smGD.etcPath, sizeof(smGD.etcPath), "%s.%s", smGD.clientPath, SM_OLD_EXTENSION);
             status = stat(smGD.etcPath, &buf);
             if(status == 0)
            {
-               tmpName = (char *) XtMalloc (strlen (smGD.restoreSession) + 2);
-               sprintf (tmpName, "%s.", smGD.restoreSession);
-                strcpy (savedOldDir, smGD.etcPath);
-               if (strlen (tmpName) > 5) {
-                       tmpName[4] = '.';
-                       tmpName[5] = '\000';
-               }
-               tmpDir = (char *) tempnam (smGD.savePath, tmpName);
-               MoveDirectory (smGD.etcPath, tmpDir, False);
-
-               strcpy (savedTmpDir, tmpDir);
-               free (tmpDir);
-               XtFree ((char *) tmpName);
+              int len, tfd;
+
+              len = strlen(smGD.savePath) + strlen(smGD.restoreSession) 
+                + strlen("XXXXXX") + 3;
+              tmpName = (char *) XtCalloc(1, len);
+              snprintf(tmpName, len, "%s/%s.XXXXXX", smGD.savePath, 
+                      smGD.restoreSession);
+
+              strcpy (savedOldDir, smGD.etcPath);
+
+              if ((tfd = mkstemp(tmpName)) == -1)
+                {
+                  PrintErrnoError(DtError, smNLS.cantCreateDirsString);
+                }
+              else
+                {
+                  close(tfd);
+                  unlink(tmpName);
+
+                  MoveDirectory (smGD.etcPath, tmpName, False);
+              
+                  strcpy (savedTmpDir, tmpName);
+                }
+              XtFree((char *) tmpName);
            }
 
             MoveDirectory(smGD.clientPath, smGD.etcPath, False);
@@ -1188,13 +1215,21 @@ SetSavePath(
               status = mkdir(smGD.clientPath, 0000);
               if(status == -1)
               {
+                 PrintErrnoError(DtError, smNLS.cantCreateDirsString);
+                 smGD.clientPath[0] = 0;
+                 smGD.settingPath[0] = 0;
+                 smGD.resourcePath[0] = 0;
+                 return(-1);
+               }
+            status = chmod(smGD.clientPath, 0755);
+            if(status == -1)
+              {
                 PrintErrnoError(DtError, smNLS.cantCreateDirsString);
-                smGD.clientPath[0] = NULL;
-                smGD.settingPath[0] = NULL;
-                smGD.resourcePath[0] = NULL;
+                smGD.clientPath[0] = 0;
+                smGD.settingPath[0] = 0;
+                smGD.resourcePath[0] = 0;
                 return(-1);
-            }
-            chmod(smGD.clientPath, 0755);
+              }
         }
         
         strcat(smGD.clientPath, "/");
@@ -1249,7 +1284,7 @@ SetFontSavePath(char *langPtr)
         sessionSaved = SM_HOME_FONT_DIRECTORY;
     }
 
-    sprintf(smGD.fontPath, "%s/%s", smGD.savePath, sessionSaved);
+    snprintf(smGD.fontPath, MAXPATHLEN, "%s/%s", smGD.savePath, sessionSaved);
     status = stat(smGD.fontPath, &buf);
     if(status == -1)
     {
@@ -1257,17 +1292,20 @@ SetFontSavePath(char *langPtr)
         if(status == -1)
         {
             PrintErrnoError(DtError, smNLS.cantCreateDirsString);
-            smGD.fontPath[0] = NULL;
+            smGD.fontPath[0] = 0;
             return(-1);
         }
-        chmod(smGD.fontPath, 0755);
+        if(-1 == chmod(smGD.fontPath, 0755))
+        {
+                       fprintf(stderr, "%s chmod error %s\n", smGD.fontPath, strerror(errno)); 
+               }
     }
 
     /*
      * Now add the lang subdirectory and see if it exists
      */
-    strcat(smGD.fontPath, "/");
-    strcat(smGD.fontPath, langPtr);
+    strncat(smGD.fontPath, "/", MAXPATHLEN);
+    strncat(smGD.fontPath, langPtr, MAXPATHLEN);
     status = stat(smGD.fontPath, &buf);
     if(status == -1)
     {
@@ -1275,10 +1313,16 @@ SetFontSavePath(char *langPtr)
         if(status == -1)
         {
             PrintErrnoError(DtError, smNLS.cantCreateDirsString);
-            smGD.fontPath[0] = NULL;
+            smGD.fontPath[0] = 0;
             return(-1);
         }
-        chmod(smGD.fontPath, 0755);
+        status = chmod(smGD.fontPath, 0755);
+        if(status == -1)
+          {
+            PrintErrnoError(DtError, smNLS.cantCreateDirsString);
+            smGD.fontPath[0] = 0;
+            return(-1);
+          }
     }
 
     return(0);
@@ -1352,11 +1396,11 @@ RemoveFiles(
 
         _DtEnvControl(DT_ENV_RESTORE_PRE_DT);
         
-#ifdef __osf__
+#if defined(CSRG_BASED)
         setsid();
 #else
         (void) setpgrp();
-#endif /* __osf__ */
+#endif /* CSRG_BASED */
          
         execStatus = execlp("rm","rm", "-rf", path, (char *) 0);
         if(execStatus != 0)
@@ -1457,11 +1501,11 @@ MoveDirectory(
 
         _DtEnvControl(DT_ENV_RESTORE_PRE_DT);
         
-#ifdef __osf__
+#if defined(CSRG_BASED)
         setsid();
 #else
         (void) setpgrp();
-#endif /* __osf__ */
+#endif /* CSRG_BASED */
         
         execStatus = execlp("mv","mv", pathFrom, pathTo, (char *) 0);
         if(execStatus != 0)
@@ -1626,7 +1670,7 @@ TrimErrorlog( void )
     
     len = strlen(home) + strlen(DtPERSONAL_CONFIG_DIRECTORY) + 2;
     if (len > MAXPATHLEN) savePath = SM_REALLOC(savePath, len);
-    sprintf(savePath, "%s/%s", home, DtPERSONAL_CONFIG_DIRECTORY);
+    snprintf(savePath, len, "%s/%s", home, DtPERSONAL_CONFIG_DIRECTORY);
     
     /*
      * If errorlog.old exists and it is not empty, delete
@@ -1655,7 +1699,7 @@ TrimErrorlog( void )
      */
     if (len + strlen(DtERRORLOG_FILE) > MAXPATHLEN)
       checkPath1 = SM_REALLOC(savePath, len + strlen(DtERRORLOG_FILE));
-    sprintf(checkPath1, "%s/%s", savePath, DtERRORLOG_FILE);
+    snprintf(checkPath1, len + strlen(DtERRORLOG_FILE), "%s/%s", savePath, DtERRORLOG_FILE);
 
     status = stat(checkPath1, &buf);
     if((status != -1) && (buf.st_size > 0))
@@ -2346,11 +2390,11 @@ InitializeSpecificSession (
                                if (len > MAXPATHLEN)
                                  alt_dir = XtRealloc(alt_dir, len + 1);
 
-                               (void) sprintf (alt_dir, "%s/%s/%s/%s",
-                                               home,
-                                               DtPERSONAL_CONFIG_DIRECTORY,
-                                               DtSM_SESSION_DIRECTORY,
-                                               SM_HOME_DIRECTORY);
+                               snprintf(alt_dir, len, "%s/%s/%s/%s",
+                                         home,
+                                         DtPERSONAL_CONFIG_DIRECTORY,
+                                         DtSM_SESSION_DIRECTORY,
+                                         SM_HOME_DIRECTORY);
 
                                if (!SetAlternateSession (session_dir, 
                                                          alt_dir, 
@@ -2406,6 +2450,12 @@ InitializePaths (
        char            *db_file = (char *) XtMalloc(MAXPATHLEN);
        struct stat     buf;
 
+        if (!db_file)
+          {
+            PrintError(DtError, smNLS.cantMallocErrorString);
+            return;
+          }
+
        smGD.savePath = _DtCreateDtDirs(disp);
 
        (void) sprintf (smGD.settingPath, "%s/%s/%s", 
@@ -2428,7 +2478,7 @@ InitializePaths (
                if ((stat(db_file, &buf)) == 0)
                        (void) strcpy (smGD.clientPath, db_file);
        }
-       if (db_file) XtFree(db_file);
+       XtFree(db_file);
 }
 
 
@@ -2466,6 +2516,13 @@ SetAlternateSession (
        char            *db_file2 = (char *) XtMalloc(MAXPATHLEN);
        struct stat     buf;
 
+        if (!db_file1 || !db_file2)
+          {
+            PrintError(DtError, smNLS.cantMallocErrorString);
+            return False;
+          }
+
+
        if ((stat (session_dir, &buf)) != 0) {
                /*
                 * The requested dir does not exist, create it
@@ -2547,15 +2604,15 @@ SetAlternateSession (
                /*
                 * The directory for the specified session exists
                 * but it doesn't have any client databases.  Start
-                * a new user session.  If a user wants a sesssion
+                * a new user session.  If a user wants a session
                 * with no apps, they should create a zero-length
                 * session database.
                 */
                SetSysDefaults ();
        }
 
-       if (db_file1) XtFree(db_file1);
-       if (db_file2) XtFree(db_file2);
+       XtFree(db_file1);
+       XtFree(db_file2);
        return (True);
 }
 
@@ -2580,7 +2637,7 @@ SmExit (
 }
 
 void
-SetSIGPIPEToDefault ()
+SetSIGPIPEToDefault (void)
 {
        struct sigaction pipeSig;