aboutsummaryrefslogtreecommitdiff
path: root/coreutils
diff options
context:
space:
mode:
authorGravatar Denis Vlasenko <vda.linux@googlemail.com>2008-12-26 14:56:03 +0000
committerGravatar Denis Vlasenko <vda.linux@googlemail.com>2008-12-26 14:56:03 +0000
commite40e76f3cd3702b1891231f585e5f38867a52b29 (patch)
treea18368c79c1a40ad84db25aaaaf4b759aaa657a5 /coreutils
parent73ac056f5042b34e4009cdf8d6d08112683825f4 (diff)
downloadbusybox-e40e76f3cd3702b1891231f585e5f38867a52b29.tar.gz
busybox-e40e76f3cd3702b1891231f585e5f38867a52b29.tar.bz2
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)
static void unexpand(FILE *file, unsigned tab_size, unsigned opt)
{
char *line;
- char *ptr;
- int convert;
- int pos;
- int i = 0;
- unsigned column = 0;
while ((line = xmalloc_fgets(file)) != NULL) {
- convert = 1;
- pos = 0;
- ptr = line;
- while (*line) {
- while ((*line == ' ' || *line == '\t') && convert) {
- pos += (*line == ' ') ? 1 : tab_size;
- line++;
+ char *ptr = line;
+ unsigned column = 0;
+
+ while (*ptr) {
+ unsigned n;
+
+ while (*ptr == ' ') {
column++;
- if ((opt & OPT_ALL) && column == tab_size) {
- column = 0;
- goto put_tab;
- }
+ ptr++;
+ }
+ if (*ptr == '\t') {
+ column += tab_size - (column % tab_size);
+ ptr++;
+ continue;
}
- if (pos) {
- i = pos / tab_size;
- if (i) {
- for (; i > 0; i--) {
- put_tab:
- bb_putchar('\t');
- }
- } else {
- for (i = pos % tab_size; i > 0; i--) {
- bb_putchar(' ');
- }
- }
- pos = 0;
- } else {
- if (opt & OPT_INITIAL) {
- convert = 0;
- }
- if (opt & OPT_ALL) {
- column++;
- }
- bb_putchar(*line);
- line++;
+
+ n = column / tab_size;
+ column = column % tab_size;
+ while (n--)
+ putchar('\t');
+
+ if ((opt & OPT_INITIAL) && ptr != line) {
+ printf("%*s%s", column, "", ptr);
+ break;
}
+ n = strcspn(ptr, "\t ");
+ printf("%*s%.*s", column, "", n, ptr);
+ ptr += n;
+ column = (column + n) % tab_size;
}
- free(ptr);
+ free(line);
}
}
#endif