Sync usage with man page.
[netbsd-mini2440.git] / gnu / dist / gettext / gettext-tools / lib / tmpdir.c
blob5c19f7ee595c74b107af26c74f03414dc998cc5a
1 /* Copyright (C) 1999, 2001-2002 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 /* Extracted from sysdeps/posix/tempname.c. */
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
25 /* Specification. */
26 #include "tmpdir.h"
28 #include <stdbool.h>
29 #include <stdlib.h>
30 #include <string.h>
32 #include <errno.h>
33 #ifndef __set_errno
34 # define __set_errno(Val) errno = (Val)
35 #endif
37 #include <stdio.h>
38 #ifndef P_tmpdir
39 # define P_tmpdir "/tmp"
40 #endif
42 #include <sys/stat.h>
43 #if STAT_MACROS_BROKEN
44 # undef S_ISDIR
45 #endif
46 #if !defined S_ISDIR && defined S_IFDIR
47 # define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
48 #endif
49 #if !S_IRUSR && S_IREAD
50 # define S_IRUSR S_IREAD
51 #endif
52 #if !S_IRUSR
53 # define S_IRUSR 00400
54 #endif
55 #if !S_IWUSR && S_IWRITE
56 # define S_IWUSR S_IWRITE
57 #endif
58 #if !S_IWUSR
59 # define S_IWUSR 00200
60 #endif
61 #if !S_IXUSR && S_IEXEC
62 # define S_IXUSR S_IEXEC
63 #endif
64 #if !S_IXUSR
65 # define S_IXUSR 00100
66 #endif
68 #if _LIBC
69 # define struct_stat64 struct stat64
70 #else
71 # define struct_stat64 struct stat
72 # define __xstat64(version, path, buf) stat (path, buf)
73 #endif
75 #if ! (HAVE___SECURE_GETENV || _LIBC)
76 # define __secure_getenv getenv
77 #endif
80 /* Return nonzero if DIR is an existent directory. */
81 static bool
82 direxists (const char *dir)
84 struct_stat64 buf;
85 return __xstat64 (_STAT_VER, dir, &buf) == 0 && S_ISDIR (buf.st_mode);
88 /* Path search algorithm, for tmpnam, tmpfile, etc. If DIR is
89 non-null and exists, uses it; otherwise uses the first of $TMPDIR,
90 P_tmpdir, /tmp that exists. Copies into TMPL a template suitable
91 for use with mk[s]temp. Will fail (-1) if DIR is non-null and
92 doesn't exist, none of the searched dirs exists, or there's not
93 enough space in TMPL. */
94 int
95 path_search (char *tmpl, size_t tmpl_len, const char *dir, const char *pfx,
96 bool try_tmpdir)
98 const char *d;
99 size_t dlen, plen;
101 if (!pfx || !pfx[0])
103 pfx = "file";
104 plen = 4;
106 else
108 plen = strlen (pfx);
109 if (plen > 5)
110 plen = 5;
113 if (try_tmpdir)
115 d = __secure_getenv ("TMPDIR");
116 if (d != NULL && direxists (d))
117 dir = d;
118 else if (dir != NULL && direxists (dir))
119 /* nothing */ ;
120 else
121 dir = NULL;
123 if (dir == NULL)
125 if (direxists (P_tmpdir))
126 dir = P_tmpdir;
127 else if (strcmp (P_tmpdir, "/tmp") != 0 && direxists ("/tmp"))
128 dir = "/tmp";
129 else
131 __set_errno (ENOENT);
132 return -1;
136 dlen = strlen (dir);
137 while (dlen > 1 && dir[dlen - 1] == '/')
138 dlen--; /* remove trailing slashes */
140 /* check we have room for "${dir}/${pfx}XXXXXX\0" */
141 if (tmpl_len < dlen + 1 + plen + 6 + 1)
143 __set_errno (EINVAL);
144 return -1;
147 sprintf (tmpl, "%.*s/%.*sXXXXXX", (int) dlen, dir, (int) plen, pfx);
148 return 0;