home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / DMAKE37S.ZIP / DMAKE / MSDOS / MSCDOS / TEMPNAM.C < prev   
Encoding:
C/C++ Source or Header  |  1991-05-06  |  1.7 KB  |  84 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 char *mktemp();
  13. extern int access();
  14. int _access();
  15.  
  16. /* MSC stdio.h defines P_tmpdir, so let's undo it here */
  17. /* Under DOS leave the default tmpdir pointing here!        */
  18. #ifdef P_tmpdir
  19. #undef P_tmpdir
  20. #endif
  21. static char *P_tmpdir = "";
  22.  
  23. char *
  24. tempnam(dir, prefix)
  25. char *dir;        /* use this directory please (if non-NULL) */
  26. char *prefix;        /* use this (if non-NULL) as filename prefix */
  27. {
  28.    static         int count = 0;
  29.    register char *p, *q, *tmpdir;
  30.    int            tl=0, dl=0, pl;
  31.    char          buf[30];
  32.  
  33.    pl = strlen(P_tmpdir);
  34.  
  35.    if( (tmpdir = getenv("TMPDIR")) != NULL ) tl = strlen(tmpdir);
  36.    if( dir != NULL ) dl = strlen(dir);
  37.  
  38.    if( (p = malloc((unsigned)(max(max(dl,tl),pl)+13))) == NULL )
  39.      return(NULL);
  40.  
  41.    *p = '\0';
  42.  
  43.    if( (tl == 0) || (_access( strcpy(p, tmpdir), 0) != 0) )
  44.      if( (dl == 0) || (_access( strcpy(p, dir), 0) != 0) )
  45.     if( _access( strcpy(p, P_tmpdir), 0) != 0 )
  46.        if( !prefix )
  47.           prefix = "tp";
  48.  
  49.    if(prefix)
  50.    {
  51.       *(p+strlen(p)+2) = '\0';
  52.       (void)strncat(p, prefix, 2);
  53.    }
  54.  
  55.    sprintf( buf, "%08x", _psp );
  56.    buf[6]='\0';
  57.    (void)strcat(p, buf );
  58.    sprintf( buf, "%04d", count++ );
  59.    q=p+strlen(p)-6;
  60.    *q++ = buf[0]; *q++ = buf[1];
  61.    *q++ = buf[2]; *q++ = buf[3];
  62.  
  63.    if( (q = strrchr(p,'.')) != NULL ) *q = '\0';
  64.  
  65.    return(p);
  66. }
  67.  
  68.  
  69.  
  70. _access( name, flag )
  71. char *name;
  72. int  flag;
  73. {
  74.    char *p;
  75.    int r;
  76.  
  77.    if( name == NULL || !*name ) return(1);  /* NULL dir means current dir */
  78.    r = access( name, flag );
  79.    p = name+strlen(name)-1;
  80.    if(*p != '/' && *p != '\\') strcat( p, "/" );
  81.  
  82.    return( r );
  83. }
  84.