home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / minix / libsrc~1.z / libsrc~1 / tempnam.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-12-28  |  1.8 KB  |  77 lines

  1. /* author:    Monty Walls
  2.  * written:    4/17/89
  3.  * Copyright:    Copyright (c) 1989 by Monty Walls.
  4.  *        Not derived from licensed software.
  5.  *
  6.  *        Permission to copy and/or distribute granted under the
  7.  *        following conditions:
  8.  *    
  9.  *        1). This notice must remain intact.
  10.  *        2). The author is not responsible for the consequences of use
  11.  *            this software, no matter how awful, even if they
  12.  *            arise from defects in it.
  13.  *        3). Altered version must not be represented as being the 
  14.  *            original software.
  15.  */
  16. #include <stdio.h>
  17. #include <sys/types.h>
  18. #include <unistd.h>
  19. #include <string.h>
  20.  
  21. #define MAXPREFIX    5
  22. #define TMPNAME        "tmp"
  23. #ifndef P_tmpdir
  24. #define P_tmpdir    "/tmp"
  25. #endif
  26.  
  27. #ifndef __STDC__
  28. extern char *mktemp();
  29. extern char *strcat();
  30. extern char *strcpy();
  31. extern char *getenv();
  32. extern char *malloc();
  33. #endif
  34.  
  35. char * tempnam(dir, name)
  36. char *dir;
  37. char *name;
  38. {
  39.     char *buf, *tmpdir;
  40.     
  41.     /* 
  42.      * This is kind of like the chicken & the egg.
  43.      * Do we use the users preference or the programmers?
  44.      */
  45. #if 1        /* this seems to be implied in the man page */
  46.     if ((tmpdir = getenv("TMPDIR")) == (char *)NULL) {
  47.         if ((tmpdir = dir) == (char *)NULL)
  48.             tmpdir = P_tmpdir;
  49.     }
  50. #else
  51.     if ((tmpdir = dir) == (char *)NULL) {
  52.         if ((tmpdir = getenv("TMPDIR")) == (char *)NULL)
  53.             tmpdir = P_tmpdir;
  54.     }
  55. #endif
  56.     /* now lets check and see if we can work there */
  57.     if (access(tmpdir, R_OK+W_OK+X_OK) < 0)
  58.         return ((char *)NULL);
  59.             
  60.     if (name == (char *)NULL) 
  61.         name = TMPNAME;
  62.     else if (strlen(name) > MAXPREFIX)
  63.         name[5] = '\0';    /* this is according to SYS5 */
  64.     
  65.     /* the magic value 2 is for '\0' & '/' */
  66.     if ((buf = (char *)malloc(L_tmpnam + strlen(tmpdir) + 2)) == (char *)NULL)
  67.         return ((char *)NULL);
  68.         
  69.     strcpy(buf, tmpdir);
  70.     strcat(buf, "/");
  71.     strcat(buf, name);
  72.     strcat(buf, ".XXXXXX");
  73.     
  74.     /* pass our completed pattern to mktemp */
  75.     return (mktemp(buf));
  76. }
  77.