Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /****************************************************************************
  2. *
  3. *                            Open Watcom Project
  4. *
  5. *    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
  6. *
  7. *  ========================================================================
  8. *
  9. *    This file contains Original Code and/or Modifications of Original
  10. *    Code as defined in and that are subject to the Sybase Open Watcom
  11. *    Public License version 1.0 (the 'License'). You may not use this file
  12. *    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
  13. *    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
  14. *    provided with the Original Code and Modifications, and is also
  15. *    available at www.sybase.com/developer/opensource.
  16. *
  17. *    The Original Code and all software distributed under the License are
  18. *    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  19. *    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
  20. *    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
  21. *    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
  22. *    NON-INFRINGEMENT. Please see the License for the specific language
  23. *    governing rights and limitations under the License.
  24. *
  25. *  ========================================================================
  26. *
  27. * Description:  Internal helper routines __MkTmpFile() and __RmTmpFile().
  28. *
  29. ****************************************************************************/
  30.  
  31.  
  32. #include "variety.h"
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <stddef.h>
  36. #include <direct.h>
  37. #include <string.h>
  38. #include <process.h>
  39. #include "rtdata.h"
  40. #include "tmpfname.h"
  41.  
  42.  
  43. static unsigned __GetTmpPath( char *buf )
  44. {
  45. #ifndef __NETWARE__
  46.     static char *evars[] = { "TMP", "TEMP", "TMPDIR", "TEMPDIR", "" };
  47.     char        **evar;
  48.     char        *tmp;
  49. #endif
  50.     unsigned    i;
  51.  
  52.     buf[0] = '\0';  // initialize path
  53. #ifdef __NETWARE__
  54.     getcwd( buf, PATH_MAX );    // No environment vars on Netware
  55. #else
  56.     for( evar = evars; **evar; ++evar ) {
  57.         tmp = getenv( *evar );
  58.         if( (tmp != NULL) && (strlen( tmp ) <= PATH_MAX) ) {
  59.             tmp = _fullpath( buf, tmp, PATH_MAX );
  60.             break;
  61.         }
  62.     }
  63.     /*
  64.      * If we didn't match on any environment vars, get current working dir
  65.      */
  66.     if( buf[0] == '\0' ) {
  67.         getcwd( buf, PATH_MAX );
  68.     }
  69. #endif
  70.  
  71.     // if last char is not a path delimiter then append one
  72.     i = strlen( buf );
  73.     if( i > 0 ) i--;
  74.     if( (buf[i] != '\\') && (buf[i] != '/') ) {
  75.         // if buf[i] is a null char then the following has no effect as planned
  76.         i++;
  77.         buf[i] = '\\';
  78.         i++;
  79.         buf[i] = '\0';
  80.     }
  81.     return( i );
  82. }
  83.  
  84. static char __hex( int num )
  85. {
  86.     num += '0';
  87.     if( num > '9' ) {
  88.         num += 'a' - '0' - 10;
  89.     }
  90.     return( num );
  91. }
  92.  
  93. #if defined( __NETWARE__ )
  94.     extern int              GetThreadID( void );
  95.     #define getuniqueid()   GetThreadID()
  96. #elif defined( __NT__ )
  97.     #define getuniqueid()   ((getpid() << 12) + *_threadid)
  98. #elif defined( __OS2__ )
  99.     #define getuniqueid()   ((getpid() << 12) + *_threadid)
  100. #else
  101.     #define getuniqueid()   (getpid())
  102. #endif
  103.  
  104. void __MkTmpFile( char *buf, int num )
  105. {
  106.     unsigned    pid;
  107.     unsigned    i;
  108.     char        *ptr;
  109.  
  110.     pid = getuniqueid();
  111. //  JBS on Win32 pid's range from 0 to n where n is not very large for
  112. //  most systems (e.g. 500 would indicate many, many processes are active).
  113. //  #if defined(__386__) || defined(__AXP__) || defined(__PPC__)
  114. //      // try to use more of the 32bit pid bits
  115. //      pid |= pid >> 16;
  116. //  #endif
  117.  
  118.     i = __GetTmpPath( buf );
  119.     ptr = &buf[ i ];
  120.     ptr[0] = 't';
  121.     for( i = 7; i != 0; i-- ) {     // JBS use 7 hex digits instead of 4
  122.         ptr[i] = __hex( pid & 0x000F );
  123.         pid = pid >> 4;
  124.     }
  125.     ptr[8] = '.';
  126.     ptr[9] = 't';
  127.     ptr[10] = __hex( (num >> 4) & 0x000F );
  128.     ptr[11] = __hex( num & 0x000F );
  129.     ptr[12] = '\0';
  130. }
  131.  
  132. void __RmTmpFile( FILE *fp )
  133. {
  134.     char    name[PATH_MAX + _TMPFNAME_LENGTH + 1]; /* 02-aug-90 */
  135.  
  136.     __MkTmpFile( name, _FP_TMPFCHAR(fp) );
  137.     remove( name );
  138. }
  139.