1 /* libSoX internal functions that apply to both formats and effects
2 * All public functions & data are prefixed with lsx_ .
4 * Copyright (c) 2008 robs@users.sourceforge.net
6 * This library is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or (at
9 * your option) any later version.
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
14 * General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this library; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
32 #if defined(_MSC_VER) || defined(__MINGW32__)
33 #define MKTEMP_X _O_BINARY|_O_TEMPORARY
40 #include <sys/types.h>
42 #define mkstemp(t) open(mktemp(t), MKTEMP_X|O_RDWR|O_TRUNC|O_CREAT, S_IREAD|S_IWRITE)
43 #define FAKE_MKSTEMP "fake "
49 static int check_dir(char * buf
, size_t buflen
, char const * name
)
52 if (!name
|| stat(name
, &st
) || (st
.st_mode
& S_IFMT
) != S_IFDIR
)
58 strncpy(buf
, name
, buflen
);
60 return strlen(name
) == strlen(buf
);
65 FILE * lsx_tmpfile(void)
67 char const * path
= sox_globals
.tmp_path
;
70 On Win32, tmpfile() is broken - it creates the file in the root directory of
71 the current drive (the user probably doesn't have permission to write there!)
72 instead of in a valid temporary directory (like TEMP or TMP). So if tmp_path
73 is null, figure out a reasonable default.
74 To force use of tmpfile(), set sox_globals.tmp_path = "".
79 static char default_path
[260] = "";
80 if (default_path
[0] == 0
81 && !check_dir(default_path
, sizeof(default_path
), getenv("TEMP"))
82 && !check_dir(default_path
, sizeof(default_path
), getenv("TMP"))
84 && !check_dir(default_path
, sizeof(default_path
), "/tmp")
88 strcpy(default_path
, ".");
95 if (path
&& path
[0]) {
96 /* Emulate tmpfile (delete on close); tmp dir is given tmp_path: */
97 char const * const end
= "/libSoX.tmp.XXXXXX";
98 char * name
= lsx_malloc(strlen(path
) + strlen(end
) + 1);
102 fildes
= mkstemp(name
);
104 lsx_debug(FAKE_MKSTEMP
"mkstemp, name=%s (unlinked)", name
);
107 lsx_debug(FAKE_MKSTEMP
"mkstemp, name=%s (O_TEMPORARY)", name
);
110 return fildes
== -1? NULL
: fdopen(fildes
, "w+b");
113 /* Use standard tmpfile (delete on close); tmp dir is undefined: */
114 lsx_debug("tmpfile()");