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

  1. /*LINTLIBRARY*/
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5.  
  6. #define max(A,B) (((A)<(B))?(B):(A))
  7.  
  8. extern int access();
  9.  
  10. static char *cpdir();
  11. static char  seed[4]="AAA";
  12.  
  13. /* BSD stdio.h doesn't define P_tmpdir, so let's do it here */
  14. #ifndef P_tmpdir
  15. static char *P_tmpdir = "/tmp";
  16. #endif
  17.  
  18. char *
  19. tempnam(dir, prefix)
  20. char *dir;        /* use this directory please (if non-NULL) */
  21. char *prefix;        /* use this (if non-NULL) as filename prefix */
  22. {
  23.    register char *p, *q, *tmpdir;
  24.    int            tl=0, dl=0, pl;
  25.  
  26.    pl = strlen(P_tmpdir);
  27.  
  28.    if( (tmpdir = getenv("TMPDIR")) != NULL ) tl = strlen(tmpdir);
  29.    if( dir != NULL ) dl = strlen(dir);
  30.  
  31.    if( (p = malloc((unsigned)(max(max(dl,tl),pl)+16))) == NULL )
  32.      return(NULL);
  33.  
  34.    *p = '\0';
  35.  
  36.    if( (tl == 0) || (access( cpdir(p, tmpdir), 3) != 0) )
  37.      if( (dl == 0) || (access( cpdir(p, dir), 3) != 0) )
  38.     if( access( cpdir(p, P_tmpdir),   3) != 0 )
  39.        if( access( cpdir(p, "/tmp"),  3) != 0 )
  40.           return(NULL);
  41.  
  42.    (void) strcat(p, "/");
  43.    if(prefix)
  44.    {
  45.       *(p+strlen(p)+5) = '\0';
  46.       (void)strncat(p, prefix, 5);
  47.    }
  48.  
  49.    (void)strcat(p, seed);
  50.    (void)strcat(p, "XXXXXX");
  51.  
  52.    q = seed;
  53.    while(*q == 'Z') *q++ = 'A';
  54.    ++*q;
  55.  
  56.    if(*tmpnam(p) == '\0') return(NULL);
  57.    return(p);
  58. }
  59.  
  60.  
  61.  
  62. static char *
  63. cpdir(buf, str)
  64. char *buf;
  65. char *str;
  66. {
  67.    char *p;
  68.  
  69.    if(str != NULL)
  70.    {
  71.       (void) strcpy(buf, str);
  72.       p = buf - 1 + strlen(buf);
  73.       if(*p == '/') *p = '\0';
  74.    }
  75.  
  76.    return(buf);
  77. }
  78.