summaryrefslogtreecommitdiff
path: root/coreutils
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2008-12-26 14:56:03 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2008-12-26 14:56:03 +0000
commite40e76f3cd3702b1891231f585e5f38867a52b29 (patch)
treea18368c79c1a40ad84db25aaaaf4b759aaa657a5 /coreutils
parent73ac056f5042b34e4009cdf8d6d08112683825f4 (diff)
downloadbusybox-w32-e40e76f3cd3702b1891231f585e5f38867a52b29.tar.gz
busybox-w32-e40e76f3cd3702b1891231f585e5f38867a52b29.tar.bz2
busybox-w32-e40e76f3cd3702b1891231f585e5f38867a52b29.zip
unexpand: fix incorrect expansion, add test for it
function old new delta expand_main 676 656 -20
Diffstat (limited to 'coreutils')
-rw-r--r--coreutils/expand.c66
1 files changed, 27 insertions, 39 deletions
diff --git a/coreutils/expand.c b/coreutils/expand.c
index 3ca7e5c29..0967e2534 100644
--- a/coreutils/expand.c
+++ b/coreutils/expand.c
@@ -68,51 +68,39 @@ static void expand(FILE *file, int tab_size, unsigned opt)
68static void unexpand(FILE *file, unsigned tab_size, unsigned opt) 68static void unexpand(FILE *file, unsigned tab_size, unsigned opt)
69{ 69{
70 char *line; 70 char *line;
71 char *ptr;
72 int convert;
73 int pos;
74 int i = 0;
75 unsigned column = 0;
76 71
77 while ((line = xmalloc_fgets(file)) != NULL) { 72 while ((line = xmalloc_fgets(file)) != NULL) {
78 convert = 1; 73 char *ptr = line;
79 pos = 0; 74 unsigned column = 0;
80 ptr = line; 75
81 while (*line) { 76 while (*ptr) {
82 while ((*line == ' ' || *line == '\t') && convert) { 77 unsigned n;
83 pos += (*line == ' ') ? 1 : tab_size; 78
84 line++; 79 while (*ptr == ' ') {
85 column++; 80 column++;
86 if ((opt & OPT_ALL) && column == tab_size) { 81 ptr++;
87 column = 0; 82 }
88 goto put_tab; 83 if (*ptr == '\t') {
89 } 84 column += tab_size - (column % tab_size);
85 ptr++;
86 continue;
90 } 87 }
91 if (pos) { 88
92 i = pos / tab_size; 89 n = column / tab_size;
93 if (i) { 90 column = column % tab_size;
94 for (; i > 0; i--) { 91 while (n--)
95 put_tab: 92 putchar('\t');
96 bb_putchar('\t'); 93
97 } 94 if ((opt & OPT_INITIAL) && ptr != line) {
98 } else { 95 printf("%*s%s", column, "", ptr);
99 for (i = pos % tab_size; i > 0; i--) { 96 break;
100 bb_putchar(' ');
101 }
102 }
103 pos = 0;
104 } else {
105 if (opt & OPT_INITIAL) {
106 convert = 0;
107 }
108 if (opt & OPT_ALL) {
109 column++;
110 }
111 bb_putchar(*line);
112 line++;
113 } 97 }
98 n = strcspn(ptr, "\t ");
99 printf("%*s%.*s", column, "", n, ptr);
100 ptr += n;
101 column = (column + n) % tab_size;
114 } 102 }
115 free(ptr); 103 free(line);
116 } 104 }
117} 105}
118#endif 106#endif