diff options
author | jimb <jimb@138bc75d-0d04-0410-961f-82ee72b054a4> | 2006-04-12 06:24:40 +0000 |
---|---|---|
committer | jimb <jimb@138bc75d-0d04-0410-961f-82ee72b054a4> | 2006-04-12 06:24:40 +0000 |
commit | 9d353bf21a5095f930d3ee1248c09b67f521fb25 (patch) | |
tree | 153d88b95194f16886d9bb4f3b71a493c6657de5 /libiberty | |
parent | 4e7cf9f8c73a2628d81b7306969d8020bfe8287e (diff) | |
download | gcc-9d353bf21a5095f930d3ee1248c09b67f521fb25.tar.gz |
2006-04-10 Jim Blandy <jimb@codesourcery.com>
* pex-common.c (temp_file): New function, containing guts of
pex-style temporary file name generation.
(pex_run): Use it.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@112882 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libiberty')
-rw-r--r-- | libiberty/ChangeLog | 6 | ||||
-rw-r--r-- | libiberty/pex-common.c | 103 |
2 files changed, 66 insertions, 43 deletions
diff --git a/libiberty/ChangeLog b/libiberty/ChangeLog index e79c27f7da4..c588c1afdc2 100644 --- a/libiberty/ChangeLog +++ b/libiberty/ChangeLog @@ -1,3 +1,9 @@ +2006-04-10 Jim Blandy <jimb@codesourcery.com> + + * pex-common.c (temp_file): New function, containing guts of + pex-style temporary file name generation. + (pex_run): Use it. + 2006-04-06 Carlos O'Donell <carlos@codesourcery.com> * Makefile.in: Add install-html, install-html-am, and diff --git a/libiberty/pex-common.c b/libiberty/pex-common.c index db842aed243..eb14ce5b1ff 100644 --- a/libiberty/pex-common.c +++ b/libiberty/pex-common.c @@ -91,6 +91,56 @@ pex_add_remove (struct pex_obj *obj, const char *name, int allocated) obj->remove[obj->remove_count - 1] = add; } +/* Generate a temporary file name based on OBJ, FLAGS, and NAME. + Return NULL if we were unable to reserve a temporary filename. + + If non-NULL, the result is either allocated with malloc, or the + same pointer as NAME. */ +static char * +temp_file (struct pex_obj *obj, int flags, char *name) +{ + if (name == NULL) + { + if (obj->tempbase == NULL) + { + name = make_temp_file (NULL); + } + else + { + int len = strlen (obj->tempbase); + int out; + + if (len >= 6 + && strcmp (obj->tempbase + len - 6, "XXXXXX") == 0) + name = xstrdup (obj->tempbase); + else + name = concat (obj->tempbase, "XXXXXX", NULL); + + out = mkstemps (name, 0); + if (out < 0) + { + free (name); + return NULL; + } + + /* This isn't obj->funcs->close because we got the + descriptor from mkstemps, not from a function in + obj->funcs. Calling close here is just like what + make_temp_file does. */ + close (out); + } + } + else if ((flags & PEX_SUFFIX) != 0) + { + if (obj->tempbase == NULL) + name = make_temp_file (name); + else + name = concat (obj->tempbase, name, NULL); + } + + return name; +} + /* Run a program. */ const char * @@ -161,49 +211,16 @@ pex_run (struct pex_obj *obj, int flags, const char *executable, } else if ((obj->flags & PEX_USE_PIPES) == 0) { - if (outname == NULL) - { - if (obj->tempbase == NULL) - { - outname = make_temp_file (NULL); - outname_allocated = 1; - } - else - { - int len = strlen (obj->tempbase); - - if (len >= 6 - && strcmp (obj->tempbase + len - 6, "XXXXXX") == 0) - outname = xstrdup (obj->tempbase); - else - outname = concat (obj->tempbase, "XXXXXX", NULL); - - outname_allocated = 1; - - out = mkstemps (outname, 0); - if (out < 0) - { - *err = 0; - errmsg = "could not create temporary output file"; - goto error_exit; - } - - /* This isn't obj->funcs->close because we got the - descriptor from mkstemps, not from a function in - obj->funcs. Calling close here is just like what - make_temp_file does. */ - close (out); - out = -1; - } - } - else if ((flags & PEX_SUFFIX) != 0) - { - if (obj->tempbase == NULL) - outname = make_temp_file (outname); - else - outname = concat (obj->tempbase, outname, NULL); - outname_allocated = 1; - } + outname = temp_file (obj, flags, outname); + if (! outname) + { + *err = 0; + errmsg = "could not create temporary file"; + goto error_exit; + } + + if (outname != orig_outname) + outname_allocated = 1; if ((obj->flags & PEX_SAVE_TEMPS) == 0) { |