aboutsummaryrefslogtreecommitdiff
path: root/cut.c
diff options
context:
space:
mode:
authorandersen <andersen@69ca8d6d-28ef-0310-b511-8ec308f3f277>2000-06-26 12:14:30 +0000
committerandersen <andersen@69ca8d6d-28ef-0310-b511-8ec308f3f277>2000-06-26 12:14:30 +0000
commit632e730928b75d318dd1181c56522d12955fa9eb (patch)
tree05cc6c41012fe0f7ff2df50a4ba84d4b5ced4d8f /cut.c
parent382feb719285834ab2a06982b0e04eb94f899dc5 (diff)
downloadbusybox-w32-632e730928b75d318dd1181c56522d12955fa9eb.tar.gz
busybox-w32-632e730928b75d318dd1181c56522d12955fa9eb.tar.bz2
busybox-w32-632e730928b75d318dd1181c56522d12955fa9eb.zip
Fixed segfault with 'cut -f 1 -d:' and added 'cut -s' suport.
Fix thanks to Arne Bernin <arne@matrix.loopback.org> -Erik git-svn-id: svn://busybox.net/trunk/busybox@704 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'cut.c')
-rw-r--r--cut.c30
1 files changed, 23 insertions, 7 deletions
diff --git a/cut.c b/cut.c
index 7b183e8fd..a8a9d7fd1 100644
--- a/cut.c
+++ b/cut.c
@@ -51,15 +51,14 @@ int num_args;
51#define OPTIONC 2 /* define for mode: option -c */ 51#define OPTIONC 2 /* define for mode: option -c */
52#define OPTIONB 3 /* define for mode: option -b */ 52#define OPTIONB 3 /* define for mode: option -b */
53#define NOTSET 0 /* option not selected */ 53#define NOTSET 0 /* option not selected */
54#define SET 1 /* option selected */ 54#define SET 1 /* option selected */
55 55#define OPTIONS 1 /*define option -s */
56/* Defines for the warnings */ 56/* Defines for the warnings */
57#define DELIMITER_NOT_APPLICABLE 0 57#define DELIMITER_NOT_APPLICABLE 0
58#define OVERRIDING_PREVIOUS_MODE 1 58#define OVERRIDING_PREVIOUS_MODE 1
59#define OPTION_NOT_APPLICABLE 2 59#define OPTION_NOT_APPLICABLE 2
60#define UNKNOWN_OPTION 3 60#define UNKNOWN_OPTION 3
61#define FILE_NOT_READABLE 4 61#define FILE_NOT_READABLE 4
62
63/* Defines for the fatal errors */ 62/* Defines for the fatal errors */
64#define SYNTAX_ERROR 101 63#define SYNTAX_ERROR 101
65#define POSITION_ERROR 102 64#define POSITION_ERROR 102
@@ -75,6 +74,7 @@ FILE *fd;
75char *name; 74char *name;
76char line[BUFSIZ]; 75char line[BUFSIZ];
77int exit_status; 76int exit_status;
77int option = 0; /* for -s option */
78 78
79int cut_main(int argc, char **argv); 79int cut_main(int argc, char **argv);
80void warn(int warn_number, char *option); 80void warn(int warn_number, char *option);
@@ -85,7 +85,7 @@ void cut(void);
85void warn(int warn_number, char *option) 85void warn(int warn_number, char *option)
86{ 86{
87 static char *warn_msg[] = { 87 static char *warn_msg[] = {
88 "%s: Option -d allowed only with -f\n", 88 "%s: Option -%s allowed only with -f\n",
89 "%s: -%s overrides earlier option\n", 89 "%s: -%s overrides earlier option\n",
90 "%s: -%s not allowed in current mode\n", 90 "%s: -%s not allowed in current mode\n",
91 "%s: Cannot open %s\n" 91 "%s: Cannot open %s\n"
@@ -161,7 +161,8 @@ void cut()
161 char *columns[MAX_FIELD]; 161 char *columns[MAX_FIELD];
162 162
163 while (fgets(line, BUFSIZ, fd)) { 163 while (fgets(line, BUFSIZ, fd)) {
164 length = strlen(line) - 1; 164 maxcol=0;
165 length = strlen(line) - 1;
165 *(line + length) = 0; 166 *(line + length) = 0;
166 switch (mode) { 167 switch (mode) {
167 case DUMP_STDIN: 168 case DUMP_STDIN:
@@ -182,11 +183,15 @@ void cut()
182 for (i = 0; i < num_args; i++) { 183 for (i = 0; i < num_args; i++) {
183 for (j = args[i * 2]; j <= args[i * 2 + 1]; j++) 184 for (j = args[i * 2]; j <= args[i * 2 + 1]; j++)
184 if (j <= maxcol) { 185 if (j <= maxcol) {
186
185 printf("%s", columns[j - 1]); 187 printf("%s", columns[j - 1]);
188
186 if (i != num_args - 1 || j != args[i * 2 + 1]) 189 if (i != num_args - 1 || j != args[i * 2 + 1])
187 putchar(delim); 190 putchar(delim);
188 } 191 }
189 } 192 }
193 } else if (option != OPTIONS) {
194 printf("%s",line);
190 } 195 }
191 break; 196 break;
192 case OPTIONC: 197 case OPTIONC:
@@ -217,7 +222,8 @@ int cut_main(int argc, char **argv)
217 "Options:\n" 222 "Options:\n"
218 "\t-b LIST\tOutput only bytes from LIST\n" 223 "\t-b LIST\tOutput only bytes from LIST\n"
219 "\t-c LIST\tOutput only characters from LIST\n" 224 "\t-c LIST\tOutput only characters from LIST\n"
220 "\t-d DELIM\tUse DELIM instead of tab as the field delimiter\n" 225 "\t-d CHAR\tUse CHAR instead of tab as the field delimiter\n"
226 "\t-s\tOnly output Lines if the include DELIM\n"
221 "\t-f N\tPrint only these fields\n" 227 "\t-f N\tPrint only these fields\n"
222 "\t-n\tIgnored\n" 228 "\t-n\tIgnored\n"
223#endif 229#endif
@@ -229,7 +235,10 @@ int cut_main(int argc, char **argv)
229 case 'd': 235 case 'd':
230 if (mode == OPTIONC || mode == OPTIONB) 236 if (mode == OPTIONC || mode == OPTIONB)
231 warn(DELIMITER_NOT_APPLICABLE, "d"); 237 warn(DELIMITER_NOT_APPLICABLE, "d");
232 delim = argv[i++][0]; 238 if (argc > i)
239 delim = argv[i++][0];
240 else
241 cuterror(SYNTAX_ERROR);
233 break; 242 break;
234 case 'f': 243 case 'f':
235 sprintf(line, "%s", argv[i++]); 244 sprintf(line, "%s", argv[i++]);
@@ -249,6 +258,10 @@ int cut_main(int argc, char **argv)
249 warn(OVERRIDING_PREVIOUS_MODE, "c"); 258 warn(OVERRIDING_PREVIOUS_MODE, "c");
250 mode = OPTIONC; 259 mode = OPTIONC;
251 break; 260 break;
261 case 's':
262 option = OPTIONS;
263
264 break;
252 case '\0': /* - means: read from stdin */ 265 case '\0': /* - means: read from stdin */
253 numberFilenames++; 266 numberFilenames++;
254 break; 267 break;
@@ -268,6 +281,9 @@ int cut_main(int argc, char **argv)
268/* Here follow the checks, if the selected options are reasonable. */ 281/* Here follow the checks, if the selected options are reasonable. */
269 if (mode == OPTIONB) /* since in Minix char := byte */ 282 if (mode == OPTIONB) /* since in Minix char := byte */
270 mode = OPTIONC; 283 mode = OPTIONC;
284
285 if (mode != OPTIONF && option == OPTIONS)
286 warn(DELIMITER_NOT_APPLICABLE,"s");
271 get_args(); 287 get_args();
272 if (numberFilenames != 0) { 288 if (numberFilenames != 0) {
273 i = 1; 289 i = 1;