diff options
author | Rob Landley <rob@landley.net> | 2006-02-24 02:30:39 +0000 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2006-02-24 02:30:39 +0000 |
commit | 2b26fd5570ebd6efb395aac4773051a5f3ed4dcd (patch) | |
tree | 432c38fd4819faa22fb61c5f5a7114774a97493b | |
parent | 5c22c11de2dacac3c024a70ae01ee7afb64dddb8 (diff) | |
download | busybox-w32-2b26fd5570ebd6efb395aac4773051a5f3ed4dcd.tar.gz busybox-w32-2b26fd5570ebd6efb395aac4773051a5f3ed4dcd.tar.bz2 busybox-w32-2b26fd5570ebd6efb395aac4773051a5f3ed4dcd.zip |
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...
-rw-r--r-- | coreutils/sort.c | 18 | ||||
-rw-r--r-- | editors/sed.c | 44 | ||||
-rw-r--r-- | libbb/get_line_from_file.c | 66 |
3 files changed, 50 insertions, 78 deletions
diff --git a/coreutils/sort.c b/coreutils/sort.c index b56a5abb8..98cea7cd7 100644 --- a/coreutils/sort.c +++ b/coreutils/sort.c | |||
@@ -4,19 +4,9 @@ | |||
4 | * | 4 | * |
5 | * Copyright (C) 2004 by Rob Landley <rob@landley.net> | 5 | * Copyright (C) 2004 by Rob Landley <rob@landley.net> |
6 | * | 6 | * |
7 | * This program is free software; you can redistribute it and/or modify | 7 | * MAINTAINER: Rob Landley <rob@landley.net> |
8 | * it under the terms of the GNU General Public License as published by | 8 | * |
9 | * the Free Software Foundation; either version 2 of the License, or | 9 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
10 | * (at your option) any later version. | ||
11 | * | ||
12 | * This program is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
15 | * General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU General Public License | ||
18 | * along with this program; if not, write to the Free Software | ||
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
20 | * | 10 | * |
21 | * See SuS3 sort standard at: | 11 | * See SuS3 sort standard at: |
22 | * http://www.opengroup.org/onlinepubs/007904975/utilities/sort.html | 12 | * http://www.opengroup.org/onlinepubs/007904975/utilities/sort.html |
@@ -142,7 +132,7 @@ static struct sort_key *add_key(void) | |||
142 | return *pkey=xcalloc(1,sizeof(struct sort_key)); | 132 | return *pkey=xcalloc(1,sizeof(struct sort_key)); |
143 | } | 133 | } |
144 | 134 | ||
145 | #define GET_LINE(fp) (global_flags&FLAG_z) ? bb_get_chunk_from_file(fp) \ | 135 | #define GET_LINE(fp) (global_flags&FLAG_z) ? bb_get_chunk_from_file(fp,NULL) \ |
146 | : bb_get_chomped_line_from_file(fp) | 136 | : bb_get_chomped_line_from_file(fp) |
147 | #else | 137 | #else |
148 | #define GET_LINE(fp) bb_get_chomped_line_from_file(fp) | 138 | #define GET_LINE(fp) bb_get_chomped_line_from_file(fp) |
diff --git a/editors/sed.c b/editors/sed.c index dcc2ca06b..b08eae4dc 100644 --- a/editors/sed.c +++ b/editors/sed.c | |||
@@ -8,6 +8,8 @@ | |||
8 | * Copyright (C) 2003 by Glenn McGrath <bug1@iinet.net.au> | 8 | * Copyright (C) 2003 by Glenn McGrath <bug1@iinet.net.au> |
9 | * Copyright (C) 2003,2004 by Rob Landley <rob@landley.net> | 9 | * Copyright (C) 2003,2004 by Rob Landley <rob@landley.net> |
10 | * | 10 | * |
11 | * MAINTAINER: Rob Landley <rob@landley.net> | ||
12 | * | ||
11 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. | 13 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
12 | */ | 14 | */ |
13 | 15 | ||
@@ -74,8 +76,8 @@ typedef struct sed_cmd_s { | |||
74 | regex_t *sub_match; /* For 's/sub_match/string/' */ | 76 | regex_t *sub_match; /* For 's/sub_match/string/' */ |
75 | int beg_line; /* 'sed 1p' 0 == apply commands to all lines */ | 77 | int beg_line; /* 'sed 1p' 0 == apply commands to all lines */ |
76 | int end_line; /* 'sed 1,3p' 0 == one line only. -1 = last line ($) */ | 78 | int end_line; /* 'sed 1,3p' 0 == one line only. -1 = last line ($) */ |
77 | 79 | ||
78 | FILE *file; /* File (sr) command writes to, -1 for none. */ | 80 | FILE *file; /* File (sw) command writes to, -1 for none. */ |
79 | char *string; /* Data string for (saicytb) commands. */ | 81 | char *string; /* Data string for (saicytb) commands. */ |
80 | 82 | ||
81 | unsigned short which_match; /* (s) Which match to replace (0 for all) */ | 83 | unsigned short which_match; /* (s) Which match to replace (0 for all) */ |
@@ -83,7 +85,7 @@ typedef struct sed_cmd_s { | |||
83 | /* Bitfields (gcc won't group them if we don't) */ | 85 | /* Bitfields (gcc won't group them if we don't) */ |
84 | unsigned int invert:1; /* the '!' after the address */ | 86 | unsigned int invert:1; /* the '!' after the address */ |
85 | unsigned int in_match:1; /* Next line also included in match? */ | 87 | unsigned int in_match:1; /* Next line also included in match? */ |
86 | unsigned int no_newline:1; /* Last line written by (sr) had no '\n' */ | 88 | unsigned int no_newline:1; /* Last line written by (sw) had no '\n' */ |
87 | unsigned int sub_p:1; /* (s) print option */ | 89 | unsigned int sub_p:1; /* (s) print option */ |
88 | 90 | ||
89 | 91 | ||
@@ -120,6 +122,7 @@ struct append_list { | |||
120 | }; | 122 | }; |
121 | static struct append_list *append_head=NULL, *append_tail=NULL; | 123 | static struct append_list *append_head=NULL, *append_tail=NULL; |
122 | 124 | ||
125 | void free_and_close_stuff(void); | ||
123 | #ifdef CONFIG_FEATURE_CLEAN_UP | 126 | #ifdef CONFIG_FEATURE_CLEAN_UP |
124 | static void free_and_close_stuff(void) | 127 | static void free_and_close_stuff(void) |
125 | { | 128 | { |
@@ -729,13 +732,13 @@ static char *get_next_line(int *no_newline) | |||
729 | int len; | 732 | int len; |
730 | 733 | ||
731 | flush_append(); | 734 | flush_append(); |
732 | while(current_input_file<input_file_count) { | 735 | while (current_input_file<input_file_count) { |
733 | temp=bb_get_line_from_file(input_file_list[current_input_file]); | 736 | temp = bb_get_chunk_from_file(input_file_list[current_input_file],&len); |
734 | if(temp) { | 737 | if (temp) { |
735 | len=strlen(temp); | 738 | *no_newline = !(len && temp[len-1]=='\n'); |
736 | *no_newline=!(len && temp[len-1]=='\n'); | 739 | if (!*no_newline) temp[len-1] = 0; |
737 | if(!*no_newline) temp[len-1]=0; | ||
738 | break; | 740 | break; |
741 | // Close this file and advance to next one | ||
739 | } else fclose(input_file_list[current_input_file++]); | 742 | } else fclose(input_file_list[current_input_file++]); |
740 | } | 743 | } |
741 | 744 | ||
@@ -762,12 +765,15 @@ static int puts_maybe_newline(char *s, FILE *file, int missing_newline, int no_n | |||
762 | 765 | ||
763 | #define sed_puts(s,n) missing_newline=puts_maybe_newline(s,nonstdout,missing_newline,n) | 766 | #define sed_puts(s,n) missing_newline=puts_maybe_newline(s,nonstdout,missing_newline,n) |
764 | 767 | ||
768 | /* Process all the lines in all the files */ | ||
769 | |||
765 | static void process_files(void) | 770 | static void process_files(void) |
766 | { | 771 | { |
767 | char *pattern_space, *next_line; | 772 | char *pattern_space, *next_line; |
768 | int linenum = 0, missing_newline=0; | 773 | int linenum = 0, missing_newline=0; |
769 | int no_newline,next_no_newline=0; | 774 | int no_newline,next_no_newline=0; |
770 | 775 | ||
776 | /* Prime the pump */ | ||
771 | next_line = get_next_line(&next_no_newline); | 777 | next_line = get_next_line(&next_no_newline); |
772 | 778 | ||
773 | /* go through every line in each file */ | 779 | /* go through every line in each file */ |
@@ -779,7 +785,8 @@ static void process_files(void) | |||
779 | if(!(pattern_space=next_line)) break; | 785 | if(!(pattern_space=next_line)) break; |
780 | no_newline=next_no_newline; | 786 | no_newline=next_no_newline; |
781 | 787 | ||
782 | /* Read one line in advance so we can act on the last line, the '$' address */ | 788 | /* Read one line in advance so we can act on the last line, |
789 | * the '$' address */ | ||
783 | next_line = get_next_line(&next_no_newline); | 790 | next_line = get_next_line(&next_no_newline); |
784 | linenum++; | 791 | linenum++; |
785 | restart: | 792 | restart: |
@@ -921,16 +928,16 @@ restart: | |||
921 | /* Read file, append contents to output */ | 928 | /* Read file, append contents to output */ |
922 | case 'r': | 929 | case 'r': |
923 | { | 930 | { |
924 | FILE *outfile; | 931 | FILE *rfile; |
925 | 932 | ||
926 | outfile = fopen(sed_cmd->string, "r"); | 933 | rfile = fopen(sed_cmd->string, "r"); |
927 | if (outfile) { | 934 | if (rfile) { |
928 | char *line; | 935 | char *line; |
929 | 936 | ||
930 | while ((line = bb_get_chomped_line_from_file(outfile)) | 937 | while ((line = bb_get_chomped_line_from_file(rfile)) |
931 | != NULL) | 938 | != NULL) |
932 | append(line); | 939 | append(line); |
933 | bb_xprint_and_close_file(outfile); | 940 | bb_xprint_and_close_file(rfile); |
934 | } | 941 | } |
935 | 942 | ||
936 | break; | 943 | break; |
@@ -1107,11 +1114,9 @@ extern int sed_main(int argc, char **argv) | |||
1107 | { | 1114 | { |
1108 | int status = EXIT_SUCCESS, opt, getpat = 1; | 1115 | int status = EXIT_SUCCESS, opt, getpat = 1; |
1109 | 1116 | ||
1110 | #ifdef CONFIG_FEATURE_CLEAN_UP | ||
1111 | /* destroy command strings on exit */ | 1117 | /* destroy command strings on exit */ |
1112 | if (atexit(free_and_close_stuff) == -1) | 1118 | if (ENABLE_FEATURE_CLEAN_UP && atexit(free_and_close_stuff) == -1) |
1113 | bb_perror_msg_and_die("atexit"); | 1119 | bb_perror_msg_and_die("atexit"); |
1114 | #endif | ||
1115 | 1120 | ||
1116 | /* Lie to autoconf when it starts asking stupid questions. */ | 1121 | /* Lie to autoconf when it starts asking stupid questions. */ |
1117 | if(argc==2 && !strcmp(argv[1],"--version")) { | 1122 | if(argc==2 && !strcmp(argv[1],"--version")) { |
@@ -1197,12 +1202,15 @@ extern int sed_main(int argc, char **argv) | |||
1197 | if(-1==(nonstdoutfd=mkstemp(outname))) | 1202 | if(-1==(nonstdoutfd=mkstemp(outname))) |
1198 | bb_error_msg_and_die("no temp file"); | 1203 | bb_error_msg_and_die("no temp file"); |
1199 | nonstdout=fdopen(nonstdoutfd,"w"); | 1204 | nonstdout=fdopen(nonstdoutfd,"w"); |
1205 | |||
1200 | /* Set permissions of output file */ | 1206 | /* Set permissions of output file */ |
1207 | |||
1201 | fstat(fileno(file),&statbuf); | 1208 | fstat(fileno(file),&statbuf); |
1202 | fchmod(nonstdoutfd,statbuf.st_mode); | 1209 | fchmod(nonstdoutfd,statbuf.st_mode); |
1203 | add_input_file(file); | 1210 | add_input_file(file); |
1204 | process_files(); | 1211 | process_files(); |
1205 | fclose(nonstdout); | 1212 | fclose(nonstdout); |
1213 | |||
1206 | nonstdout=stdout; | 1214 | nonstdout=stdout; |
1207 | unlink(argv[i]); | 1215 | unlink(argv[i]); |
1208 | rename(outname,argv[i]); | 1216 | rename(outname,argv[i]); |
diff --git a/libbb/get_line_from_file.c b/libbb/get_line_from_file.c index a27edc3bd..5ad497ffa 100644 --- a/libbb/get_line_from_file.c +++ b/libbb/get_line_from_file.c | |||
@@ -2,22 +2,11 @@ | |||
2 | /* | 2 | /* |
3 | * Utility routines. | 3 | * Utility routines. |
4 | * | 4 | * |
5 | * Copyright (C) many different people. | 5 | * Copyright (C) 2005, 2006 Rob Landley <rob@landley.net> |
6 | * If you wrote this, please acknowledge your work. | 6 | * Copyright (C) 2004 Erik Andersen <andersen@codepoet.org> |
7 | * Copyright (C) 2001 Matt Krai | ||
7 | * | 8 | * |
8 | * This program is free software; you can redistribute it and/or modify | 9 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
9 | * it under the terms of the GNU General Public License as published by | ||
10 | * the Free Software Foundation; either version 2 of the License, or | ||
11 | * (at your option) any later version. | ||
12 | * | ||
13 | * This program is distributed in the hope that it will be useful, | ||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
16 | * General Public License for more details. | ||
17 | * | ||
18 | * You should have received a copy of the GNU General Public License | ||
19 | * along with this program; if not, write to the Free Software | ||
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
21 | */ | 10 | */ |
22 | 11 | ||
23 | #include <stdio.h> | 12 | #include <stdio.h> |
@@ -25,14 +14,12 @@ | |||
25 | #include "libbb.h" | 14 | #include "libbb.h" |
26 | 15 | ||
27 | /* get_line_from_file() - This function reads an entire line from a text file, | 16 | /* get_line_from_file() - This function reads an entire line from a text file, |
28 | * up to a newline. It returns a malloc'ed char * which must be stored and | 17 | * up to a newline or NUL byte. It returns a malloc'ed char * which must be |
29 | * free'ed by the caller. If 'c' is nonzero, the trailing '\n' (if any) | 18 | * stored and free'ed by the caller. If end is null '\n' isn't considered |
30 | * is removed. In event of a read error or EOF, NULL is returned. */ | 19 | * and of line. If end isn't null, length of the chunk read is stored in it. */ |
31 | 20 | ||
32 | static char *private_get_line_from_file(FILE *file, int c) | 21 | char *bb_get_chunk_from_file(FILE *file, int *end) |
33 | { | 22 | { |
34 | #define GROWBY (80) /* how large we will grow strings by */ | ||
35 | |||
36 | int ch; | 23 | int ch; |
37 | int idx = 0; | 24 | int idx = 0; |
38 | char *linebuf = NULL; | 25 | char *linebuf = NULL; |
@@ -41,17 +28,12 @@ static char *private_get_line_from_file(FILE *file, int c) | |||
41 | while ((ch = getc(file)) != EOF) { | 28 | while ((ch = getc(file)) != EOF) { |
42 | /* grow the line buffer as necessary */ | 29 | /* grow the line buffer as necessary */ |
43 | if (idx > linebufsz - 2) { | 30 | if (idx > linebufsz - 2) { |
44 | linebuf = xrealloc(linebuf, linebufsz += GROWBY); | 31 | linebuf = xrealloc(linebuf, linebufsz += 80); |
45 | } | 32 | } |
46 | linebuf[idx++] = (char)ch; | 33 | linebuf[idx++] = (char)ch; |
47 | if (!ch) return linebuf; | 34 | if (!ch || (end && ch == '\n')) break; |
48 | if (c<2 && ch == '\n') { | ||
49 | if (c) { | ||
50 | --idx; | ||
51 | } | ||
52 | break; | ||
53 | } | ||
54 | } | 35 | } |
36 | if (end) *end = idx; | ||
55 | if (linebuf) { | 37 | if (linebuf) { |
56 | if (ferror(file)) { | 38 | if (ferror(file)) { |
57 | free(linebuf); | 39 | free(linebuf); |
@@ -62,27 +44,19 @@ static char *private_get_line_from_file(FILE *file, int c) | |||
62 | return linebuf; | 44 | return linebuf; |
63 | } | 45 | } |
64 | 46 | ||
47 | /* Get line, including trailing /n if any */ | ||
65 | extern char *bb_get_line_from_file(FILE *file) | 48 | extern char *bb_get_line_from_file(FILE *file) |
66 | { | 49 | { |
67 | return private_get_line_from_file(file, 0); | 50 | int i; |
51 | return bb_get_chunk_from_file(file, &i); | ||
68 | } | 52 | } |
69 | 53 | ||
54 | /* Get line. Remove trailing /n */ | ||
70 | extern char *bb_get_chomped_line_from_file(FILE *file) | 55 | extern char *bb_get_chomped_line_from_file(FILE *file) |
71 | { | 56 | { |
72 | return private_get_line_from_file(file, 1); | 57 | int i; |
73 | } | 58 | char *c=bb_get_chunk_from_file(file, &i); |
74 | 59 | if(i) c[--i]=0; | |
75 | extern char *bb_get_chunk_from_file(FILE *file) | 60 | |
76 | { | 61 | return c; |
77 | return private_get_line_from_file(file, 2); | ||
78 | } | 62 | } |
79 | |||
80 | |||
81 | /* END CODE */ | ||
82 | /* | ||
83 | Local Variables: | ||
84 | c-file-style: "linux" | ||
85 | c-basic-offset: 4 | ||
86 | tab-width: 4 | ||
87 | End: | ||
88 | */ | ||