summaryrefslogtreecommitdiff
path: root/sed
diff options
context:
space:
mode:
authorPaolo Bonzini <bonzini@gnu.org>2010-07-18 10:09:54 +0200
committerPaolo Bonzini <bonzini@gnu.org>2010-08-17 01:56:55 +0200
commit7355d316a53e89ab9f52df5ac9049bb815eaeefb (patch)
tree43846cabf75ec96b0c8a28be369403af9555f84d /sed
parente1c76b034407016c7b837a466d14e4f983f3866a (diff)
downloadsed-7355d316a53e89ab9f52df5ac9049bb815eaeefb.tar.gz
use "w" or "wb" modes for sed -i, s///w, `w' and `W' depending on --binary
2010-07-18 Paolo Bonzini <bonzini@gnu.org> * sed.c (write_mode): New. (main): Initialize it to "wb" if --binary is passed. * sed.h (write_mode): New. * compile.c (mark_subst_opts, compile_program): Use it instead of "w". * execute.c (open_next_file): Pass write_mode to ck_mkstemp. * utils.c (ck_mkstemp): Accept mode, pass it to fdopen. Constify. * utils.h (ck_mkstemp): Adjust prototype.
Diffstat (limited to 'sed')
-rw-r--r--sed/compile.c4
-rw-r--r--sed/execute.c3
-rw-r--r--sed/sed.c4
-rw-r--r--sed/sed.h3
-rw-r--r--sed/utils.c7
-rw-r--r--sed/utils.h3
6 files changed, 15 insertions, 9 deletions
diff --git a/sed/compile.c b/sed/compile.c
index 4e6c303..b842e1b 100644
--- a/sed/compile.c
+++ b/sed/compile.c
@@ -652,7 +652,7 @@ mark_subst_opts(cmd)
break;
case 'w':
- cmd->outf = get_openfile(&file_write, "w", true);
+ cmd->outf = get_openfile(&file_write, write_mode, true);
return flags;
case '0': case '1': case '2': case '3': case '4':
@@ -1262,7 +1262,7 @@ compile_program(vector)
case 'W':
case 'w':
- cur_cmd->x.outf = get_openfile(&file_write, "w", true);
+ cur_cmd->x.outf = get_openfile(&file_write, write_mode, true);
break;
case 's':
diff --git a/sed/execute.c b/sed/execute.c
index af8c4d2..14fd4e4 100644
--- a/sed/execute.c
+++ b/sed/execute.c
@@ -767,7 +767,8 @@ open_next_file(name, input)
}
#endif
- output_file.fp = ck_mkstemp (&input->out_file_name, tmpdir, "sed");
+ output_file.fp = ck_mkstemp (&input->out_file_name, tmpdir, "sed",
+ write_mode);
output_file.missing_newline = false;
free (tmpdir);
diff --git a/sed/sed.c b/sed/sed.c
index 723958d..637722d 100644
--- a/sed/sed.c
+++ b/sed/sed.c
@@ -75,8 +75,9 @@ bool follow_symlinks = false;
/* How do we edit files in-place? (we don't if NULL) */
char *in_place_extension = NULL;
-/* The mode to use to read files, either "rt" or "rb". */
+/* The mode to use to read/write files, either "rt"/"w" or "rb"/"wb". */
char *read_mode = "rt";
+char *write_mode = "w";
/* Do we need to be pedantically POSIX compliant? */
enum posixicity_types posixicity;
@@ -284,6 +285,7 @@ main(argc, argv)
case 'b':
read_mode = "rb";
+ write_mode = "wb";
break;
/* Undocumented, for compatibility with BSD sed. */
diff --git a/sed/sed.h b/sed/sed.h
index 6b4101d..f8ccccc 100644
--- a/sed/sed.h
+++ b/sed/sed.h
@@ -233,8 +233,9 @@ extern countT lcmd_out_line_len;
/* How do we edit files in-place? (we don't if NULL) */
extern char *in_place_extension;
-/* The mode to use to read files, either "rt" or "rb". */
+/* The mode to use to read and write files, either "rt"/"w" or "rb"/"wb". */
extern char *read_mode;
+extern char *write_mode;
/* Should we use EREs? */
extern bool use_extended_syntax_p;
diff --git a/sed/utils.c b/sed/utils.c
index 226089a..d5722f6 100644
--- a/sed/utils.c
+++ b/sed/utils.c
@@ -185,9 +185,10 @@ ck_fdopen(fd, name, mode, fail)
}
FILE *
-ck_mkstemp (p_filename, tmpdir, base)
+ck_mkstemp (p_filename, tmpdir, base, mode)
char **p_filename;
- char *base, *tmpdir;
+ const char *base, *tmpdir;
+ const char *mode;
{
char *template;
FILE *fp;
@@ -221,7 +222,7 @@ ck_mkstemp (p_filename, tmpdir, base)
panic(_("couldn't open temporary file %s: %s"), template, strerror(errno));
*p_filename = template;
- fp = fdopen (fd, "w");
+ fp = fdopen (fd, mode);
register_open_file (fp, template, true);
return fp;
}
diff --git a/sed/utils.h b/sed/utils.h
index d3f431d..aef8e65 100644
--- a/sed/utils.h
+++ b/sed/utils.h
@@ -30,7 +30,8 @@ void ck_fflush P_((FILE *stream));
void ck_fclose P_((FILE *stream));
const char *follow_symlink P_((const char *path));
size_t ck_getline P_((char **text, size_t *buflen, FILE *stream));
-FILE * ck_mkstemp P_((char **p_filename, char *tmpdir, char *base));
+FILE * ck_mkstemp P_((char **p_filename, const char *tmpdir, const char *base,
+ const char *mode));
void ck_rename P_((const char *from, const char *to, const char *unlink_if_fail));
VOID *ck_malloc P_((size_t size));