100bf4279Sespie /* Utility to pick a temporary filename prefix.
200bf4279Sespie Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc.
300bf4279Sespie
400bf4279Sespie This file is part of the libiberty library.
500bf4279Sespie Libiberty is free software; you can redistribute it and/or
600bf4279Sespie modify it under the terms of the GNU Library General Public
700bf4279Sespie License as published by the Free Software Foundation; either
800bf4279Sespie version 2 of the License, or (at your option) any later version.
900bf4279Sespie
1000bf4279Sespie Libiberty is distributed in the hope that it will be useful,
1100bf4279Sespie but WITHOUT ANY WARRANTY; without even the implied warranty of
1200bf4279Sespie MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1300bf4279Sespie Library General Public License for more details.
1400bf4279Sespie
1500bf4279Sespie You should have received a copy of the GNU Library General Public
1600bf4279Sespie License along with libiberty; see the file COPYING.LIB. If not,
17150b7e42Smiod write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
18150b7e42Smiod Boston, MA 02110-1301, USA. */
1900bf4279Sespie
2000bf4279Sespie #ifdef HAVE_CONFIG_H
2100bf4279Sespie #include "config.h"
2200bf4279Sespie #endif
2300bf4279Sespie
2400bf4279Sespie #include <stdio.h> /* May get P_tmpdir. */
2500bf4279Sespie #ifdef HAVE_STDLIB_H
2600bf4279Sespie #include <stdlib.h>
2700bf4279Sespie #endif
2800bf4279Sespie #ifdef HAVE_STRING_H
2900bf4279Sespie #include <string.h>
3000bf4279Sespie #endif
3100bf4279Sespie
3200bf4279Sespie #include "libiberty.h"
33150b7e42Smiod extern char *choose_tmpdir (void);
3400bf4279Sespie
3500bf4279Sespie /* Name of temporary file.
3600bf4279Sespie mktemp requires 6 trailing X's. */
3700bf4279Sespie #define TEMP_FILE "ccXXXXXX"
3837c53322Sespie #define TEMP_FILE_LEN (sizeof(TEMP_FILE) - 1)
3900bf4279Sespie
4037c53322Sespie /*
4100bf4279Sespie
4237c53322Sespie @deftypefn Extension char* choose_temp_base (void)
4337c53322Sespie
4437c53322Sespie Return a prefix for temporary file names or @code{NULL} if unable to
4537c53322Sespie find one. The current directory is chosen if all else fails so the
4637c53322Sespie program is exited if a temporary directory can't be found (@code{mktemp}
4737c53322Sespie fails). The buffer for the result is obtained with @code{xmalloc}.
4837c53322Sespie
4937c53322Sespie This function is provided for backwards compatability only. Its use is
5037c53322Sespie not recommended.
5137c53322Sespie
5237c53322Sespie @end deftypefn
5337c53322Sespie
5437c53322Sespie */
5500bf4279Sespie
56*1209fb42Schl #if defined(__MSDOS__) && !defined(__GO32__)
5700bf4279Sespie char *
choose_temp_base(void)58150b7e42Smiod choose_temp_base (void)
5900bf4279Sespie {
6037c53322Sespie const char *base = choose_tmpdir ();
6100bf4279Sespie char *temp_filename;
6200bf4279Sespie int len;
6300bf4279Sespie
6400bf4279Sespie len = strlen (base);
65150b7e42Smiod temp_filename = XNEWVEC (char, len + TEMP_FILE_LEN + 1);
6600bf4279Sespie strcpy (temp_filename, base);
6700bf4279Sespie strcpy (temp_filename + len, TEMP_FILE);
6800bf4279Sespie
6900bf4279Sespie mktemp (temp_filename);
7000bf4279Sespie if (strlen (temp_filename) == 0)
7100bf4279Sespie abort ();
7200bf4279Sespie return temp_filename;
7300bf4279Sespie }
74*1209fb42Schl #endif
75