Source

replaceables / Win16bit / _TMPPATH.C

Full commit
/* Platform specific - Win16-bit
version of 'replacables_GetTempPath()'/ */

#include <StdLib.H>
#include <String.H>
#include "Stds\DLStdInc.H"
#pragma hdrstop

#include "Replace\Win16bit\_TmpPath.H"
/*-------------------------------------------------------------------------*/
static void DoCopy(char* TargetBuffer, DWORD TargetBuffSize, const char* SourceString);
/*-------------------------------------------------------------------------*/
void replaceables_GetTempPath(
	DWORD nBufferLength, /* # chars buffer can take */
   char* lpBuffer /* Address of buffer to take string */
)
{
	/* In the Win32 API there is a function called GetTempPath(),
   unfortunately this is unavailable to us! */
   char* Env; /* Current environment */

   Env = getenv("TMP"); /* Get TMP environment string */
   if (Env)
   {
   	DoCopy(lpBuffer, nBufferLength, Env);
      return;
   }

   Env = getenv("TEMP"); /* Use TEMP environment string instead */
   if (Env)
   {
   	DoCopy(lpBuffer, nBufferLength, Env);
      return;
   }
   /* If here, it means we must use the current directory for temp names */
   DoCopy(lpBuffer, nBufferLength, ".");
}
/*-------------------------------------------------------------------------*/
static void DoCopy(char* TargetBuffer, DWORD TargetBuffSize, const char* SourceString)
{
	/* This function safely copies 'SourceString' to 'TargetBuffer' even
   if TargetBuffer is much to small! <-- via truncation. */

   size_t SourceOffset = 0;

   if (!TargetBuffer || !TargetBuffSize || !SourceString) /* Any parameter invalid? */

   while (SourceOffset <= strlen(SourceString)) /* All through SourceString */
   {
   	TargetBuffer[SourceOffset] = SourceString[SourceOffset]; /* Copy a byte */
      SourceOffset++; /* Next we will have the next byte! */
      if (SourceOffset > TargetBuffSize) /* It seems we have copied it all! */
      	return;
   }
}
/*-------------------------------------------------------------------------*/