home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dmake40.zip / os2 / mscdos / tempnam.c < prev   
C/C++ Source or Header  |  1994-10-23  |  2KB  |  91 lines

  1. /*LINTLIBRARY*/
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <dos.h>
  6.  
  7. #if defined(max)
  8. #   undef  max
  9. #endif
  10. #define max(A,B) (((A)<(B))?(B):(A))
  11.  
  12. extern int access();
  13. int _access();
  14.  
  15. /* MSC stdio.h defines P_tmpdir, so let's undo it here */
  16. /* Under DOS leave the default tmpdir pointing here!        */
  17. #ifdef P_tmpdir
  18. #undef P_tmpdir
  19. #endif
  20. static char *P_tmpdir = "";
  21.  
  22. char *
  23. tempnam(dir, prefix)
  24. char *dir;        /* use this directory please (if non-NULL) */
  25. char *prefix;        /* use this (if non-NULL) as filename prefix */
  26. {
  27.    static         int count = 0;
  28.    register char *p, *q, *tmpdir;
  29.    int            tl=0, dl=0, pl;
  30.    char          buf[30];
  31.  
  32.    pl = strlen(P_tmpdir);
  33.  
  34.    if( (tmpdir = getenv("TMPDIR")) != NULL )
  35.       tl = strlen(tmpdir);
  36.    else if( (tmpdir = getenv("TMP")) != NULL )
  37.       tl = strlen(tmpdir);
  38.    if( dir != NULL ) dl = strlen(dir);
  39.  
  40.    if( (p = malloc((unsigned)(max(max(dl,tl),pl)+13))) == NULL )
  41.      return(NULL);
  42.  
  43.    *p = '\0';
  44.  
  45.    if( (tl == 0) || (_access( strcpy(p, tmpdir), 0) != 0) )
  46.      if( (dl == 0) || (_access( strcpy(p, dir), 0) != 0) )
  47.     if( _access( strcpy(p, P_tmpdir), 0) != 0 )
  48.        if( !prefix )
  49.           prefix = "tp";
  50.  
  51.    if(prefix)
  52.    {
  53.       *(p+strlen(p)+2) = '\0';
  54.       (void)strncat(p, prefix, 2);
  55.    }
  56.  
  57. #ifdef OS2
  58.    sprintf( buf, "%08x", getpid() );
  59. #else
  60.    sprintf( buf, "%08x", _psp );
  61. #endif
  62.    buf[6]='\0';
  63.    (void)strcat(p, buf );
  64.    sprintf( buf, "%04d", count++ );
  65.    q=p+strlen(p)-6;
  66.    *q++ = buf[0]; *q++ = buf[1];
  67.    *q++ = buf[2]; *q++ = buf[3];
  68.  
  69.    if( (q = strrchr(p,'.')) != NULL ) *q = '\0';
  70.  
  71.    return strlwr(p);
  72. }
  73.  
  74.  
  75.  
  76. _access( name, flag )
  77. char *name;
  78. int  flag;
  79. {
  80.    char *p;
  81.    int r;
  82.  
  83.    if( name == NULL || !*name ) return(1);  /* NULL dir means current dir */
  84.    p = name+strlen(name)-1;
  85.    if(*p == ':' ) strcat( p++, "\\" );
  86.    r = access( name, flag );
  87.    if(*p != '/' && *p != '\\') strcat( p, "\\" );
  88.  
  89.    return( r );
  90. }
  91.