home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / mag&info / msjv7_4.zip / OPEN.ARJ / OPENTEMP.C < prev    next >
Text File  |  1992-07-01  |  4KB  |  106 lines

  1. /*
  2.       Copyright 1991 by David Thielen, All Rights Reserved.
  3.  
  4.       This code example is from a commercial product and has restricted
  5.       rights.  This code, or any code derived from this code may be
  6.       incorporated into any programs with the following restrictions;
  7.       1) It cannot be sold as source code, and 2) It cannot be sold in a
  8.       product which provides this code as an API.
  9. */
  10.  
  11.  
  12. #include "file_io.h"
  13. #include "stdlib.h"
  14. #include "string.h"
  15.  
  16.  
  17. // pFile is used to return the name of the file created (for when it
  18. // is closed & deleted - nothing is passed in in it.  It should be at
  19. // least 128 bytes long.
  20. // lNum is used to initially size the file to insure there is enough
  21. // room on the temp drive.
  22.  
  23. // The env variables used to determine the file directory are in order;
  24. // TempPath, Temp, & Tmp.  The TempPath var can have ';' between choices
  25. // just like path.  If none of those work, it will use the default
  26. // directory.
  27.  
  28. // To use a directory from one of the environment vars above, not only
  29. // must the dir exist but there must be enough room on the disk (lNum).
  30.  
  31. #define     MAX_PATH  114
  32. #define     NUM_ENV    3
  33. BYTE *aEnv[NUM_ENV] = {"TEMPPATH", "TEMP", "TMP"};
  34. BYTE sDef[] = ".";
  35.  
  36. int FileOpenTemp (BYTE *pFile,unsigned long ulNum)
  37. {
  38. int iNum, iLen, hRtn;
  39. unsigned uErr;
  40. BYTE *pEnv, **pName, *pTmp;
  41. BYTE sTemp[128];
  42.  
  43. #ifdef   DEBUG
  44.    memset (pFile, '$', 128);
  45. #endif
  46.  
  47.    // We walk through, once for each env var and once for the default
  48.    // directory.
  49.    for (iNum=0, pName=aEnv; iNum<=NUM_ENV; iNum++, pName++)
  50.       {
  51.  
  52.       // If we are on our last run, we use ".\" as the env var.
  53.       if (iNum < NUM_ENV)
  54.          pEnv = getenv (*pName);        // Env var
  55.       else
  56.          pEnv = sDef;             // default directory
  57.  
  58.       // If we got something, we try each directory in the path
  59.       if (pEnv)
  60.          while (*pEnv)             // Try a directory
  61.             {
  62.  
  63.             // Build up the directory
  64.             // advance pEnv to next dir
  65.             strncpy (pFile, pEnv, MAX_PATH);
  66.             *(pFile + MAX_PATH) = 0;
  67.             if ((pTmp = strchr (pEnv, ';')) == NULL)
  68.                pEnv = NULL;
  69.             else
  70.                {
  71.                if ((iLen = pTmp - pEnv) < MAX_PATH)
  72.                   *(pFile + iLen) = 0;
  73.                pEnv = pTmp + 1;
  74.                }
  75.  
  76.             // lets try opening it.
  77.             // OpenFile will handle a missing '\' at the end of the string
  78.             if ((hRtn = FileOpen (pFile, O_READ | O_WRITE | O_TEMP |
  79.                            S_DENY_READ | S_DENY_WRITE, 0, &uErr)) >= 0)
  80.                {
  81.  
  82.                // If we have a length - try to make the file that long
  83.                if (ulNum)
  84.                   {
  85.                   uErr = FileSetSize (hRtn, ulNum);
  86.                   FileSeek (hRtn, 0L);
  87.                   }
  88.  
  89.                // We made a file of the length we wanted - return it
  90.                // We return its true name so when its time to delete, we
  91.                // can find it even if it has been re-mapped.
  92.                if (! uErr)
  93.                   {
  94.                   strcpy (sTemp, pFile);
  95.                   FileTrueName (sTemp, pFile);
  96.                   return (hRtn);
  97.                   }
  98.  
  99.                // We had an error - close & delete the file
  100.                FileCloseTemp (hRtn, pFile);
  101.  
  102.                }  // end if (OpenFile (...))
  103.             }     // while (*pEnv)
  104.       }           // for (...)
  105. }
  106.