2003-09-02 05:51:35 -04:00
|
|
|
/* temp.c
|
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright 1990, 1991, 1992, 1993, 1994, 1995, Oliver Laumann, Berlin
|
|
|
|
* Copyright 2002, 2003 Sam Hocevar <sam@zoy.org>, Paris
|
|
|
|
*
|
|
|
|
* This software was derived from Elk 1.2, which was Copyright 1987, 1988,
|
|
|
|
* 1989, Nixdorf Computer AG and TELES GmbH, Berlin (Elk 1.2 has been written
|
|
|
|
* by Oliver Laumann for TELES Telematic Services, Berlin, in a joint project
|
|
|
|
* between TELES and Nixdorf Microprocessor Engineering, Berlin).
|
|
|
|
*
|
|
|
|
* Oliver Laumann, TELES GmbH, Nixdorf Computer AG and Sam Hocevar, as co-
|
|
|
|
* owners or individual owners of copyright in this software, grant to any
|
|
|
|
* person or company a worldwide, royalty free, license to
|
|
|
|
*
|
|
|
|
* i) copy this software,
|
|
|
|
* ii) prepare derivative works based on this software,
|
|
|
|
* iii) distribute copies of this software or derivative works,
|
|
|
|
* iv) perform this software, or
|
|
|
|
* v) display this software,
|
|
|
|
*
|
|
|
|
* provided that this notice is not removed and that neither Oliver Laumann
|
|
|
|
* nor Teles nor Nixdorf are deemed to have made any representations as to
|
|
|
|
* the suitability of this software for any purpose nor are held responsible
|
|
|
|
* for any defects of this software.
|
|
|
|
*
|
|
|
|
* THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2003-08-19 15:19:38 -04:00
|
|
|
#include "unix.h"
|
|
|
|
|
2003-09-04 09:29:16 -04:00
|
|
|
#include <string.h>
|
|
|
|
|
2003-08-25 10:17:09 -04:00
|
|
|
#ifdef HAVE_TEMPNAM /* Make sure only one of these is defined (if any) */
|
|
|
|
# undef HAVE_TMPNAM /* Order of preference: tempnam, mktemp, tmpnam */
|
|
|
|
# undef HAVE_MKTEMP
|
2003-08-19 15:19:38 -04:00
|
|
|
#endif
|
2003-08-25 10:17:09 -04:00
|
|
|
#ifdef HAVE_MKTEMP
|
|
|
|
# undef HAVE_TMPNAM
|
|
|
|
# undef HAVE_TEMPNAM
|
2003-08-19 15:19:38 -04:00
|
|
|
#endif
|
2003-08-25 10:17:09 -04:00
|
|
|
#ifdef HAVE_TMPNAM
|
|
|
|
# undef HAVE_TEMPNAM
|
|
|
|
# undef HAVE_MKTEMP
|
2003-08-19 15:19:38 -04:00
|
|
|
#endif
|
|
|
|
|
2003-09-04 09:29:16 -04:00
|
|
|
static Object P_Tempname(int argc, Object *argv) {
|
2003-08-19 15:19:38 -04:00
|
|
|
char *name, *dir = 0, *pref = 0;
|
|
|
|
Object ret;
|
2003-08-25 10:17:09 -04:00
|
|
|
#ifdef HAVE_TMPNAM
|
2003-08-19 15:19:38 -04:00
|
|
|
extern char *tmpnam();
|
|
|
|
#else
|
2003-08-25 10:17:09 -04:00
|
|
|
#ifdef HAVE_TEMPNAM
|
2003-08-19 15:19:38 -04:00
|
|
|
extern char *tempnam();
|
|
|
|
#else
|
|
|
|
char buf[1024];
|
2003-08-25 10:17:09 -04:00
|
|
|
#ifdef HAVE_MKTEMP
|
2003-08-19 15:19:38 -04:00
|
|
|
extern char *mktemp();
|
|
|
|
#else
|
|
|
|
char *p, *q;
|
|
|
|
struct stat st;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (argc > 0)
|
2003-09-02 04:12:11 -04:00
|
|
|
dir = Get_Strsym(argv[0]);
|
2003-08-19 15:19:38 -04:00
|
|
|
if (argc > 1)
|
2003-09-02 04:12:11 -04:00
|
|
|
pref = Get_Strsym(argv[1]);
|
2003-08-25 10:17:09 -04:00
|
|
|
#ifdef HAVE_TMPNAM
|
2003-08-19 15:19:38 -04:00
|
|
|
name = tmpnam((char *)0);
|
|
|
|
#else
|
2003-08-25 10:17:09 -04:00
|
|
|
#ifdef HAVE_TEMPNAM
|
2003-08-19 15:19:38 -04:00
|
|
|
Disable_Interrupts; /* Make sure result gets freed */
|
|
|
|
name = tempnam(dir, pref);
|
|
|
|
#else
|
|
|
|
if (!dir) dir = "/tmp";
|
|
|
|
if (!pref) pref = "elk";
|
|
|
|
if (strlen(dir) + strlen(pref) > 1000)
|
2003-09-02 04:12:11 -04:00
|
|
|
Primitive_Error("directory/prefix argument too long");
|
2003-08-25 10:17:09 -04:00
|
|
|
#ifdef HAVE_MKTEMP
|
2003-08-19 15:19:38 -04:00
|
|
|
sprintf(buf, "%s/%sXXXXXX", dir, pref);
|
|
|
|
name = mktemp(buf);
|
|
|
|
#else
|
|
|
|
name = 0;
|
|
|
|
sprintf(buf, "%s/%sa%d", dir, pref, getpid());
|
|
|
|
p = buf+strlen(dir)+strlen(pref)+1;
|
|
|
|
while (stat(buf, &st) == 0) { /* Simple ersatz mktemp */
|
2003-09-02 04:12:11 -04:00
|
|
|
q = p;
|
|
|
|
while (1) {
|
|
|
|
if (*q == '\0') goto fail;
|
|
|
|
if (*q == 'z') {
|
|
|
|
*q++ = 'a';
|
|
|
|
} else {
|
|
|
|
if (*q >= '0' && *q <= '9')
|
|
|
|
*q = 'a';
|
|
|
|
else
|
|
|
|
*++q;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2003-08-19 15:19:38 -04:00
|
|
|
}
|
|
|
|
if (errno == ENOENT)
|
2003-09-02 04:12:11 -04:00
|
|
|
name = buf;
|
2003-08-19 15:19:38 -04:00
|
|
|
fail: ;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
if (name == 0 || name[0] == '\0') {
|
2003-09-02 04:12:11 -04:00
|
|
|
Enable_Interrupts;
|
|
|
|
Raise_Error("cannot create temp file name");
|
2003-08-19 15:19:38 -04:00
|
|
|
}
|
|
|
|
ret = Make_String(name, strlen(name));
|
2003-08-25 10:17:09 -04:00
|
|
|
#ifdef HAVE_TEMPNAM
|
2003-08-19 15:19:38 -04:00
|
|
|
free(name);
|
|
|
|
Enable_Interrupts;
|
|
|
|
#endif
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2003-09-04 09:29:16 -04:00
|
|
|
void elk_init_unix_temp() {
|
2003-08-19 15:19:38 -04:00
|
|
|
Def_Prim(P_Tempname, "unix-tempname", 0, 2, VARARGS);
|
|
|
|
}
|