From 2b26fd5570ebd6efb395aac4773051a5f3ed4dcd Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Fri, 24 Feb 2006 02:30:39 +0000 Subject: A few changes falling out from the effort to make sed handle embedded NUL bytes. Checking in to reduce the diff between my tree and svn... --- editors/sed.c | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) (limited to 'editors') diff --git a/editors/sed.c b/editors/sed.c index dcc2ca0..b08eae4 100644 --- a/editors/sed.c +++ b/editors/sed.c @@ -8,6 +8,8 @@ * Copyright (C) 2003 by Glenn McGrath * Copyright (C) 2003,2004 by Rob Landley * + * MAINTAINER: Rob Landley + * * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. */ @@ -74,8 +76,8 @@ typedef struct sed_cmd_s { regex_t *sub_match; /* For 's/sub_match/string/' */ int beg_line; /* 'sed 1p' 0 == apply commands to all lines */ int end_line; /* 'sed 1,3p' 0 == one line only. -1 = last line ($) */ - - FILE *file; /* File (sr) command writes to, -1 for none. */ + + FILE *file; /* File (sw) command writes to, -1 for none. */ char *string; /* Data string for (saicytb) commands. */ unsigned short which_match; /* (s) Which match to replace (0 for all) */ @@ -83,7 +85,7 @@ typedef struct sed_cmd_s { /* Bitfields (gcc won't group them if we don't) */ unsigned int invert:1; /* the '!' after the address */ unsigned int in_match:1; /* Next line also included in match? */ - unsigned int no_newline:1; /* Last line written by (sr) had no '\n' */ + unsigned int no_newline:1; /* Last line written by (sw) had no '\n' */ unsigned int sub_p:1; /* (s) print option */ @@ -120,6 +122,7 @@ struct append_list { }; static struct append_list *append_head=NULL, *append_tail=NULL; +void free_and_close_stuff(void); #ifdef CONFIG_FEATURE_CLEAN_UP static void free_and_close_stuff(void) { @@ -729,13 +732,13 @@ static char *get_next_line(int *no_newline) int len; flush_append(); - while(current_input_filestring, "r"); - if (outfile) { + rfile = fopen(sed_cmd->string, "r"); + if (rfile) { char *line; - while ((line = bb_get_chomped_line_from_file(outfile)) + while ((line = bb_get_chomped_line_from_file(rfile)) != NULL) append(line); - bb_xprint_and_close_file(outfile); + bb_xprint_and_close_file(rfile); } break; @@ -1107,11 +1114,9 @@ extern int sed_main(int argc, char **argv) { int status = EXIT_SUCCESS, opt, getpat = 1; -#ifdef CONFIG_FEATURE_CLEAN_UP /* destroy command strings on exit */ - if (atexit(free_and_close_stuff) == -1) + if (ENABLE_FEATURE_CLEAN_UP && atexit(free_and_close_stuff) == -1) bb_perror_msg_and_die("atexit"); -#endif /* Lie to autoconf when it starts asking stupid questions. */ if(argc==2 && !strcmp(argv[1],"--version")) { @@ -1197,12 +1202,15 @@ extern int sed_main(int argc, char **argv) if(-1==(nonstdoutfd=mkstemp(outname))) bb_error_msg_and_die("no temp file"); nonstdout=fdopen(nonstdoutfd,"w"); + /* Set permissions of output file */ + fstat(fileno(file),&statbuf); fchmod(nonstdoutfd,statbuf.st_mode); add_input_file(file); process_files(); fclose(nonstdout); + nonstdout=stdout; unlink(argv[i]); rename(outname,argv[i]); -- cgit v1.1