Rewrite -c and -b processing to shrink code and eliminate buffer overrun.
authorMatt Kraai <kraai@debian.org>
Sat, 5 May 2001 16:19:13 +0000 (16:19 -0000)
committerMatt Kraai <kraai@debian.org>
Sat, 5 May 2001 16:19:13 +0000 (16:19 -0000)
coreutils/cut.c
cut.c

index d852ab3be957a9230e07f6b43bb0b5f2bae26789..efcb325dfea6c46bce72bd8a2c8f4abdacd3e53e 100644 (file)
@@ -108,20 +108,10 @@ static void cut_file(FILE *file)
        for (line = NULL; (line = get_line_from_file(file)) != NULL; free(line)) {
                /* cut based on chars/bytes */
                if (part == 'c' || part == 'b') {
-                       int i;
-                       /* a valid end position has been specified */
-                       if (endpos > 0) {
-                               for (i = startpos-1; i < endpos; i++) {
-                                       fputc(line[i], stdout);
-                               }
-                               fputc('\n', stdout);
-                       }
-                       /* otherwise, just go to the end of the line */
-                       else {
-                               for (i = startpos-1; line[i]; i++) {
-                                       fputc(line[i], stdout);
-                               }
-                       }
+                       chomp(line);
+                       if (0 < endpos && endpos < strlen(line))
+                               line[endpos] = '\0';
+                       puts(line + startpos - 1);
                } 
                /* cut based on fields */
                else if (part == 'f') {
diff --git a/cut.c b/cut.c
index d852ab3be957a9230e07f6b43bb0b5f2bae26789..efcb325dfea6c46bce72bd8a2c8f4abdacd3e53e 100644 (file)
--- a/cut.c
+++ b/cut.c
@@ -108,20 +108,10 @@ static void cut_file(FILE *file)
        for (line = NULL; (line = get_line_from_file(file)) != NULL; free(line)) {
                /* cut based on chars/bytes */
                if (part == 'c' || part == 'b') {
-                       int i;
-                       /* a valid end position has been specified */
-                       if (endpos > 0) {
-                               for (i = startpos-1; i < endpos; i++) {
-                                       fputc(line[i], stdout);
-                               }
-                               fputc('\n', stdout);
-                       }
-                       /* otherwise, just go to the end of the line */
-                       else {
-                               for (i = startpos-1; line[i]; i++) {
-                                       fputc(line[i], stdout);
-                               }
-                       }
+                       chomp(line);
+                       if (0 < endpos && endpos < strlen(line))
+                               line[endpos] = '\0';
+                       puts(line + startpos - 1);
                } 
                /* cut based on fields */
                else if (part == 'f') {