From: Paul Smith Date: Sat, 11 Feb 2017 18:42:37 +0000 (-0500) Subject: Rename output_tmpfile() to a misc function get_tmpfile() X-Git-Tag: 4.2.90~91 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=5acc59c70460ca350470e314c90459da8c206158;p=thirdparty%2Fmake.git Rename output_tmpfile() to a misc function get_tmpfile() * output.c: Remove output_tmpfile() and umask handling. * output.h: Ditto. * misc.c: Add get_tmpfile() and umask handling. * makeint.h: Ditto. * function.c: Rename output_tmpfile() to get_tmpfile(). * main.c: Ditto. * vmsjobs.c: Ditto. --- diff --git a/function.c b/function.c index 7a2fcd4b..8d8f198f 100644 --- a/function.c +++ b/function.c @@ -1924,9 +1924,8 @@ func_shell_base (char *o, char **argv, int trim_newlines) return o; /* Note the mktemp() is a security hole, but this only runs on Amiga. - Ideally we would use output_tmpfile(), but this uses a special - Open(), not fopen(), and I'm not familiar enough with the code to mess - with it. */ + Ideally we would use get_tmpfile(), but this uses a special Open(), not + fopen(), and I'm not familiar enough with the code to mess with it. */ strcpy (tmp_output, "t:MakeshXXXXXXXX"); mktemp (tmp_output); child_stdout = Open (tmp_output, MODE_NEWFILE); diff --git a/main.c b/main.c index 5dd539b8..939a1577 100644 --- a/main.c +++ b/main.c @@ -1824,7 +1824,7 @@ main (int argc, char **argv, char **envp) #endif /* !HAVE_DOS_PATHS */ strcat (template, DEFAULT_TMPFILE); - outfile = output_tmpfile (&stdin_nm, template); + outfile = get_tmpfile (&stdin_nm, template); if (outfile == 0) pfatal_with_name (_("fopen (temporary file)")); while (!feof (stdin) && ! ferror (stdin)) diff --git a/makeint.h b/makeint.h index 53a895e3..f7af77b3 100644 --- a/makeint.h +++ b/makeint.h @@ -291,6 +291,14 @@ char *strerror (int errnum); char *strsignal (int signum); #endif +#if defined(HAVE_UMASK) +# define UMASK(_m) umask (_m) +# define MODE_T mode_t +#else +# define UMASK(_m) 0 +# define MODE_T int +#endif + /* ISDIGIT offers the following features: - Its arg may be any int or unsigned int; it need not be an unsigned char. - It's guaranteed to evaluate its argument exactly once. @@ -520,6 +528,7 @@ int alpha_compare (const void *, const void *); void print_spaces (unsigned int); char *find_percent (char *); const char *find_percent_cached (const char **); +FILE *get_tmpfile (char **, const char *); #ifndef NO_ARCHIVES int ar_name (const char *); diff --git a/misc.c b/misc.c index c9e88b61..5dc1881d 100644 --- a/misc.c +++ b/misc.c @@ -23,6 +23,10 @@ this program. If not, see . */ #include +#ifdef WINDOWS32 +# include +#endif + #ifdef HAVE_FCNTL_H # include #else @@ -391,6 +395,67 @@ free_ns_chain (struct nameseq *ns) } +/* Provide support for temporary files. */ + +#ifndef HAVE_STDLIB_H +# ifdef HAVE_MKSTEMP +int mkstemp (char *template); +# else +char *mktemp (char *template); +# endif +#endif + +FILE * +get_tmpfile (char **name, const char *template) +{ + FILE *file; +#ifdef HAVE_FDOPEN + int fd; +#endif + + /* Preserve the current umask, and set a restrictive one for temp files. */ + MODE_T mask = UMASK (0077); + +#if defined(HAVE_MKSTEMP) || defined(HAVE_MKTEMP) +# define TEMPLATE_LEN strlen (template) +#else +# define TEMPLATE_LEN L_tmpnam +#endif + *name = xmalloc (TEMPLATE_LEN + 1); + strcpy (*name, template); + +#if defined(HAVE_MKSTEMP) && defined(HAVE_FDOPEN) + /* It's safest to use mkstemp(), if we can. */ + EINTRLOOP (fd, mkstemp (*name)); + if (fd == -1) + file = NULL; + else + file = fdopen (fd, "w"); +#else +# ifdef HAVE_MKTEMP + (void) mktemp (*name); +# else + (void) tmpnam (*name); +# endif + +# ifdef HAVE_FDOPEN + /* Can't use mkstemp(), but guard against a race condition. */ + EINTRLOOP (fd, open (*name, O_CREAT|O_EXCL|O_WRONLY, 0600)); + if (fd == -1) + return 0; + file = fdopen (fd, "w"); +# else + /* Not secure, but what can we do? */ + file = fopen (*name, "w"); +# endif +#endif + + UMASK (mask); + + return file; +} + + #if !HAVE_STRCASECMP && !HAVE_STRICMP && !HAVE_STRCMPI /* If we don't have strcasecmp() (from POSIX), or anything that can substitute for it, define our own version. */ diff --git a/output.c b/output.c index 4945433d..142a9642 100644 --- a/output.c +++ b/output.c @@ -53,14 +53,6 @@ unsigned int stdio_traced = 0; # define STREAM_OK(_s) 1 #endif -#if defined(HAVE_UMASK) -# define UMASK(_m) umask (_m) -# define MODE_T mode_t -#else -# define UMASK(_m) 0 -# define MODE_T int -#endif - /* Write a BUFFER of size LEN to file descriptor FD. Handle EINTR and other short writes. If we get an error, ignore it. */ int @@ -428,67 +420,6 @@ output_dump (struct output *out) #endif /* NO_OUTPUT_SYNC */ -/* Provide support for temporary files. */ - -#ifndef HAVE_STDLIB_H -# ifdef HAVE_MKSTEMP -int mkstemp (char *template); -# else -char *mktemp (char *template); -# endif -#endif - -FILE * -output_tmpfile (char **name, const char *template) -{ - FILE *file; -#ifdef HAVE_FDOPEN - int fd; -#endif - - /* Preserve the current umask, and set a restrictive one for temp files. */ - MODE_T mask = UMASK (0077); - -#if defined(HAVE_MKSTEMP) || defined(HAVE_MKTEMP) -# define TEMPLATE_LEN strlen (template) -#else -# define TEMPLATE_LEN L_tmpnam -#endif - *name = xmalloc (TEMPLATE_LEN + 1); - strcpy (*name, template); - -#if defined(HAVE_MKSTEMP) && defined(HAVE_FDOPEN) - /* It's safest to use mkstemp(), if we can. */ - EINTRLOOP (fd, mkstemp (*name)); - if (fd == -1) - file = NULL; - else - file = fdopen (fd, "w"); -#else -# ifdef HAVE_MKTEMP - (void) mktemp (*name); -# else - (void) tmpnam (*name); -# endif - -# ifdef HAVE_FDOPEN - /* Can't use mkstemp(), but guard against a race condition. */ - EINTRLOOP (fd, open (*name, O_CREAT|O_EXCL|O_WRONLY, 0600)); - if (fd == -1) - return 0; - file = fdopen (fd, "w"); -# else - /* Not secure, but what can we do? */ - file = fopen (*name, "w"); -# endif -#endif - - UMASK (mask); - - return file; -} - - /* This code is stolen from gnulib. If/when we abandon the requirement to work with K&R compilers, we can remove this (and perhaps other parts of GNU make!) and migrate to using diff --git a/output.h b/output.h index b398cdd0..083cd246 100644 --- a/output.h +++ b/output.h @@ -34,8 +34,6 @@ extern unsigned int stdio_traced; #define OUTPUT_TRACED() do{ stdio_traced = 1; }while(0) #define OUTPUT_IS_TRACED() (!!stdio_traced) -FILE *output_tmpfile (char **, const char *); - /* Write a buffer directly to the given file descriptor. This handles errors etc. */ int output_write (int fd, const void *buffer, size_t len); diff --git a/vmsjobs.c b/vmsjobs.c index f45c8a80..77a142eb 100644 --- a/vmsjobs.c +++ b/vmsjobs.c @@ -1245,9 +1245,9 @@ child_execute_job (struct child *child, char *argv) FILE *outfile; int cmd_len; - outfile = output_tmpfile (&child->comname, - "sys$scratch:gnv$make_cmdXXXXXX.com"); - /* 012345678901234567890 */ + outfile = get_tmpfile (&child->comname, + "sys$scratch:gnv$make_cmdXXXXXX.com"); + /* 123456789012345678901234567890 */ if (outfile == 0) pfatal_with_name (_("fopen (temporary file)")); comnamelen = strlen (child->comname);