home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dmake40.zip / msdos / ztcdos / tempnam.c < prev   
C/C++ Source or Header  |  1994-10-23  |  2KB  |  82 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. /* Zortech C stdio.h doesn't define P_tmpdir, so let's do it here */
  17. /* Under DOS leave the default tmpdir pointing here!        */
  18. static char *P_tmpdir = "";
  19.  
  20. char *
  21. tempnam(dir, prefix)
  22. const char *dir;        /* use this directory please (if non-NULL) */
  23. const char *prefix;        /* use this (if non-NULL) as filename prefix */
  24. {
  25.    static         int count = 0;
  26.    register char *p, *q, *tmpdir;
  27.    int            tl=0, dl=0, pl;
  28.    char          buf[30];
  29.  
  30.    pl = strlen(P_tmpdir);
  31.  
  32.    if( (tmpdir = getenv("TMPDIR")) != NULL ) tl = strlen(tmpdir);
  33.    if( dir != NULL ) dl = strlen(dir);
  34.  
  35.    if( (p = malloc((unsigned)(max(max(dl,tl),pl)+13))) == NULL )
  36.      return(NULL);
  37.  
  38.    *p = '\0';
  39.  
  40.    if( (tl == 0) || (_access( strcpy(p, tmpdir), 0) != 0) )
  41.      if( (dl == 0) || (_access( strcpy(p, dir), 0) != 0) )
  42.     if( _access( strcpy(p, P_tmpdir), 0) != 0 )
  43.        if( !prefix )
  44.           prefix = "tp";
  45.  
  46.    if(prefix)
  47.    {
  48.       *(p+strlen(p)+2) = '\0';
  49.       (void)strncat(p, prefix, 2);
  50.    }
  51.  
  52.    sprintf( buf, "%08x", _psp );
  53.    buf[6]='\0';
  54.    (void)strcat(p, buf );
  55.    sprintf( buf, "%04d", count++ );
  56.    q=p+strlen(p)-6;
  57.    *q++ = buf[0]; *q++ = buf[1];
  58.    *q++ = buf[2]; *q++ = buf[3];
  59.  
  60.    if( (q = strrchr(p,'.')) != NULL ) *q = '\0';
  61.  
  62.    return(p);
  63. }
  64.  
  65.  
  66.  
  67. _access( name, flag )
  68. char *name;
  69. int  flag;
  70. {
  71.    extern char *DirSepStr;
  72.    char *p;
  73.    int r;
  74.  
  75.    if( name == NULL || !*name ) return(1);  /* NULL dir means current dir */
  76.    r = access( name, flag );
  77.    p = name+strlen(name)-1;
  78.    if(*p != '/' && *p != '\\') strcat( p, DirSepStr );
  79.  
  80.    return( r );
  81. }
  82.