dtdocbook: Coverity 86763
[oweals/cde.git] / cde / programs / dtsession / SmRestore.c
index 7dbcbe0f14c3f907f43267dfb09b91b82eb4f085..5b1dfd526da194d63455b800b37ad98627467c26 100644 (file)
@@ -1,3 +1,25 @@
+/*
+ * CDE - Common Desktop Environment
+ *
+ * Copyright (c) 1993-2012, The Open Group. All rights reserved.
+ *
+ * These libraries and programs are free software; you can
+ * redistribute them and/or modify them under the terms of the GNU
+ * Lesser General Public License as published by the Free Software
+ * Foundation; either version 2 of the License, or (at your option)
+ * any later version.
+ *
+ * These libraries and programs are distributed in the hope that
+ * they will be useful, but WITHOUT ANY WARRANTY; without even the
+ * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU Lesser General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * 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
+ */
 /* $TOG: SmRestore.c /main/26 1998/12/14 20:13:07 mgreess $ */
 /*                                                                      *
  * (c) Copyright 1993, 1994 Hewlett-Packard Company                     *
 #include <stdio.h>
 #include <fcntl.h>
 #include <unistd.h>
+#include <string.h>
 #ifdef _SUN_OS   /* to get the define for NOFILE */
 #include <sys/param.h>
 #endif /* _SUN_OS */
 #include <sys/types.h>
 #include <stdlib.h>
-#ifdef __apollo
-#include <X11/apollosys.h>        /* for pid_t struct in hp-ux sys/types.h */
-#endif
 #include <sys/socket.h>
+#include <sys/wait.h>
 #include <netinet/in.h>
 #include <netdb.h>
 #include <errno.h>
@@ -74,6 +95,7 @@
 #include <Dt/MsgLog.h>
 #include <bms/spc.h>
 #include <Dt/CmdInv.h>
+#include <Dt/ActionUtilP.h>
 
 #include "Sm.h"
 #include "SmResource.h"
 #include "SmDB.h"
 #include "SmProp.h"
 
-#ifndef __osf__
 #include <X11/Xlibint.h>
-#endif /* __osf__ */
 
 /*
  * Local variables
@@ -109,7 +129,7 @@ static char * savedDisplay = NULL;
 
 #define SM_MAX_ARGS MAXLINE
 
-#if defined (SVR4) || defined (__osf__)
+#if defined (SVR4)
 #define REMOTE_CMD_STRING "rsh %s -n %s &"
 #else
 #define REMOTE_CMD_STRING "remsh %s -n %s &"
@@ -312,6 +332,8 @@ static void RemoteRequestSucceeded(char *, void *);
 static void SetTemporaryDisplay (
        int                     screenNum);
 
+int RestorePreferences(char *filename);
+
 static void RestoreDisplay (
        int                     screenNum);
 
@@ -451,11 +473,11 @@ SystemCmd (char *pchCmd)
 {
     void (*signalHandler) ();
 
-    signalHandler = (void (*)())signal (SIGCLD, SIG_DFL);
+    signalHandler = (void (*)())signal (SIGCHLD, SIG_DFL);
 
     system (pchCmd);
 
-    signal (SIGCLD, signalHandler);
+    signal (SIGCHLD, signalHandler);
 } /* END OF FUNTION SystemCmd */
 
 
@@ -564,7 +586,7 @@ RestoreState( void )
              fileSize = MAXLINE + 1;
          }
 
-         line = (unsigned char *) malloc ((fileSize + 1) * sizeof(char *));
+         line = malloc(fileSize + 1);
          if (line == NULL)
          {
              line = fallBackLine;
@@ -628,7 +650,7 @@ StartWM( void )
     Boolean goodWmStartup = True;
     int status;
   
-    if((smGD.wmStartup == NULL) || (*smGD.wmStartup == NULL))
+    if((smGD.wmStartup == NULL) || (*smGD.wmStartup == 0))
     {
        ForkWM();
     }
@@ -802,6 +824,8 @@ RestoreResources( Boolean errorHandlerInstalled, ... )
     char *argv[20]; 
     va_list  args;
 
+#if 0
+    /* JET - this seems like a bad (and unused) idea */
     /*
      * Check for alternate resource loader.
      */
@@ -809,6 +833,9 @@ RestoreResources( Boolean errorHandlerInstalled, ... )
      {
        pgrm = CDE_INSTALLATION_TOP "/bin/dtsession_res";
      }
+#else
+       pgrm = CDE_INSTALLATION_TOP "/bin/dtsession_res";
+#endif
 
     /*
      * By convention, exec() wants arg0 to be the program name. Ex: if pgrm
@@ -874,11 +901,11 @@ RestoreResources( Boolean errorHandlerInstalled, ... )
        setegid(smGD.runningGID);
 #endif
 #endif
-#ifdef __osf__
+#if defined(CSRG_BASED)
         setsid();
 #else
        (void) setpgrp();
-#endif /* __osf__ */
+#endif /* CSRG_BASED */
 
        MarkFileDescriptors (3, F_SETFD, 1);
 
@@ -888,7 +915,9 @@ RestoreResources( Boolean errorHandlerInstalled, ... )
        {
             char   clientMessage[MAXPATHLEN + 256];
 
-           sprintf(clientMessage, ((char *)GETMESSAGE(16, 1, "Unable to exec process %s.  No session resources will be restored.")), pgrm);
+            memset(clientMessage, 0, MAXPATHLEN + 256);
+           snprintf(clientMessage, (MAXPATHLEN + 256) - 1,
+                     ((char *)GETMESSAGE(16, 1, "Unable to exec process %s.  No session resources will be restored.")), pgrm);
            PrintErrnoError(DtError, clientMessage);
            SM_EXIT(-1);
        }
@@ -1083,7 +1112,7 @@ RestoreSettings( void )
     /*
      * Load the resources from the SM database file
      */
-    if (smGD.settingPath[0] != NULL)
+    if (smGD.settingPath[0] != 0)
     {
        smBase = XrmGetFileDatabase(smGD.settingPath);
 
@@ -1258,7 +1287,7 @@ RestoreSettings( void )
            ptrSize += 50;
            restorePtrArray = (char **)SM_REALLOC((char *)
                                                  restorePtrArray, ptrSize *
-                                                 sizeof(char **));
+                                                 sizeof(char *));
            if(restorePtrArray == NULL)
            {
                PrintErrnoError(DtError, smNLS.cantMallocErrorString);
@@ -1352,7 +1381,7 @@ RestoreSettings( void )
                /*
                 * This is only involked when there is auto repeats set for
                 * specific keys only.  It is VERY SLOW code so unless you
-                * have to save off auto repeats for single keys - DONT
+                * have to save off auto repeats for single keys - DON'T
                 */
                while(tmpKey != NULL)
                {
@@ -1543,7 +1572,7 @@ RestoreIndependentResources( void )
     Boolean resIndep = False, resRet;
     char *resdata;
 
-    if(((smGD.sessionLang == NULL) || (*smGD.sessionLang == NULL)) &&
+    if(((smGD.sessionLang == NULL) || (*smGD.sessionLang == 0)) &&
        (smRes.displayResolution == 0))
     {
        /*
@@ -1611,8 +1640,10 @@ RestoreIndependentResources( void )
            sessionType = SM_CURRENT_FONT_DIRECTORY;
        }
 
-       sprintf(fontPath, "%s/%s/%s/%s.%s", smGD.savePath, sessionType,
-               currentLangPtr, SM_FONT_FILE, sessionRes);
+        memset(fontPath, 0, MAXPATHLEN + 1);
+       snprintf(fontPath, MAXPATHLEN, "%s/%s/%s/%s.%s",
+                 smGD.savePath, sessionType,
+                 currentLangPtr, SM_FONT_FILE, sessionRes);
        status = stat(fontPath, &buf);
        if(status == -1)
        {
@@ -1620,16 +1651,16 @@ RestoreIndependentResources( void )
             * User has nothing there - look in the system defaults
             * first in the language dep -then in lang independent
             */
-            fontPath[0] = '\0';
+            memset(fontPath, 0, MAXPATHLEN + 1);
 
             if((currentLangPtr != NULL) && (*currentLangPtr != 0))
             {
                 strcat(fontPath, "/");
-                strcat(fontPath, currentLangPtr);
+                strncat(fontPath, currentLangPtr, MAXPATHLEN);
             }
 
-            strcat(fontPath, "/");
-            strcat(fontPath, SM_SYSTEM_FONT_FILE);
+            strncat(fontPath, "/", MAXPATHLEN);
+            strncat(fontPath, SM_SYSTEM_FONT_FILE, MAXPATHLEN);
 
             FixPath(fontPath);
 
@@ -1639,8 +1670,9 @@ RestoreIndependentResources( void )
                 if((currentLangPtr != NULL) && (*currentLangPtr != 0) &&
                    (strcmp(currentLangPtr, "C")))
                 {
-                   strcpy(fontPath, "/C/");
-                    strcat(fontPath, SM_SYSTEM_FONT_FILE);
+                    memset(fontPath, 0, MAXPATHLEN + 1);
+                    strcpy(fontPath, "/C/");
+                    strncat(fontPath, SM_SYSTEM_FONT_FILE, MAXPATHLEN);
 
                     FixPath(fontPath);
                
@@ -1760,7 +1792,7 @@ RestoreIndependentResources( void )
  *  If this is the first DT 3.0 session for a DT 2.0 user then we will
  *  also launch the helpviewer. If this is the first DT 3.0 session for a
  *  DT 2.0 user then we will also run convertVS.sh to change all
- *  occurances of /usr/bin/X11/hpterm to .../dt/bin/hpterm,
+ *  occurrences of /usr/bin/X11/hpterm to .../dt/bin/hpterm,
  *  /usr/bin/X11/xterm to .../dt/bin/xterm and
  *  /usr/bin/X11/xload to .../dt/bin/xload.
  *
@@ -1784,7 +1816,7 @@ static int
 RestoreClients( void )
 {
     unsigned char *lineP, *string;
-    char *pch, *dispPtr;
+    char *pch, *dispPtr = NULL;
     char *dispEnv, *dispSav, *dispEnvHelpview, *dispSavHelpview;
     unsigned char *hostPtr=NULL, *cmdPtr=NULL, *hintPtr = NULL;
     unsigned char *remoteDisplay;
@@ -1976,6 +2008,7 @@ RestoreClients( void )
                            SM_FREE((char *) remoteBuf[i]);
                        }
                    }
+                   free(displayName);
                    return(-1);
                }
                hintPtr = NULL;
@@ -2034,6 +2067,7 @@ RestoreClients( void )
                                SM_FREE((char *) remoteBuf[i]);
                            }
                        }
+                        free(displayName);
                        return(-1);
                    }
                    cmdPtr = NULL;
@@ -2419,12 +2453,12 @@ GetNextLine( void )
     {
        string = (unsigned char *) fgets((char *)line, fileSize, cfileP);
     }
-    else if ((parseP != NULL) && (*parseP != NULL))
+    else if ((parseP != NULL) && (*parseP != 0))
     /* read parse string */
     {
        string = line;
 #ifdef MULTIBYTE
-       while ((*parseP != NULL) &&
+       while ((*parseP != 0) &&
                ((chlen = mblen ((char *) parseP, MB_CUR_MAX)) > 0) &&
               (*parseP != '\n'))
        /* copy all but NULL and newlines to line buffer */
@@ -2441,7 +2475,7 @@ GetNextLine( void )
            *(string++) = *(parseP++);
         }
 #endif
-       *string = NULL;
+       *string = 0;
        if (*parseP == '\n')
        {
            parseP++;
@@ -2781,7 +2815,7 @@ GetSmartString(
        lnwsP++;
        if (lnwsP < endP)
         {
-           *lnwsP = NULL;
+           *lnwsP = 0;
         }
     }
 
@@ -2845,9 +2879,9 @@ GetSmartString(
      *   NULL -> point to NULL 
      */
 
-    if (*endP != NULL)
+    if (*endP != 0)
     {
-       *endP = NULL;       /* write NULL over terminator */
+       *endP = 0;       /* write NULL over terminator */
        *linePP = ++curP;   /* point beyond terminator */
     }
     else
@@ -3466,14 +3500,20 @@ StartClient(
                                        smRes.ignoreEnvironment, ',');
        }
 
-       if (!defaultCwd) {
-               if (getenv ("HOME"))
-                       defaultCwd = strdup (getenv ("HOME"));
-               else
-                       defaultCwd = getcwd (NULL, MAXPATHLEN + 1);
-
-               (void) gethostname (localHost, MAXHOSTNAMELEN);
-       }
+       if (!defaultCwd) 
+          {
+            char *tstr = getenv("HOME");
+            if (tstr)
+              {
+                int slen = strlen(tstr) + 1;
+                defaultCwd = XtCalloc(1, slen);
+                snprintf(defaultCwd, slen, "%s", tstr);
+              }
+            else
+              defaultCwd = getcwd (NULL, MAXPATHLEN + 1);
+            
+            (void) gethostname (localHost, MAXHOSTNAMELEN);
+          }
 
        if (!cwd) {
                cwdNull = True;
@@ -3567,7 +3607,7 @@ StartLocalClient (
            else
                tmpEnv = envp;
 
-           for (ppchar = tmpEnv; ppchar && *ppchar; *ppchar++) 
+           for (ppchar = tmpEnv; ppchar && *ppchar; ppchar++) 
                putenv (strdup (*ppchar));
        }
 
@@ -3585,11 +3625,11 @@ StartLocalClient (
 
        _DtEnvControl(DT_ENV_RESTORE_PRE_DT);
 
-#ifdef __osf__
+#if defined(CSRG_BASED)
         setsid();
 #else
        (void)setpgrp();
-#endif /* __osf__ */
+#endif /* CSRG_BASED */
        
        MarkFileDescriptors (3, F_SETFD, 1);
 
@@ -3821,11 +3861,11 @@ ForkWM( void )
 #endif
        _DtEnvControl(DT_ENV_RESTORE_PRE_DT);
 
-#ifdef __osf__
+#if defined(CSRG_BASED)
         setsid();
 #else
         (void)setpgrp();
-#endif /* __osf__ */
+#endif /* CSRG_BASED */
 
        MarkFileDescriptors (3, F_SETFD, 1);
 
@@ -3906,11 +3946,11 @@ KillParent( void )
     /*
      * Disassociate from parent
      */
-#ifdef __osf__
+#if defined(CSRG_BASED)
     setsid();
 #else
     setpgrp();
-#endif /* __osf__ */
+#endif /* CSRG_BASED */
 }
 
 
@@ -4018,7 +4058,7 @@ WaitWMTimeout(
  *
  *  Description:
  *  -----------
- *  This is the event handler registered to recieve the client message
+ *  This is the event handler registered to receive the client message
  *  from dtwm when dtwm is ready for business
  *
  *
@@ -4067,14 +4107,14 @@ FixEnvironmentData( void )
    int i;
    extern char **environ;  /* MODIFIED - DISPLAY is remove if found. */
 
-   for (i=0, ppchar = environ; *ppchar; *ppchar++, i++)
+   for (i=0, ppchar = environ; *ppchar; ppchar++, i++)
    {
       if ((strncmp (*ppchar, DISPLAY_NAME_EQUAL, strlen(DISPLAY_NAME_EQUAL))) == 0)
       {
          /*
          * Change the DISPLAY environment variable.
          */
-        for (; *ppchar; *ppchar++, i++)
+        for (; *ppchar; ppchar++, i++)
         {
             environ[i]=environ[i+1];
         }
@@ -4732,7 +4772,7 @@ char ** RemoveEnvironmentVars (
        if (!envp)
                return (NULL);
 
-       for (count = 0, ppchar = envp; ppchar && *ppchar; count++, *ppchar++) ;
+       for (count = 0, ppchar = envp; ppchar && *ppchar; count++, ppchar++) ;
 
        retEnv = (char **) XtMalloc ((count + 1) * sizeof (char *));
        if (!retEnv)
@@ -4740,7 +4780,7 @@ char ** RemoveEnvironmentVars (
 
        if (!ignoreEnvPtr) {
                for (count = 0, ppchar = envp; ppchar && *ppchar; 
-                       count++, *ppchar++) {
+                       count++, ppchar++) {
                        retEnv[count] = *ppchar;
                }
                retEnv[count] = NULL;
@@ -4748,11 +4788,11 @@ char ** RemoveEnvironmentVars (
                return (retEnv);
        }
 
-       for (count = 0, ppchar = envp; ppchar && *ppchar; *ppchar++) {
+       for (count = 0, ppchar = envp; ppchar && *ppchar; ppchar++) {
 
                found = False;
 
-               for (ppchar2 = ignoreEnvPtr; ppchar2 && *ppchar2; *ppchar2++) {
+               for (ppchar2 = ignoreEnvPtr; ppchar2 && *ppchar2; ppchar2++) {
 
                        if ((!strncmp (*ppchar, *ppchar2, strlen (*ppchar2))) &&
                            (((*ppchar)[strlen(*ppchar2)]) == '=')) {
@@ -4791,7 +4831,7 @@ MarkFileDescriptors (
 #ifdef _SUN_OS
                open_max = NOFILE;
 #else
-#if defined(USL) || defined(__uxp__) || defined(_AIX)
+#if defined(_AIX)
                open_max = FOPEN_MAX;
 #else
                open_max = FD_SETSIZE;