home *** CD-ROM | disk | FTP | other *** search
- /* author: Monty Walls
- * written: 4/17/89
- * Copyright: Copyright (c) 1989 by Monty Walls.
- * Not derived from licensed software.
- *
- * Permission to copy and/or distribute granted under the
- * following conditions:
- *
- * 1). This notice must remain intact.
- * 2). The author is not responsible for the consequences of use
- * this software, no matter how awful, even if they
- * arise from defects in it.
- * 3). Altered version must not be represented as being the
- * original software.
- */
- #include <stdio.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <string.h>
-
- #define MAXPREFIX 5
- #define TMPNAME "tmp"
- #ifndef P_tmpdir
- #define P_tmpdir "/tmp"
- #endif
-
- #ifndef __STDC__
- extern char *mktemp();
- extern char *strcat();
- extern char *strcpy();
- extern char *getenv();
- extern char *malloc();
- #endif
-
- char * tempnam(dir, name)
- char *dir;
- char *name;
- {
- char *buf, *tmpdir;
-
- /*
- * This is kind of like the chicken & the egg.
- * Do we use the users preference or the programmers?
- */
- #if 1 /* this seems to be implied in the man page */
- if ((tmpdir = getenv("TMPDIR")) == (char *)NULL) {
- if ((tmpdir = dir) == (char *)NULL)
- tmpdir = P_tmpdir;
- }
- #else
- if ((tmpdir = dir) == (char *)NULL) {
- if ((tmpdir = getenv("TMPDIR")) == (char *)NULL)
- tmpdir = P_tmpdir;
- }
- #endif
- /* now lets check and see if we can work there */
- if (access(tmpdir, R_OK+W_OK+X_OK) < 0)
- return ((char *)NULL);
-
- if (name == (char *)NULL)
- name = TMPNAME;
- else if (strlen(name) > MAXPREFIX)
- name[5] = '\0'; /* this is according to SYS5 */
-
- /* the magic value 2 is for '\0' & '/' */
- if ((buf = (char *)malloc(L_tmpnam + strlen(tmpdir) + 2)) == (char *)NULL)
- return ((char *)NULL);
-
- strcpy(buf, tmpdir);
- strcat(buf, "/");
- strcat(buf, name);
- strcat(buf, ".XXXXXX");
-
- /* pass our completed pattern to mktemp */
- return (mktemp(buf));
- }
-