diff options
-rw-r--r-- | gnu/usr.bin/gcc/choose-temp.c | 102 | ||||
-rw-r--r-- | gnu/usr.bin/gcc/gcc.c | 35 |
2 files changed, 106 insertions, 31 deletions
diff --git a/gnu/usr.bin/gcc/choose-temp.c b/gnu/usr.bin/gcc/choose-temp.c index 7f6aa965a38..595ce16a0f3 100644 --- a/gnu/usr.bin/gcc/choose-temp.c +++ b/gnu/usr.bin/gcc/choose-temp.c @@ -1,5 +1,6 @@ +/* slightly hacked up version of choose-temp.c, use only in OpenBSD gcc */ /* Utility to pick a temporary filename prefix. - Copyright (C) 1996, 1997 Free Software Foundation, Inc. + Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc. This file is part of the libiberty library. Libiberty is free software; you can redistribute it and/or @@ -17,17 +18,21 @@ License along with libiberty; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/* This file exports one function: choose_temp_base. */ +/* This file exports two functions: choose_temp_base and make_temp_file. */ /* This file lives in at least two places: libiberty and gcc. Don't change one without the other. */ -#ifdef IN_GCC +#ifdef HAVE_CONFIG_H #include "config.h" #endif -#ifdef HAVE_SYS_FILE_H +#include <stdio.h> /* May get P_tmpdir. */ #include <sys/types.h> +#ifdef HAVE_UNISTD_H +#include <unistd.h> +#endif +#ifdef HAVE_SYS_FILE_H #include <sys/file.h> /* May get R_OK, etc. on some systems. */ #endif @@ -37,14 +42,8 @@ Boston, MA 02111-1307, USA. */ #define X_OK 1 #endif -#include <stdio.h> /* May get P_tmpdir. */ - -#ifdef IN_GCC -#include "gansidecl.h" -extern char *xmalloc (); -#else -#include "ansidecl.h" -#include "libiberty.h" +extern int mkstemps (); +#ifndef IN_GCC #if defined (__MSDOS__) || defined (_WIN32) #define DIR_SEPARATOR '\\' #endif @@ -91,7 +90,10 @@ try (dir, base) /* Return a prefix for temporary file names or NULL if unable to find one. The current directory is chosen if all else fails so the program is exited if a temporary directory can't be found (mktemp fails). - The buffer for the result is obtained with xmalloc. */ + The buffer for the result is obtained with xmalloc. + + This function is provided for backwards compatability only. It use + is not recommended. */ char * choose_temp_base () @@ -102,16 +104,13 @@ choose_temp_base () static char tmp[] = { DIR_SEPARATOR, 't', 'm', 'p', 0 }; static char usrtmp[] = { DIR_SEPARATOR, 'u', 's', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 }; -#ifndef MPW base = try (getenv ("TMPDIR"), base); base = try (getenv ("TMP"), base); base = try (getenv ("TEMP"), base); -#ifdef 0 /* XXX - P_tmpdir is not /tmp - etheisen */ #ifdef P_tmpdir base = try (P_tmpdir, base); #endif -#endif /* XXX */ /* Try /usr/tmp, then /tmp. */ base = try (usrtmp, base); @@ -121,24 +120,15 @@ choose_temp_base () if (base == 0) base = "."; -#else /* MPW */ - base = ":"; -#endif - len = strlen (base); temp_filename = xmalloc (len + 1 /*DIR_SEPARATOR*/ + strlen (TEMP_FILE) + 1); strcpy (temp_filename, base); -#ifndef MPW if (len != 0 && temp_filename[len-1] != '/' && temp_filename[len-1] != DIR_SEPARATOR) temp_filename[len++] = DIR_SEPARATOR; -#else /* MPW */ - if (temp_filename[len-1] != ':') - temp_filename[len++] = ':'; -#endif /* MPW */ strcpy (temp_filename + len, TEMP_FILE); mktemp (temp_filename); @@ -146,3 +136,65 @@ choose_temp_base () abort (); return temp_filename; } + +/* Return a temporary file name (as a string) or NULL if unable to create + one. */ + +char * +make_temp_file (suffix) + char *suffix; +{ + char *base = 0; + char *temp_filename; + int base_len, suffix_len; + int fd; + static char tmp[] = { DIR_SEPARATOR, 't', 'm', 'p', 0 }; + static char usrtmp[] = { DIR_SEPARATOR, 'u', 's', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 }; + + base = try (getenv ("TMPDIR"), base); + base = try (getenv ("TMP"), base); + base = try (getenv ("TEMP"), base); + +#ifdef P_tmpdir + base = try (P_tmpdir, base); +#endif + + /* Try /usr/tmp, then /tmp. */ + base = try (usrtmp, base); + base = try (tmp, base); + + /* If all else fails, use the current directory! */ + if (base == 0) + base = "."; + + base_len = strlen (base); + + if (suffix) + suffix_len = strlen (suffix); + else + suffix_len = 0; + + temp_filename = xmalloc (base_len + 1 /*DIR_SEPARATOR*/ + + strlen (TEMP_FILE) + + suffix_len + 1); + strcpy (temp_filename, base); + + if (base_len != 0 + && temp_filename[base_len-1] != '/' + && temp_filename[base_len-1] != DIR_SEPARATOR) + temp_filename[base_len++] = DIR_SEPARATOR; + strcpy (temp_filename + base_len, TEMP_FILE); + + if (suffix) + strcat (temp_filename, suffix); + + fd = mkstemps (temp_filename, suffix_len); + /* If mkstemps failed, then something bad is happening. Maybe we should + issue a message about a possible security attack in progress? */ + if (fd == -1) + abort (); + /* Similarly if we can not close the file. */ + if (close (fd)) + abort (); + return temp_filename; +} diff --git a/gnu/usr.bin/gcc/gcc.c b/gnu/usr.bin/gcc/gcc.c index c548d7fdb62..dce5654eab8 100644 --- a/gnu/usr.bin/gcc/gcc.c +++ b/gnu/usr.bin/gcc/gcc.c @@ -1224,8 +1224,12 @@ static int argbuf_length; static int argbuf_index; +#ifdef MKTEMP_EACH_FILE + +extern char *make_temp_file PROTO((char *)); + /* This is the list of suffixes and codes (%g/%u/%U) and the associated - temp file. Used only if MKTEMP_EACH_FILE. */ + temp file. */ static struct temp_name { char *suffix; /* suffix associated with the code. */ @@ -1235,6 +1239,8 @@ static struct temp_name { int filename_length; /* strlen (filename). */ struct temp_name *next; } *temp_names; +#endif + /* Number of commands executed so far. */ @@ -3404,7 +3410,24 @@ do_spec_1 (spec, inswitch, soft_matched_part) That matters for the names of object files. In 2.4, do something about that. */ struct temp_name *t; + int suffix_length; char *suffix = p; + + if (p[0] == '%' && p[1] == 'O') + { + p += 2; + /* We don't support extra suffix characters after %O. */ + if (p[2] == '.' || isalpha ((unsigned char)p[3])) + abort (); + suffix = OBJECT_SUFFIX; + suffix_length = strlen (OBJECT_SUFFIX); + } + else + { + while (*p == '.' || isalpha ((unsigned char)*p)) + p++; + suffix_length = p - suffix; + } while (*p == '.' || isalpha (*p) || (p[0] == '%' && p[1] == 'O')) p++; @@ -3412,8 +3435,8 @@ do_spec_1 (spec, inswitch, soft_matched_part) /* See if we already have an association of %g/%u/%U and suffix. */ for (t = temp_names; t; t = t->next) - if (t->length == p - suffix - && strncmp (t->suffix, suffix, p - suffix) == 0 + if (t->length == suffix_length + && strncmp (t->suffix, suffix, suffix_length) == 0 && t->unique == (c != 'g')) break; @@ -3426,10 +3449,10 @@ do_spec_1 (spec, inswitch, soft_matched_part) t->next = temp_names; temp_names = t; } - t->length = p - suffix; - t->suffix = save_string (suffix, p - suffix); + t->length = suffix_length; + t->suffix = save_string (suffix, suffix_length); t->unique = (c != 'g'); - temp_filename = choose_temp_base (); + temp_filename = make_temp_file (t->suffix); temp_filename_length = strlen (temp_filename); t->filename = temp_filename; t->filename_length = temp_filename_length; |