home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dmake40.zip / msdos / bccdos / tempnam.c < prev    next >
C/C++ Source or Header  |  1994-10-23  |  2KB  |  85 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. /* Turbo C stdio.h doesn't define P_tmpdir, so let's do it here */
  17. /* Under DOS leave the default tmpdir pointing here!        */
  18. #ifndef P_tmpdir
  19. static char *P_tmpdir = "";
  20. #endif
  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 ) tl = strlen(tmpdir);
  35.    if( dir != NULL ) dl = strlen(dir);
  36.  
  37.    if( (p = malloc((unsigned)(max(max(dl,tl),pl)+13))) == NULL )
  38.      return(NULL);
  39.  
  40.    *p = '\0';
  41.  
  42.    if( (tl == 0) || (_access( strcpy(p, tmpdir), 0) != 0) )
  43.      if( (dl == 0) || (_access( strcpy(p, dir), 0) != 0) )
  44.     if( _access( strcpy(p, P_tmpdir), 0) != 0 )
  45.        if( !prefix )
  46.           prefix = "tp";
  47.  
  48.    if(prefix)
  49.    {
  50.       *(p+strlen(p)+2) = '\0';
  51.       (void)strncat(p, prefix, 2);
  52.    }
  53.  
  54.    sprintf( buf, "%08x", _psp );
  55.    buf[6]='\0';
  56.    (void)strcat(p, buf );
  57.    sprintf( buf, "%04d", count++ );
  58.    q=p+strlen(p)-6;
  59.    *q++ = buf[0]; *q++ = buf[1];
  60.    *q++ = buf[2]; *q++ = buf[3];
  61.  
  62.    if( (q = strrchr(p,'.')) != NULL ) *q = '\0';
  63.  
  64.    return(p);
  65. }
  66.  
  67.  
  68.  
  69. _access( name, flag )
  70. char *name;
  71. int  flag;
  72. {
  73.    extern char *DirSepStr;
  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.  
  81.    if(*p != '/' && *p != '\\') strcat( p, DirSepStr );
  82.  
  83.    return( r );
  84. }
  85.