Back to index

lightning-sunbird  0.9+nobinonly
mapiutl.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 //
00038 //  Various utils needed for the MAPI functions
00039 //  Written by: Rich Pizzarro (rhp@netscape.com)
00040 //  November 1997
00041 //
00042 #include <windows.h> 
00043 #include <time.h> 
00044 #include <sys/stat.h>
00045 #include <io.h>
00046 #include "xpapi.h"
00047 #include "trace.h"
00048 #include "mapiipc.h"
00049 #include "mapiutl.h"
00050 
00051 //
00052 // Global variables
00053 //
00054 BOOL     gLoggingEnabled = FALSE;
00055 
00056 void
00057 SetLoggingEnabled(BOOL val)
00058 {
00059   gLoggingEnabled = val;
00060 }
00061 
00062 // Log File
00063 void 
00064 LogString(LPCSTR pStr1)
00065 {
00066   // Off of the declaration line...
00067   LPCSTR pStr2 = NULL;
00068   BOOL useStr1 = TRUE;
00069 
00070        if (gLoggingEnabled)
00071        {
00072               char tempPath[_MAX_PATH] = "";
00073     if (getenv("TEMP"))
00074     {
00075       lstrcpy((LPSTR) tempPath, getenv("TEMP"));  // environmental variable
00076     } 
00077 
00078               int len = lstrlen(tempPath);
00079               if ((len > 1) && tempPath[len - 1] != '\\')
00080               {
00081                      lstrcat(tempPath, "\\");
00082               }
00083               lstrcat(tempPath, szMapiLog);
00084               HFILE hFile = _lopen(tempPath, OF_WRITE);
00085               if (hFile == HFILE_ERROR)
00086               {
00087                      hFile = _lcreat(tempPath, 0);
00088               }
00089               if (hFile != HFILE_ERROR)
00090               {
00091                      _llseek(hFile, 0, SEEK_END);              // seek to the end of the file
00092                      LPCSTR pTemp = useStr1 ? pStr1 : pStr2;
00093                      _lwrite(hFile, pTemp, lstrlen(pTemp));
00094                      _lclose(hFile);
00095               }
00096        }
00097 }
00098 
00099 //
00100 // Find Communicator and return an HWND, if not, start Communicator,
00101 // then find an HWND
00102 //
00103 HWND 
00104 GetCommunicatorIPCWindow(void)
00105 {
00106   HWND  hWnd = NULL; 
00107   DWORD timeCount = 0;
00108   BOOL  launchTry = FALSE;
00109 
00110   //
00111   // This will wait for 10 seconds before giving up and failing
00112   //
00113   while ((hWnd == NULL) && (timeCount < 20))
00114   {
00115     if ((hWnd = FindWindow("AfxFrameOrView", NULL)) && !FindWindow("aHiddenFrameClass", NULL))
00116       return(hWnd);
00117     else if ((hWnd = FindWindow("aHiddenFrameClass", NULL)))
00118       return(hWnd);
00119 
00120     if (!launchTry)
00121     {
00122     char    szPath[_MAX_PATH] = "";
00123               DWORD   nMAPIERROR; 
00124 
00125       if ((nMAPIERROR = XP_GetInstallLocation(szPath, _MAX_PATH)) != SUCCESS_SUCCESS)
00126       {
00127         return(NULL); 
00128       }
00129               
00130                 WORD nReturn = XP_CallProcess(szPath, " -MAPICLIENT");
00131       launchTry = TRUE;
00132     }
00133 
00134     //
00135     // Pause for 1/2 a second and try to connect again...
00136     //
00137 #ifdef WIN32
00138     Sleep(500);
00139 #else 
00140     Yield();
00141 #endif        
00142 
00143     timeCount++;
00144   }
00145 
00146        return(hWnd);
00147 }
00148 
00149 void
00150 BuildMemName(LPSTR name, ULONG winSeed)
00151 {
00152   static DWORD    id = 0;
00153 
00154   if (id == 0)
00155   {
00156     // Seed the random-number generator with current time so that
00157     // the numbers will be different every time we run.
00158     srand( (unsigned)time( NULL ) );
00159     id = rand();
00160   }
00161 
00162   wsprintf(name, "MAPI_IPC_SMEM-%d", (winSeed + id++));
00163   TRACE("Shared Memory Name = [%s]\n", name);
00164 }
00165 
00166 DWORD 
00167 ValidateFile(LPCSTR szFile) 
00168 {
00169        struct _stat buf;
00170     int result;
00171 
00172     result = _stat( szFile, &buf );
00173     if (result != 0)
00174       return(1);
00175 
00176     if (!(buf.st_mode & S_IREAD))
00177       return(2);
00178 
00179     return(0);
00180 }
00181 
00182 //
00183 // return of zero is ok 
00184 // 1 = MAPI_E_ATTACHMENT_NOT_FOUND
00185 // 2 = MAPI_E_ATTACHMENT_OPEN_FAILURE
00186 //
00187 DWORD
00188 SanityCheckAttachmentFiles(lpMapiMessage lpMessage)
00189 {
00190   ULONG i;
00191   DWORD rc;
00192 
00193   for (i=0; i<lpMessage->nFileCount; i++)
00194   {
00195     if ((rc = ValidateFile(lpMessage->lpFiles[i].lpszPathName)) != 0)
00196     {
00197       return(rc);
00198     }
00199   }
00200 
00201   return(0);
00202 }
00203 
00204 DWORD 
00205 GetFileCount(LPSTR pFiles, LPSTR delimChar)
00206 {
00207   DWORD   count = 1;
00208        if ((!pFiles) || (!*pFiles))
00209               return(0);
00210 
00211   for (DWORD i=0; i<strlen(pFiles); i++)
00212   {
00213     if (pFiles[i] == delimChar[0])
00214     {
00215       ++count;
00216     }
00217   }
00218 
00219   return(count);
00220 }
00221 
00222 //
00223 // Extract a filename from a string
00224 // Return TRUE if file found, else FALSE
00225 //
00226 BOOL
00227 ExtractFile(LPSTR pFiles, LPSTR delimChar, DWORD fIndex, LPSTR fName)
00228 {
00229   LPSTR   ptr = pFiles;
00230   DWORD   loc;
00231   DWORD   count = 0;
00232 
00233        if ((!pFiles) || (!*pFiles))
00234               return(0);
00235 
00236   // Get to the fIndex'th entry
00237   for (loc=0; loc<strlen(pFiles); loc++)
00238   {
00239     if (count == fIndex)
00240       break;
00241     if (pFiles[loc] == delimChar[0])
00242       count++;
00243   }
00244 
00245   if (loc >= strlen(pFiles))    // Got to the end of string!
00246     return(FALSE);
00247 
00248   lstrcpy(fName, (LPSTR)pFiles + loc);
00249   //
00250   // Truncate at 2nd delimiter
00251   //
00252   for (DWORD i=0; i<strlen(fName); i++)
00253   {
00254     if (fName[i] == delimChar[0])
00255     {
00256       fName[i] = '\0';
00257       break;
00258     }
00259   }
00260 
00261   return(TRUE);
00262 }
00263 
00264 ULONG
00265 GetFileSize(LPSTR fName)
00266 {
00267   struct _stat buf;
00268   int result;
00269   
00270   result = _stat( fName, &buf );
00271   if (result != 0)
00272     return(0);
00273   
00274   return(buf.st_size);
00275 }
00276  
00277 LPVOID
00278 LoadBlobToMemory(LPSTR fName)
00279 {
00280   UCHAR     *ptr = NULL;
00281   ULONG     bufSize = GetFileSize(fName);
00282 
00283   if (bufSize == 0)
00284   {
00285     _unlink(fName);
00286     return(NULL);
00287   }
00288 
00289   ptr = (UCHAR *)malloc( (size_t) bufSize);
00290   if (!ptr)
00291   {
00292     _unlink(fName);
00293     return(NULL);
00294   }
00295 
00296   HFILE hFile = _lopen(fName, OF_READ);
00297   if (hFile == HFILE_ERROR)
00298   {
00299     _unlink(fName);
00300     free(ptr);
00301     return(NULL);
00302   }
00303 
00304   UINT numRead = _lread(hFile, ptr, (size_t) bufSize);
00305   _lclose(hFile);
00306 
00307   if (numRead != bufSize)
00308   {
00309     _unlink(fName);
00310     free(ptr);
00311     return(NULL);
00312   }
00313 
00314   _unlink(fName);
00315        return(ptr);  
00316 }
00317 
00318 LONG
00319 WriteMemoryBufferToDisk(LPSTR fName, LONG bufSize, LPSTR buf)
00320 {
00321   if (!buf)
00322   {
00323     return(-1);
00324   }
00325 
00326   HFILE hFile = _lcreat(fName, 0);
00327   if (hFile == HFILE_ERROR)
00328   {
00329     return(-1);
00330   }
00331 
00332   LONG writeCount = _lwrite(hFile, buf, (size_t) bufSize);
00333   _lclose(hFile);
00334 
00335   if (writeCount != bufSize)
00336   {
00337     _unlink(fName);
00338     return(-1);
00339   }
00340 
00341   return(0);
00342 }
00343 
00344 LPSTR
00345 GetTheTempDirectoryOnTheSystem(void)
00346 {
00347  static UCHAR retPath[_MAX_PATH];
00348   
00349   if (getenv("TEMP"))
00350   {
00351     lstrcpy((LPSTR) retPath, getenv("TEMP"));  // environmental variable
00352   } 
00353   else if (getenv("TMP"))
00354   {
00355     lstrcpy((LPSTR) retPath, getenv("TMP"));  // How about this environmental variable?
00356   }
00357   else
00358   {
00359     GetWindowsDirectory((LPSTR) retPath, sizeof(retPath));
00360   }
00361 
00362   return((LPSTR) &(retPath[0]));
00363 }
00364 
00365 #ifdef WIN16
00366 int WINAPI EXPORT ISGetTempFileName(LPCSTR a_pDummyPath,  LPCSTR a_pPrefix, UINT a_uUnique, LPSTR a_pResultName)
00367 {
00368 #ifdef GetTempFileName      // we need the real thing comming up next...
00369 #undef GetTempFileName
00370 #endif
00371        return GetTempFileName(0, a_pPrefix, a_uUnique, a_pResultName);
00372 }
00373 #endif
00374 
00375 LONG
00376 GetTempAttachmentName(LPSTR fName) 
00377 {
00378   UINT        res;
00379   static UINT uUnique = 1;
00380 
00381   if (!fName)
00382     return(-1);
00383 
00384   LPSTR szTempPath = GetTheTempDirectoryOnTheSystem();
00385 
00386 TRYAGAIN:
00387 #ifdef WIN32
00388   res = GetTempFileName(szTempPath, "MAPI", uUnique++, fName);
00389 #else
00390   res = ISGetTempFileName(szTempPath, "MAPI", uUnique++, fName);
00391 #endif
00392 
00393   if (ValidateFile(fName) != 1) 
00394   {
00395     if (uUnique < 32000)
00396     {
00397       goto TRYAGAIN;
00398     }
00399     else
00400     {
00401       return(-1);
00402     }
00403   }
00404 
00405   return 0;
00406 }
00407 
00408 // RICHIE - strip all of the HTML stuff out of the message...
00409 int
00410 CheckForInlineHTML(char *noteBody, DWORD len, DWORD *curPos, char *newBody, DWORD *realLen)
00411 {
00412   LPSTR   tags[] = {"&nbsp;", "&lt;", "&amp;", NULL};
00413   UCHAR   tagsSubst[] = {' ', '<', '&', NULL};
00414   int     x = 0;
00415 
00416   while (tags[x])
00417   {
00418     // should we check for first tag
00419     if ( (*curPos+strlen(tags[x])) < len)
00420     {
00421       if (strncmp(tags[x], noteBody, strlen(tags[x])) == 0)
00422       {
00423         *curPos += strlen(tags[x]) - 1;
00424         newBody[*realLen] = tagsSubst[x];
00425         *realLen += 1;
00426         return(-1);
00427       }
00428     }
00429 
00430     ++x;
00431   }
00432 
00433   return(0);
00434 }
00435 
00436 //
00437 // RICHIE - This is also temporary fix for now...
00438 //
00439 LPSTR
00440 StripSignedMessage(LPSTR noteText, DWORD totalCR) 
00441 {
00442 char    *newBuf;
00443 LPSTR   startTag = "<HTML>";
00444 LPSTR   endTag = "/HTML>";
00445 DWORD   i;
00446 DWORD   realLen = 0;
00447 DWORD   startPos = 0;
00448 DWORD   len = strlen(noteText);
00449 
00450   // create a new buffer...
00451   newBuf = (char *) malloc((size_t)(len + totalCR));
00452   if (!newBuf)
00453     return(noteText);
00454 
00455   newBuf[0] = '\0';
00456 
00457   // First, find the start of the HTML for the message...
00458   for (i=0; i<len; i++)
00459   {
00460     // should we check for first tag
00461     if ( (i+strlen(startTag)) < len)
00462     {
00463       if (strncmp(startTag, (noteText + i), strlen(startTag)) == 0)
00464       {
00465         startPos = i + strlen(startTag);
00466         break;
00467       }
00468     }
00469   }
00470 
00471   // Didn't find any HTML start tag
00472   if (i == len)
00473     return(noteText);
00474 
00475   BOOL    inHTML = FALSE;
00476   BOOL    firstChar = FALSE;
00477 
00478   for (i=startPos; i<len; i++)
00479   {
00480     char  *ptr = (noteText + i);
00481 
00482     if ( ((*ptr == 0x0D) || (*ptr == 0x20)) && (!firstChar) )
00483       continue;
00484     else
00485       firstChar = TRUE;
00486 
00487     // First, check for the end /HTML> tag
00488     if ( (i+strlen(endTag)) < len)
00489     {
00490       if (strncmp(endTag, ptr, strlen(endTag)) == 0)
00491       {
00492         break;
00493       }
00494     }
00495 
00496     // If we are in HTML, check for a ">"...
00497     if (inHTML)
00498     {
00499       if (*ptr == '>')
00500       {
00501         inHTML = FALSE;
00502       }
00503       continue;
00504     }
00505 
00506     // Check for NEW HTML...
00507     if (*ptr == '<')
00508     {
00509       inHTML = TRUE;
00510       continue;
00511     }
00512 
00513     if (CheckForInlineHTML(ptr, len, &i, newBuf, &realLen))
00514       continue;
00515 
00516     newBuf[realLen++] = *ptr;
00517     // Tack on a line feed if we hit a CR...
00518     if ( *ptr == 0x0D )
00519     {
00520       newBuf[realLen++] = 0x0A;
00521     }
00522   }
00523 
00524   // terminate the buffer - reallocate and move on...
00525   newBuf[realLen++] = '\0';
00526   newBuf = (LPSTR) realloc(newBuf, (size_t) realLen);
00527 
00528   // check if the realloc worked and if so, free old memory and
00529   // return...if not, just return the original buffer
00530   if (!newBuf)
00531   {
00532     return(noteText);
00533   }
00534   else
00535   {
00536     free(noteText);
00537     return(newBuf);
00538   }
00539 }
00540 
00541 //
00542 // RICHIE - this is a temporary fix for now to get rid of
00543 // html stuff within the text of a message - if there was a
00544 // valid noteText buffer coming into this call, we need to 
00545 // free it on the way out.
00546 //
00547 LPSTR
00548 StripHTML(LPSTR noteText)
00549 {
00550   char    *newBuf;
00551   LPSTR   signTag = "This is a cryptographically signed message in MIME format.";
00552   LPSTR   mimeTag = "This is a multi-part message in MIME format.";
00553 
00554   DWORD   i;
00555   DWORD   realLen = 0;
00556   DWORD   totalCR = 0;
00557 
00558   // do sanity checking...
00559   if ((!noteText) || (!(*noteText)))
00560     return(noteText);
00561 
00562   // more sanity checking...
00563   DWORD len = strlen(noteText) + 1;
00564   if (len <= 0) 
00565     return(noteText);
00566 
00567   // Get the number of CR's in this message and add room for
00568   // the LF's
00569   for (i=0; i<len; i++)
00570   {
00571     if ( (*(noteText + i)) == 0x0D )
00572       ++totalCR;
00573   }
00574 
00575   // This is a check for a signed message in the start of a message
00576   // check for sign line...    
00577   if ( strlen(signTag) < len)
00578   {
00579     if ( 
00580         (strncmp(signTag, noteText, strlen(signTag)) == 0) ||
00581         (strncmp(mimeTag, noteText, strlen(mimeTag)) == 0) 
00582        )
00583     {
00584       return( StripSignedMessage(noteText, totalCR) );
00585     }
00586   }
00587 
00588   // create a new buffer...
00589   newBuf = (char *) malloc((size_t)(len + totalCR));
00590   if (!newBuf)
00591     return(noteText);
00592 
00593   newBuf[0] = '\0';
00594 
00595   BOOL    firstChar = FALSE;
00596 
00597   // Now do the translation for the body of the note...
00598   for (i=0; i<len; i++)
00599   {
00600     char  *ptr = (noteText + i);
00601     
00602     if ( ((*ptr == 0x0D) || (*ptr == 0x20)) && (!firstChar) )
00603       continue;
00604     else
00605       firstChar = TRUE;
00606 
00607     if (CheckForInlineHTML(ptr, len, &i, newBuf, &realLen))
00608       continue;
00609 
00610     newBuf[realLen++] = *ptr;
00611     if ( *ptr == 0x0D )
00612     {
00613       newBuf[realLen++] = 0x0A;
00614     }
00615   }
00616 
00617   // terminate the buffer - reallocate and move on...
00618   newBuf[realLen++] = '\0';
00619   newBuf = (LPSTR) realloc(newBuf, (size_t) realLen);
00620 
00621   // check if the realloc worked and if so, free old memory and
00622   // return...if not, just return the original buffer
00623   if (!newBuf)
00624   {
00625     return(noteText);
00626   }
00627   else
00628   {
00629     free(noteText);
00630     return(newBuf);
00631   }
00632 }
00633 
00634 #ifdef WIN16
00635 
00636 void
00637 GetWin16TempName(LPSTR realFileName, LPSTR tempPath, 
00638                              LPSTR szTempFileName, UINT uUnique)
00639 {
00640   char  *dotPtr = strrchr(realFileName, '.');
00641   if (dotPtr != NULL)
00642   {
00643     *dotPtr = '\0';
00644   }                
00645   
00646   int nameLen = lstrlen(realFileName); 
00647   if (dotPtr != NULL)
00648   {
00649     *dotPtr = '.';
00650   }
00651   
00652   if (nameLen <= 7)
00653   {
00654        wsprintf(szTempFileName, "%s\\%d%s", tempPath, uUnique, realFileName);
00655   }
00656   else
00657   {
00658        wsprintf(szTempFileName, "%s\\%d%s", tempPath, uUnique, (realFileName + 1));
00659   }
00660 }
00661 
00662 #endif
00663 
00664 #define MAXTRY  9999  // How many times do we try..
00665 
00666 UINT
00667 GetTempMailNameWithExtension(LPSTR szTempFileName, 
00668                              LPSTR origName)
00669 {
00670   UINT        res = 1;
00671   UINT        uUnique = 0;
00672   char        *szTempPath = GetTheTempDirectoryOnTheSystem();
00673   char        *tmpPtr;
00674   char        *realFileName = NULL;
00675 
00676   if ( (origName != NULL) && (*origName != '\0') )
00677   {
00678     tmpPtr = origName;
00679   }
00680   else
00681   {
00682     tmpPtr = szTempFileName;
00683   }
00684 
00685   realFileName = strrchr(tmpPtr, '\\');
00686   if (!realFileName)
00687     realFileName = tmpPtr;
00688   else
00689     realFileName++;
00690 
00691 TRYAGAIN:
00692 
00693 #ifdef WIN32
00694   if (uUnique == 0)
00695   {
00696     wsprintf(szTempFileName, "%s\\%s", szTempPath, realFileName);
00697   }
00698   else
00699   {
00700     wsprintf(szTempFileName, "%s\\%d_%s", 
00701                 szTempPath, uUnique, realFileName);
00702   }
00703 #else // WIN16
00704   if ( (uUnique == 0) && (strlen(realFileName) <= 12) )
00705   {
00706     wsprintf(szTempFileName, "%s\\%s", szTempPath, realFileName);
00707   }
00708   else
00709   {              
00710     if (uUnique < 10)
00711     {
00712       GetWin16TempName(realFileName, szTempPath, szTempFileName, uUnique);
00713     }
00714     else 
00715     { 
00716       res = ISGetTempFileName(szTempPath, "ns", uUnique++, szTempFileName);    
00717     }
00718 
00719     // Now add the correct extension...
00720     char *origExt = strrchr(realFileName, '.');
00721     if (origExt != NULL)
00722     {
00723       char *tmpExt = strrchr(szTempFileName, '.');
00724       if (tmpExt != NULL)
00725       {
00726         origExt++;
00727         tmpExt++;
00728 
00729         while ( ((tmpExt) && (origExt)) && (*origExt != '\0') )
00730         {
00731           *tmpExt = *origExt;
00732           tmpExt++;
00733           origExt++;
00734         }
00735 
00736         *tmpExt = '\0';
00737       }
00738     }
00739   }
00740 #endif
00741 
00742   if ( (ValidateFile(szTempFileName) != 1) && (uUnique < MAXTRY) )
00743   {
00744     uUnique++;
00745     if (uUnique >= MAXTRY)
00746       return(1);
00747     goto TRYAGAIN;
00748   }
00749 
00750   return res;
00751 }
00752 
00753 #define kMaxTempFiles   10
00754 #define kMaxListLength  (10 * _MAX_PATH)
00755 
00756 void GetTempFiles(LPSTR pBuf, int lenBuf)
00757 {
00758   if (!GetConfigInfoStr(szMapiSection, szTempFiles, pBuf, lenBuf, HKEY_ROOT))
00759   {
00760     *pBuf = 0;
00761   }
00762 }
00763 
00764 void WriteTempFiles(LPSTR pBuf)
00765 {
00766   SetConfigInfoStr(szMapiSection, szTempFiles, pBuf, HKEY_ROOT);
00767 }
00768 
00769 void  AddTempFile(LPCSTR pFileName)
00770 {
00771   if ( (!pFileName) || (pFileName[0] == '\0') )
00772     return;
00773 
00774   char *files = (char *)malloc(kMaxListLength);
00775 
00776   if (!files)
00777     return;
00778 
00779   GetTempFiles(files, kMaxListLength);
00780   if ((lstrlen(files) + lstrlen(pFileName) + 2) >= kMaxListLength)
00781   {
00782     free(files);
00783     return;
00784   }
00785 
00786   if (lstrlen(files) != 0)
00787   {
00788           lstrcat(files, ";");
00789   }
00790 
00791   lstrcat(files, pFileName);
00792   WriteTempFiles(files);
00793   free(files);
00794 }
00795 
00796 void DeleteFirstTempFile(LPSTR pFiles)
00797 {
00798   if (!*pFiles)
00799     return;
00800   LPSTR pTemp = strchr(pFiles, ';');
00801   if (pTemp)
00802   {
00803     *pTemp = 0;
00804   }
00805  
00806 //#ifndef _DEBUG  
00807   _unlink(pFiles);
00808 //#endif
00809 
00810   if (pTemp)
00811   {
00812     memmove(pFiles, pTemp + 1, lstrlen(pTemp + 1) + 1);
00813   }
00814   else
00815   {
00816     *pFiles = 0;
00817   }
00818 }
00819 
00820 void 
00821 RemoveAllTempFiles(void)
00822 {
00823   char *files = (char *)malloc(kMaxListLength);
00824 
00825   if (!files)
00826     return;
00827 
00828   GetTempFiles(files, kMaxListLength);
00829 
00830   while (*files)
00831   {
00832     DeleteFirstTempFile(files);
00833   }
00834 
00835   WriteTempFiles(files);
00836   free(files);
00837 }
00838 
00839 void CheckAgeTempFiles(void)
00840 {
00841   char *files = (char *)malloc(kMaxListLength);
00842 
00843   if (!files)
00844     return;
00845 
00846   GetTempFiles(files, kMaxListLength);
00847   int i = 0;
00848   LPSTR pTemp = files;
00849   while (TRUE)
00850   {
00851     pTemp = strchr(pTemp, ';');
00852     if (!pTemp)
00853       break;
00854     ++pTemp;
00855     ++i;
00856   }
00857 
00858   if (i >= 10)
00859   {
00860     DeleteFirstTempFile(files);
00861     WriteTempFiles(files);
00862   }
00863 
00864   free(files);
00865 }
00866 
00867 void 
00868 CleanupMAPITempFiles(void)
00869 {
00870   if (Is_16_OR_32_BIT_CommunitorRunning() == 0)
00871   {
00872     RemoveAllTempFiles();           // if Communicator not running, clean up all the temp files
00873   }
00874   else
00875   {
00876     CheckAgeTempFiles();
00877   }
00878 }
00879 
00880 void *
00881 CleanMalloc(size_t mallocSize)
00882 {
00883   void *ptr = malloc(mallocSize);
00884   if (!ptr)
00885     return(NULL);
00886 
00887   memset(ptr, 0, mallocSize);
00888   return(ptr);
00889 }
00890 
00891 void
00892 SafeFree(void *ptr)
00893 {
00894   if (!ptr)
00895     return;
00896 
00897   free(ptr);
00898   ptr = NULL;
00899 }