2828
2929#include "portab.h"
3030#include "globals.h"
31+ #include "debug.h"
32+
3133
32- #ifdef VERSION_STRINGS
33- static BYTE * RcsId =
34- "$Id: task.c 1563 2011-04-08 16:04:24Z bartoldeman $" ;
35- #endif
3634
3735#define toupper (c ) ((c) >= 'a' && (c) <= 'z' ? (c) + ('A' - 'a') : (c))
3836
@@ -130,6 +128,9 @@ STATIC UWORD SetverGetVersion(BYTE FAR *table, BYTE FAR *name)
130128 return 0 ;
131129}
132130
131+ /* allocate memory for and copy current process env to child environment
132+ returns segment of env MCB (not env block itself) in pChildEnvSeg
133+ */
133134STATIC COUNT ChildEnv (exec_blk * exp , UWORD * pChildEnvSeg , char far * pathname )
134135{
135136 BYTE FAR * pSrc ;
@@ -167,18 +168,27 @@ STATIC COUNT ChildEnv(exec_blk * exp, UWORD * pChildEnvSeg, char far * pathname)
167168 if (nEnvSize >= MAXENV - ENV_KEEPFREE )
168169 return DE_INVLDENV ;
169170
171+ /* loop until first double terminator '\0\0' found */
170172 if (* (UWORD FAR * ) (pSrc + nEnvSize ) == 0 )
171173 break ;
172174 }
173175 nEnvSize += 2 ; /* account for trailing \0\0 */
174176 }
175177
176- /* allocate enough space for env + path */
178+ /* allocate enough space for env + path (rounding up to nearest para)
179+ Note: we must allocate at least 1 paragraph (16 bytes) for empty environment
180+ + ENV_KEEPFREE for argv[0] (program name)
181+ */
182+ DebugPrintf (("PriPathName is %lu bytes\n" , sizeof (PriPathName )));
183+ assert (sizeof (PriPathName )+ 3 == ENV_KEEPFREE );
177184 if ((RetCode = DosMemAlloc ((nEnvSize + ENV_KEEPFREE + 15 )/16 ,
178185 mem_access_mode , pChildEnvSeg ,
179186 NULL /*(UWORD FAR *) MaxEnvSize ska */ )) < 0 )
187+ {
188+ DebugPrintf (("Error alloc Env space\n" ));
180189 return RetCode ;
181- pDest = MK_FP (* pChildEnvSeg + 1 , 0 );
190+ }
191+ pDest = MK_FP (* pChildEnvSeg + 1 , 0 ); /* skip past MCB and set pDest to start of env block */
182192
183193 /* fill the new env and inform the process of its */
184194 /* location throught the psp */
@@ -199,8 +209,10 @@ STATIC COUNT ChildEnv(exec_blk * exp, UWORD * pChildEnvSeg, char far * pathname)
199209 /* copy complete pathname */
200210 if ((RetCode = truename (pathname , PriPathName , CDS_MODE_SKIP_PHYSICAL )) < SUCCESS )
201211 {
212+ DebugPrintf (("Failed to get truename for env argv0\n" ));
202213 return RetCode ;
203214 }
215+ DebugPrintf (("ChildEnv for [%s]\n" , PriPathName ));
204216 fstrcpy (pDest , PriPathName );
205217
206218 /* Theoretically one could either:
@@ -472,6 +484,9 @@ COUNT DosComLoader(BYTE FAR * namep, exec_blk * exp, COUNT mode, COUNT fd)
472484 }
473485
474486 rc = ChildEnv (exp , & env , namep );
487+ #if DEBUG
488+ if (rc != SUCCESS ) DebugPrintf (("Failed to create ChildEnv\n" ));
489+ #endif
475490
476491 /* COMFILES will always be loaded in largest area. is that true TE */
477492 /* yes, see RBIL, int21/ah=48 -- Bart */
@@ -652,6 +667,9 @@ COUNT DosExeLoader(BYTE FAR * namep, exec_blk * exp, COUNT mode, COUNT fd)
652667 }
653668
654669 rc = ChildEnv (exp , & env , namep );
670+ #if DEBUG
671+ if (rc != SUCCESS ) DebugPrintf (("Failed to create ChildEnv\n" ));
672+ #endif
655673
656674 if (rc == SUCCESS )
657675 /* Now find out how many paragraphs are available */
0 commit comments