annotate lib/tmpdir.c @ 7302:8a1a9361108c

* _fpending.c: Include <config.h> unconditionally, since we no longer worry about uses that don't define HAVE_CONFIG_H. * acl.c, alloca.c, argmatch.c, atexit.c, backupfile.c: * basename.c, c-stack.c, c-strtod.c, calloc.c, canon-host.c: * canonicalize.c, chdir-long.c, chdir-safer.c, chown.c: * cloexec.c, close-stream.c, closeout.c, creat-safer.c: * cycle-check.c, diacrit.c, dirchownmod.c, dirfd.c, dirname.c: * dup-safer.c, dup2.c, error.c, euidaccess.c, exclude.c: * exitfail.c, fchmodat.c, fchown-stub.c, fd-safer.c: * file-type.c, fileblocks.c, filemode.c, filenamecat.c: * fnmatch.c, fopen-safer.c, fprintftime.c, free.c, fsusage.c: * ftruncate.c, fts-cycle.c, fts.c, full-write.c, gai_strerror.c: * getcwd.c, getdate.y, getdomainname.c, getgroups.c: * gethostname.c, gethrxtime.c, getloadavg.c, getlogin_r.c: * getndelim2.c, getnline.c, getopt.c, getopt1.c, getpass.c: * gettime.c, gettimeofday.c, getugroups.c, getusershell.c: * glob.c, group-member.c, hard-locale.c, hash-pjw.c, hash.c: * human.c, idcache.c, inet_ntop.c, inet_pton.c, inttostr.c: * isdir.c, lchown.c, linebuffer.c, long-options.c, lstat.c: * malloc.c, md5.c, memcasecmp.c, memchr.c, memcmp.c, memcoll.c: * memcpy.c, memmove.c, memrchr.c, mkancesdirs.c, mkdir-p.c: * mkdir.c, mkdirat.c, mkstemp-safer.c, mkstemp.c, modechange.c: * mountlist.c, nanosleep.c, obstack.c, open-safer.c: * openat-die.c, openat.c, pagealign_alloc.c, physmem.c: * pipe-safer.c, posixtm.c, posixver.c, putenv.c, quote.c: * quotearg.c, raise.c, readtokens.c, readtokens0.c, readutmp.c: * realloc.c, regex.c, rename.c, rmdir.c, rpmatch.c, safe-read.c: * same.c, save-cwd.c, savedir.c, setenv.c, settime.c, sha1.c: * sig2str.c, snprintf.c, strdup.c, strerror.c, strftime.c: * stripslash.c, strndup.c, strnlen.c, strpbrk.c, strtod.c: * strtoimax.c, strtol.c, strverscmp.c, tempname.c, time_r.c: * timegm.c, tmpfile-safer.c, unlinkdir.c, userspec.c, utime.c: * utimecmp.c, utimens.c, version-etc-fsf.c, version-etc.c: * xalloc-die.c, xgetcwd.c, xgethostname.c, xmalloc.c: * xmemcoll.c, xnanosleep.c, xreadlink.c, xstrtod.c: * xstrtoimax.c, xstrtol.c, xstrtoumax.c, yesno.c: Likewise.
author Paul Eggert <eggert@cs.ucla.edu>
date Wed, 13 Sep 2006 22:38:14 +0000
parents adb21c293305
children 1c4ed7637c24
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7043
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Copyright (C) 1999, 2001-2002, 2006 Free Software Foundation, Inc.
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
2 This file is part of the GNU C Library.
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3
7139
adb21c293305 Add copyright notices to long-enough files that lack them, since
Paul Eggert <eggert@cs.ucla.edu>
parents: 7043
diff changeset
4 This program is free software; you can redistribute it and/or modify
adb21c293305 Add copyright notices to long-enough files that lack them, since
Paul Eggert <eggert@cs.ucla.edu>
parents: 7043
diff changeset
5 it under the terms of the GNU General Public License as published by
adb21c293305 Add copyright notices to long-enough files that lack them, since
Paul Eggert <eggert@cs.ucla.edu>
parents: 7043
diff changeset
6 the Free Software Foundation; either version 2, or (at your option)
adb21c293305 Add copyright notices to long-enough files that lack them, since
Paul Eggert <eggert@cs.ucla.edu>
parents: 7043
diff changeset
7 any later version.
7043
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8
7139
adb21c293305 Add copyright notices to long-enough files that lack them, since
Paul Eggert <eggert@cs.ucla.edu>
parents: 7043
diff changeset
9 This program is distributed in the hope that it will be useful,
7043
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
7139
adb21c293305 Add copyright notices to long-enough files that lack them, since
Paul Eggert <eggert@cs.ucla.edu>
parents: 7043
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
adb21c293305 Add copyright notices to long-enough files that lack them, since
Paul Eggert <eggert@cs.ucla.edu>
parents: 7043
diff changeset
12 GNU General Public License for more details.
7043
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13
7139
adb21c293305 Add copyright notices to long-enough files that lack them, since
Paul Eggert <eggert@cs.ucla.edu>
parents: 7043
diff changeset
14 You should have received a copy of the GNU General Public License
adb21c293305 Add copyright notices to long-enough files that lack them, since
Paul Eggert <eggert@cs.ucla.edu>
parents: 7043
diff changeset
15 along with this program; if not, write to the Free Software Foundation,
adb21c293305 Add copyright notices to long-enough files that lack them, since
Paul Eggert <eggert@cs.ucla.edu>
parents: 7043
diff changeset
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
7043
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18 /* Extracted from sysdeps/posix/tempname.c. */
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 #ifdef HAVE_CONFIG_H
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 # include "config.h"
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 #endif
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 /* Specification. */
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 #include "tmpdir.h"
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 #include <stdbool.h>
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 #include <stdlib.h>
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 #include <string.h>
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 #include <errno.h>
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 #ifndef __set_errno
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 # define __set_errno(Val) errno = (Val)
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 #endif
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 #include <stdio.h>
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 #ifndef P_tmpdir
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 # define P_tmpdir "/tmp"
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 #endif
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 #include <sys/stat.h>
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 #if !defined S_ISDIR && defined S_IFDIR
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 # define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 #endif
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 #if !S_IRUSR && S_IREAD
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 # define S_IRUSR S_IREAD
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 #endif
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 #if !S_IRUSR
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 # define S_IRUSR 00400
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 #endif
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 #if !S_IWUSR && S_IWRITE
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 # define S_IWUSR S_IWRITE
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 #endif
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 #if !S_IWUSR
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 # define S_IWUSR 00200
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 #endif
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 #if !S_IXUSR && S_IEXEC
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 # define S_IXUSR S_IEXEC
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 #endif
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 #if !S_IXUSR
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 # define S_IXUSR 00100
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 #endif
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 #if _LIBC
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 # define struct_stat64 struct stat64
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 #else
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 # define struct_stat64 struct stat
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 # define __xstat64(version, path, buf) stat (path, buf)
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 #endif
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 #if ! (HAVE___SECURE_GETENV || _LIBC)
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 # define __secure_getenv getenv
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 #endif
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 /* Pathname support.
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 ISSLASH(C) tests whether C is a directory separator character.
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 */
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 #if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__ || defined __EMX__ || defined __DJGPP__
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 /* Win32, Cygwin, OS/2, DOS */
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 # define ISSLASH(C) ((C) == '/' || (C) == '\\')
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 #else
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 /* Unix */
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 # define ISSLASH(C) ((C) == '/')
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 #endif
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 /* Return nonzero if DIR is an existent directory. */
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 static bool
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 direxists (const char *dir)
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 {
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91 struct_stat64 buf;
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92 return __xstat64 (_STAT_VER, dir, &buf) == 0 && S_ISDIR (buf.st_mode);
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93 }
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95 /* Path search algorithm, for tmpnam, tmpfile, etc. If DIR is
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96 non-null and exists, uses it; otherwise uses the first of $TMPDIR,
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97 P_tmpdir, /tmp that exists. Copies into TMPL a template suitable
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98 for use with mk[s]temp. Will fail (-1) if DIR is non-null and
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
99 doesn't exist, none of the searched dirs exists, or there's not
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
100 enough space in TMPL. */
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
101 int
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
102 path_search (char *tmpl, size_t tmpl_len, const char *dir, const char *pfx,
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
103 bool try_tmpdir)
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104 {
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105 const char *d;
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
106 size_t dlen, plen;
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
107
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
108 if (!pfx || !pfx[0])
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
109 {
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
110 pfx = "file";
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
111 plen = 4;
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
112 }
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
113 else
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
114 {
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
115 plen = strlen (pfx);
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
116 if (plen > 5)
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
117 plen = 5;
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
118 }
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
119
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
120 if (try_tmpdir)
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
121 {
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
122 d = __secure_getenv ("TMPDIR");
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
123 if (d != NULL && direxists (d))
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
124 dir = d;
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
125 else if (dir != NULL && direxists (dir))
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
126 /* nothing */ ;
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
127 else
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
128 dir = NULL;
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
129 }
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
130 if (dir == NULL)
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
131 {
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
132 if (direxists (P_tmpdir))
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
133 dir = P_tmpdir;
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
134 else if (strcmp (P_tmpdir, "/tmp") != 0 && direxists ("/tmp"))
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
135 dir = "/tmp";
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
136 else
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
137 {
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
138 __set_errno (ENOENT);
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
139 return -1;
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
140 }
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
141 }
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
142
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
143 dlen = strlen (dir);
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
144 while (dlen >= 1 && ISSLASH (dir[dlen - 1]))
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
145 dlen--; /* remove trailing slashes */
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
146
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
147 /* check we have room for "${dir}/${pfx}XXXXXX\0" */
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
148 if (tmpl_len < dlen + 1 + plen + 6 + 1)
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
149 {
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
150 __set_errno (EINVAL);
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
151 return -1;
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
152 }
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
153
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
154 sprintf (tmpl, "%.*s/%.*sXXXXXX", (int) dlen, dir, (int) plen, pfx);
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
155 return 0;
4f84bccd7da0 New module 'tmpdir'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
156 }